open-nomad/ui/app/components/json-viewer.js

38 lines
925 B
JavaScript
Raw Normal View History

import Component from '@ember/component';
import { computed } from '@ember/object';
import { run } from '@ember/runloop';
import { copy } from '@ember/object/internals';
2017-09-19 14:47:10 +00:00
import JSONFormatterPkg from 'npm:json-formatter-js';
// json-formatter-js is packaged in a funny way that ember-cli-browserify
// doesn't unwrap properly.
const { default: JSONFormatter } = JSONFormatterPkg;
export default Component.extend({
classNames: ['json-viewer'],
json: null,
expandDepth: Infinity,
2017-09-19 14:47:10 +00:00
formatter: computed('json', 'expandDepth', function() {
return new JSONFormatter(copy(this.get('json'), true), this.get('expandDepth'), {
2017-09-19 14:47:10 +00:00
theme: 'nomad',
});
}),
didReceiveAttrs() {
const json = this.get('json');
if (!json) {
return;
}
run.scheduleOnce('afterRender', this, embedViewer);
},
});
function embedViewer() {
this.$()
.empty()
.append(this.get('formatter').render());
2017-09-19 14:47:10 +00:00
}