From 5e207b343c60d72393eaf627ad5c3b1c461637e5 Mon Sep 17 00:00:00 2001 From: Roman Gotsiy Date: Wed, 21 Oct 2015 17:22:22 +0300 Subject: [PATCH] Moved dereference to base class --- .../JsonSchemaView/json-schema-view.js | 22 ------------------ lib/components/base.js | 23 +++++++++++++++++++ 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/components/JsonSchemaView/json-schema-view.js b/lib/components/JsonSchemaView/json-schema-view.js index a75cdf68..0c2063a4 100644 --- a/lib/components/JsonSchemaView/json-schema-view.js +++ b/lib/components/JsonSchemaView/json-schema-view.js @@ -9,7 +9,6 @@ import JSONSchemaView from 'json-schema-view-js/src/index'; import 'json-schema-view-js/dist/style.css!'; import {ElementRef} from 'angular2/angular2'; -import {JsonPointer} from '../../utils/JsonPointer'; @RedocComponent({ selector: 'schema', @@ -21,27 +20,6 @@ export class JsonSchemaView extends BaseComponent { 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() { this.dereference(); const formatter = new JSONSchemaView(this.componentSchema, 1); diff --git a/lib/components/base.js b/lib/components/base.js index 338b22e0..6676a394 100644 --- a/lib/components/base.js +++ b/lib/components/base.js @@ -2,6 +2,7 @@ import {Component, View, OnInit, CORE_DIRECTIVES} from 'angular2/angular2'; import {SchemaManager} from '../utils/SchemaManager'; import {JsonPointerEscapePipe} from '../utils/pipes'; +import {JsonPointer} from '../utils/JsonPointer'; // common inputs for all components let commonInputs = ['pointer']; // json pointer to the schema chunk @@ -74,6 +75,28 @@ export class BaseComponent { 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 * @abstract