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

140 lines
4.1 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-06-22 21:17:48 +03:00
import { RedocComponent, BaseComponent, SpecManager } from '../base';
2016-05-06 00:48:41 +03:00
import { JsonFormatter } from '../../utils/JsonFormatterPipe';
2016-06-22 21:17:48 +03:00
import { SchemaNormalizer } from '../../services/schema-normalizer.service';
2016-07-17 18:07:51 +03:00
import { CopyButton } from '../../shared/components/CopyButton/copy-button.directive';
2015-10-21 17:24:04 +03:00
@RedocComponent({
selector: 'schema-sample',
templateUrl: './schema-sample.html',
pipes: [JsonFormatter],
2016-07-17 18:07:51 +03:00
directives: [CopyButton],
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;
sample: any;
2016-07-17 18:07:51 +03:00
enableButtons: boolean = false;
@Input() skipReadOnly:boolean;
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
constructor(specMgr:SpecManager, elementRef:ElementRef) {
super(specMgr);
this.element = elementRef.nativeElement;
this._normalizer = new SchemaNormalizer(specMgr);
2015-10-21 17:24:04 +03:00
}
init() {
2016-06-22 19:13:57 +03:00
this.bindEvents();
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 {
let selectedDescendant;
2016-06-22 19:13:57 +03:00
this.componentSchema = this._normalizer.normalize(this.componentSchema, this.pointer);
let discriminator = this.componentSchema.discriminator;
if (discriminator) {
let descendants = this.specMgr.findDerivedDefinitions(this.componentSchema._pointer || this.pointer);
if (descendants.length) {
// TODO: sync up with dropdown
selectedDescendant = descendants[0];
let descSchema = this.specMgr.byPointer(selectedDescendant.$ref);
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 {
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
}
if (selectedDescendant) {
sample[discriminator] = selectedDescendant.name;
}
}
2016-06-22 19:13:57 +03:00
this.cache(sample);
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() {
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-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']) {
this.sample = this.componentSchema['x-redoc-ro-sample'];
2016-06-22 19:13:57 +03:00
return true;
} else if (this.componentSchema['x-redoc-rw-sample']) {
this.sample = this.componentSchema['x-redoc-rw-sample'];
2016-06-22 19:13:57 +03:00
return true;
}
return false;
}
2016-06-22 19:13:57 +03:00
bindEvents() {
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');
}
}
2015-10-21 17:24:04 +03:00
}