redoc/lib/components/ResponsesList/responses-list.ts

103 lines
2.7 KiB
TypeScript
Raw Normal View History

2015-10-21 12:20:14 +03:00
'use strict';
import { Component,
Input,
OnInit,
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef
} from '@angular/core';
import { BaseSearchableComponent, SpecManager } from '../base';
2015-10-27 20:44:08 +03:00
import JsonPointer from '../../utils/JsonPointer';
2016-05-09 22:55:16 +03:00
import { statusCodeType } from '../../utils/helpers';
import { OptionsService, AppStateService } from '../../services/index';
import { SchemaHelper } from '../../services/schema-helper.service';
2015-10-21 12:20:14 +03:00
function isNumeric(n) {
return (!isNaN(parseFloat(n)) && isFinite(n));
}
2016-08-22 12:12:13 +03:00
@Component({
2015-10-21 12:20:14 +03:00
selector: 'responses-list',
templateUrl: './responses-list.html',
styleUrls: ['./responses-list.css'],
changeDetection: ChangeDetectionStrategy.OnPush
2015-10-21 12:20:14 +03:00
})
export class ResponsesList extends BaseSearchableComponent implements OnInit {
2016-08-22 12:12:13 +03:00
@Input() pointer:string;
responses: Array<any>;
options: any;
2016-08-22 12:12:13 +03:00
constructor(specMgr:SpecManager,
optionsMgr:OptionsService,
app: AppStateService,
private cdr: ChangeDetectorRef
) {
super(specMgr, app);
2016-02-16 16:31:12 +03:00
this.options = optionsMgr.options;
2015-10-21 12:20:14 +03:00
}
init() {
this.responses = [];
2015-10-21 12:20:14 +03:00
let responses = this.componentSchema;
if (!responses) return;
2015-10-21 12:20:14 +03:00
responses = Object.keys(responses).filter(respCode => {
// only response-codes and "default"
return ( isNumeric(respCode) || (respCode === 'default'));
}).map(respCode => {
let resp = responses[respCode];
resp.pointer = JsonPointer.join(this.pointer, respCode);
if (resp.$ref) {
let ref = resp.$ref;
resp = Object.assign({}, this.specMgr.byPointer(resp.$ref));
2015-10-21 12:20:14 +03:00
resp.pointer = ref;
}
resp.empty = !resp.schema;
2015-10-21 12:20:14 +03:00
resp.code = respCode;
2015-11-23 23:57:49 +03:00
resp.type = statusCodeType(resp.code);
2016-12-14 15:49:02 +03:00
resp.expanded = false;
2016-12-14 15:49:02 +03:00
if (this.options.expandResponses) {
if (this.options.expandResponses === 'all' || this.options.expandResponses.has(respCode.toString())) {
resp.expanded = true;
}
}
2016-06-30 16:42:36 +03:00
if (resp.headers && !(resp.headers instanceof Array)) {
2015-11-26 01:36:07 +03:00
resp.headers = Object.keys(resp.headers).map((k) => {
let respInfo = resp.headers[k];
respInfo.name = k;
return SchemaHelper.preprocess(respInfo, this.pointer, this.pointer);
2015-11-26 01:36:07 +03:00
});
resp.empty = false;
2015-11-26 01:36:07 +03:00
}
resp.extendable = resp.headers || resp.length;
2015-10-21 12:20:14 +03:00
return resp;
});
this.responses = responses;
2015-10-21 12:20:14 +03:00
}
2016-06-30 16:42:36 +03:00
2016-12-02 12:59:29 +03:00
trackByCode(_, el) {
2016-06-30 16:42:36 +03:00
return el.code;
}
2016-08-28 21:46:10 +03:00
ensureSearchIsShown(ptr: string) {
if (ptr.startsWith(this.pointer)) {
let code = JsonPointer.relative(this.pointer, ptr)[0];
if (code && this.componentSchema[code]) {
this.componentSchema[code].expanded = true;
this.cdr.markForCheck();
this.cdr.detectChanges();
}
}
}
2016-08-28 21:46:10 +03:00
ngOnInit() {
this.preinit();
}
2015-10-21 12:20:14 +03:00
}