sendproof-core 0.1.0.alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +55 -0
- data/lib/sendproof/assets.rb +70 -0
- data/lib/sendproof/attribution.rb +98 -0
- data/lib/sendproof/capture.rb +66 -0
- data/lib/sendproof/clock.rb +54 -0
- data/lib/sendproof/configuration.rb +241 -0
- data/lib/sendproof/data_store.rb +34 -0
- data/lib/sendproof/dynamodb/store.rb +183 -0
- data/lib/sendproof/dynamodb.rb +17 -0
- data/lib/sendproof/elasticsearch/store.rb +24 -0
- data/lib/sendproof/elasticsearch.rb +17 -0
- data/lib/sendproof/firestore/interceptor.rb +38 -0
- data/lib/sendproof/firestore/store.rb +150 -0
- data/lib/sendproof/firestore.rb +99 -0
- data/lib/sendproof/handler.rb +23 -0
- data/lib/sendproof/identity.rb +37 -0
- data/lib/sendproof/limits.rb +113 -0
- data/lib/sendproof/middleware.rb +68 -0
- data/lib/sendproof/mongo/engine.rb +692 -0
- data/lib/sendproof/mongo/guarded.rb +100 -0
- data/lib/sendproof/mongo/plain.rb +45 -0
- data/lib/sendproof/mongo/store.rb +54 -0
- data/lib/sendproof/mongo/subscriber.rb +21 -0
- data/lib/sendproof/mongo.rb +41 -0
- data/lib/sendproof/mongoid/store.rb +58 -0
- data/lib/sendproof/mongoid.rb +65 -0
- data/lib/sendproof/observed_request.rb +108 -0
- data/lib/sendproof/opensearch/store.rb +21 -0
- data/lib/sendproof/opensearch.rb +17 -0
- data/lib/sendproof/operation_matcher.rb +28 -0
- data/lib/sendproof/pool_hooks.rb +80 -0
- data/lib/sendproof/preview_request.rb +44 -0
- data/lib/sendproof/preview_transport.rb +331 -0
- data/lib/sendproof/protocol.rb +81 -0
- data/lib/sendproof/provider.rb +83 -0
- data/lib/sendproof/recorder.rb +298 -0
- data/lib/sendproof/redaction.rb +98 -0
- data/lib/sendproof/redis/commands.rb +76 -0
- data/lib/sendproof/redis/store.rb +205 -0
- data/lib/sendproof/redis.rb +18 -0
- data/lib/sendproof/search/middleware.rb +39 -0
- data/lib/sendproof/search/store.rb +213 -0
- data/lib/sendproof/search.rb +82 -0
- data/lib/sendproof/stores/classify.rb +346 -0
- data/lib/sendproof/stores/guard.rb +127 -0
- data/lib/sendproof/stores/tables.rb +1328 -0
- data/lib/sendproof/stores.rb +12 -0
- data/lib/sendproof/trace_store.rb +110 -0
- data/lib/sendproof/transaction_control.rb +187 -0
- data/lib/sendproof/ui/action-bar.js +91 -0
- data/lib/sendproof/ui/api.js +239 -0
- data/lib/sendproof/ui/app.css +848 -0
- data/lib/sendproof/ui/app.js +742 -0
- data/lib/sendproof/ui/autofill.js +137 -0
- data/lib/sendproof/ui/code-copy.js +71 -0
- data/lib/sendproof/ui/code-picker.js +149 -0
- data/lib/sendproof/ui/commands.js +197 -0
- data/lib/sendproof/ui/components.js +77 -0
- data/lib/sendproof/ui/context-bar.js +82 -0
- data/lib/sendproof/ui/copy.js +92 -0
- data/lib/sendproof/ui/coverage-popover.js +35 -0
- data/lib/sendproof/ui/crumb-fit.js +80 -0
- data/lib/sendproof/ui/deprecation.js +164 -0
- data/lib/sendproof/ui/display-settings.js +175 -0
- data/lib/sendproof/ui/doc-diff.js +209 -0
- data/lib/sendproof/ui/docs-view.js +347 -0
- data/lib/sendproof/ui/favicon.png +0 -0
- data/lib/sendproof/ui/favicon.svg +1 -0
- data/lib/sendproof/ui/field-help.js +52 -0
- data/lib/sendproof/ui/field-modules.js +23 -0
- data/lib/sendproof/ui/fields/controls.js +179 -0
- data/lib/sendproof/ui/fields/dom.js +36 -0
- data/lib/sendproof/ui/fields/infer.js +71 -0
- data/lib/sendproof/ui/fields/lookup-source.js +63 -0
- data/lib/sendproof/ui/fields/lookup.js +285 -0
- data/lib/sendproof/ui/fields/render.js +496 -0
- data/lib/sendproof/ui/fields/select.js +175 -0
- data/lib/sendproof/ui/fields/validate.js +363 -0
- data/lib/sendproof/ui/fields/validation-formats.js +164 -0
- data/lib/sendproof/ui/fields/validation-references.js +68 -0
- data/lib/sendproof/ui/fields/validation-values.js +137 -0
- data/lib/sendproof/ui/generate-menu.js +44 -0
- data/lib/sendproof/ui/generate.js +184 -0
- data/lib/sendproof/ui/header-editor.js +777 -0
- data/lib/sendproof/ui/headers.js +188 -0
- data/lib/sendproof/ui/highlight.js +257 -0
- data/lib/sendproof/ui/history-popover.js +24 -0
- data/lib/sendproof/ui/i18n.js +253 -0
- data/lib/sendproof/ui/icons.js +87 -0
- data/lib/sendproof/ui/index.html +70 -0
- data/lib/sendproof/ui/issues.js +187 -0
- data/lib/sendproof/ui/json-view.js +166 -0
- data/lib/sendproof/ui/lang-icons.js +62 -0
- data/lib/sendproof/ui/layout.js +262 -0
- data/lib/sendproof/ui/listbox.js +334 -0
- data/lib/sendproof/ui/locales/ar-capture.js +366 -0
- data/lib/sendproof/ui/locales/ar-stores.js +116 -0
- data/lib/sendproof/ui/locales/ar.js +561 -0
- data/lib/sendproof/ui/locales/cs-capture.js +442 -0
- data/lib/sendproof/ui/locales/cs-stores.js +116 -0
- data/lib/sendproof/ui/locales/cs.js +689 -0
- data/lib/sendproof/ui/locales/da-capture.js +347 -0
- data/lib/sendproof/ui/locales/da-stores.js +116 -0
- data/lib/sendproof/ui/locales/da.js +522 -0
- data/lib/sendproof/ui/locales/de-capture.js +347 -0
- data/lib/sendproof/ui/locales/de-stores.js +116 -0
- data/lib/sendproof/ui/locales/de.js +519 -0
- data/lib/sendproof/ui/locales/el-capture.js +347 -0
- data/lib/sendproof/ui/locales/el-stores.js +116 -0
- data/lib/sendproof/ui/locales/el.js +520 -0
- data/lib/sendproof/ui/locales/en-capture.js +349 -0
- data/lib/sendproof/ui/locales/en-stores.js +95 -0
- data/lib/sendproof/ui/locales/en.js +522 -0
- data/lib/sendproof/ui/locales/es-capture.js +353 -0
- data/lib/sendproof/ui/locales/es-stores.js +116 -0
- data/lib/sendproof/ui/locales/es.js +525 -0
- data/lib/sendproof/ui/locales/fa-capture.js +348 -0
- data/lib/sendproof/ui/locales/fa-stores.js +116 -0
- data/lib/sendproof/ui/locales/fa.js +521 -0
- data/lib/sendproof/ui/locales/fi-capture.js +347 -0
- data/lib/sendproof/ui/locales/fi-stores.js +116 -0
- data/lib/sendproof/ui/locales/fi.js +523 -0
- data/lib/sendproof/ui/locales/fr-capture.js +353 -0
- data/lib/sendproof/ui/locales/fr-stores.js +116 -0
- data/lib/sendproof/ui/locales/fr.js +525 -0
- data/lib/sendproof/ui/locales/he-capture.js +358 -0
- data/lib/sendproof/ui/locales/he-stores.js +116 -0
- data/lib/sendproof/ui/locales/he.js +529 -0
- data/lib/sendproof/ui/locales/hi-capture.js +348 -0
- data/lib/sendproof/ui/locales/hi-stores.js +116 -0
- data/lib/sendproof/ui/locales/hi.js +523 -0
- data/lib/sendproof/ui/locales/id-capture.js +347 -0
- data/lib/sendproof/ui/locales/id-stores.js +116 -0
- data/lib/sendproof/ui/locales/id.js +521 -0
- data/lib/sendproof/ui/locales/it-capture.js +361 -0
- data/lib/sendproof/ui/locales/it-stores.js +116 -0
- data/lib/sendproof/ui/locales/it.js +523 -0
- data/lib/sendproof/ui/locales/ja-capture.js +346 -0
- data/lib/sendproof/ui/locales/ja-stores.js +116 -0
- data/lib/sendproof/ui/locales/ja.js +521 -0
- data/lib/sendproof/ui/locales/ko-capture.js +346 -0
- data/lib/sendproof/ui/locales/ko-stores.js +116 -0
- data/lib/sendproof/ui/locales/ko.js +522 -0
- data/lib/sendproof/ui/locales/nb-capture.js +349 -0
- data/lib/sendproof/ui/locales/nb-stores.js +116 -0
- data/lib/sendproof/ui/locales/nb.js +521 -0
- data/lib/sendproof/ui/locales/nl-capture.js +355 -0
- data/lib/sendproof/ui/locales/nl-stores.js +116 -0
- data/lib/sendproof/ui/locales/nl.js +520 -0
- data/lib/sendproof/ui/locales/pl-capture.js +432 -0
- data/lib/sendproof/ui/locales/pl-stores.js +116 -0
- data/lib/sendproof/ui/locales/pl.js +528 -0
- data/lib/sendproof/ui/locales/pt-BR-capture.js +350 -0
- data/lib/sendproof/ui/locales/pt-BR-stores.js +116 -0
- data/lib/sendproof/ui/locales/pt-BR.js +523 -0
- data/lib/sendproof/ui/locales/pt-PT-capture.js +350 -0
- data/lib/sendproof/ui/locales/pt-PT-stores.js +116 -0
- data/lib/sendproof/ui/locales/pt-PT.js +523 -0
- data/lib/sendproof/ui/locales/ru-capture.js +437 -0
- data/lib/sendproof/ui/locales/ru-stores.js +116 -0
- data/lib/sendproof/ui/locales/ru.js +624 -0
- data/lib/sendproof/ui/locales/sv-capture.js +349 -0
- data/lib/sendproof/ui/locales/sv-stores.js +116 -0
- data/lib/sendproof/ui/locales/sv.js +518 -0
- data/lib/sendproof/ui/locales/th-capture.js +347 -0
- data/lib/sendproof/ui/locales/th-stores.js +116 -0
- data/lib/sendproof/ui/locales/th.js +521 -0
- data/lib/sendproof/ui/locales/tr-capture.js +348 -0
- data/lib/sendproof/ui/locales/tr-stores.js +116 -0
- data/lib/sendproof/ui/locales/tr.js +521 -0
- data/lib/sendproof/ui/locales/uk-capture.js +442 -0
- data/lib/sendproof/ui/locales/uk-stores.js +116 -0
- data/lib/sendproof/ui/locales/uk.js +630 -0
- data/lib/sendproof/ui/locales/vi-capture.js +346 -0
- data/lib/sendproof/ui/locales/vi-stores.js +116 -0
- data/lib/sendproof/ui/locales/vi.js +523 -0
- data/lib/sendproof/ui/locales/zh-Hans-capture.js +347 -0
- data/lib/sendproof/ui/locales/zh-Hans-stores.js +116 -0
- data/lib/sendproof/ui/locales/zh-Hans.js +523 -0
- data/lib/sendproof/ui/locales/zh-Hant-capture.js +346 -0
- data/lib/sendproof/ui/locales/zh-Hant-stores.js +116 -0
- data/lib/sendproof/ui/locales/zh-Hant.js +522 -0
- data/lib/sendproof/ui/manifest.json +866 -0
- data/lib/sendproof/ui/markdown.js +39 -0
- data/lib/sendproof/ui/model-view.js +182 -0
- data/lib/sendproof/ui/navigation.js +233 -0
- data/lib/sendproof/ui/operation-header.js +76 -0
- data/lib/sendproof/ui/operations.js +7 -0
- data/lib/sendproof/ui/overview-view.js +63 -0
- data/lib/sendproof/ui/personas.js +59 -0
- data/lib/sendproof/ui/popover.js +71 -0
- data/lib/sendproof/ui/preview-false.js +108 -0
- data/lib/sendproof/ui/request-editor.js +496 -0
- data/lib/sendproof/ui/response-metadata.js +354 -0
- data/lib/sendproof/ui/response-viewer.js +618 -0
- data/lib/sendproof/ui/result-blocked.js +155 -0
- data/lib/sendproof/ui/result-capture.js +25 -0
- data/lib/sendproof/ui/result-database.js +185 -0
- data/lib/sendproof/ui/result-effects.js +176 -0
- data/lib/sendproof/ui/result-headers.js +106 -0
- data/lib/sendproof/ui/result-logs.js +408 -0
- data/lib/sendproof/ui/result-states.js +213 -0
- data/lib/sendproof/ui/results.js +371 -0
- data/lib/sendproof/ui/router.js +118 -0
- data/lib/sendproof/ui/schema-model.js +336 -0
- data/lib/sendproof/ui/schema-tree.js +344 -0
- data/lib/sendproof/ui/schema.js +397 -0
- data/lib/sendproof/ui/security.js +31 -0
- data/lib/sendproof/ui/seeded-menu.js +69 -0
- data/lib/sendproof/ui/settings.js +93 -0
- data/lib/sendproof/ui/shortcuts-sheet.js +73 -0
- data/lib/sendproof/ui/shortcuts.js +190 -0
- data/lib/sendproof/ui/sidebar.js +289 -0
- data/lib/sendproof/ui/signin.js +259 -0
- data/lib/sendproof/ui/snippets.js +530 -0
- data/lib/sendproof/ui/store-marks.js +47 -0
- data/lib/sendproof/ui/stores.js +131 -0
- data/lib/sendproof/ui/styles.css +971 -0
- data/lib/sendproof/ui/toggletip.js +53 -0
- data/lib/sendproof/ui/uuid.js +10 -0
- data/lib/sendproof/ui/view.js +122 -0
- data/lib/sendproof/ui/views.css +653 -0
- data/lib/sendproof/version.rb +9 -0
- data/lib/sendproof/workbench.rb +102 -0
- data/lib/sendproof-core.rb +3 -0
- data/lib/sendproof.rb +121 -0
- metadata +292 -0
|
@@ -0,0 +1,777 @@
|
|
|
1
|
+
import { dereference } from "./api.js";
|
|
2
|
+
import {
|
|
3
|
+
coerceHeaderValue, createHeaderRow, formatHeaderLines, headerNameFix, headerRowsToObject,
|
|
4
|
+
headerVariables, inspectHeaderRows, normalizeHeaderRows, parseHeaderLines,
|
|
5
|
+
} from "./headers.js";
|
|
6
|
+
import { resolveSchema, validateSchemaValue } from "./schema.js";
|
|
7
|
+
import { newUuid } from "./uuid.js";
|
|
8
|
+
import { esc, icon, stateBlock } from "./components.js";
|
|
9
|
+
import { t, th } from "./i18n.js";
|
|
10
|
+
import { matchesShortcut, shortcutAttrs, shortcutHint } from "./shortcuts.js";
|
|
11
|
+
|
|
12
|
+
// Presets: [message key for the menu label, header name, header value]. Names and values are
|
|
13
|
+
// sent as written; only the label is translated.
|
|
14
|
+
const presets = [
|
|
15
|
+
["kv.preset.acceptJson", "Accept", "application/json"],
|
|
16
|
+
["kv.preset.requestId", "X-Request-ID", "{{uuid}}"],
|
|
17
|
+
["kv.preset.bypassCache", "Cache-Control", "no-cache"],
|
|
18
|
+
];
|
|
19
|
+
// Suggested header names (never translated) with the message key for their hint.
|
|
20
|
+
const standardHeaders = [
|
|
21
|
+
["Accept", "kv.std.accept"],
|
|
22
|
+
["Accept-Language", "kv.std.acceptLanguage"],
|
|
23
|
+
["Authorization", "kv.std.authorization"],
|
|
24
|
+
["Cache-Control", "kv.std.cacheControl"],
|
|
25
|
+
["Content-Type", "kv.std.contentType"],
|
|
26
|
+
["If-Match", "kv.std.ifMatch"],
|
|
27
|
+
["If-Modified-Since", "kv.std.ifModifiedSince"],
|
|
28
|
+
["If-None-Match", "kv.std.ifNoneMatch"],
|
|
29
|
+
["If-Unmodified-Since", "kv.std.ifUnmodifiedSince"],
|
|
30
|
+
["Prefer", "kv.std.prefer"],
|
|
31
|
+
["Range", "kv.std.range"],
|
|
32
|
+
["X-Request-ID", "kv.std.requestId"],
|
|
33
|
+
];
|
|
34
|
+
const keyOf = name => name.trim().toLowerCase();
|
|
35
|
+
const parameterKey = name => "header:" + name;
|
|
36
|
+
const declaredId = name => "declared-header-" + name;
|
|
37
|
+
const hasValue = value => value !== undefined && value !== null && value !== "";
|
|
38
|
+
const asText = value => value != null && typeof value === "object" ? JSON.stringify(value) : String(value ?? "");
|
|
39
|
+
const isIdempotency = name => keyOf(name) === "idempotency-key";
|
|
40
|
+
|
|
41
|
+
/** Own header draft controls and prepare their values for the existing request API. */
|
|
42
|
+
export function createHeaderEditor({ state, $, parameterSchema, getMediaType, onChange = () => {} }) {
|
|
43
|
+
let owner;
|
|
44
|
+
let optionalDraft = new Map();
|
|
45
|
+
let manualKeys = new Set();
|
|
46
|
+
let bulk;
|
|
47
|
+
let navTarget;
|
|
48
|
+
let popup;
|
|
49
|
+
|
|
50
|
+
function syncDraft() {
|
|
51
|
+
state.paramValues ||= {};
|
|
52
|
+
if (owner === state.paramValues) return;
|
|
53
|
+
owner = state.paramValues;
|
|
54
|
+
optionalDraft = new Map();
|
|
55
|
+
manualKeys = new Set();
|
|
56
|
+
bulk = undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function customRows() {
|
|
60
|
+
return state.headers = normalizeHeaderRows(state.headers);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function schemaFor(parameter) {
|
|
64
|
+
return resolveSchema(parameterSchema(parameter), state.doc);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function documentedParameters() {
|
|
68
|
+
return (state.op?.parameters || []).map(raw => dereference(raw, state.doc))
|
|
69
|
+
.filter(parameter => parameter?.in === "header" && parameter.name);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function managedRows() {
|
|
73
|
+
if (!state.op) return [];
|
|
74
|
+
const session = state.cap?.identity;
|
|
75
|
+
const selectedBody = dereference(state.op.operation?.requestBody, state.doc);
|
|
76
|
+
const content = selectedBody?.content || {};
|
|
77
|
+
const mediaType = getMediaType ? getMediaType() :
|
|
78
|
+
(Object.hasOwn(content, "application/json") ? "application/json" : Object.keys(content)[0]);
|
|
79
|
+
return [
|
|
80
|
+
...(session ? [{ id: "managed-header-cookie", name: "Cookie", value: t("kv.sessionCookie"), tag: session.name, managed: true }] : []),
|
|
81
|
+
...(mediaType ? [{ id: "managed-header-content-type", name: "Content-Type", value: mediaType, managed: true }] : []),
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function getRows() {
|
|
86
|
+
syncDraft();
|
|
87
|
+
const managed = managedRows();
|
|
88
|
+
const managedNames = new Set(managed.map(row => keyOf(row.name)));
|
|
89
|
+
const documented = documentedParameters().filter(parameter => !managedNames.has(keyOf(parameter.name)));
|
|
90
|
+
const declared = [];
|
|
91
|
+
const optional = [];
|
|
92
|
+
for (const parameter of documented) {
|
|
93
|
+
const name = parameter.name;
|
|
94
|
+
const value = state.paramValues[parameterKey(name)];
|
|
95
|
+
const draft = optionalDraft.get(name);
|
|
96
|
+
const primary = !!parameter.required || isIdempotency(name);
|
|
97
|
+
if (!primary && !hasValue(value) && !draft) {
|
|
98
|
+
optional.push(parameter);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
declared.push({
|
|
102
|
+
id: declaredId(name), name, parameter, primary,
|
|
103
|
+
enabled: !!parameter.required || (draft?.enabled ?? true),
|
|
104
|
+
value: draft?.enabled === false ? draft.value : value,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return { declared, managed, custom: customRows(), optional };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function context() {
|
|
111
|
+
return { lastId: state.lastResponse?.body?.id };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function inspect(data, requestTime = false) {
|
|
115
|
+
const rows = [...data.managed, ...data.declared, ...data.custom].map(row => ({
|
|
116
|
+
...row,
|
|
117
|
+
// A session name is descriptive metadata. The browser supplies its cookie.
|
|
118
|
+
value: row.managed ? "" : asText(row.value),
|
|
119
|
+
}));
|
|
120
|
+
const resolved = inspectHeaderRows(rows, { ...context(), ...(requestTime ? { uuid: newUuid } : {}) });
|
|
121
|
+
const errors = [...resolved.errors];
|
|
122
|
+
const parameters = [];
|
|
123
|
+
for (const row of resolved.rows.filter(row => row.parameter)) {
|
|
124
|
+
const schema = parameterSchema(row.parameter);
|
|
125
|
+
const original = data.declared.find(item => item.id === row.id).value;
|
|
126
|
+
const value = typeof original !== "string" && !headerVariables(asText(original)).length
|
|
127
|
+
? original : coerceHeaderValue(row.value, schemaFor(row.parameter));
|
|
128
|
+
if (!errors.some(error => error.id === row.id)) {
|
|
129
|
+
const schemaErrors = schema == null ? [] : validateSchemaValue({
|
|
130
|
+
schema, value, document: state.doc, required: !!row.parameter.required,
|
|
131
|
+
});
|
|
132
|
+
const messages = schemaErrors.map(error => error.message);
|
|
133
|
+
if (!messages.length && row.parameter.required && !hasValue(value)) messages.push(t("kv.requiredValue"));
|
|
134
|
+
for (const message of messages) errors.push({ id: row.id, field: "value", message });
|
|
135
|
+
}
|
|
136
|
+
parameters.push({ ...row.parameter, value });
|
|
137
|
+
}
|
|
138
|
+
return { errors, parameters };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function issues() {
|
|
142
|
+
const data = getRows();
|
|
143
|
+
const customIds = new Set(data.custom.map(row => row.id));
|
|
144
|
+
const errors = inspect(data).errors.filter(issue => !bulk?.errors.length || !customIds.has(issue.id));
|
|
145
|
+
return [
|
|
146
|
+
...(bulk?.errors ?? []).map(message => ({ path: "Headers", message, inputId: "header-bulk" })),
|
|
147
|
+
...errors.map(issue => ({
|
|
148
|
+
path: "Headers",
|
|
149
|
+
message: issue.message,
|
|
150
|
+
inputId: bulk && customIds.has(issue.id) ? "header-bulk" : `${issue.id}-${issue.field}`,
|
|
151
|
+
})),
|
|
152
|
+
];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function values() {
|
|
156
|
+
const data = getRows();
|
|
157
|
+
return headerRowsToObject(data.custom, context());
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function parameters() {
|
|
161
|
+
const prepared = inspect({ ...getRows(), custom: [] }, true);
|
|
162
|
+
if (prepared.errors.length) {
|
|
163
|
+
const error = new Error(prepared.errors.map(issue => issue.message).join("\n"));
|
|
164
|
+
error.validation = true;
|
|
165
|
+
error.issues = prepared.errors.map(issue => ({
|
|
166
|
+
path: "Headers", tab: "Headers", message: issue.message, inputId: `${issue.id}-${issue.field}`,
|
|
167
|
+
}));
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
return prepared.parameters;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function headerCount(data) {
|
|
174
|
+
return data.managed.length + data.declared.filter(row => row.enabled && hasValue(row.value)).length +
|
|
175
|
+
data.custom.filter(row => row.enabled !== false && row.name.trim()).length;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function group(title, rows, detail = "") {
|
|
179
|
+
if (!rows.length) return "";
|
|
180
|
+
return `<div class="kv-row group" role="row"><span role="rowheader" aria-colspan="4">
|
|
181
|
+
${title}${detail ? `<span class="spacer"></span><span>${detail}</span>` : ""}
|
|
182
|
+
</span></div>${rows.join("")}`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function cell(column, classes, markup = "", attrs = "", interactive = Boolean(markup)) {
|
|
186
|
+
return `<div class="c ${classes}" role="gridcell" aria-colindex="${column}"
|
|
187
|
+
${interactive ? `data-grid-cell="${column}" tabindex="-1"` : ""} ${attrs}>${markup}</div>`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function checkbox(row) {
|
|
191
|
+
if (row.managed) return `<span class="checkbox locked" role="img" aria-label="${esc(t("kv.managed"))}">${icon("lock")}</span>`;
|
|
192
|
+
const enabled = row.enabled !== false;
|
|
193
|
+
const label = sendLabel(row.name);
|
|
194
|
+
return `<label class="kv-check">
|
|
195
|
+
<input type="checkbox" data-header-enabled id="${esc(row.id)}-enabled" aria-label="${esc(label)}"
|
|
196
|
+
tabindex="-1" ${enabled ? "checked" : ""} ${row.parameter?.required ? "disabled" : ""}>
|
|
197
|
+
<span class="checkbox ${enabled ? "on" : ""}" aria-hidden="true">${icon("check")}</span>
|
|
198
|
+
</label>`;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Accessible names for a row's controls; an unnamed row is a "new header".
|
|
202
|
+
const sendLabel = name => name ? t("kv.sendName", { name }) : t("kv.sendNew");
|
|
203
|
+
const valueLabel = name => name ? t("kv.valueFor", { name }) : t("kv.headerValue");
|
|
204
|
+
const actionsLabel = name => t("kv.moreActions", { name: name || t("kv.newHeader") });
|
|
205
|
+
|
|
206
|
+
function valuePreview(value) {
|
|
207
|
+
const tokens = headerVariables(value);
|
|
208
|
+
if (!tokens.length) return esc(value);
|
|
209
|
+
let result = "";
|
|
210
|
+
let offset = 0;
|
|
211
|
+
for (const token of tokens) {
|
|
212
|
+
result += esc(value.slice(offset, token.index)) + `<span class="var-token">${esc(token[0])}</span>`;
|
|
213
|
+
offset = token.index + token[0].length;
|
|
214
|
+
}
|
|
215
|
+
const guidance = tokens.length === 1 && tokens[0][0] === value
|
|
216
|
+
? { uuid: "kv.hint.uuid", now: "kv.hint.now", "last.id": "kv.hint.lastId" }[tokens[0][1]] : "";
|
|
217
|
+
return result + esc(value.slice(offset)) + (guidance ? `<span class="kv-variable-hint faint">${esc(t(guidance))}</span>` : "");
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function preview(field, value) {
|
|
221
|
+
return value ? (field === "value" ? valuePreview(value) : esc(value)) : esc(t(field === "value" ? "kv.valuePlaceholder" : "kv.namePlaceholder"));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function inputField(row, field) {
|
|
225
|
+
const nameField = field === "name";
|
|
226
|
+
const value = asText(row[field]);
|
|
227
|
+
return `<div class="kv-field">
|
|
228
|
+
<input id="${esc(row.id)}-${field}" data-header-field="${field}" tabindex="-1"
|
|
229
|
+
aria-label="${nameField ? esc(t("kv.namePlaceholder")) : esc(valueLabel(row.name))}"
|
|
230
|
+
value="${esc(value)}" placeholder="${esc(t(nameField ? "kv.namePlaceholder" : "kv.valuePlaceholder"))}" spellcheck="false" autocomplete="off"
|
|
231
|
+
${nameField ? 'role="combobox" aria-autocomplete="list" aria-controls="header-suggestions" aria-expanded="false"' : ""}>
|
|
232
|
+
<span class="kv-display ${nameField ? "name" : "val"}${value ? "" : " placeholder"}" aria-hidden="true">${preview(field, value)}</span>
|
|
233
|
+
</div>`;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function rowMarkup(row) {
|
|
237
|
+
const id = esc(row.id);
|
|
238
|
+
const kind = row.managed ? "managed" : row.parameter ? "declared" : "custom";
|
|
239
|
+
const auto = isIdempotency(row.name) && row.parameter && hasValue(row.value) && !manualKeys.has(row.name);
|
|
240
|
+
const name = row.parameter || row.managed ? `<span class="name">${esc(row.name)}</span>` : inputField(row, "name");
|
|
241
|
+
const value = row.managed ? `<span class="val">${esc(row.value)}</span>` : inputField(row, "value");
|
|
242
|
+
const badge = auto ? `<span class="tag tag-accent" data-header-auto>${icon("wand", true)} ${esc(t("kv.auto"))}</span>` :
|
|
243
|
+
row.tag ? `<span class="tag">${esc(row.tag)}</span>` : "";
|
|
244
|
+
const regenerate = row.parameter && isIdempotency(row.name) ?
|
|
245
|
+
`<button class="btn btn-ghost btn-icon btn-xs" type="button" data-header-command="regenerate" tabindex="-1"
|
|
246
|
+
aria-label="${esc(t("kv.regenerate", { name: row.name }))}">${icon("refresh", true)}</button>` : "";
|
|
247
|
+
const actions = row.managed ? "" : regenerate +
|
|
248
|
+
`<button class="btn btn-ghost btn-icon btn-xs" type="button" data-header-command="actions" tabindex="-1"
|
|
249
|
+
aria-label="${esc(actionsLabel(row.name))}" aria-expanded="false">${icon("more", true)}</button>`;
|
|
250
|
+
return `<div class="kv-row${row.managed ? " is-managed" : ""}${row.enabled === false ? " is-disabled" : ""}"
|
|
251
|
+
role="row" data-header-row data-header-id="${id}" data-header-kind="${kind}">
|
|
252
|
+
${cell(1, "check", checkbox(row))}
|
|
253
|
+
${cell(2, "name-c", name + (row.parameter || row.managed ? "" : invalidIcon()), row.parameter || row.managed ? 'aria-readonly="true"' : "")}
|
|
254
|
+
${cell(3, "val-c", value + badge + (row.managed ? "" : invalidIcon()), row.managed ? 'aria-readonly="true"' : "")}
|
|
255
|
+
${cell(4, "acts", actions)}
|
|
256
|
+
</div>`;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function invalidIcon() {
|
|
260
|
+
return `<span class="kv-invalid-icon" aria-hidden="true">${icon("alert", true)}</span>`;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function toolbar(data) {
|
|
264
|
+
return `<div class="kv-toolbar">
|
|
265
|
+
<span class="faint kv-count" data-header-count aria-live="polite">${countText(data)}</span>
|
|
266
|
+
<span class="spacer"></span>
|
|
267
|
+
<button class="btn btn-ghost btn-sm" type="button" data-header-command="presets" aria-expanded="false">
|
|
268
|
+
${icon("list", true)}${esc(t("kv.presets"))}${icon("chevron-down", true)}
|
|
269
|
+
</button>
|
|
270
|
+
<div class="segmented" role="group" aria-label="${esc(t("kv.editorMode"))}">
|
|
271
|
+
<button type="button" data-header-command="table" aria-pressed="${!bulk}">${esc(t("kv.table"))}</button>
|
|
272
|
+
<button type="button" data-header-command="bulk" aria-pressed="${!!bulk}">${esc(t("kv.bulkEdit"))}</button>
|
|
273
|
+
</div>
|
|
274
|
+
</div>`;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function countText(data) {
|
|
278
|
+
return t("kv.count", { n: headerCount(data) });
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const hasCustomRows = data => data.custom.length || data.declared.some(row => !row.primary);
|
|
282
|
+
|
|
283
|
+
/** No custom headers yet (mock 36): the managed ones stay listed and nothing looks empty. */
|
|
284
|
+
function emptyCustom(data) {
|
|
285
|
+
const sent = data.managed.length + data.declared.filter(row => row.primary).length;
|
|
286
|
+
return `<div class="kv-row kv-empty" role="row" style="display:block"><div role="gridcell">${stateBlock({
|
|
287
|
+
glyph: '', compact: true, title: esc(t("kv.emptyTitle")), body: esc(t("kv.emptyBody", { n: sent })),
|
|
288
|
+
actions: [`<button type="button" class="btn btn-sm" data-action="add-header" ${shortcutAttrs('add-header')}>${icon('plus', true)}${esc(t("kv.add"))} ${shortcutHint('add-header')}</button>`,
|
|
289
|
+
`<button type="button" class="btn btn-ghost btn-sm" data-header-command="presets">${icon('list', true)}${esc(t("kv.applyPreset"))}</button>`],
|
|
290
|
+
})}</div></div>`;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function grid(data) {
|
|
294
|
+
const addRow = `<div class="kv-row kv-add" role="row">
|
|
295
|
+
${cell(1, "check", '<span class="checkbox" aria-hidden="true"></span>', "", false)}
|
|
296
|
+
${cell(2, "name-c", `<button class="kv-add-button" type="button" data-action="add-header" tabindex="-1" ${shortcutAttrs('add-header', 'add-row')}>${esc(t("kv.add"))}</button>`)}
|
|
297
|
+
${cell(3, "val-c", `<span class="val placeholder" aria-hidden="true">${esc(t("kv.valuePlaceholder"))}</span>`, "", false)}
|
|
298
|
+
${cell(4, "acts")}
|
|
299
|
+
</div>`;
|
|
300
|
+
return `<div class="kv-table" id="header-list" role="grid" aria-label="${esc(t("kv.grid"))}" aria-colcount="4">
|
|
301
|
+
<div class="kv-row head" role="row">
|
|
302
|
+
<div class="c" role="columnheader"><span class="sr-only">${esc(t("kv.col.send"))}</span></div>
|
|
303
|
+
<div class="c" role="columnheader">${esc(t("kv.col.name"))}</div><div class="c" role="columnheader">${esc(t("kv.col.value"))}</div>
|
|
304
|
+
<div class="c" role="columnheader"><span class="sr-only">${esc(t("kv.col.actions"))}</span></div>
|
|
305
|
+
</div>
|
|
306
|
+
${group(esc(t("kv.group.required")), data.declared.filter(row => row.primary).map(rowMarkup))}
|
|
307
|
+
${group(esc(t("kv.group.managed")), data.managed.map(rowMarkup), esc(t("kv.group.managedDetail")))}
|
|
308
|
+
${group(esc(t("kv.group.custom")), [...data.declared.filter(row => !row.primary).map(rowMarkup), ...(!bulk ? [...data.custom.map(rowMarkup), hasCustomRows(data) ? addRow : emptyCustom(data)] : [])])}
|
|
309
|
+
</div>`;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function specChips(data) {
|
|
313
|
+
const used = new Set(data.custom.map(row => keyOf(row.name)));
|
|
314
|
+
const options = data.optional.filter(parameter => !used.has(keyOf(parameter.name)));
|
|
315
|
+
return options.length ? `<div class="optional-props kv-spec" role="group" aria-label="${esc(t("kv.addDocumented"))}">
|
|
316
|
+
<span class="lead-label">${esc(t("kv.fromSpec"))}</span>
|
|
317
|
+
${options.map(parameter => `<button type="button" class="prop-chip" data-header-command="spec" data-header-name="${esc(parameter.name)}">
|
|
318
|
+
${icon("plus", true)}${esc(parameter.name)}<span class="t">${esc(t(schemaFor(parameter).format === "uuid" ? "kv.uuid" : "field.optional"))}</span>
|
|
319
|
+
</button>`).join("")}
|
|
320
|
+
</div>` : "";
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function bulkMarkup() {
|
|
324
|
+
return `<div class="kv-bulk">
|
|
325
|
+
<label class="label" for="header-bulk">${esc(t("kv.customHeaders"))}</label>
|
|
326
|
+
<textarea class="code" id="header-bulk" spellcheck="false" placeholder="${esc(t("kv.bulkPlaceholder"))}"
|
|
327
|
+
aria-describedby="header-bulk-hint">${esc(bulk.text)}</textarea>
|
|
328
|
+
<p class="kv-note" id="header-bulk-hint">${esc(t("kv.bulkHint"))}</p>
|
|
329
|
+
</div>`;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function markErrors(data = getRows()) {
|
|
333
|
+
const editor = $("#editor");
|
|
334
|
+
const status = editor.querySelector("[data-header-count]");
|
|
335
|
+
const summary = editor.querySelector("[data-header-notes]");
|
|
336
|
+
if (!status || !summary) return;
|
|
337
|
+
const count = countText(data);
|
|
338
|
+
if (status.textContent !== count) status.textContent = count;
|
|
339
|
+
const rows = [...data.declared, ...data.custom];
|
|
340
|
+
const byId = new Map(rows.map(row => [row.id, row]));
|
|
341
|
+
const errors = inspect(data).errors.filter(error => {
|
|
342
|
+
const row = byId.get(error.id);
|
|
343
|
+
return row?.parameter || row?.name || row?.value;
|
|
344
|
+
});
|
|
345
|
+
const byRow = Map.groupBy(errors, error => error.id);
|
|
346
|
+
const notes = [...byRow].filter(([id]) => !bulk || byId.get(id).parameter).map(([id, errors]) => {
|
|
347
|
+
const row = byId.get(id);
|
|
348
|
+
const fix = !row.parameter && errors.some(error => error.field === "name") && headerNameFix(row.name);
|
|
349
|
+
return `<div class="kv-note is-error" id="${esc(id)}-error">${icon("alert", true)}
|
|
350
|
+
<span>${th("kv.noteLine", { name: esc(row.name || t("kv.customFallback")), message: esc(errors.map(error => error.message).join(" ")) })}</span>
|
|
351
|
+
${fix && !bulk ? `<button class="btn btn-ghost btn-xs" type="button" data-header-command="fix" data-header-fix="${esc(id)}">${esc(t("kv.fix"))}</button>` : ""}
|
|
352
|
+
</div>`;
|
|
353
|
+
});
|
|
354
|
+
const bulkErrors = bulk && [...bulk.errors, ...data.custom.flatMap(row =>
|
|
355
|
+
(byRow.get(row.id) || []).map(error => error.message))];
|
|
356
|
+
if (bulkErrors?.length) {
|
|
357
|
+
notes.unshift(`<div class="kv-note is-error" id="header-bulk-error">${icon("alert", true)}
|
|
358
|
+
<span>${esc(bulkErrors.join(" "))}</span></div>`);
|
|
359
|
+
}
|
|
360
|
+
const contents = notes.join("") || `<div class="kv-note">${icon("info", true)}
|
|
361
|
+
<span>${th("kv.variables", { uuid: "<code>{{uuid}}</code>", now: "<code>{{now}}</code>", last: "<code>{{last.id}}</code>" })}</span></div>`;
|
|
362
|
+
if (summary.dataset.content !== contents) {
|
|
363
|
+
summary.innerHTML = contents;
|
|
364
|
+
summary.dataset.content = contents;
|
|
365
|
+
}
|
|
366
|
+
const bulkInput = $("#header-bulk");
|
|
367
|
+
if (bulkInput) {
|
|
368
|
+
bulkInput.setAttribute("aria-invalid", String(!!bulkErrors.length));
|
|
369
|
+
bulkInput.setAttribute("aria-describedby", bulkErrors.length ? "header-bulk-hint header-bulk-error" : "header-bulk-hint");
|
|
370
|
+
}
|
|
371
|
+
for (const node of editor.querySelectorAll("[data-header-row]")) {
|
|
372
|
+
const id = node.dataset.headerId;
|
|
373
|
+
const problems = byRow.get(id) || [];
|
|
374
|
+
for (const input of node.querySelectorAll("[data-header-field]")) {
|
|
375
|
+
const invalid = problems.some(error => error.field === input.dataset.headerField);
|
|
376
|
+
input.closest("[data-grid-cell]").classList.toggle("is-invalid", invalid);
|
|
377
|
+
input.setAttribute("aria-invalid", String(invalid));
|
|
378
|
+
if (invalid) input.setAttribute("aria-describedby", id + "-error");
|
|
379
|
+
else input.removeAttribute("aria-describedby");
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function update() {
|
|
385
|
+
markErrors();
|
|
386
|
+
onChange();
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function render(focusId) {
|
|
390
|
+
const data = getRows();
|
|
391
|
+
navTarget = popup = undefined;
|
|
392
|
+
const editor = $("#editor");
|
|
393
|
+
editor.innerHTML = `<div class="header-editor">
|
|
394
|
+
${toolbar(data)}${grid(data)}${bulk ? bulkMarkup() : ""}
|
|
395
|
+
<div data-header-notes aria-live="polite"></div>${specChips(data)}
|
|
396
|
+
<div class="pop kv-suggest" id="hdr-suggest" hidden>
|
|
397
|
+
<div role="listbox" aria-label="${esc(t("kv.suggestions"))}" class="pop-group" id="header-suggestions"></div>
|
|
398
|
+
<div class="pop-foot" aria-hidden="true"><span>${th("kv.move", { key: shortcutHint('up', 'down') })}</span><span>${th("kv.pick", { key: shortcutHint('enter') })}</span>
|
|
399
|
+
<span>${th("kv.toValue", { key: shortcutHint('tab') })}</span><span class="spacer"></span><span>${shortcutHint('escape')}</span></div>
|
|
400
|
+
</div>
|
|
401
|
+
<div class="pop kv-menu" id="header-menu" hidden></div>
|
|
402
|
+
</div>`;
|
|
403
|
+
const root = editor.firstElementChild;
|
|
404
|
+
root.addEventListener("input", onInput);
|
|
405
|
+
root.addEventListener("change", event => {
|
|
406
|
+
if (event.target.matches("[data-header-enabled]")) toggleRow(event.target.closest("[data-header-row]").dataset.headerId);
|
|
407
|
+
});
|
|
408
|
+
root.addEventListener("click", onClick);
|
|
409
|
+
root.addEventListener("keydown", onKey);
|
|
410
|
+
root.addEventListener("focusin", event => {
|
|
411
|
+
const cell = event.target.closest("[data-grid-cell]");
|
|
412
|
+
if (cell) setRoving(event.target);
|
|
413
|
+
});
|
|
414
|
+
root.addEventListener("focusout", event => {
|
|
415
|
+
if (popup && event.relatedTarget !== popup.anchor && !popup.node.contains(event.relatedTarget)) closePopup();
|
|
416
|
+
});
|
|
417
|
+
root.addEventListener("pointerdown", event => {
|
|
418
|
+
if (event.target.closest("[data-header-option]")) event.preventDefault();
|
|
419
|
+
else if (popup && !popup.node.contains(event.target) && !popup.anchor.contains(event.target)) closePopup();
|
|
420
|
+
});
|
|
421
|
+
const first = root.querySelector("[data-grid-cell]");
|
|
422
|
+
if (first) setRoving(first);
|
|
423
|
+
markErrors(data);
|
|
424
|
+
if (focusId) editor.ownerDocument.getElementById(focusId)?.focus();
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function setRoving(node) {
|
|
428
|
+
if (navTarget) navTarget.tabIndex = -1;
|
|
429
|
+
navTarget = node;
|
|
430
|
+
navTarget.tabIndex = 0;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function rowFor(target) {
|
|
434
|
+
const id = target.closest("[data-header-row]")?.dataset.headerId;
|
|
435
|
+
const data = getRows();
|
|
436
|
+
return [...data.declared, ...data.custom].find(row => row.id === id);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function setDeclaredValue(row, value) {
|
|
440
|
+
state.paramValues[parameterKey(row.name)] = coerceHeaderValue(value, schemaFor(row.parameter));
|
|
441
|
+
if (!row.parameter.required) optionalDraft.set(row.name, { enabled: true });
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function refreshField(input) {
|
|
445
|
+
const value = input.value;
|
|
446
|
+
const display = input.nextElementSibling;
|
|
447
|
+
display.classList.toggle("placeholder", !value);
|
|
448
|
+
display.innerHTML = preview(input.dataset.headerField, value);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function onInput(event) {
|
|
452
|
+
const input = event.target;
|
|
453
|
+
if (input.id === "header-bulk") {
|
|
454
|
+
bulk.text = input.value;
|
|
455
|
+
const result = parseHeaderLines(input.value, customRows());
|
|
456
|
+
bulk.errors = result.errors;
|
|
457
|
+
if (!result.errors.length) state.headers = result.rows;
|
|
458
|
+
update();
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
const field = input.dataset.headerField;
|
|
462
|
+
if (!field) return;
|
|
463
|
+
const row = rowFor(input);
|
|
464
|
+
if (row.parameter) {
|
|
465
|
+
if (row.enabled === false) optionalDraft.set(row.name, { enabled: false, value: input.value });
|
|
466
|
+
else setDeclaredValue(row, input.value);
|
|
467
|
+
manualKeys.add(row.name);
|
|
468
|
+
input.closest("[data-grid-cell]").querySelector("[data-header-auto]")?.remove();
|
|
469
|
+
} else {
|
|
470
|
+
row[field] = input.value;
|
|
471
|
+
}
|
|
472
|
+
if (field === "name") {
|
|
473
|
+
const rowNode = input.closest("[data-header-row]");
|
|
474
|
+
rowNode.querySelector("[data-header-enabled]").setAttribute("aria-label", sendLabel(row.name));
|
|
475
|
+
rowNode.querySelector('[data-header-field="value"]').setAttribute("aria-label", valueLabel(row.name));
|
|
476
|
+
rowNode.querySelector('[data-header-command="actions"]').setAttribute("aria-label", actionsLabel(row.name));
|
|
477
|
+
suggest(input);
|
|
478
|
+
}
|
|
479
|
+
refreshField(input);
|
|
480
|
+
update();
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function toggleRow(id) {
|
|
484
|
+
const data = getRows();
|
|
485
|
+
const declared = data.declared.find(row => row.id === id);
|
|
486
|
+
if (declared?.parameter.required) return;
|
|
487
|
+
if (declared) {
|
|
488
|
+
if (declared.enabled) {
|
|
489
|
+
optionalDraft.set(declared.name, { enabled: false, value: declared.value });
|
|
490
|
+
state.paramValues[parameterKey(declared.name)] = undefined;
|
|
491
|
+
} else {
|
|
492
|
+
setDeclaredValue(declared, asText(declared.value));
|
|
493
|
+
}
|
|
494
|
+
} else {
|
|
495
|
+
const row = data.custom.find(item => item.id === id);
|
|
496
|
+
if (!row) return;
|
|
497
|
+
row.enabled = row.enabled === false;
|
|
498
|
+
if (bulk) bulk.text = formatHeaderLines(data.custom);
|
|
499
|
+
}
|
|
500
|
+
render(id + "-enabled");
|
|
501
|
+
onChange();
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function add() {
|
|
505
|
+
if (bulk?.errors.length) return;
|
|
506
|
+
bulk = undefined;
|
|
507
|
+
const row = createHeaderRow();
|
|
508
|
+
customRows().push(row);
|
|
509
|
+
render(row.id + "-name");
|
|
510
|
+
onChange();
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function remove(id) {
|
|
514
|
+
if (bulk?.errors.length) return;
|
|
515
|
+
state.headers = customRows().filter(row => row.id !== id);
|
|
516
|
+
if (bulk) bulk.text = formatHeaderLines(state.headers);
|
|
517
|
+
render();
|
|
518
|
+
$("#editor").querySelector('[data-action="add-header"], #header-bulk')?.focus();
|
|
519
|
+
onChange();
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function showBulk() {
|
|
523
|
+
syncDraft();
|
|
524
|
+
bulk ||= { text: formatHeaderLines(customRows()), errors: [] };
|
|
525
|
+
render("header-bulk");
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function showTable() {
|
|
529
|
+
if (bulk?.errors.length) {
|
|
530
|
+
$("#header-bulk").focus();
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
bulk = undefined;
|
|
534
|
+
render();
|
|
535
|
+
$('[data-header-command="table"]').focus();
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function addDocumented(parameter, customId, value) {
|
|
539
|
+
const name = parameter.name;
|
|
540
|
+
const schema = schemaFor(parameter);
|
|
541
|
+
const initial = value || (schema.format === "uuid" ? "{{uuid}}" : asText(parameter.example ?? schema.default));
|
|
542
|
+
setDeclaredValue({ name, parameter }, initial);
|
|
543
|
+
if (customId) {
|
|
544
|
+
state.headers = customRows().filter(row => row.id !== customId);
|
|
545
|
+
if (bulk) bulk.text = formatHeaderLines(state.headers);
|
|
546
|
+
}
|
|
547
|
+
render(declaredId(name) + "-value");
|
|
548
|
+
onChange();
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function applyPreset(index) {
|
|
552
|
+
if (bulk?.errors.length) {
|
|
553
|
+
$("#header-bulk").focus();
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
const [, name, value] = presets[index];
|
|
557
|
+
const data = getRows();
|
|
558
|
+
const declared = [...data.declared.map(row => row.parameter), ...data.optional]
|
|
559
|
+
.find(parameter => keyOf(parameter.name) === keyOf(name));
|
|
560
|
+
let row = data.custom.find(item => keyOf(item.name) === keyOf(name));
|
|
561
|
+
if (declared) {
|
|
562
|
+
addDocumented(declared, row?.id, value);
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
if (row) Object.assign(row, { value, enabled: true });
|
|
566
|
+
else data.custom.push(row = createHeaderRow(name, value));
|
|
567
|
+
if (bulk) bulk.text = formatHeaderLines(data.custom);
|
|
568
|
+
render(bulk ? "header-bulk" : row.id + "-value");
|
|
569
|
+
onChange();
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
function closePopup() {
|
|
573
|
+
if (!popup) return;
|
|
574
|
+
popup.node.hidden = true;
|
|
575
|
+
popup.anchor.setAttribute("aria-expanded", "false");
|
|
576
|
+
popup.anchor.removeAttribute("aria-activedescendant");
|
|
577
|
+
popup = undefined;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function openPopup(anchor, node, details, alignRight = false) {
|
|
581
|
+
closePopup();
|
|
582
|
+
popup = { anchor, node, ...details };
|
|
583
|
+
node.hidden = false;
|
|
584
|
+
anchor.setAttribute("aria-expanded", "true");
|
|
585
|
+
const editor = anchor.closest(".header-editor");
|
|
586
|
+
const rect = editor.getBoundingClientRect();
|
|
587
|
+
const from = (details.options ? anchor.closest("[data-grid-cell]") : anchor).getBoundingClientRect();
|
|
588
|
+
const width = node.offsetWidth;
|
|
589
|
+
const left = (alignRight ? from.right - width : from.left) - rect.left;
|
|
590
|
+
node.style.left = Math.max(0, Math.min(left, rect.width - width)) + "px";
|
|
591
|
+
node.style.top = from.bottom - rect.top + 4 + "px";
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
function suggest(input) {
|
|
595
|
+
const query = keyOf(input.value);
|
|
596
|
+
const data = getRows();
|
|
597
|
+
const activeRow = input.closest("[data-header-row]");
|
|
598
|
+
const used = new Set([...data.declared, ...data.managed, ...data.custom]
|
|
599
|
+
.filter(row => row.id !== activeRow.dataset.headerId && row.enabled !== false).map(row => keyOf(row.name)));
|
|
600
|
+
const documented = data.optional.map(parameter => ({ name: parameter.name, detail: parameter.description, parameter }));
|
|
601
|
+
const documentedNames = new Set(documented.map(option => keyOf(option.name)));
|
|
602
|
+
const options = [...documented, ...standardHeaders
|
|
603
|
+
.filter(([name]) => !documentedNames.has(keyOf(name))).map(([name, hint]) => ({ name, detail: t(hint) }))]
|
|
604
|
+
.filter(option => query && keyOf(option.name).startsWith(query) && !used.has(keyOf(option.name))).slice(0, 6);
|
|
605
|
+
if (!options.length) {
|
|
606
|
+
closePopup();
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
const list = $("#header-suggestions");
|
|
610
|
+
list.innerHTML = options.map((option, index) => `<div class="opt-row" id="header-option-${index}" role="option"
|
|
611
|
+
aria-selected="false" data-header-option="${index}">
|
|
612
|
+
<span class="main mono"><mark>${esc(option.name.slice(0, query.length))}</mark>${esc(option.name.slice(query.length))}</span>
|
|
613
|
+
${option.detail ? `<span class="sub">${esc(option.detail)}</span>` : ""}
|
|
614
|
+
${option.parameter ? `<span class="end"><span class="tag tag-accent">${esc(t("kv.inSpec"))}</span></span>` : ""}
|
|
615
|
+
</div>`).join("");
|
|
616
|
+
openPopup(input, $("#hdr-suggest"), { options });
|
|
617
|
+
selectSuggestion(0);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function selectSuggestion(index) {
|
|
621
|
+
popup.selected = (index + popup.options.length) % popup.options.length;
|
|
622
|
+
popup.node.querySelectorAll("[data-header-option]").forEach((option, index) => {
|
|
623
|
+
const active = index === popup.selected;
|
|
624
|
+
option.classList.toggle("is-active", active);
|
|
625
|
+
option.setAttribute("aria-selected", String(active));
|
|
626
|
+
if (active) popup.anchor.setAttribute("aria-activedescendant", option.id);
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function pickSuggestion(index) {
|
|
631
|
+
const { anchor, options } = popup;
|
|
632
|
+
const option = options[index];
|
|
633
|
+
const row = rowFor(anchor);
|
|
634
|
+
closePopup();
|
|
635
|
+
if (option.parameter) {
|
|
636
|
+
addDocumented(option.parameter, row.id, row.value);
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
anchor.value = option.name;
|
|
640
|
+
// Use the same draft update and live validation path as ordinary typing.
|
|
641
|
+
anchor.dispatchEvent(new Event("input", { bubbles: true }));
|
|
642
|
+
closePopup();
|
|
643
|
+
anchor.closest("[data-header-row]").querySelector('[data-header-field="value"]').focus();
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
function menu(anchor, choices, rowId) {
|
|
647
|
+
if (popup?.anchor === anchor) {
|
|
648
|
+
closePopup();
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
const node = $("#header-menu");
|
|
652
|
+
node.innerHTML = `<div class="pop-group">${choices.map(([label, command, index]) =>
|
|
653
|
+
`<button class="opt-row" type="button" data-header-command="${command}" ${index == null ? "" : `data-header-preset="${index}"`}>
|
|
654
|
+
<span class="main">${esc(label)}</span></button>`).join("")}</div>`;
|
|
655
|
+
openPopup(anchor, node, { rowId }, true);
|
|
656
|
+
node.querySelector("button").focus();
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
function onClick(event) {
|
|
660
|
+
const option = event.target.closest("[data-header-option]");
|
|
661
|
+
if (option) {
|
|
662
|
+
pickSuggestion(Number(option.dataset.headerOption));
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
const button = event.target.closest("[data-header-command]");
|
|
666
|
+
if (!button) return;
|
|
667
|
+
const command = button.dataset.headerCommand;
|
|
668
|
+
if (command === "bulk") showBulk();
|
|
669
|
+
if (command === "table") showTable();
|
|
670
|
+
if (command === "presets") menu(button, presets.map(([label], index) => [t(label), "preset", index]));
|
|
671
|
+
if (command === "preset") applyPreset(Number(button.dataset.headerPreset));
|
|
672
|
+
if (command === "spec") {
|
|
673
|
+
if (bulk?.errors.length) {
|
|
674
|
+
$("#header-bulk").focus();
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
const data = getRows();
|
|
678
|
+
const parameter = data.optional.find(parameter => parameter.name === button.dataset.headerName);
|
|
679
|
+
const draft = data.custom.find(row => keyOf(row.name) === keyOf(button.dataset.headerName));
|
|
680
|
+
if (parameter) addDocumented(parameter, draft?.id, draft?.value);
|
|
681
|
+
}
|
|
682
|
+
if (command === "actions") {
|
|
683
|
+
const row = rowFor(button);
|
|
684
|
+
const choices = row.parameter ? [[t("kv.clear"), "clear"]] : [[t("kv.remove"), "remove"]];
|
|
685
|
+
if (!row.parameter?.required) choices.unshift([t(row.enabled === false ? "kv.enable" : "kv.disable"), "toggle"]);
|
|
686
|
+
menu(button, choices, row.id);
|
|
687
|
+
}
|
|
688
|
+
if (command === "toggle") toggleRow(popup.rowId);
|
|
689
|
+
if (command === "remove") remove(popup.rowId);
|
|
690
|
+
if (command === "clear" || command === "regenerate") {
|
|
691
|
+
const row = command === "clear" ? getRows().declared.find(row => row.id === popup.rowId) : rowFor(button);
|
|
692
|
+
if (command === "clear" && row.enabled === false) optionalDraft.set(row.name, { enabled: false, value: "" });
|
|
693
|
+
else setDeclaredValue(row, command === "clear" ? "" : newUuid());
|
|
694
|
+
if (command === "clear") manualKeys.add(row.name);
|
|
695
|
+
else manualKeys.delete(row.name);
|
|
696
|
+
render(row.id + "-value");
|
|
697
|
+
onChange();
|
|
698
|
+
}
|
|
699
|
+
if (command === "fix") {
|
|
700
|
+
const row = customRows().find(row => row.id === button.dataset.headerFix);
|
|
701
|
+
row.name = headerNameFix(row.name) || row.name;
|
|
702
|
+
render(row.id + "-name");
|
|
703
|
+
onChange();
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
function focusCell(cell, fromRight = false) {
|
|
708
|
+
if (!cell) return false;
|
|
709
|
+
const controls = [...cell.querySelectorAll("button, input:not(:disabled)")];
|
|
710
|
+
const target = controls.at(fromRight ? -1 : 0) || cell;
|
|
711
|
+
target.focus();
|
|
712
|
+
return true;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
function onKey(event) {
|
|
716
|
+
if (event.isComposing) return;
|
|
717
|
+
const { target, key } = event;
|
|
718
|
+
if (popup && matchesShortcut('escape', event)) {
|
|
719
|
+
const { anchor } = popup;
|
|
720
|
+
closePopup();
|
|
721
|
+
anchor.focus();
|
|
722
|
+
event.preventDefault();
|
|
723
|
+
event.stopPropagation();
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
if (popup?.options && target === popup.anchor && ['up', 'down', 'enter'].some(id => matchesShortcut(id, event))) {
|
|
727
|
+
if (matchesShortcut('enter', event)) pickSuggestion(popup.selected);
|
|
728
|
+
else selectSuggestion(popup.selected + (key === "ArrowDown" ? 1 : -1));
|
|
729
|
+
event.preventDefault();
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
if (popup && popup.node.contains(target) && ["ArrowUp", "ArrowDown", "Home", "End"].includes(key)) {
|
|
733
|
+
const buttons = [...popup.node.querySelectorAll("button")];
|
|
734
|
+
const index = buttons.indexOf(target) + (key === "ArrowUp" ? -1 : 1);
|
|
735
|
+
const next = key === "Home" ? 0 : key === "End" ? buttons.length - 1 : (index + buttons.length) % buttons.length;
|
|
736
|
+
buttons[next]?.focus();
|
|
737
|
+
event.preventDefault();
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
const cell = target.closest("[data-grid-cell]");
|
|
741
|
+
if (!cell) return;
|
|
742
|
+
const isText = !!target.dataset.headerField;
|
|
743
|
+
if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) return;
|
|
744
|
+
if ((key === "Tab" || key === "Enter") && target.dataset.headerField === "name") {
|
|
745
|
+
event.preventDefault();
|
|
746
|
+
closePopup();
|
|
747
|
+
target.closest("[data-header-row]").querySelector('[data-header-field="value"]').focus();
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
if (key === "ArrowLeft" || key === "ArrowRight") {
|
|
751
|
+
if (isText && (target.selectionStart !== target.selectionEnd ||
|
|
752
|
+
(key === "ArrowLeft" ? target.selectionStart > 0 : target.selectionEnd < target.value.length))) return;
|
|
753
|
+
const fromRight = key === "ArrowLeft";
|
|
754
|
+
const buttons = [...cell.querySelectorAll("button")];
|
|
755
|
+
const adjacent = buttons[buttons.indexOf(target) + (fromRight ? -1 : 1)];
|
|
756
|
+
if (buttons.includes(target) && adjacent) {
|
|
757
|
+
adjacent.focus();
|
|
758
|
+
event.preventDefault();
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
const cells = [...cell.parentElement.querySelectorAll("[data-grid-cell]")];
|
|
762
|
+
if (focusCell(cells[cells.indexOf(cell) + (fromRight ? -1 : 1)], fromRight)) event.preventDefault();
|
|
763
|
+
}
|
|
764
|
+
if (key === "ArrowUp" || key === "ArrowDown") {
|
|
765
|
+
const rows = [...$("#header-list").querySelectorAll(".kv-row:not(.head, .group)")];
|
|
766
|
+
const offset = key === "ArrowDown" ? 1 : -1;
|
|
767
|
+
for (let index = rows.indexOf(cell.parentElement) + offset; rows[index]; index += offset) {
|
|
768
|
+
if (focusCell(rows[index].querySelector(`[data-grid-cell="${cell.dataset.gridCell}"]`))) {
|
|
769
|
+
event.preventDefault();
|
|
770
|
+
break;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
return { render, issues, values, parameters, count: () => headerCount(getRows()), add, remove, showBulk, toggleRow };
|
|
777
|
+
}
|