Debian Codegraph @

src/ext/hx-prompt.js

raw symbols SCIP
1 //==========================================================
2 // hx-prompt.js
3 //
4 // Restores htmx 2's `hx-prompt` attribute. Prompts the user
5 // before the request, sends the answer as `HX-Prompt` header,
6 // aborts on cancel. Override `window.htmxPrompt` for a custom
7 // (sync) dialog.
8 //==========================================================
9 (() => {
10 let api;
11 htmx.registerExtension('hx-prompt', {
12 init(internalAPI) { api = internalAPI },
13
14 htmx_config_request(elt, {ctx}) {
15 let question = api.attributeValue(ctx.sourceElement, 'hx-prompt');
16 if (question == null) return;
17 let answer = (window.htmxPrompt || window.prompt)(question);
18 if (answer === null) return false;
19 if (!htmx.trigger(ctx.sourceElement, 'htmx:prompt', { prompt: answer, target: ctx.target })) return false;
20 ctx.request.headers['HX-Prompt'] = encodeURI(answer);
21 }
22 });
23 })();