2015-10-21 17:24:04 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-06 00:48:41 +03:00
|
|
|
import { ElementRef } from '@angular/core';
|
2015-10-21 17:24:04 +03:00
|
|
|
|
|
|
|
import SchemaSampler from 'json-schema-instantiator';
|
|
|
|
|
2016-05-06 00:48:41 +03:00
|
|
|
import { RedocComponent, BaseComponent, SchemaManager } from '../base';
|
|
|
|
import { JsonFormatter } from '../../utils/JsonFormatterPipe';
|
2016-01-24 00:35:44 +03:00
|
|
|
|
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',
|
2016-01-24 00:35:44 +03:00
|
|
|
pipes: [JsonFormatter],
|
|
|
|
styleUrls: ['./lib/components/SchemaSample/schema-sample.css']
|
2015-10-21 17:24:04 +03:00
|
|
|
})
|
2016-02-10 14:19:50 +03:00
|
|
|
@Reflect.metadata('parameters', [[SchemaManager], [ElementRef]])
|
2016-05-06 00:48:41 +03:00
|
|
|
export class SchemaSample extends BaseComponent {
|
2016-01-24 00:35:44 +03:00
|
|
|
constructor(schemaMgr, elementRef) {
|
2015-10-21 17:24:04 +03:00
|
|
|
super(schemaMgr);
|
2016-01-24 00:35:44 +03:00
|
|
|
this.element = elementRef.nativeElement;
|
2015-10-21 17:24:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.data = {};
|
2015-10-22 21:31:12 +03:00
|
|
|
|
2015-10-25 14:26:38 +03:00
|
|
|
let base = {};
|
2015-10-22 21:31:12 +03:00
|
|
|
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'];
|
2015-10-22 21:31:12 +03:00
|
|
|
} else {
|
2015-10-25 14:26:38 +03:00
|
|
|
this.dereference(this.componentSchema);
|
|
|
|
sample = SchemaSampler.instantiate(this.componentSchema);
|
2015-10-22 21:31:12 +03:00
|
|
|
}
|
|
|
|
|
2015-10-21 17:24:04 +03:00
|
|
|
this.data.sample = sample;
|
2016-01-24 00:35:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|