Moved dereference to base class

This commit is contained in:
Roman Gotsiy 2015-10-21 17:22:22 +03:00
parent c3efcf6756
commit 5e207b343c
2 changed files with 23 additions and 22 deletions

View File

@ -9,7 +9,6 @@ import JSONSchemaView from 'json-schema-view-js/src/index';
import 'json-schema-view-js/dist/style.css!'; import 'json-schema-view-js/dist/style.css!';
import {ElementRef} from 'angular2/angular2'; import {ElementRef} from 'angular2/angular2';
import {JsonPointer} from '../../utils/JsonPointer';
@RedocComponent({ @RedocComponent({
selector: 'schema', selector: 'schema',
@ -21,27 +20,6 @@ export class JsonSchemaView extends BaseComponent {
this.element = elementRef.nativeElement; 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);
let baseName = JsonPointer.baseName(schema.$ref);
// if resolved schema doesn't have title use name from ref
resolved.title = resolved.title || baseName;
Object.assign(schema, resolved);
schema.$ref = null;
}
Object.keys(schema).forEach((key) => {
let value = schema[key];
if (value && typeof value === 'object') {
this.dereference(value);
}
});
}
init() { init() {
this.dereference(); this.dereference();
const formatter = new JSONSchemaView(this.componentSchema, 1); const formatter = new JSONSchemaView(this.componentSchema, 1);

View File

@ -2,6 +2,7 @@
import {Component, View, OnInit, CORE_DIRECTIVES} from 'angular2/angular2'; import {Component, View, OnInit, CORE_DIRECTIVES} from 'angular2/angular2';
import {SchemaManager} from '../utils/SchemaManager'; import {SchemaManager} from '../utils/SchemaManager';
import {JsonPointerEscapePipe} from '../utils/pipes'; import {JsonPointerEscapePipe} from '../utils/pipes';
import {JsonPointer} from '../utils/JsonPointer';
// common inputs for all components // common inputs for all components
let commonInputs = ['pointer']; // json pointer to the schema chunk let commonInputs = ['pointer']; // json pointer to the schema chunk
@ -74,6 +75,28 @@ export class BaseComponent {
this.init(); this.init();
} }
/**
* simple in-place schema dereferencing. Schema is already bundled so no need in global dereferencing.
* TODO: doesn't support circular references
*/
dereference(schema = this.componentSchema) {
if (schema && schema.$ref) {
let resolved = this.schemaMgr.byPointer(schema.$ref);
let baseName = JsonPointer.baseName(schema.$ref);
// if resolved schema doesn't have title use name from ref
resolved.title = resolved.title || baseName;
Object.assign(schema, resolved);
delete schema.$ref;
}
Object.keys(schema).forEach((key) => {
let value = schema[key];
if (value && typeof value === 'object') {
this.dereference(value);
}
});
}
/** /**
* Used to prepare model based on component schema * Used to prepare model based on component schema
* @abstract * @abstract