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

66 lines
1.8 KiB
TypeScript
Raw Normal View History

2015-10-21 17:24:04 +03:00
'use strict';
import { ElementRef, Input } from '@angular/core';
2015-10-21 17:24:04 +03:00
2016-05-29 20:26:20 +03:00
import * as OpenAPISampler from 'openapi-sampler';
2015-10-21 17:24:04 +03:00
2016-05-06 00:48:41 +03:00
import { RedocComponent, BaseComponent, SchemaManager } from '../base';
import { JsonFormatter } from '../../utils/JsonFormatterPipe';
2015-10-21 17:24:04 +03:00
@RedocComponent({
selector: 'schema-sample',
templateUrl: './schema-sample.html',
pipes: [JsonFormatter],
styleUrls: ['./schema-sample.css']
2015-10-21 17:24:04 +03:00
})
2016-05-06 00:48:41 +03:00
export class SchemaSample extends BaseComponent {
element: any;
data: any;
@Input() skipReadOnly:boolean;
constructor(schemaMgr:SchemaManager, elementRef: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 = {};
let base:any = {};
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);
2016-05-29 20:26:20 +03:00
try {
sample = OpenAPISampler.sample(this.componentSchema, {
skipReadOnly: this.skipReadOnly
});
2016-05-29 20:26:20 +03:00
} catch(e) {
2016-06-06 19:32:20 +03:00
// no sample available
2016-05-29 20:26:20 +03:00
}
}
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
}
}