2015-10-21 12:20:14 +03:00
|
|
|
'use strict';
|
|
|
|
|
2017-01-19 00:48:55 +03:00
|
|
|
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';
|
2017-01-19 00:48:55 +03:00
|
|
|
import { OptionsService, AppStateService } from '../../services/index';
|
2016-07-28 07:24:12 +03:00
|
|
|
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',
|
2016-05-25 18:34:31 +03:00
|
|
|
templateUrl: './responses-list.html',
|
2016-08-30 17:07:54 +03:00
|
|
|
styleUrls: ['./responses-list.css'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
2015-10-21 12:20:14 +03:00
|
|
|
})
|
2017-01-19 00:48:55 +03:00
|
|
|
export class ResponsesList extends BaseSearchableComponent implements OnInit {
|
2016-08-22 12:12:13 +03:00
|
|
|
@Input() pointer:string;
|
|
|
|
|
2016-07-20 11:07:08 +03:00
|
|
|
responses: Array<any>;
|
2016-05-25 18:34:31 +03:00
|
|
|
options: any;
|
2016-08-22 12:12:13 +03:00
|
|
|
|
2017-01-19 00:48:55 +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
|
|
|
}
|
|
|
|
|
2016-07-20 11:07:08 +03:00
|
|
|
init() {
|
|
|
|
this.responses = [];
|
2015-12-19 18:18:48 +03:00
|
|
|
|
2015-10-21 12:20:14 +03:00
|
|
|
let responses = this.componentSchema;
|
2015-12-19 18:18:48 +03:00
|
|
|
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;
|
2017-01-04 15:49:49 +03:00
|
|
|
resp = Object.assign({}, this.specMgr.byPointer(resp.$ref));
|
2015-10-21 12:20:14 +03:00
|
|
|
resp.pointer = ref;
|
|
|
|
}
|
|
|
|
|
2016-01-24 22:27:15 +03:00
|
|
|
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
|
|
|
|
2017-01-19 00:48:55 +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;
|
2016-07-28 07:24:12 +03:00
|
|
|
return SchemaHelper.preprocess(respInfo, this.pointer, this.pointer);
|
2015-11-26 01:36:07 +03:00
|
|
|
});
|
2016-01-24 22:27:15 +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;
|
|
|
|
});
|
2016-07-20 11:07:08 +03:00
|
|
|
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
|
|
|
|
2017-01-19 00:48:55 +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
|
|
|
}
|