Proyectos de Subversion Moodle

Rev

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

{"version":3,"file":"form-display-errors.min.js","sources":["../src/form-display-errors.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 * Custom form error event handler to manipulate the bootstrap markup and show\n * nicely styled errors in an mform.\n *\n * @module     theme_monocolor/form-display-errors\n * @copyright  2016 Damyon Wiese <damyon@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core_form/events'], function($, FormEvent) {\n    return {\n        /**\n         * Enhance the supplied element to handle form field errors.\n         *\n         * @method\n         * @param {String} elementid\n         * @listens event:formFieldValidationFailed\n         */\n        enhance: function(elementid) {\n            var element = document.getElementById(elementid);\n            if (!element) {\n                // Some elements (e.g. static) don't have a form field.\n                // Hence there is no validation. So, no setup required here.\n                return;\n            }\n\n            element.addEventListener(FormEvent.eventTypes.formFieldValidationFailed, e => {\n                const msg = e.detail.message;\n                e.preventDefault();\n\n                var parent = $(element).closest('.form-group');\n                var feedback = parent.find('.form-control-feedback');\n                const feedbackId = feedback.attr('id');\n\n                // Get current aria-describedby value.\n                let describedBy = $(element).attr('aria-describedby');\n                if (typeof describedBy === \"undefined\") {\n                    describedBy = '';\n                }\n                // Split aria-describedby attribute into an array of IDs if necessary.\n                let describedByIds = [];\n                if (describedBy.length) {\n                    describedByIds = describedBy.split(\" \");\n                }\n                // Find the the feedback container in the aria-describedby attribute.\n                const feedbackIndex = describedByIds.indexOf(feedbackId);\n\n                // Sometimes (atto) we have a hidden textarea backed by a real contenteditable div.\n                if (($(element).prop(\"tagName\") == 'TEXTAREA') && parent.find('[contenteditable]').length > 0) {\n                    element = parent.find('[contenteditable]');\n                }\n                if (msg !== '') {\n                    parent.addClass('has-danger');\n                    parent.data('client-validation-error', true);\n                    $(element).addClass('is-invalid');\n                    // Append the feedback ID to the aria-describedby attribute if it doesn't exist yet.\n                    if (feedbackIndex === -1) {\n                        describedByIds.push(feedbackId);\n                        $(element).attr('aria-describedby', describedByIds.join(\" \"));\n                    }\n                    $(element).attr('aria-invalid', true);\n                    feedback.attr('tabindex', 0);\n                    feedback.html(msg);\n\n                    // Only display and focus when the error was not already visible.\n                    // This is so that, when tabbing around the form, you don't get stuck.\n                    if (!feedback.is(':visible')) {\n                        feedback.show();\n                        feedback.focus();\n                    }\n\n                } else {\n                    if (parent.data('client-validation-error') === true) {\n                        parent.removeClass('has-danger');\n                        parent.data('client-validation-error', false);\n                        $(element).removeClass('is-invalid');\n                        // If the aria-describedby attribute contains the error container's ID, remove it.\n                        if (feedbackIndex > -1) {\n                            describedByIds.splice(feedbackIndex, 1);\n                        }\n                        // Check the remaining element IDs in the aria-describedby attribute.\n                        if (describedByIds.length) {\n                            // If there's at least one, combine them with a blank space and update the aria-describedby attribute.\n                            describedBy = describedByIds.join(\" \");\n                            // Put back the new describedby attribute.\n                            $(element).attr('aria-describedby', describedBy);\n                        } else {\n                            // If there's none, remove the aria-describedby attribute.\n                            $(element).removeAttr('aria-describedby');\n                        }\n                        $(element).attr('aria-invalid', false);\n                        feedback.hide();\n                    }\n                }\n            });\n\n            var form = element.closest('form');\n            if (form && !('boostFormErrorsEnhanced' in form.dataset)) {\n                form.addEventListener('submit', function() {\n                    var visibleError = $('.form-control-feedback:visible');\n                    if (visibleError.length) {\n                        visibleError[0].focus();\n                    }\n                });\n                form.dataset.boostFormErrorsEnhanced = 1;\n            }\n        }\n    };\n});\n"],"names":["define","$","FormEvent","enhance","elementid","element","document","getElementById","addEventListener","eventTypes","formFieldValidationFailed","e","msg","detail","message","preventDefault","parent","closest","feedback","find","feedbackId","attr","describedBy","describedByIds","length","split","feedbackIndex","indexOf","prop","addClass","data","push","join","html","is","show","focus","removeClass","splice","removeAttr","hide","form","dataset","visibleError","boostFormErrorsEnhanced"],"mappings":";;;;;;;;AAuBAA,yCAAO,CAAC,SAAU,qBAAqB,SAASC,EAAGC,iBACxC,CAQHC,QAAS,SAASC,eACVC,QAAUC,SAASC,eAAeH,cACjCC,SAMLA,QAAQG,iBAAiBN,UAAUO,WAAWC,2BAA2BC,UAC/DC,IAAMD,EAAEE,OAAOC,QACrBH,EAAEI,qBAEEC,OAASf,EAAEI,SAASY,QAAQ,eAC5BC,SAAWF,OAAOG,KAAK,gCACrBC,WAAaF,SAASG,KAAK,UAG7BC,YAAcrB,EAAEI,SAASgB,KAAK,yBACP,IAAhBC,cACPA,YAAc,QAGdC,eAAiB,GACjBD,YAAYE,SACZD,eAAiBD,YAAYG,MAAM,YAGjCC,cAAgBH,eAAeI,QAAQP,YAGV,YAA9BnB,EAAEI,SAASuB,KAAK,YAA6BZ,OAAOG,KAAK,qBAAqBK,OAAS,IACxFnB,QAAUW,OAAOG,KAAK,sBAEd,KAARP,KACAI,OAAOa,SAAS,cAChBb,OAAOc,KAAK,2BAA2B,GACvC7B,EAAEI,SAASwB,SAAS,eAEG,IAAnBH,gBACAH,eAAeQ,KAAKX,YACpBnB,EAAEI,SAASgB,KAAK,mBAAoBE,eAAeS,KAAK,OAE5D/B,EAAEI,SAASgB,KAAK,gBAAgB,GAChCH,SAASG,KAAK,WAAY,GAC1BH,SAASe,KAAKrB,KAITM,SAASgB,GAAG,cACbhB,SAASiB,OACTjB,SAASkB,WAIkC,IAA3CpB,OAAOc,KAAK,6BACZd,OAAOqB,YAAY,cACnBrB,OAAOc,KAAK,2BAA2B,GACvC7B,EAAEI,SAASgC,YAAY,cAEnBX,eAAiB,GACjBH,eAAee,OAAOZ,cAAe,GAGrCH,eAAeC,QAEfF,YAAcC,eAAeS,KAAK,KAElC/B,EAAEI,SAASgB,KAAK,mBAAoBC,cAGpCrB,EAAEI,SAASkC,WAAW,oBAE1BtC,EAAEI,SAASgB,KAAK,gBAAgB,GAChCH,SAASsB,eAKjBC,KAAOpC,QAAQY,QAAQ,QACvBwB,QAAU,4BAA6BA,KAAKC,WAC5CD,KAAKjC,iBAAiB,UAAU,eACxBmC,aAAe1C,EAAE,kCACjB0C,aAAanB,QACbmB,aAAa,GAAGP,WAGxBK,KAAKC,QAAQE,wBAA0B"}