{"version":3,"file":"form.module-85F-vKtj.js","sources":["../../../../../Flow.Showcase.Static.Bundler/Root/modules/form.module.ts"],"sourcesContent":["//@ts-nocheck\n\nimport { commonState } from \"./common-state.module.ts\";\nimport type {HTMLFormFieldElement} from './form-field.module.ts';\n\nexport async function sendFormData(target, onSuccess, onError) {\n clearErrors(target);\n /**\n * @type HTMLElement\n */\n const preloader = target.querySelector(\"[data-form-preloader]\");\n if (preloader) preloader.classList.remove(\"display-none\");\n\n const url = target.action;\n const method = target.method;\n const resp = await fetch(url, {\n method: method,\n body: new FormData(target),\n headers: {\n \"X-Requested-With\": \"XMLHttpRequest\",\n },\n });\n try {\n if (resp.ok) {\n if (onSuccess) {\n const json = await resp.json();\n onSuccess(json);\n } else {\n window.location.hash = \"#modal-closed\";\n location.reload();\n }\n } else {\n const error = await resp.json();\n if (resp.status >= 500) {\n throw new Error(error.message);\n }\n\n if (error.code === \"ValidationError\") {\n for (let prop in error.validation_errors) {\n if (error.validation_errors.hasOwnProperty(prop)) {\n const errorElem = /** @type {HTMLElement} */ (\n target.querySelector(\n `[data-validation-key=\"${prop}\"]`\n )\n );\n setValidationErrorMessage(\n errorElem,\n error.validation_errors[prop]\n );\n }\n }\n } else if (onError) {\n onError(target, error);\n } else {\n showFormErrorMessage(target, error.message);\n }\n }\n } catch (error) {\n showFormErrorMessage(\n target,\n commonState.translations[\"Sorry, something went wrong\"]\n );\n console.error(error);\n }\n if (preloader) preloader.classList.add(\"display-none\");\n}\n\n/**\n * Показать соообщение об ошибке\n * @param {HTMLElement} target - форма\n * @param {string} message - сообщение\n */\nexport function showFormErrorMessage(target, message) {\n const formFeedback = target.querySelector(\"[data-form-feedback]\");\n if (formFeedback) {\n formFeedback.classList.remove(\"display-none\");\n const feedbackMessage = formFeedback.querySelector(\"[data-message]\");\n if (feedbackMessage) {\n feedbackMessage.innerHTML = message;\n }\n }\n}\n\n/**\n * Показать сообщение об ошибке валидации в элементе формы\n * @param {HTMLElement} errorElem - элемент, содержащий сообщение об ошибке\n * @param {Array | string | null} error - ошибка\n */\nexport function setValidationErrorMessage(errorElem, error) {\n let message;\n if (Array.isArray(error)) {\n if (error.length > 0) {\n message = error[0];\n }\n } else {\n message = error;\n }\n const span = /** @type {HTMLSpanElement} */ (\n errorElem.querySelector(\"span\")\n );\n if (message) {\n errorElem.classList.remove(\"display-none\");\n } else {\n errorElem.classList.add(\"display-none\");\n }\n span.textContent = message;\n}\n\n/**\n * Очистка ошибок формы\n * @param {HTMLElement} target\n */\nexport function clearErrors(target) {\n const validationErrorElements = /** @type {NodeListOf} */ (\n target.querySelectorAll(`[data-validation-key]`)\n );\n for (const errorElem of validationErrorElements) {\n setValidationErrorMessage(errorElem, null);\n }\n const formFeedback = target.querySelector(\"[data-form-feedback]\");\n if (formFeedback) {\n formFeedback.classList.add(\"display-none\");\n const feedbackMessage = formFeedback.querySelector(\"[data-message]\");\n if (feedbackMessage) {\n feedbackMessage.innerHTML = \"\";\n }\n }\n}\n\n/**\n * Контрол гуард\n */\nexport function isControlElement(element: unknown): element is HTMLFormFieldElement {\n return (\n element instanceof HTMLInputElement ||\n element instanceof HTMLSelectElement ||\n element instanceof HTMLTextAreaElement\n );\n}\n\n/**\n * Добавить валидатор наличия изменений на форму\n * @param {HTMLFormElement} updateFormElement\n */\nexport function addHasFormValuesChangedValidator(updateFormElement: HTMLFormElement) {\n const submitButton = (\n updateFormElement.querySelector(\"[type='submit']\")\n );\n\n if (!submitButton) return;\n\n submitButton.disabled = true;\n\n // Логика основывается на ведении списка полей, чьё значение отличается от начального (значения на момент запуска скрипта)\n const formInitialValue = new Map();\n const changedFieldNames = new Set();\n\n /**\n * @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement#elements_that_are_considered_form_controls\n */\n const controlElements = updateFormElement.elements;\n\n // Собрать начальные значения\n for (const element of controlElements) {\n // INFO: не работает с input[type=\"image\"]\n if (isControlElement(element)) {\n formInitialValue.set(element.name, element.value);\n }\n }\n\n // На изменение данных формы блокировать\\активировать кнопку отправки\n updateFormElement.addEventListener(\"input\", (event) => {\n const element = event.target;\n if (isControlElement(element)) {\n const initialValue = formInitialValue.get(element.name);\n const newValue = element.value;\n\n if (initialValue === newValue) {\n changedFieldNames.delete(element.name);\n } else {\n changedFieldNames.add(element.name);\n }\n\n submitButton.disabled = changedFieldNames.size === 0;\n }\n });\n}\n"],"names":["sendFormData","target","onSuccess","onError","clearErrors","preloader","url","method","resp","json","error","prop","errorElem","setValidationErrorMessage","showFormErrorMessage","commonState","message","formFeedback","feedbackMessage","span","validationErrorElements","isControlElement","element","addHasFormValuesChangedValidator","updateFormElement","submitButton","formInitialValue","changedFieldNames","controlElements","event","initialValue","newValue"],"mappings":"sDAKsB,eAAAA,EAAaC,EAAQC,EAAWC,EAAS,CAC3DC,EAAYH,CAAM,EAIZ,MAAAI,EAAYJ,EAAO,cAAc,uBAAuB,EAC1DI,GAAWA,EAAU,UAAU,OAAO,cAAc,EAExD,MAAMC,EAAML,EAAO,OACbM,EAASN,EAAO,OAChBO,EAAO,MAAM,MAAMF,EAAK,CAC1B,OAAAC,EACA,KAAM,IAAI,SAASN,CAAM,EACzB,QAAS,CACL,mBAAoB,gBAAA,CACxB,CACH,EACG,GAAA,CACA,GAAIO,EAAK,GACL,GAAIN,EAAW,CACL,MAAAO,EAAO,MAAMD,EAAK,KAAK,EAC7BN,EAAUO,CAAI,CAAA,MAEd,OAAO,SAAS,KAAO,gBACvB,SAAS,OAAO,MAEjB,CACG,MAAAC,EAAQ,MAAMF,EAAK,KAAK,EAC1B,GAAAA,EAAK,QAAU,IACT,MAAA,IAAI,MAAME,EAAM,OAAO,EAG7B,GAAAA,EAAM,OAAS,mBACN,QAAAC,KAAQD,EAAM,kBACnB,GAAIA,EAAM,kBAAkB,eAAeC,CAAI,EAAG,CACxC,MAAAC,EACFX,EAAO,cACH,yBAAyBU,CAAI,IAAA,EAGrCE,EACID,EACAF,EAAM,kBAAkBC,CAAI,CAChC,CAAA,OAGDR,EACPA,EAAQF,EAA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