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E,GAAQ,GAAwB,CAEzC,GADA,EAAS,KAAK,MAAM,EAAO,QAAQ,KAAM,MAC1B,YAAX,EAAsB,CACtB,MAAM,EAAQ,KAAK,MAAM,EAAO,aAC5B,IAEA,EAAc,MAAI,GAAG,MAU7B,CAEA,EAAE,GAAQ,GAAQ,EACtB,CACJ,CAAE,MAAO,GACL,QAAQ,MAAM,sCAAuC,EACzD,CAER,GAEc,EAAQ,iBAAiB,iCACjC,QAAQ,SAAU,GAWpB,IAAI,EACJ,IACI,EAAgB,EAAK,aAAa,0BAC5B,KAAK,MACD,EACK,aAAa,0BACb,QAAQ,KAAM,MAEvB,CAAC,CACX,CAAE,MAAO,GACL,EAAgB,CAAC,EACjB,QAAQ,MAAM,yCAA0C,EAC5D,CACA,MAAM,EAAS,CArBX,OAAQ,aACR,UAAW,EACX,SAAU,SACV,SAAU,KACV,gBAAgB,EAChB,YAAa,OACb,WAAW,KAeuB,GAEtC,GAAsB,WAAlB,EAAO,QAAuB,EAAK,MAAO,CAC1C,MAAM,EAAO,EAAK,MAAM,UAAU,EAAG,GAC/B,EAAQ,EAAK,MAAM,UAAU,EAAG,GAAK,EAC3C,EAAE,GACG,WAAW,GACX,WAAW,SAAU,IAAI,KAAK,EAAM,GAC7C,MACI,EAAE,GAAM,WAAW,EAE3B,EACJ,GAGA,SAAS,iBAAiB,mBAAoB,WAC1C,WAAW,WACf,GAGsB,iBAAX,QAAuB,OAAO,UACrC,OAAO,QAAU","file":"/npm/derafu-js@0.1.4/src/form/fields.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.48.0.\n * Original file: /npm/derafu-js@0.1.4/src/form/fields.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","/*! Form fields functions | (c) 2025 Derafu DEV | MIT */\n\n// Object where form fields functions will be assigned.\nconst FormFields = {};\n\n/**\n * Sends a form via POST to a specific URL.\n * Can optionally open the form in a new window.\n *\n * @param {string} url URL where the form should be sent.\n * @param {Object} data Object with the variables to pass to the form.\n * @param {boolean} [newWindow] Indicates if the form should be opened in a new\n * window.\n * @returns {void}\n */\nFormFields.post = function (url, data, newWindow) {\n    'use strict';\n    if (typeof url !== 'string' || typeof data !== 'object') {\n        console.error('FormFields.post: Invalid arguments.');\n        return;\n    }\n    if (newWindow === undefined) {\n        newWindow = false;\n    }\n\n    const form = document.createElement('form');\n    form.method = 'post';\n    form.action = url;\n    if (newWindow) {\n        form.target = '_blank';\n    }\n\n    Object.keys(data).forEach(key => {\n        const input = document.createElement('input');\n        input.type = 'hidden';\n        input.name = key;\n        input.value = data[key];\n        form.appendChild(input);\n    });\n\n    document.body.appendChild(form);\n    form.submit();\n    document.body.removeChild(form);\n};\n\n/**\n * Allows toggling the display of a password field between hidden and visible.\n *\n * Changes the input type from 'password' to 'text' and vice versa, and updates\n * the corresponding icon.\n *\n * @param {HTMLElement} button - Button used to activate the function, located\n * next to the password field.\n * @returns {void}\n */\nFormFields.showPassword = function (button) {\n    'use strict';\n    if (!(button instanceof HTMLElement)) {\n        console.error(\n            'FormFields.showPassword: The provided argument is not a valid HTML element.',\n        );\n        return;\n    }\n\n    // Try to find the related password field.\n    const input = button\n        .closest('.input-group')\n        .querySelector('input[type=\"password\"], input[type=\"text\"]');\n    const icon = button.querySelector('i');\n\n    // Check if the necessary elements are present.\n    if (!input || !icon) {\n        console.error('FormFields.showPassword: Input or icon not found.');\n        return;\n    }\n\n    // Toggle between showing and hiding the password.\n    if (input.type === 'password') {\n        input.type = 'text';\n        icon.className = 'fa-regular fa-eye-slash fa-fw';\n    } else {\n        input.type = 'password';\n        icon.className = 'fa-regular fa-eye fa-fw';\n    }\n};\n\n/**\n * Fills the password field next to the button with a randomly generated\n * password.\n *\n * Relies on Utils.generatePassword() to create the value, so utils.js must\n * be loaded before this file for this function to work.\n *\n * @param {HTMLElement} button - Button used to activate the function, located\n * next to the password field.\n * @param {number} [length] - Length of the generated password. Default is 12\n * (see Utils.generatePassword()).\n * @returns {void}\n */\nFormFields.generatePassword = function (button, length) {\n    'use strict';\n    if (!(button instanceof HTMLElement)) {\n        console.error(\n            'FormFields.generatePassword: The provided argument is not a valid HTML element.',\n        );\n        return;\n    }\n    if (\n        typeof Utils === 'undefined' ||\n        typeof Utils.generatePassword !== 'function'\n    ) {\n        console.error(\n            'FormFields.generatePassword: Utils.generatePassword() is not available. Make sure utils.js is loaded before this file.',\n        );\n        return;\n    }\n\n    // Try to find the related password field.\n    const input = button\n        .closest('.input-group')\n        .querySelector('input[type=\"password\"], input[type=\"text\"]');\n\n    if (!input) {\n        console.error('FormFields.generatePassword: Input not found.');\n        return;\n    }\n\n    // Fill the field and notify listeners (e.g. validation) of the change.\n    input.value = Utils.generatePassword(length);\n    input.dispatchEvent(new Event('input', { bubbles: true }));\n};\n\n/**\n * Allows editing the content of a text field in a larger dialog box.\n * Useful for text fields with extensive content that would benefit from an\n * expanded view.\n *\n * @param {HTMLElement} field - Text field that is desired to be edited in a\n * larger dialog box.\n * @returns {void}\n */\nFormFields.growup = function (field) {\n    'use strict';\n    if (!(field instanceof HTMLElement)) {\n        console.error(\n            'FormFields.growup: The provided argument is not a valid HTML element.',\n        );\n        return;\n    }\n\n    const maxLength = field.getAttribute('maxlength');\n    bootbox.prompt({\n        title: `Edit field: ${field.name.replace('[]', '')}`,\n        inputType: 'textarea',\n        value: field.value,\n        rows: 5,\n        backdrop: true,\n        centerVertical: true,\n        buttons: {\n            confirm: {\n                label: 'Save changes',\n                className: 'btn-success',\n            },\n            cancel: {\n                label: 'Cancel',\n                className: 'btn-danger',\n            },\n        },\n        callback(result) {\n            if (result !== null) {\n                field.value = maxLength\n                    ? result.substring(0, maxLength)\n                    : result;\n            }\n        },\n    });\n};\n\n/**\n * Changes the check state of a group of checkboxes with the same name.\n *\n * @param {string} name - Name of the checkbox array to modify.\n * @param {boolean} checked - Check state to apply to the checkboxes.\n * @returns {void}\n */\nFormFields.checkboxesSet = function (name, checked) {\n    'use strict';\n    const checkboxes = document.querySelectorAll(`input[name='${name}[]']`);\n    checkboxes.forEach(function (checkbox) {\n        checkbox.checked = checked;\n    });\n};\n\n/**\n * Removes options from a select element.\n *\n * @param {HTMLSelectElement} selectbox - Select element to be cleared.\n * @param {number} [from] - Index from which to start removing options.\n * Default is 0.\n * @returns {void}\n */\nFormFields.removeOptions = function (selectbox, from) {\n    'use strict';\n    if (from === undefined) {\n        from = 0;\n    }\n    if (!(selectbox instanceof HTMLSelectElement)) {\n        console.error(\n            'FormFields.removeOptions: The provided element is not a valid select.',\n        );\n        return;\n    }\n    while (selectbox.options.length > from) {\n        selectbox.remove(from);\n    }\n};\n\n/**\n * Adds options to a select element using a list of options.\n *\n * @param {string} selectID - Identifier of the select element to modify.\n * @param {Object} opcionesListado - Object with options indexed by a higher\n * category.\n * @param {string} seleccionada - Selected category from which options are\n * wanted to be loaded.\n * @param {number} [dejar] - Number of initial options that should be kept when\n * updating. Default is 1.\n * @returns {void}\n */\nFormFields.addOptions = function (\n    selectID,\n    opcionesListado,\n    seleccionada,\n    dejar,\n) {\n    'use strict';\n    const select = document.getElementById(selectID);\n    if (!select) {\n        console.error(\n            `FormFields.addOptions: No select element found with ID '${selectID}'.`,\n        );\n        return;\n    }\n    if (dejar === undefined) {\n        dejar = 1;\n    }\n\n    FormFields.removeOptions(select, dejar);\n    select.disabled = opcionesListado[seleccionada] === undefined;\n    if (!select.disabled) {\n        const opciones =\n            opcionesListado[seleccionada] instanceof Array\n                ? opcionesListado[seleccionada]\n                : [opcionesListado[seleccionada]];\n        opciones.forEach(function (opcion) {\n            const option = document.createElement('option');\n            option.value = opcion.id;\n            option.textContent = opcion.glosa;\n            select.appendChild(option);\n        });\n    }\n};\n\n/**\n * Processes form elements that require additional configuration or\n * initialization within a specific element. This method searches for elements\n * that have specific attributes, such as `data-wrapper-method` and\n * `data-wrapper-config`, and applies the required configuration or\n * initialization, such as activating select2 with specific options.\n *\n * @param {Element} element - The DOM element in which to search and process\n * fields. Usually would be a row recently inserted in a table.\n * @returns {void}\n */\nFormFields.fixFields = function (element) {\n    'use strict';\n    // Check if window or jQuery is not defined and return immediately.\n    if (typeof window === 'undefined' || typeof window.jQuery !== 'function') {\n        console.error('window or jQuery is not available.');\n        return;\n    }\n    // If element is not provided, use the entire document body.\n    if (element === undefined) {\n        element = document.body;\n    }\n    // Adjust select fields through their wrapper if it exists.\n    const selects = element.querySelectorAll('select[data-wrapper-method]');\n    selects.forEach(function (select) {\n        const method = select.getAttribute('data-wrapper-method');\n        let config = select.getAttribute('data-wrapper-config');\n        if (method && config) {\n            try {\n                if (typeof $(select)[method] === 'function') {\n                    config = JSON.parse(config.replace(/'/g, '\"'));\n                    if (method === 'select2') {\n                        const width = Math.round(select.offsetWidth);\n                        if (width) {\n                            // Fix size prevents overflow in tables.\n                            config['width'] = `${width}px`;\n                        } else {\n                            // TODO: if there's no size, some width should be\n                            // assigned or something should be done because as\n                            // it is, without width, it stays at 100% and\n                            // overflows the same in these cases where there's\n                            // no width of the select field. Using:\n                            // let width = Math.round(select.parentElement.offsetWidth);\n                            // didn't work either. New ideas?\n                        }\n                    }\n                    // Initialize (e.g., select2) in the select field.\n                    $(select)[method](config);\n                }\n            } catch (e) {\n                console.error('Error parsing data-wrapper-config: ', e);\n            }\n        }\n    });\n    // Adjust input fields that use datepicker.\n    const dates = element.querySelectorAll('input[data-datepicker-config]');\n    dates.forEach(function (date) {\n        // Datepicker configuration.\n        const defaultConfig = {\n            format: 'yyyy-mm-dd',\n            weekStart: 1,\n            todayBtn: 'linked',\n            language: 'es',\n            todayHighlight: true,\n            orientation: 'auto',\n            autoclose: true,\n        };\n        let elementConfig;\n        try {\n            elementConfig = date.getAttribute('data-datepicker-config')\n                ? JSON.parse(\n                      date\n                          .getAttribute('data-datepicker-config')\n                          .replace(/'/g, '\"'),\n                  )\n                : {};\n        } catch (e) {\n            elementConfig = {};\n            console.error('Error parsing data-datepicker-config: ', e);\n        }\n        const config = { ...defaultConfig, ...elementConfig };\n        // Initialize datepicker according to configuration.\n        if (config.format === 'yyyymm' && date.value) {\n            const year = date.value.substring(0, 4);\n            const month = date.value.substring(4, 6) - 1;\n            $(date)\n                .datepicker(config)\n                .datepicker('update', new Date(year, month));\n        } else {\n            $(date).datepicker(config);\n        }\n    });\n};\n\n// Fix form fields automatically when loading the script.\ndocument.addEventListener('DOMContentLoaded', function () {\n    FormFields.fixFields();\n});\n\n// Export module for use in node.js.\nif (typeof module === 'object' && module.exports) {\n    module.exports = FormFields;\n}\n"]}