{
  "version": 3,
  "sources": ["password-check.js"],
  "sourcesContent": ["import $ from \"../../core/jquery.js\";\nimport { api } from \"../../core/api.js\";\nimport { register } from \"../../core/components.js\";\nimport { debounce } from \"../../core/debounce.js\";\nimport { gettext } from \"../../core/i18n.js\";\nimport { trimmedFieldValue } from \"../../core/forms.js\";\nimport { play } from \"../../core/motion.js\";\n\n/** Long enough not to ask on every keystroke, short enough to feel live. */\nconst DEBOUNCE_MS = 250;\n\nconst SCORE_CLASSES = [0, 1, 2, 3, 4].map(\n    (score) => `password-strength--score-${score}`,\n);\n\nconst MET_CLASS = \"password-requirements__item--met\";\n\n/**\n * A new password, checked against the requirements and rated as it is typed.\n * The requirements are listed by the template from `PasswordPolicy`, each with\n * what it is checked by: its length and its characters are checked here, as\n * each key is pressed, and the rest - a common password, one like the address -\n * and the strength are asked of the API once typing pauses. The server checks\n * the password again when the form is submitted anyway.\n *\n * Triggers `password-check:checked` on its root with each result of the API,\n * for a form that wants to act on whether the password would be accepted.\n */\nregister(\"password-check\", (root, { emailField, nameField }) => {\n    const input = root.find(\"[data-ref=input]\");\n    const form = root.closest(\"form\");\n    const strength = root.find(\"[data-ref=strength]\");\n    const strengthLabel = root.find(\"[data-ref=strength-label]\");\n    const requirements = root\n        .find(\"[data-ref=requirement]\")\n        .map(function () {\n            const item = $(this);\n            return {\n                item,\n                state: item.find(\"[data-ref=requirement-state]\"),\n                mark: item.find(\"[data-ref=requirement-mark]\"),\n                code: item.attr(\"data-code\"),\n                minLength: Number(item.attr(\"data-min-length\")) || null,\n                // Read as an attribute: `.data()` would make the digits a number.\n                characters: item.attr(\"data-characters\") ?? null,\n                isCheckedByApi: item.is(\"[data-checked-by-api]\"),\n            };\n        })\n        .get();\n    const answered = new Map();\n    let score = null;\n    let pending = null;\n    let started = false;\n\n    function valueOf(field) {\n        return field ? trimmedFieldValue(form, field) : \"\";\n    }\n\n    function meets(requirement, password) {\n        if (requirement.minLength !== null) {\n            return password.length >= requirement.minLength;\n        }\n        if (requirement.characters !== null) {\n            return [...password].some((c) => requirement.characters.includes(c));\n        }\n        return password !== \"\" && answered.get(requirement.code) === true;\n    }\n\n    /** Marks each requirement met or not - only a change restarts its animation. */\n    function renderRequirements() {\n        const password = input.val();\n        for (const requirement of requirements) {\n            const isMet = meets(requirement, password);\n            if (requirement.item.hasClass(MET_CLASS) === isMet) {\n                continue;\n            }\n            requirement.item.toggleClass(MET_CLASS, isMet);\n            requirement.mark.toggleClass(\"check-mark--checked\", isMet);\n            requirement.state.text(\n                isMet ? gettext(\"(requirement met)\") : gettext(\"(requirement not met)\"),\n            );\n        }\n    }\n\n    function renderStrength(result) {\n        strength.prop(\"hidden\", input.val() === \"\");\n        strengthLabel.text(result.strength.label);\n        if (result.strength.score === score) {\n            return;\n        }\n        score = result.strength.score;\n        strength.removeClass(SCORE_CLASSES).addClass(SCORE_CLASSES[score]);\n        play(strength.find(\".password-strength__meter\"), \"shake\");\n        play(strengthLabel, \"fadeIn\");\n    }\n\n    function render(result) {\n        for (const requirement of result.requirements) {\n            answered.set(requirement.code, requirement.is_met);\n        }\n        // The address it is compared with may be the signed-in user's, so the\n        // API says whether there is one.\n        for (const requirement of requirements.filter((r) => r.isCheckedByApi)) {\n            requirement.item.prop(\"hidden\", !answered.has(requirement.code));\n        }\n        renderRequirements();\n        renderStrength(result);\n        root.trigger(\"password-check:checked\", [result]);\n    }\n\n    function check() {\n        schedule.cancel();\n        pending?.abort();\n        const data = { password: input.val() };\n        if (valueOf(emailField)) {\n            data.email = valueOf(emailField);\n        }\n        if (valueOf(nameField)) {\n            data.full_name = valueOf(nameField);\n        }\n        // Silent: a check that fails costs nothing but the hints, and the\n        // password is checked again on submit anyway.\n        pending = api\n            .post(\"auth.passwordCheck\", {\n                data,\n                silent: true,\n                unauthorizedRedirect: false,\n            })\n            .done(render)\n            .always(() => {\n                pending = null;\n            });\n    }\n\n    const schedule = debounce(check, DEBOUNCE_MS);\n\n    // The API is first asked once the field is reached, so that a form nobody\n    // is filling in makes no requests.\n    input.on(\"focus\", () => {\n        if (!started) {\n            started = true;\n            check();\n        }\n    });\n    input.on(\"input\", () => {\n        renderRequirements();\n        if (input.val() === \"\") {\n            strength.prop(\"hidden\", true);\n        }\n        schedule();\n    });\n\n    for (const field of [emailField, nameField].filter(Boolean)) {\n        form.on(\"input\", `[name=\"${field}\"]`, () => {\n            if (started) {\n                schedule();\n            }\n        });\n    }\n\n    form.on(\"reset\", () => {\n        schedule.cancel();\n        pending?.abort();\n        started = false;\n        answered.clear();\n        score = null;\n        strength.prop(\"hidden\", true).removeClass(SCORE_CLASSES);\n        // After the browser has emptied the fields.\n        setTimeout(renderRequirements);\n    });\n});\n"],
  "mappings": "AAAA,OAAOA,MAAO,uBACd,OAAS,OAAAC,MAAW,oBACpB,OAAS,YAAAC,MAAgB,2BACzB,OAAS,YAAAC,MAAgB,yBACzB,OAAS,WAAAC,MAAe,qBACxB,OAAS,qBAAAC,MAAyB,sBAClC,OAAS,QAAAC,MAAY,uBAGrB,MAAMC,EAAc,IAEdC,EAAgB,CAAC,EAAG,EAAG,EAAG,EAAG,CAAC,EAAE,IACjCC,GAAU,4BAA4BA,CAAK,EAChD,EAEMC,EAAY,mCAalBR,EAAS,iBAAkB,CAACS,EAAM,CAAE,WAAAC,EAAY,UAAAC,CAAU,IAAM,CAC5D,MAAMC,EAAQH,EAAK,KAAK,kBAAkB,EACpCI,EAAOJ,EAAK,QAAQ,MAAM,EAC1BK,EAAWL,EAAK,KAAK,qBAAqB,EAC1CM,EAAgBN,EAAK,KAAK,2BAA2B,EACrDO,EAAeP,EAChB,KAAK,wBAAwB,EAC7B,IAAI,UAAY,CACb,MAAMQ,EAAOnB,EAAE,IAAI,EACnB,MAAO,CACH,KAAAmB,EACA,MAAOA,EAAK,KAAK,8BAA8B,EAC/C,KAAMA,EAAK,KAAK,6BAA6B,EAC7C,KAAMA,EAAK,KAAK,WAAW,EAC3B,UAAW,OAAOA,EAAK,KAAK,iBAAiB,CAAC,GAAK,KAEnD,WAA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ldValue", "play", "DEBOUNCE_MS", "SCORE_CLASSES", "score", "MET_CLASS", "root", "emailField", "nameField", "input", "form", "strength", "strengthLabel", "requirements", "item", "answered", "pending", "started", "valueOf", "field", "meets", "requirement", "password", "c", "renderRequirements", "isMet", "renderStrength", "result", "render", "r", "check", "schedule", "data"]
}
