{
  "version": 3,
  "sources": ["forms.js"],
  "sourcesContent": ["import $ from \"./jquery.js\";\nimport { gettext } from \"./i18n.js\";\nimport { play } from \"./motion.js\";\n\nconst ERROR_CLASS = \"has-errors\";\n\nexport function clearFieldErrors(form) {\n    form.find(`.${ERROR_CLASS}`).removeClass(ERROR_CLASS);\n    form.find(\".field-error\").remove();\n}\n\n/**\n * Attach a `{field: [message]}` map from a 400 response to the matching inputs:\n * each edged in red, its message under it and a shake, as `field-errors.js`\n * does for the sites' forms.\n */\nfunction showFieldErrors(form, fieldErrors) {\n    for (const [field, messages] of Object.entries(fieldErrors)) {\n        const input = form.find(`[name=\"${field}\"]`);\n        input.addClass(ERROR_CLASS);\n        const label = input.closest(\"label\");\n        if (label.length > 0) {\n            $(\"<span></span>\")\n                .addClass(\"field-error\")\n                .text(messages.join(\" \"))\n                .appendTo(label);\n        }\n        play(label.length > 0 ? label : input, \"shake\");\n    }\n}\n\n/** A form field's value, as the browser holds it. */\nexport function fieldValue(form, name) {\n    return form.find(`[name=\"${name}\"]`).val();\n}\n\n/** A form field's value as text with the spaces around it taken off - \"\" for none. */\nexport function trimmedFieldValue(form, name) {\n    return String(fieldValue(form, name) ?? \"\").trim();\n}\n\nexport function showFieldError(form, field, message) {\n    showFieldErrors(form, { [field]: [message] });\n}\n\n/**\n * Whether the form's `passwordField` and its confirmation (`confirm_password`)\n * agree. When they do not, says so under the confirmation, which is focused for\n * the password to be typed again.\n */\nexport function passwordsMatch(form, passwordField) {\n    const confirmation = form.find(\"[name=confirm_password]\");\n    if (fieldValue(form, passwordField) === confirmation.val()) {\n        return true;\n    }\n    showFieldError(form, \"confirm_password\", gettext(\"Passwords do not match.\"));\n    confirmation.trigger(\"focus\");\n    return false;\n}\n\nfunction submitButtons(form) {\n    return form\n        .find(\"button, input\")\n        .filter((index, control) => control.type === \"submit\");\n}\n\n/**\n * Mark `target` - a form, or a button outside one - busy until `request` (a\n * request or any promise) settles, and hand the request back. A busy form is\n * not submitted again, by a click or by Enter, and a busy button not clicked\n * again; a form's submit buttons, and a busy button, draw a spinner in place of\n * their words once the wait is long enough to notice (`_button.css`). The rest\n * of a dialog stays as it was: it can be closed, the request going on.\n */\nexport function whileBusy(target, request) {\n    const element = $(target);\n    const marked = element.add(element.is(\"form\") ? submitButtons(element) : $());\n    const count = (element.data(\"busyCount\") ?? 0) + 1;\n    element.data(\"busyCount\", count);\n    marked.attr({ \"aria-busy\": \"true\", \"data-busy\": \"\" });\n    const settle = () => {\n        const left = element.data(\"busyCount\") - 1;\n        element.data(\"busyCount\", left);\n        if (left === 0) {\n            marked.removeAttr(\"aria-busy data-busy\");\n        }\n    };\n    Promise.resolve(request).then(settle, settle);\n    return request;\n}\n\n/**\n * Keep `target` busy for as long as the page stays - for an answer sending the\n * browser elsewhere (a payment), which takes a moment to leave.\n */\nexport function busyUntilLeaving(target) {\n    whileBusy(target, new Promise(() => {}));\n}\n\n/** Whether `target` is waiting for its request (`whileBusy`). */\nexport function isBusy(target) {\n    return $(target).is(\"[data-busy]\");\n}\n\n// Capturing, so that a component's own handler never hears the second try.\ndocument.addEventListener(\n    \"submit\",\n    (event) => {\n        if (isBusy(event.target)) {\n            event.preventDefault();\n            event.stopImmediatePropagation();\n        }\n    },\n    true,\n);\ndocument.addEventListener(\n    \"click\",\n    (event) => {\n        if (event.target.closest?.(\"button[data-busy], a[data-busy]\")) {\n            event.preventDefault();\n            event.stopImmediatePropagation();\n        }\n    },\n    true,\n);\n\n/**\n * Run a form's API request, clearing stale errors first and rendering field errors\n * from the response. The `detail` message is reported by `core/http.js`. The form\n * is busy while it waits (`whileBusy`).\n */\nexport function submitForm(form, request) {\n    clearFieldErrors(form);\n    whileBusy(form, request);\n    request.fail((response) => {\n        const fieldErrors = response.responseJSON?.field_errors;\n        if (response.status === 400 && fieldErrors) {\n            showFieldErrors(form, fieldErrors);\n        }\n    });\n    return request;\n}\n\n// Stale errors are cleared as soon as the visitor starts fixing the field.\n$(document).on(\"input change\", `form .${ERROR_CLASS}`, function () {\n    $(this).removeClass(ERROR_CLASS);\n    $(this).closest(\"label\").find(\".field-error\").remove();\n});\n\n// A browser opens the calendar of a date field only from its icon, which is\n// easy to miss - so a click anywhere on the field opens it, the browser's own.\n$(document).on(\"click\", 'input[type=\"date\"]', function () {\n    if (this.disabled || this.readOnly || typeof this.showPicker !== \"function\") {\n        return;\n    }\n    try {\n        this.showPicker();\n    } catch {\n        // Refused, say within a cross-origin frame: the icon still opens it.\n    }\n});\n"],
  "mappings": "AAAA,OAAOA,MAAO,cACd,OAAS,WAAAC,MAAe,YACxB,OAAS,QAAAC,MAAY,cAErB,MAAMC,EAAc,aAEb,gBAAS,iBAAiBC,EAAM,CACnCA,EAAK,KAAK,IAAID,CAAW,EAAE,EAAE,YAAYA,CAAW,EACpDC,EAAK,KAAK,cAAc,EAAE,OAAO,CACrC,CAOA,SAASC,EAAgBD,EAAME,EAAa,CACxC,SAAW,CAACC,EAAOC,CAAQ,IAAK,OAAO,QAAQF,CAAW,EAAG,CACzD,MAAMG,EAAQL,EAAK,KAAK,UAAUG,CAAK,IAAI,EAC3CE,EAAM,SAASN,CAAW,EAC1B,MAAMO,EAAQD,EAAM,QAAQ,OAAO,EAC/BC,EAAM,OAAS,GACfV,EAAE,eAAe,EACZ,SAAS,aAAa,EACtB,KAAKQ,EAAS,KAAK,GAAG,CAAC,EACvB,SAASE,CAAK,EAEvBR,EAAKQ,EAAM,OAAS,EAAIA,EAAQD,EAAO,OAAO,CAClD,CACJ,CAGO,gBAAS,WAAWL,EAAMO,EAAM,CACnC,OAAOP,EAAK,KAAK,UAAUO,CAAI,IAAI,EAAE,IAAI,CAC7C,CAGO,gBAAS,kBAAkBP,EAAMO,EAAM,CAC1C,OAAO,OAAO,WAAWP,EAAMO,CAAI,GAAK,EAAE,EAAE,KAAK,CACrD,CAEO,gBAAS,eAAeP,EAAMG,EAAOK,EAAS,CACjDP,EAAgBD,EAAM,CAAE,CAACG,CAAK,EAAG,CAACK,CAAO,CAAE,CAAC,C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A,EAAM,yBAAyB,EAEvC,EACA,EACJ,EAOO,gBAAS,WAAWrB,EAAMe,EAAS,CACtC,wBAAiBf,CAAI,EACrB,UAAUA,EAAMe,CAAO,EACvBA,EAAQ,KAAMO,GAAa,CACvB,MAAMpB,EAAcoB,EAAS,cAAc,aACvCA,EAAS,SAAW,KAAOpB,GAC3BD,EAAgBD,EAAME,CAAW,CAEzC,CAAC,EACMa,CACX,CAGAnB,EAAE,QAAQ,EAAE,GAAG,eAAgB,SAASG,CAAW,GAAI,UAAY,CAC/DH,EAAE,IAAI,EAAE,YAAYG,CAAW,EAC/BH,EAAE,IAAI,EAAE,QAAQ,OAAO,EAAE,KAAK,cAAc,EAAE,OAAO,CACzD,CAAC,EAIDA,EAAE,QAAQ,EAAE,GAAG,QAAS,qBAAsB,UAAY,CACtD,GAAI,OAAK,UAAY,KAAK,UAAY,OAAO,KAAK,YAAe,YAGjE,GAAI,CACA,KAAK,WAAW,CACpB,MAAQ,CAER,CACJ,CAAC",
  "names": ["$", "gettext", "play", "ERROR_CLASS", "form", "showFieldErrors", "fieldErrors", "field", "messages", "input", "label", "name", "message", "passwordField", "confirmation", "submitButtons", "index", "control", "target", "request", "element", "marked", "count", "settle", "left", "event", "response"]
}
