2015-11-19 00:23:18 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-29 19:07:18 +03:00
|
|
|
import { Input, Renderer, ElementRef, forwardRef } from '@angular/core';
|
2016-02-10 14:19:50 +03:00
|
|
|
|
2016-06-22 21:17:48 +03:00
|
|
|
import { RedocComponent, BaseComponent, SpecManager } from '../base';
|
2016-05-09 22:55:16 +03:00
|
|
|
import { DropDown } from '../../shared/components/index';
|
2016-06-22 21:17:48 +03:00
|
|
|
import { SchemaNormalizer, SchemaHelper } from '../../services/index';
|
2016-06-29 18:02:19 +03:00
|
|
|
import { JsonSchemaLazy } from './json-schema-lazy';
|
|
|
|
import { Zippy } from '../../shared/components/Zippy/zippy';
|
2015-11-19 00:23:18 +03:00
|
|
|
|
|
|
|
@RedocComponent({
|
|
|
|
selector: 'json-schema',
|
2016-05-25 18:34:31 +03:00
|
|
|
templateUrl: './json-schema.html',
|
|
|
|
styleUrls: ['./json-schema.css'],
|
2016-06-29 19:07:18 +03:00
|
|
|
directives: [JsonSchema, DropDown, forwardRef(() => JsonSchemaLazy), Zippy],
|
2016-05-18 16:59:54 +03:00
|
|
|
detect: true
|
2015-11-19 00:23:18 +03:00
|
|
|
})
|
2016-05-06 00:48:41 +03:00
|
|
|
export class JsonSchema extends BaseComponent {
|
2016-07-26 14:07:25 +03:00
|
|
|
schema: any = {};
|
2016-06-22 21:17:48 +03:00
|
|
|
activeDescendant:any = {};
|
|
|
|
hasDescendants: boolean = false;
|
2016-06-29 19:07:18 +03:00
|
|
|
_hasSubSchemas: boolean = false;
|
2016-06-30 16:42:36 +03:00
|
|
|
properties: any;
|
|
|
|
_isArray: boolean;
|
2016-05-25 18:34:31 +03:00
|
|
|
@Input() isArray: boolean;
|
|
|
|
@Input() final: boolean = false;
|
|
|
|
@Input() nestOdd: boolean;
|
|
|
|
@Input() childFor: string;
|
|
|
|
@Input() isRequestSchema: boolean;
|
2016-06-22 21:17:48 +03:00
|
|
|
normalizer: SchemaNormalizer;
|
2016-07-10 14:28:05 +03:00
|
|
|
autoExpand = false;
|
2016-06-13 20:54:24 +03:00
|
|
|
|
2016-06-29 18:02:19 +03:00
|
|
|
constructor(specMgr:SpecManager, private _renderer: Renderer, private _elementRef: ElementRef) {
|
2016-06-23 17:36:38 +03:00
|
|
|
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.schema._descendants[idx];
|
|
|
|
if (!activeDescendant || activeDescendant.active) return;
|
|
|
|
this.schema._descendants.forEach(subSchema => {
|
2016-03-18 16:06:22 +03:00
|
|
|
subSchema.active = false;
|
|
|
|
});
|
2016-06-22 21:17:48 +03:00
|
|
|
activeDescendant.active = true;
|
|
|
|
this.activeDescendant = activeDescendant;
|
2016-03-27 01:44:11 +03:00
|
|
|
}
|
2015-11-29 13:04:28 +03:00
|
|
|
|
2016-06-22 21:17:48 +03:00
|
|
|
initDescendants() {
|
|
|
|
if (!this.schema._descendants || !this.schema._descendants.length) {
|
2016-06-22 19:13:57 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-06-22 21:17:48 +03:00
|
|
|
this.hasDescendants = true;
|
2016-06-22 19:13:57 +03:00
|
|
|
let enumArr = this.schema._properties[this.schema._properties.length - 1].enum;
|
2016-04-20 22:04:30 +03:00
|
|
|
if (enumArr) {
|
|
|
|
let enumOrder = {};
|
|
|
|
enumArr.forEach((enumItem, idx) => {
|
|
|
|
enumOrder[enumItem.val] = idx;
|
|
|
|
});
|
|
|
|
|
2016-06-25 13:02:13 +03:00
|
|
|
this.schema._descendants.sort((a, b) => {
|
2016-06-22 19:13:57 +03:00
|
|
|
return enumOrder[a.name] > enumOrder[b.name] ? 1 : -1;
|
2016-04-20 22:04:30 +03:00
|
|
|
});
|
|
|
|
}
|
2016-06-22 21:17:48 +03:00
|
|
|
this.selectDescendant(0);
|
2016-04-28 20:17:38 +03:00
|
|
|
}
|
2016-04-27 23:07:06 +03:00
|
|
|
|
2016-07-20 11:07:08 +03:00
|
|
|
init() {
|
2016-07-26 14:07:25 +03:00
|
|
|
if (!this.pointer) return;
|
2016-06-29 18:02:19 +03:00
|
|
|
if (this.nestOdd) {
|
|
|
|
this._renderer.setElementAttribute(this._elementRef.nativeElement, 'nestodd', 'true');
|
|
|
|
}
|
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
|
|
|
|
2016-06-25 13:02:13 +03:00
|
|
|
this.schema = this.normalizer.normalize(this.schema, this.normPointer);
|
|
|
|
this.schema = SchemaHelper.unwrapArray(this.schema, this.normPointer);
|
|
|
|
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;
|
|
|
|
this._isArray = this.isArray || this.schema._isArray;
|
|
|
|
if (this.isRequestSchema) {
|
|
|
|
this.properties = this.properties && this.properties.filter(prop => !prop.readOnly);
|
|
|
|
}
|
|
|
|
|
2016-06-22 21:17:48 +03:00
|
|
|
this.initDescendants();
|
2016-06-30 16:42:36 +03:00
|
|
|
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
|
|
|
}
|
2016-06-29 18:02:19 +03:00
|
|
|
|
2016-06-30 16:42:36 +03:00
|
|
|
trackByIdx(index: number, item: any): number {
|
|
|
|
return index;
|
2016-06-29 18:02:19 +03:00
|
|
|
}
|
2016-06-13 20:54:24 +03:00
|
|
|
}
|