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

97 lines
3.0 KiB
TypeScript
Raw Normal View History

2015-11-19 00:23:18 +03:00
'use strict';
import { Input, Renderer, ElementRef, forwardRef } from '@angular/core';
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';
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',
templateUrl: './json-schema.html',
styleUrls: ['./json-schema.css'],
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 {
schema: any;
2016-06-22 21:17:48 +03:00
activeDescendant:any = {};
hasDescendants: boolean = false;
_hasSubSchemas: boolean = false;
@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-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.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-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;
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-06-22 21:17:48 +03:00
this.selectDescendant(0);
}
2016-04-27 23:07:06 +03:00
2016-06-22 21:17:48 +03:00
prepareModel() {
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-22 21:17:48 +03:00
childFor: this.childFor,
skipReadOnly: this.isRequestSchema
});
2016-05-16 23:02:23 +03:00
}
2016-06-13 20:54:24 +03:00
2016-06-22 21:17:48 +03:00
this.initDescendants();
this._hasSubSchemas = this.schema._properties && this.schema._properties.some(
propSchema => propSchema.type === 'object' && propSchema._pointer);
2016-06-13 20:54:24 +03:00
}
trackByName(index: number, item: any): string {
return item['name'];
}
2016-06-13 20:54:24 +03:00
}