github.com/emate/nomad@v0.8.2-wo-binpacking/ui/app/components/json-viewer.js (about) 1 import Component from '@ember/component'; 2 import { computed } from '@ember/object'; 3 import { run } from '@ember/runloop'; 4 import { copy } from '@ember/object/internals'; 5 import JSONFormatterPkg from 'npm:json-formatter-js'; 6 7 // json-formatter-js is packaged in a funny way that ember-cli-browserify 8 // doesn't unwrap properly. 9 const { default: JSONFormatter } = JSONFormatterPkg; 10 11 export default Component.extend({ 12 classNames: ['json-viewer'], 13 14 json: null, 15 expandDepth: Infinity, 16 17 formatter: computed('json', 'expandDepth', function() { 18 return new JSONFormatter(copy(this.get('json'), true), this.get('expandDepth'), { 19 theme: 'nomad', 20 }); 21 }), 22 23 didReceiveAttrs() { 24 const json = this.get('json'); 25 if (!json) { 26 return; 27 } 28 29 run.scheduleOnce('afterRender', this, embedViewer); 30 }, 31 }); 32 33 function embedViewer() { 34 this.$() 35 .empty() 36 .append(this.get('formatter').render()); 37 }