forked from Redocly/redoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema-sample.ts
144 lines (124 loc) · 4.27 KB
/
schema-sample.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
import { Component, ElementRef, Input, ChangeDetectionStrategy, OnInit } from '@angular/core';
import * as OpenAPISampler from 'openapi-sampler';
import { BaseComponent, SpecManager } from '../base';
import { SchemaNormalizer } from '../../services/schema-normalizer.service';
import { getJsonLike } from '../../utils/helpers';
@Component({
selector: 'schema-sample',
templateUrl: './schema-sample.html',
styleUrls: ['./schema-sample.css'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class SchemaSample extends BaseComponent implements OnInit {
@Input() pointer:string;
@Input() skipReadOnly:boolean;
element: any;
sample: any;
enableButtons: boolean = false;
private _normalizer:SchemaNormalizer;
constructor(specMgr:SpecManager, elementRef:ElementRef) {
super(specMgr);
this.element = elementRef.nativeElement;
this._normalizer = new SchemaNormalizer(specMgr);
}
init() {
this.bindEvents();
let base:any = {};
let sample;
// got pointer not directly to the schema but e.g. to the response obj
if (this.componentSchema.schema) {
base = this.componentSchema;
this.componentSchema = this.componentSchema.schema;
this.pointer += '/schema';
}
let jsonLikeSample = base.examples && getJsonLike(base.examples);
if (jsonLikeSample) {
sample = jsonLikeSample;
} else {
let selectedDescendant;
this.componentSchema = this._normalizer.normalize(this.componentSchema, this.pointer);
let discriminator = this.componentSchema.discriminator || this.componentSchema['x-discriminatorBasePointer'];
if (discriminator) {
let descendants = this.specMgr.findDerivedDefinitions(this.componentSchema._pointer || this.pointer, this.componentSchema);
if (descendants.length) {
// TODO: sync up with dropdown
selectedDescendant = descendants[0];
let descSchema = this.specMgr.getDescendant(selectedDescendant, this.componentSchema);
this.componentSchema = this._normalizer.normalize(Object.assign({}, descSchema), selectedDescendant.$ref,
{omitParent: false});
}
}
if (this.fromCache()) {
this.initButtons();
return;
}
try {
sample = OpenAPISampler.sample(this.componentSchema, {
skipReadOnly: this.skipReadOnly
});
} catch(e) {
// no sample available
}
if (selectedDescendant) {
sample[discriminator] = selectedDescendant.name;
}
}
this.cache(sample);
this.sample = sample;
this.initButtons();
}
initButtons() {
if (typeof this.sample === 'object') {
this.enableButtons = true;
}
}
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'];
return true;
} else if (!this.skipReadOnly && this.componentSchema['x-redoc-rw-sample']) {
this.sample = this.componentSchema['x-redoc-rw-sample'];
return true;
}
return false;
}
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');
}
}
});
}
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');
}
}
ngOnInit() {
this.preinit();
}
}