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

76 lines
2.1 KiB
JavaScript
Raw Normal View History

2015-11-19 00:23:18 +03:00
'use strict';
import {RedocComponent, BaseComponent} from '../base';
import {ElementRef} from 'angular2/angular2';
@RedocComponent({
selector: 'json-schema',
templateUrl: './lib/components/JsonSchema/json-schema.html',
styleUrls: ['./lib/components/JsonSchema/json-schema.css'],
directives: [JsonSchema]
})
export default class JsonSchema extends BaseComponent {
constructor(schemaMgr, elementRef) {
super(schemaMgr);
this.element = elementRef.nativeElement;
}
prepareModel() {
this.dereference();
2015-11-25 02:51:54 +03:00
this.joinAllOf();
2015-11-19 00:23:18 +03:00
this.requiredMap = {};
if (this.schema.required) {
this.schema.required.forEach(prop => this.requiredMap[prop] = true);
}
let schema = this.componentSchema;
this.data = {};
this.data.properties = [];
if (schema.type !== 'object') {
// TODO
2015-11-25 02:51:54 +03:00
this.errorMessage = 'Non-object (array-based) schemas are not implemented yet';
2015-11-19 00:23:18 +03:00
return;
}
2015-11-19 00:51:19 +03:00
if (!schema.properties) return;
2015-11-19 00:23:18 +03:00
let props = Object.keys(schema.properties).map(prop => {
let propData = schema.properties[prop];
this.injectPropData(prop, propData);
return propData;
2015-11-19 00:51:19 +03:00
});
2015-11-19 00:23:18 +03:00
this.data.properties = props;
}
adjustNameColumnWidth() {
// TODO handle internal schemes differently
let names = [].slice.call(this.element.querySelectorAll('.param-name'));
let widths = names.map(el => el.offsetWidth);
let maxWidth = Math.max(...widths);
2015-11-24 01:08:11 +03:00
if (!maxWidth) return;
2015-11-19 00:23:18 +03:00
names.forEach(el => {
el.style.minWidth = maxWidth + 'px';
});
}
injectPropData(prop, propData) {
propData._name = prop;
propData.isRequired = this.requiredMap[prop];
if (propData.type === 'array') {
let itemType = propData.items.type;
2015-11-19 00:51:19 +03:00
if (itemType === 'object') {
itemType = propData.items.title || 'object';
propData._pointer = propData.items._pointer;
}
2015-11-19 00:23:18 +03:00
propData.type = `array of ${itemType}`;
}
if (propData.type === 'object') {
2015-11-19 00:51:19 +03:00
propData.type = propData.title || 'object';
2015-11-19 00:23:18 +03:00
}
}
init() {
setTimeout(() => this.adjustNameColumnWidth());
}
}
JsonSchema.parameters = JsonSchema.parameters.concat([[ElementRef]]);