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

106 lines
3.0 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';
2015-11-29 18:06:08 +03:00
import JsonPointer from '../../utils/JsonPointer';
2015-11-19 00:23:18 +03:00
@RedocComponent({
selector: 'json-schema',
templateUrl: './lib/components/JsonSchema/json-schema.html',
styleUrls: ['./lib/components/JsonSchema/json-schema.css'],
directives: [JsonSchema],
inputs: ['isArray']
2015-11-19 00:23:18 +03:00
})
export default class JsonSchema extends BaseComponent {
constructor(schemaMgr, elementRef) {
super(schemaMgr);
this.element = elementRef.nativeElement;
}
prepareModel() {
this.data = {};
this.data.properties = [];
if (!this.componentSchema) {
2015-11-19 00:23:18 +03:00
// TODO
this.errorMessage = 'Can\'t load component schema';
console.warn(`${this.errorMessage}: ${this.pointer}`);
2015-11-19 00:23:18 +03:00
return;
}
2015-11-28 01:44:35 +03:00
this.dereference();
let schema = this.componentSchema;
2015-11-28 01:44:35 +03:00
if (schema.type === 'array') {
this.isArray = true;
2015-11-28 01:44:35 +03:00
schema = schema.items;
}
this.joinAllOf(schema);
2015-11-28 01:44:35 +03:00
if (schema.type !== 'object') {
this.isTrivial = true;
this._displayType = schema.type;
if (schema.format) this._displayType = `${this.displayType} <${schema.format}>`;
return;
2015-11-28 01:44:35 +03:00
}
this.pointer = schema._pointer || this.pointer;
this.requiredMap = {};
if (this.schema.required) {
this.schema.required.forEach(prop => this.requiredMap[prop] = true);
}
2015-11-28 01:44:35 +03:00
if (!schema.properties) {
this.isTrivial = true;
this._displayType = `${schema.type} (Custom key-value pairs)`;
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];
2015-11-28 01:44:35 +03:00
propData._displayType = propData.type;
2015-11-19 00:23:18 +03:00
if (propData.type === 'array') {
let itemType = propData.items.type;
let itemFormat = propData.items.format;
2015-11-19 00:51:19 +03:00
if (itemType === 'object') {
itemType = propData.items.title || 'object';
2015-11-29 18:06:08 +03:00
propData._pointer = propData.items._pointer || JsonPointer.join(this.pointer, ['properties', prop, 'items']);
2015-11-19 00:51:19 +03:00
}
propData._displayType = `array of ${itemType}`;
propData.format = itemFormat;
2015-11-28 01:44:35 +03:00
propData._isArray = true;
2015-11-19 00:23:18 +03:00
}
if (propData.type === 'object') {
2015-11-28 01:44:35 +03:00
propData._displayType = propData.title || 'object';
2015-11-19 00:23:18 +03:00
}
if (propData.format) propData._displayFormat = `<${propData.format}>`;
2015-11-19 00:23:18 +03:00
}
init() {
setTimeout(() => this.adjustNameColumnWidth());
}
}
JsonSchema.parameters = JsonSchema.parameters.concat([[ElementRef]]);