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

112 lines
3.4 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;
2016-06-30 16:42:36 +03:00
properties: any;
_isArray: boolean;
@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
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-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-30 16:42:36 +03:00
trackByIdx(index: number, item: any): number {
return index;
}
2016-06-13 20:54:24 +03:00
}