redoc/lib/components/RequestSamples/request-samples.js

50 lines
1.6 KiB
JavaScript
Raw Normal View History

2016-01-31 20:37:51 +03:00
'use strict';
import {ViewChildren, QueryList, ChangeDetectorRef, ChangeDetectionStrategy} from 'angular2/core';
import {RedocComponent, BaseComponent, SchemaManager} from '../base';
2016-01-31 20:37:51 +03:00
import JsonPointer from '../../utils/JsonPointer';
import {Tabs, Tab} from '../../common/components/Tabs/tabs';
import SchemaSample from '../SchemaSample/schema-sample';
import {PrismPipe} from '../../utils/pipes';
2016-02-03 17:47:20 +03:00
import {redocEvents} from '../../events';
2016-01-31 20:37:51 +03:00
@RedocComponent({
selector: 'request-samples',
templateUrl: './lib/components/RequestSamples/request-samples.html',
styleUrls: ['./lib/components/RequestSamples/request-samples.css'],
directives: [SchemaSample, Tabs, Tab],
inputs: ['bodySchemaPtr'],
2016-02-03 17:47:20 +03:00
pipes: [PrismPipe],
changeDetection: ChangeDetectionStrategy.OnPush
2016-01-31 20:37:51 +03:00
})
@Reflect.metadata('parameters', [[SchemaManager], [new ViewChildren(Tabs), QueryList], [ChangeDetectorRef]])
2016-01-31 20:37:51 +03:00
export default class RequestSamples extends BaseComponent {
2016-02-03 17:47:20 +03:00
constructor(schemaMgr, tabs, changeDetector) {
2016-01-31 20:37:51 +03:00
super(schemaMgr);
2016-02-03 17:47:20 +03:00
tabs.changes.subscribe(_ => {
this.tabs = tabs.first;
this.subscribeForEvents(_);
});
this.changeDetector = changeDetector;
}
changeLangNotify(lang) {
redocEvents.samplesLanguageChanged.next(lang);
}
subscribeForEvents() {
if (!this.tabs) return;
redocEvents.samplesLanguageChanged.subscribe((sampleLang) => {
this.tabs.selectyByTitle(sampleLang);
this.changeDetector.markForCheck();
});
2016-01-31 20:37:51 +03:00
}
prepareModel() {
this.data = {};
this.data.bodySchemaPtr = JsonPointer.join(this.bodySchemaPtr, 'schema');
this.data.samples = this.componentSchema['x-code-samples'] || [];
}
}