redoc/lib/components/JsonSchemaView/json-schema-view.js

53 lines
1.6 KiB
JavaScript
Raw Normal View History

2015-10-17 21:03:08 +03:00
'use strict';
import {RedocComponent, BaseComponent} from '../base';
/* temporarily this component uses json-schema-view-js lib */
import 'json-formatter-js/src/index';
import 'json-formatter-js/dist/style.css!';
import JSONSchemaView from 'json-schema-view-js/src/index';
import 'json-schema-view-js/dist/style.css!';
import {ElementRef} from 'angular2/angular2';
2015-10-18 12:28:48 +03:00
import {JsonPointer} from '../../utils/JsonPointer';
2015-10-17 21:03:08 +03:00
@RedocComponent({
selector: 'schema',
2015-10-18 12:28:48 +03:00
template: ''
2015-10-17 21:03:08 +03:00
})
export class JsonSchemaView extends BaseComponent {
constructor(schemaMgr, elementRef) {
super(schemaMgr);
this.element = elementRef.nativeElement;
}
dereference(schema = this.componentSchema) {
// simple in-place schema dereferencing. Schema is already bundled so no need in
// global dereferencing.
// TODO: doesn't support circular references
if (schema && schema.$ref) {
let resolved = this.schemaMgr.byPointer(schema.$ref);
2015-10-18 12:28:48 +03:00
let baseName = JsonPointer.baseName(schema.$ref);
// if resolved schema doesn't have title use name from ref
resolved.title = resolved.title || baseName;
2015-10-17 21:03:08 +03:00
Object.assign(schema, resolved);
2015-10-18 12:28:48 +03:00
schema.$ref = null;
2015-10-17 21:03:08 +03:00
}
Object.keys(schema).forEach((key) => {
let value = schema[key];
if (value && typeof value === 'object') {
this.dereference(value);
}
});
}
init() {
this.dereference();
2015-10-17 23:31:01 +03:00
const formatter = new JSONSchemaView(this.componentSchema, 1);
2015-10-17 21:03:08 +03:00
this.element.appendChild(formatter.render());
}
}
JsonSchemaView.parameters = JsonSchemaView.parameters.concat([[ElementRef]]);