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

85 lines
2.4 KiB
TypeScript
Raw Normal View History

2015-11-19 00:23:18 +03:00
'use strict';
2016-06-22 21:17:48 +03:00
import { Input } 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';
2015-11-19 00:23:18 +03:00
@RedocComponent({
selector: 'json-schema',
templateUrl: './json-schema.html',
styleUrls: ['./json-schema.css'],
directives: [JsonSchema, DropDown],
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;
@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) {
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() {
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();
2016-06-13 20:54:24 +03:00
}
}