redoc/lib/components/JsonSchema/json-schema.ts

131 lines
3.8 KiB
TypeScript
Raw Normal View History

2015-11-19 00:23:18 +03:00
'use strict';
import { Component, Input, Renderer, ElementRef, OnInit, ChangeDetectionStrategy } from '@angular/core';
2016-08-22 12:12:13 +03:00
import { BaseComponent, SpecManager } from '../base';
2016-06-22 21:17:48 +03:00
import { SchemaNormalizer, SchemaHelper } from '../../services/index';
2015-11-19 00:23:18 +03:00
2016-08-22 12:12:13 +03:00
@Component({
2015-11-19 00:23:18 +03:00
selector: 'json-schema',
templateUrl: './json-schema.html',
styleUrls: ['./json-schema.css'],
changeDetection: ChangeDetectionStrategy.OnPush
2015-11-19 00:23:18 +03:00
})
2016-08-28 21:46:10 +03:00
export class JsonSchema extends BaseComponent implements OnInit {
2016-08-22 12:12:13 +03:00
@Input() pointer: string;
@Input() final: boolean = false;
@Input() nestOdd: boolean;
@Input() childFor: string;
@Input() isRequestSchema: boolean;
schema: any = {};
2016-06-22 21:17:48 +03:00
activeDescendant:any = {};
hasDescendants: boolean = false;
_hasSubSchemas: boolean = false;
2016-06-30 16:42:36 +03:00
properties: any;
_isArray: boolean;
2016-06-22 21:17:48 +03:00
normalizer: SchemaNormalizer;
2016-07-10 14:28:05 +03:00
autoExpand = false;
descendants: any;
2016-06-13 20:54:24 +03:00
constructor(specMgr:SpecManager, private _renderer: Renderer, private _elementRef: ElementRef) {
super(specMgr);
this.normalizer = new SchemaNormalizer(specMgr);
2016-06-13 20:54:24 +03:00
}
2016-06-22 21:17:48 +03:00
get normPointer() {
return this.schema._pointer || this.pointer;
2015-11-19 00:23:18 +03:00
}
2016-06-22 21:17:48 +03:00
selectDescendant(idx) {
let activeDescendant = this.descendants[idx];
2016-06-22 21:17:48 +03:00
if (!activeDescendant || activeDescendant.active) return;
this.descendants.forEach(d => {
d.active = false;
2016-03-18 16:06:22 +03:00
});
2016-06-22 21:17:48 +03:00
activeDescendant.active = true;
this.pointer = activeDescendant.$ref;
this.schema = this.specMgr.byPointer(this.pointer);
this.normalizer.reset();
2016-09-01 09:53:42 +03:00
this.schema = this.normalizer.normalize(this.schema, this.normPointer,
{resolved: true});
this.preprocessSchema();
}
2016-06-22 21:17:48 +03:00
initDescendants() {
this.descendants = this.specMgr.findDerivedDefinitions(this.normPointer);
if (!this.descendants.length) return;
2016-08-31 21:58:55 +03:00
this.hasDescendants = true;
let discriminator = this.schema.discriminator || this.schema['x-extendedDiscriminator'];
let discrProperty = this.schema._properties &&
this.schema._properties.filter((prop) => prop.name === discriminator)[0];
if (discrProperty && discrProperty.enum) {
let enumOrder = {};
discrProperty.enum.forEach((enumItem, idx) => {
enumOrder[enumItem.val] = idx;
});
this.schema._descendants.sort((a, b) => {
return enumOrder[a.name] > enumOrder[b.name] ? 1 : -1;
});
}
2016-06-22 21:17:48 +03:00
this.selectDescendant(0);
}
2016-04-27 23:07:06 +03:00
init() {
if (!this.pointer) return;
2016-06-25 13:02:13 +03:00
this.schema = this.componentSchema;
if (!this.schema) {
2016-06-22 21:17:48 +03:00
throw new Error(`Can't load component schema at ${this.pointer}`);
2016-04-27 23:07:06 +03:00
}
2016-05-16 23:02:23 +03:00
this.applyStyling();
2016-09-01 09:53:42 +03:00
this.schema = this.normalizer.normalize(this.schema, this.normPointer, {resolved: true});
2016-06-25 13:02:13 +03:00
this.schema = SchemaHelper.unwrapArray(this.schema, this.normPointer);
2016-11-28 21:03:01 +03:00
this._isArray = this.schema._isArray;
this.initDescendants();
this.preprocessSchema();
}
preprocessSchema() {
2016-06-25 13:02:13 +03:00
SchemaHelper.preprocess(this.schema, this.normPointer, this.pointer);
2016-05-16 23:02:23 +03:00
2016-06-25 13:02:13 +03:00
if (!this.schema.isTrivial) {
SchemaHelper.preprocessProperties(this.schema, this.normPointer, {
2016-06-30 16:42:36 +03:00
childFor: this.childFor
2016-06-22 21:17:48 +03:00
});
2016-05-16 23:02:23 +03:00
}
2016-06-13 20:54:24 +03:00
2016-06-30 16:42:36 +03:00
this.properties = this.schema._properties;
if (this.isRequestSchema) {
this.properties = this.properties && this.properties.filter(prop => !prop.readOnly);
}
this._hasSubSchemas = this.properties && this.properties.some(
propSchema => {
if (propSchema.type === 'array') {
propSchema = propSchema.items;
}
return (propSchema && propSchema.type === 'object' && propSchema._pointer);
});
2016-07-10 14:28:05 +03:00
this.autoExpand = this.properties && this.properties.length === 1;
2016-06-13 20:54:24 +03:00
}
applyStyling() {
if (this.nestOdd) {
this._renderer.setElementAttribute(this._elementRef.nativeElement, 'nestodd', 'true');
}
}
trackByName(index: number, item: any): number {
return item.name + (item._pointer || '');
}
2016-08-28 21:46:10 +03:00
ngOnInit() {
this.preinit();
}
2016-06-13 20:54:24 +03:00
}