-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathrequest-form-table.js
More file actions
290 lines (268 loc) · 13 KB
/
request-form-table.js
File metadata and controls
290 lines (268 loc) · 13 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* eslint-disable indent */
import { html } from 'lit';
import { toMarkdown } from '../utils/common-utils.js';
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
import { isPatternProperty } from '../utils/schema-utils.js';
import { map } from 'lit/directives/map.js';
import { range } from 'lit/directives/range.js';
function generateFormRows(data, options, dataType = 'object', key = '', description = '', schemaLevel = 0) {
const newSchemaLevel = data['::type'] && data['::type'].startsWith('xxx-of') ? schemaLevel : (schemaLevel + 1);
if (!data) {
return null;
}
if (Object.keys(data).length === 0) {
return null;
}
let rawKeyLabel = '';
let keyDescr = '';
let isOneOfLabel = false;
if (key.startsWith('::ONE~OF') || key.startsWith('::ANY~OF')) {
rawKeyLabel = key.replace('::', '').replace('~', ' ');
isOneOfLabel = true;
} else if (key.startsWith('::OPTION')) {
const parts = key.split('~');
rawKeyLabel = parts[1];
keyDescr = parts[2];
} else {
rawKeyLabel = key;
}
const keyLabel = rawKeyLabel.replace(/[*]$/, '');
const isRequired = rawKeyLabel.endsWith('*');
if (typeof data === 'object') {
const flags = data['::flags'] || {};
if (flags['🆁']) {
return undefined;
}
const displayLine = [description].filter(v => v).join(' ');
return html`
${newSchemaLevel >= 0 && key
? html`
<tr class='complex-object-display ${data['::type']}' data-obj='${keyLabel}'>
<td class="key ${data['::deprecated'] ? 'deprecated' : ''}">
<div style="display: flex; align-items: center">
${data['::type'] === 'xxx-of-option' || key.startsWith('::OPTION')
? html`<span class="xxx-of-key">${keyLabel}</span><span class="${isOneOfLabel ? 'xxx-of-key' : 'xxx-of-descr'}">${keyDescr}</span>`
: isRequired
? html`<span class="key-label requiredStar" style="display:inline-block;" title="Required">${keyLabel}</span>`
: html`<span class="key-label" style="display:inline-block;">${keyLabel === '::props' ? '' : keyLabel}</span>`
}
</div>
</td>
<td>
<!-- Leave empty, there are no objects that make sense in the form data -->
</td>
<td class='key-descr m-markdown-small'>${unsafeHTML(toMarkdown(displayLine))}</td>
</tr>`
: html`${data['::type'] === 'array' && dataType === 'array' ? html`<tr><td> ${dataType} </td> </tr>` : ''}`
}
${Array.isArray(data) && data[0] ? html`${generateFormRows.call(this, data[0], options, 'xxx-of-option', '::ARRAY~OF', '', newSchemaLevel)}`
: html`${Object.keys(data).map((dataKey) =>
!['::metadata', '::title', '::description', '::type', '::link', '::props', '::deprecated', '::array-type', '::dataTypeLabel', '::flags'].includes(dataKey)
|| data[dataKey]?.['::type'] && !data[dataKey]['::type'].includes('xxx-of')
? html`${generateFormRows.call(this, data[dataKey]['::type'] === 'array' ? data[dataKey]['::props'] : data[dataKey],
options, data[dataKey]['::type'], dataKey, data[dataKey]['::description'], newSchemaLevel)}`
: '')}`
}`;
}
// For Primitive Data types
const parsedData = JSON.parse(data);
return generatePrimitiveRow.call(this, parsedData, { key, keyLabel, keyDescr, description, dataType, isRequired, options });
}
function generatePrimitiveRow(rowData, parentRecursionOptions) {
const { type, format, readOrWriteOnly, constraints, defaultValue, example, allowedValues, pattern, schemaDescription, schemaTitle, deprecated } = rowData;
const { key, keyLabel, keyDescr, description, dataType, isRequired, options } = parentRecursionOptions;
if (readOrWriteOnly === '🆁') {
return undefined;
}
const elementId = this.elementId || `${this.method}-${this.path}`;
const duplicateRowGeneratorKey = `${elementId}-${key}`;
const rowGenerator = (e) => {
if (e.target.dataset?.ptype !== 'pattern-property-key' && !isPatternProperty(e.target.dataset?.pname)) {
return;
}
// If the row key has a value then add another row
const patternPropertyKeyEls = [...this.querySelectorAll("[data-ptype='pattern-property-key']")];
const patternPropertyInputEls = [...this.querySelectorAll("[data-ptype='form-input']")].filter(el => isPatternProperty(el.dataset.pname));
// If there is still some row that either has an empty key or an empty value, then skip adding a new row
if (patternPropertyKeyEls.some((keyElement, index) => !keyElement.value || !patternPropertyInputEls[index].value)) {
return;
}
if (e.target.value) {
this.duplicatedRowsByKey[duplicateRowGeneratorKey] = (this.duplicatedRowsByKey[duplicateRowGeneratorKey] || 1) + 1;
this.requestUpdate();
}
};
const arrayIterator = map(range(this.duplicatedRowsByKey?.[duplicateRowGeneratorKey] || 1), () => html`
<tr>
${inputFieldKeyLabel.call(this, key.startsWith('::OPTION'), keyLabel, keyDescr, dataType, deprecated, isRequired, schemaTitle, format || type, rowGenerator)}
${dataType === 'array' ? getArrayFormField.call(this, keyLabel, example, defaultValue, format, rowGenerator) : ''}
${dataType !== 'array' ? getPrimitiveFormField.call(this, keyLabel, example, defaultValue, format, options, rowGenerator) : ''}
<td>
${description ? html`<div class="param-description">${unsafeHTML(toMarkdown(description))}</div>` : ''}
${defaultValue || constraints || allowedValues || pattern
? html`
<div class="param-constraint">
${pattern ? html`<span style="font-weight:bold">Pattern: </span>${pattern}<br/>` : ''}
${constraints.length ? html`<span style="font-weight:bold">Constraints: </span>${constraints.join(', ')}<br/>` : ''}
${allowedValues?.filter(v => v !== '').map((v, i) => html`
${i > 0 ? '|' : html`<span style="font-weight:bold">Allowed: </span>`}
${html`
<a part="anchor anchor-param-constraint"
data-type="${type === 'array' ? type : 'string'}"
data-enum="${v?.trim()}"
@click="${(e) => {
const inputEl = e.target.closest('table').querySelector(`[data-pname="${keyLabel}"]`);
if (inputEl) {
inputEl.value = e.target.dataset.type === 'array' ? [e.target.dataset.enum] : e.target.dataset.enum;
}
this.computeCurlSyntax();
}}">
${v === null ? '-' : v}
</a>`
}`)
}
</div>`
: ''
}
</td>
</tr>
${schemaDescription || example ? html`<tr class="form-parameter-description">
<td> </td>
<td colspan="2" style="margin-top:0; padding:0 5px 8px 5px;">
<span class="m-markdown-small">${unsafeHTML(toMarkdown(schemaDescription || ''))}</span>
${example
? html`<span>
<span style="font-weight:bold"> Example: </span>
${type === 'array' ? '[ ' : ''}
<a part="anchor anchor-param-example"
data-example-type="${type === 'array' ? type : 'string'}"
data-example = "${Array.isArray(example) && example.join('~|~') || example || ''}"
@click="${(e) => {
const inputEl = e.target.closest('table').querySelector(`[data-pname="${keyLabel}"]`);
if (inputEl) {
inputEl.value = e.target.dataset.exampleType === 'array' ? e.target.dataset.example.split('~|~') : e.target.dataset.example;
}
this.computeCurlSyntax();
}}">
${type === 'array' ? example.join(', ') : example}
</a>
${type === 'array' ? '] ' : ''}
</span>`
: ''}
</td>
</tr>` : ''}`);
return Array.from(arrayIterator);
}
function inputFieldKeyLabel(isOption, keyLabel, keyDescription, dataType, deprecated, isRequired, schemaTitle, format, rowGenerator) {
if (isPatternProperty(keyLabel)) {
return html`
<td style="width:160px; min-width:100px;">
<div class="param-name ${deprecated ? 'deprecated' : ''}">
<input placeholder="${keyLabel}"
@input="${(e) => { rowGenerator(e); this.computeCurlSyntax(); }}"
.value = "${''}"
spellcheck = "false"
type = "${format === 'binary' ? 'file' : format === 'password' ? 'password' : 'text'}"
part = "textbox textbox-param"
style = "width:100%"
data-ptype = "pattern-property-key"
data-pname = "${keyLabel}"
data-default = "${''}"
data-array = "false"
/>
</td>`;
}
return html`
<td style="width:160px; min-width:100px;">
<div class="param-name ${deprecated ? 'deprecated' : ''}">
${!deprecated && isRequired
? html`<span class="key-label">${keyLabel}</span><span style='color:var(--red);'>*</span>`
: isOption
? html`<span class='xxx-of-key'>${keyLabel}</span><span class="xxx-of-descr">${keyDescription}</span>`
: html`${keyLabel ? html`<span class="key-label"> ${keyLabel}</span>` : html`<span class="xxx-of-descr">${schemaTitle}</span>`}`
}
</div>
<div class="param-type">
${dataType === 'array' ? html`[<span>${format}</span>]` : `${format}`}
</div>
</td>`;
}
// function getObjectFormField(keyLabel, example, defaultValue, format, options) {
// return html`
// <td>
// <div class="tab-panel row" style="min-height:300px; border-left: 6px solid var(--light-border-color); align-items: stretch;">
// <div class="tab-content col" data-tab = 'body' style="display: block; padding-left:5px; width:100%">
// <textarea
// class = "textarea" placeholder="${example || defaultValue || ''}"
// part = "textarea textarea-param"
// style = "width:100%; border:none; resize:vertical;"
// data-array = "false"
// data-ptype = "form-input"
// data-pname = "${keyLabel}"
// spellcheck = "false"
// .value="${options.fillRequestWithDefault === 'true' ? defaultValue : ''}"
// ></textarea>
// <!-- This textarea(hidden) is to store the original example value, in focused mode on navbar change it is used to update the example text -->
// <textarea data-pname = "hidden-${keyLabel}" data-ptype = "hidden-form-input" class="is-hidden" style="display:none" .value="${defaultValue}"></textarea>
// </div>
// </div>
// </td>`;
// }
function getArrayFormField(keyLabel, example, defaultValue, format, rowGenerator) {
if (format === 'binary') {
return html`<td style="min-width:100px;">
<div class="file-input-container col" style='align-items:flex-end;' @click="${(e) => this.onAddRemoveFileInput(e, keyLabel)}">
<div class='input-set row'>
<input
@input="${(e) => { rowGenerator(e); this.computeCurlSyntax(); }}"
type = "file"
part = "file-input"
class="file-input"
data-pname = "${keyLabel}"
data-ptype = "form-input"
data-array = "false"
data-file-array = "true"
/>
<button class="file-input-remove-btn"> ✕ </button>
</div>
<button class="m-btn primary file-input-add-btn" part="btn btn-fill" style="margin:2px 25px 0 0; padding:2px 6px;">ADD</button>
</div>
</td>`;
}
return html`<td style="min-width:100px;">
<tag-input
@change="${(e) => { rowGenerator(e); this.computeCurlSyntax(); }}"
style = "width:100%;"
data-ptype = "form-input"
data-pname = "${keyLabel}"
data-array = "true"
placeholder="${(Array.isArray(example) ? example[0] : example) || defaultValue || 'add-multiple ↩'}"
.value = "${defaultValue || ''}"
></tag-input>
</td>`;
}
function getPrimitiveFormField(keyLabel, example, defaultValue, format, options, rowGenerator) {
return html`<td style="min-width:100px;">
<input placeholder="${example || defaultValue || ''}"
@input="${(e) => { rowGenerator(e); this.computeCurlSyntax(); }}"
.value = "${options.fillRequestWithDefault && defaultValue || ''}"
spellcheck = "false"
type = "${format === 'binary' ? 'file' : format === 'password' ? 'password' : 'text'}"
part = "textbox textbox-param"
style = "width:100%"
data-ptype = "form-input"
data-pname = "${keyLabel}"
data-array = "false"
/>
</td>`;
}
export default function getRequestFormTable(data, mimeType) {
const options = {
mimeType: mimeType,
fillRequestWithDefault: this.fillRequestWithDefault === 'true'
};
return html`
<table id="request-form-table" role="presentation" class="request-form-table" style = 'border:1px solid var(--light-border-color); width: 100%'>
${data ? html`${generateFormRows.call(this, data['::type'] === 'array' ? data['::props'] : data, options, data['::type'])}` : ''}
</table>`;
}