Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"configtext_maxlength.min.js","sources":["../src/configtext_maxlength.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Validation for configtext_maxlength.\n *\n * @module     core_form/configtext-maxlength\n * @copyright  2021 The Open University\n */\nimport {get_string as getString} from 'core/str';\nimport Templates from 'core/templates';\nimport Notification from 'core/notification';\nimport {prefetchStrings, prefetchTemplates} from 'core/prefetch';\n\nlet registered = false;\n\n/**\n * Initialisation function.\n */\nexport const init = () => {\n    if (registered) {\n        return;\n    }\n    prefetchStrings('core', [\n        'maximumchars',\n    ]);\n\n    prefetchTemplates([\n        'core_form/setting_validation_failure',\n    ]);\n\n    registered = true;\n\n    document.addEventListener('input', e => {\n        const maxLengthField = e.target.closest('[data-validation-max-length]');\n        if (!maxLengthField) {\n            return;\n        }\n\n        if (maxLengthField.value.length > maxLengthField.dataset.validationMaxLength) {\n            // Disable the form for this field.\n            maxLengthField.form.addEventListener('submit', submissionCheck);\n            // Display an error.\n            getString('maximumchars', 'core', maxLengthField.dataset.validationMaxLength)\n                .then(errorMessage => {\n                    return Templates.renderForPromise('core_form/setting_validation_failure', {\n                        fieldid: maxLengthField.id,\n                        message: errorMessage,\n                    });\n                })\n                .then(errorTemplate => {\n                    if (!maxLengthField.dataset.validationFailureId) {\n                        const formWrapper = maxLengthField.closest('.form-text');\n                        Templates.prependNodeContents(formWrapper, errorTemplate.html, errorTemplate.js);\n                        maxLengthField.dataset.validationFailureId = `maxlength_error_${maxLengthField.id}`;\n                        // Disable submit button when the message is displayed.\n                        updateSubmitButton();\n                    }\n                    return;\n                })\n                .then(() => {\n                    maxLengthField.setAttribute('aria-invalid', true);\n                    const errorField = document.getElementById(maxLengthField.dataset.validationFailureId);\n                    if (errorField) {\n                        errorField.setAttribute('aria-describedby', maxLengthField.id);\n                    }\n                    return;\n                })\n                .catch(Notification.exception);\n        } else {\n            // Remove the old message.\n            const validationMessage = document.getElementById(maxLengthField.dataset.validationFailureId);\n            if (validationMessage) {\n                validationMessage.parentElement.remove();\n                delete maxLengthField.dataset.validationFailureId;\n                maxLengthField.removeAttribute('aria-invalid');\n                // Enable submit button when the message was removed.\n                updateSubmitButton();\n            }\n        }\n    });\n};\n\n/**\n * Handle form submission.\n *\n * @param {Event} e The event.\n */\nconst submissionCheck = e => {\n    const maxLengthFields = e.target.querySelectorAll('[data-validation-max-length]');\n    const maxLengthFieldsArray = Array.from(maxLengthFields);\n    maxLengthFieldsArray.some(maxLengthField => {\n        // Focus on the first validation failure.\n        if (maxLengthField.value.length > maxLengthField.dataset.validationMaxLength) {\n            e.preventDefault();\n            maxLengthField.focus();\n            return true;\n        }\n        return false;\n    });\n};\n\n/**\n * Update submit button.\n */\nconst updateSubmitButton = () => {\n    const shouldDisable = document.querySelector('form#adminsettings .error');\n    document.querySelector('form#adminsettings button[type=\"submit\"]').disabled = !!shouldDisable;\n};\n"],"names":["registered","document","addEventListener","e","maxLengthField","target","closest","value","length","dataset","validationMaxLength","form","submissionCheck","then","errorMessage","Templates","renderForPromise","fieldid","id","message","errorTemplate","validationFailureId","formWrapper","prependNodeContents","html","js","updateSubmitButton","setAttribute","errorField","getElementById","catch","Notification","exception","validationMessage","parentElement","remove","removeAttribute","maxLengthFields","querySelectorAll","Array","from","some","preventDefault","focus","shouldDisable","querySelector","disabled"],"mappings":"qbA0BIA,YAAa,gBAKG,KACZA,2CAGY,OAAQ,CACpB,iDAGc,CACd,yCAGJA,YAAa,EAEbC,SAASC,iBAAiB,SAASC,UACzBC,eAAiBD,EAAEE,OAAOC,QAAQ,mCACnCF,kBAIDA,eAAeG,MAAMC,OAASJ,eAAeK,QAAQC,oBAErDN,eAAeO,KAAKT,iBAAiB,SAAUU,qCAErC,eAAgB,OAAQR,eAAeK,QAAQC,qBACpDG,MAAKC,cACKC,mBAAUC,iBAAiB,uCAAwC,CACtEC,QAASb,eAAec,GACxBC,QAASL,iBAGhBD,MAAKO,oBACGhB,eAAeK,QAAQY,oBAAqB,OACvCC,YAAclB,eAAeE,QAAQ,iCACjCiB,oBAAoBD,YAAaF,cAAcI,KAAMJ,cAAcK,IAC7ErB,eAAeK,QAAQY,8CAAyCjB,eAAec,IAE/EQ,yBAIPb,MAAK,KACFT,eAAeuB,aAAa,gBAAgB,SACtCC,WAAa3B,SAAS4B,eAAezB,eAAeK,QAAQY,qBAC9DO,YACAA,WAAWD,aAAa,mBAAoBvB,eAAec,OAIlEY,MAAMC,sBAAaC,eACrB,OAEGC,kBAAoBhC,SAAS4B,eAAezB,eAAeK,QAAQY,qBACrEY,oBACAA,kBAAkBC,cAAcC,gBACzB/B,eAAeK,QAAQY,oBAC9BjB,eAAegC,gBAAgB,gBAE/BV,kCAWVd,gBAAkBT,UACdkC,gBAAkBlC,EAAEE,OAAOiC,iBAAiB,gCACrBC,MAAMC,KAAKH,iBACnBI,MAAKrC,gBAElBA,eAAeG,MAAMC,OAASJ,eAAeK,QAAQC,sBACrDP,EAAEuC,iBACFtC,eAAeuC,SACR,MASbjB,mBAAqB,WACjBkB,cAAgB3C,SAAS4C,cAAc,6BAC7C5C,SAAS4C,cAAc,4CAA4CC,WAAaF"}