redoc/lib/components/SchemaSample/schema-sample.js

58 lines
1.7 KiB
JavaScript
Raw Normal View History

2015-10-21 17:24:04 +03:00
'use strict';
import {RedocComponent, BaseComponent} from '../base';
import SchemaSampler from 'json-schema-instantiator';
import {JsonFormatter} from '../../utils/JsonFormatterPipe';
import {ElementRef} from 'angular2/core';
2015-10-21 17:24:04 +03:00
@RedocComponent({
selector: 'schema-sample',
2015-10-30 11:26:23 +03:00
templateUrl: './lib/components/SchemaSample/schema-sample.html',
pipes: [JsonFormatter],
styleUrls: ['./lib/components/SchemaSample/schema-sample.css']
2015-10-21 17:24:04 +03:00
})
2015-10-27 20:44:08 +03:00
export default class SchemaSample extends BaseComponent {
constructor(schemaMgr, elementRef) {
2015-10-21 17:24:04 +03:00
super(schemaMgr);
this.element = elementRef.nativeElement;
2015-10-21 17:24:04 +03:00
}
init() {
this.data = {};
2015-10-25 14:26:38 +03:00
let base = {};
let sample;
2015-10-25 14:26:38 +03:00
// got pointer not directly to the schema but e.g. to response obj
if (this.componentSchema.schema) {
base = this.componentSchema;
this.componentSchema = this.componentSchema.schema;
}
if (base.examples && base.examples['application/json']) {
sample = base.examples['application/json'];
} else {
2015-10-25 14:26:38 +03:00
this.dereference(this.componentSchema);
sample = SchemaSampler.instantiate(this.componentSchema);
}
2015-10-21 17:24:04 +03:00
this.data.sample = sample;
this.element.addEventListener('click', (event) => {
var collapsed, target = event.target;
if (event.target.className === 'collapser') {
collapsed = target.parentNode.getElementsByClassName('collapsible')[0];
if (collapsed.parentNode.classList.contains('collapsed')) {
collapsed.parentNode.classList.remove('collapsed');
} else {
collapsed.parentNode.classList.add('collapsed');
}
}
});
2015-10-21 17:24:04 +03:00
}
}
SchemaSample.parameters = SchemaSample.parameters.concat([[ElementRef]]);