2015-10-21 17:24:04 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-08-28 21:46:10 +03:00
|
|
|
import { Component, ElementRef, Input, ChangeDetectionStrategy, OnInit } 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';
|
2017-03-09 13:55:23 +03:00
|
|
|
import JsonPointer from '../../utils/JsonPointer';
|
2016-08-22 12:12:13 +03:00
|
|
|
import { BaseComponent, SpecManager } from '../base';
|
2016-06-22 21:17:48 +03:00
|
|
|
import { SchemaNormalizer } from '../../services/schema-normalizer.service';
|
2017-05-12 11:03:14 +03:00
|
|
|
import { getJsonLikeSample, getXmlLikeSample} from '../../utils/helpers';
|
2016-01-24 00:35:44 +03:00
|
|
|
|
2016-08-22 12:12:13 +03:00
|
|
|
@Component({
|
2015-10-21 17:24:04 +03:00
|
|
|
selector: 'schema-sample',
|
2016-05-25 18:34:31 +03:00
|
|
|
templateUrl: './schema-sample.html',
|
2016-08-22 12:12:13 +03:00
|
|
|
styleUrls: ['./schema-sample.css'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
2015-10-21 17:24:04 +03:00
|
|
|
})
|
2016-08-28 21:46:10 +03:00
|
|
|
export class SchemaSample extends BaseComponent implements OnInit {
|
2016-08-22 12:12:13 +03:00
|
|
|
@Input() pointer:string;
|
|
|
|
@Input() skipReadOnly:boolean;
|
|
|
|
|
2016-05-25 18:34:31 +03:00
|
|
|
element: any;
|
2016-07-20 11:07:08 +03:00
|
|
|
sample: any;
|
2017-05-12 11:03:14 +03:00
|
|
|
xmlSample: string;
|
2016-07-17 18:07:51 +03:00
|
|
|
enableButtons: boolean = false;
|
2016-06-22 19:13:57 +03:00
|
|
|
|
2016-06-22 21:17:48 +03:00
|
|
|
private _normalizer:SchemaNormalizer;
|
2016-06-22 19:13:57 +03:00
|
|
|
|
2016-06-23 17:36:38 +03:00
|
|
|
constructor(specMgr:SpecManager, elementRef:ElementRef) {
|
|
|
|
super(specMgr);
|
2016-01-24 00:35:44 +03:00
|
|
|
this.element = elementRef.nativeElement;
|
2016-06-23 17:36:38 +03:00
|
|
|
this._normalizer = new SchemaNormalizer(specMgr);
|
2015-10-21 17:24:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2016-06-22 19:13:57 +03:00
|
|
|
this.bindEvents();
|
2015-10-22 21:31:12 +03:00
|
|
|
|
2017-05-05 11:47:00 +03:00
|
|
|
let base:any = this.componentSchema;
|
2017-05-12 11:03:14 +03:00
|
|
|
let sample, xmlSample;
|
2015-10-25 14:26:38 +03:00
|
|
|
|
2016-11-05 17:08:39 +03:00
|
|
|
// got pointer not directly to the schema but e.g. to the response obj
|
2015-10-25 14:26:38 +03:00
|
|
|
if (this.componentSchema.schema) {
|
|
|
|
base = this.componentSchema;
|
|
|
|
this.componentSchema = this.componentSchema.schema;
|
2016-11-05 17:08:39 +03:00
|
|
|
this.pointer += '/schema';
|
2015-10-25 14:26:38 +03:00
|
|
|
}
|
|
|
|
|
2017-03-09 13:55:23 +03:00
|
|
|
// Support x-examples, allowing requests to specify an example.
|
|
|
|
let examplePointer:string = JsonPointer.join(JsonPointer.dirName(this.pointer), 'x-examples');
|
|
|
|
let requestExamples:any = this.specMgr.byPointer(examplePointer);
|
|
|
|
if (requestExamples) {
|
|
|
|
base.examples = requestExamples;
|
|
|
|
}
|
|
|
|
|
2017-05-12 11:03:14 +03:00
|
|
|
let xmlLikeSample = base.examples && getXmlLikeSample(base.examples);
|
|
|
|
if (xmlLikeSample) {
|
|
|
|
this.xmlSample = xmlLikeSample;
|
|
|
|
}
|
|
|
|
|
|
|
|
let jsonLikeSample = base.examples && getJsonLikeSample(base.examples);
|
2017-03-06 10:54:52 +03:00
|
|
|
if (jsonLikeSample) {
|
|
|
|
sample = jsonLikeSample;
|
2015-10-22 21:31:12 +03:00
|
|
|
} else {
|
2016-07-15 17:43:01 +03:00
|
|
|
let selectedDescendant;
|
|
|
|
|
2016-06-22 19:13:57 +03:00
|
|
|
this.componentSchema = this._normalizer.normalize(this.componentSchema, this.pointer);
|
2016-07-15 17:43:01 +03:00
|
|
|
|
2016-11-05 17:08:39 +03:00
|
|
|
let discriminator = this.componentSchema.discriminator || this.componentSchema['x-discriminatorBasePointer'];
|
2016-07-15 17:43:01 +03:00
|
|
|
if (discriminator) {
|
2016-12-28 15:04:57 +03:00
|
|
|
let descendants = this.specMgr.findDerivedDefinitions(this.componentSchema._pointer || this.pointer, this.componentSchema);
|
2016-07-15 17:43:01 +03:00
|
|
|
if (descendants.length) {
|
|
|
|
// TODO: sync up with dropdown
|
|
|
|
selectedDescendant = descendants[0];
|
2016-12-28 15:04:57 +03:00
|
|
|
let descSchema = this.specMgr.getDescendant(selectedDescendant, this.componentSchema);
|
2016-07-15 17:43:01 +03:00
|
|
|
this.componentSchema = this._normalizer.normalize(Object.assign({}, descSchema), selectedDescendant.$ref,
|
|
|
|
{omitParent: false});
|
|
|
|
}
|
|
|
|
}
|
2016-06-22 19:13:57 +03:00
|
|
|
if (this.fromCache()) {
|
2016-07-17 19:44:36 +03:00
|
|
|
this.initButtons();
|
2016-06-22 19:13:57 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-05-29 20:26:20 +03:00
|
|
|
try {
|
2016-06-18 21:02:01 +03:00
|
|
|
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
|
|
|
}
|
2016-07-15 17:43:01 +03:00
|
|
|
if (selectedDescendant) {
|
|
|
|
sample[discriminator] = selectedDescendant.name;
|
|
|
|
}
|
2015-10-22 21:31:12 +03:00
|
|
|
}
|
2016-06-22 19:13:57 +03:00
|
|
|
this.cache(sample);
|
2016-07-20 11:07:08 +03:00
|
|
|
this.sample = sample;
|
2016-07-17 19:44:36 +03:00
|
|
|
this.initButtons();
|
|
|
|
}
|
2016-07-17 18:07:51 +03:00
|
|
|
|
2016-07-17 19:44:36 +03:00
|
|
|
initButtons() {
|
2016-07-20 11:07:08 +03:00
|
|
|
if (typeof this.sample === 'object') {
|
2016-07-17 18:07:51 +03:00
|
|
|
this.enableButtons = true;
|
|
|
|
}
|
2016-06-22 19:13:57 +03:00
|
|
|
}
|
2016-01-24 00:35:44 +03:00
|
|
|
|
2016-06-22 19:13:57 +03:00
|
|
|
cache(sample) {
|
|
|
|
if (this.skipReadOnly) {
|
|
|
|
this.componentSchema['x-redoc-ro-sample'] = sample;
|
|
|
|
} else {
|
|
|
|
this.componentSchema['x-redoc-rw-sample'] = sample;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fromCache() {
|
|
|
|
if (this.skipReadOnly && this.componentSchema['x-redoc-ro-sample']) {
|
2016-07-20 11:07:08 +03:00
|
|
|
this.sample = this.componentSchema['x-redoc-ro-sample'];
|
2016-06-22 19:13:57 +03:00
|
|
|
return true;
|
2016-11-29 22:54:09 +03:00
|
|
|
} else if (!this.skipReadOnly && this.componentSchema['x-redoc-rw-sample']) {
|
2016-07-20 11:07:08 +03:00
|
|
|
this.sample = this.componentSchema['x-redoc-rw-sample'];
|
2016-06-22 19:13:57 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-24 00:35:44 +03:00
|
|
|
|
2016-06-22 19:13:57 +03:00
|
|
|
bindEvents() {
|
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
|
|
|
}
|
2016-07-17 18:07:51 +03:00
|
|
|
|
|
|
|
expandAll() {
|
|
|
|
let elements = this.element.getElementsByClassName('collapsible');
|
|
|
|
for (let i = 0; i < elements.length; i++) {
|
|
|
|
let collapsed = elements[i];
|
|
|
|
collapsed.parentNode.classList.remove('collapsed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
collapseAll() {
|
|
|
|
let elements = this.element.getElementsByClassName('collapsible');
|
|
|
|
for (let i = 0; i < elements.length; i++) {
|
|
|
|
let expanded = elements[i];
|
|
|
|
if (expanded.parentNode.classList.contains('redoc-json')) continue;
|
|
|
|
expanded.parentNode.classList.add('collapsed');
|
|
|
|
}
|
|
|
|
}
|
2016-08-28 21:46:10 +03:00
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.preinit();
|
|
|
|
}
|
2015-10-21 17:24:04 +03:00
|
|
|
}
|