diff --git a/lib/form/form.js b/lib/form/form.js index 0ef899f64f3..c9cde31c890 100644 --- a/lib/form/form.js +++ b/lib/form/form.js @@ -16,6 +16,7 @@ if (typeof M.form.dependencyManager === 'undefined') { _dirty: null, _nameCollections: null, _fileinputs: null, + _staticElements: null, _editors: null, _editorNameSuffix: '[text]', @@ -105,6 +106,14 @@ if (typeof M.form.dependencyManager === 'undefined') { allnames[name].push(node); } }); + // Locate any static elements for each name. + this.get('form').all('.form-control-static').each(function(node) { + var name = node.getData('name'); + if (({}).hasOwnProperty.call(allnames, name)) { + names[name].push(node); + allnames[name].push(node); + } + }); this._nameCollections = {names: names, allnames: allnames}; }, @@ -259,20 +268,47 @@ if (typeof M.form.dependencyManager === 'undefined') { * @param {Boolean} disabled True to disable, false to enable. */ _disableElement: function(name, disabled) { - var els = this.elementsByName(name), + const els = this.elementsByName(name), filepicker = this.isFilePicker(name), - editors = this.get('form').all('.fitem [data-fieldtype="editor"] textarea[name="' + name + '[text]"]'); + editors = this.get('form').all('.fitem [data-fieldtype="editor"] textarea[name="' + name + '[text]"]'), + staticElement = this.isStaticElement(name); els.each(function(node) { + const fitem = node.ancestor('.fitem'); if (disabled) { node.setAttribute('disabled', 'disabled'); } else { node.removeAttribute('disabled'); } - + // Enable/Disable static elements if exist. + if (staticElement) { + const disabledNonTextElements = 'INPUT,SELECT,TEXTAREA,BUTTON,A'; + if (disabled) { + // Mute the text inside the current static element. + fitem.addClass('text-muted'); + // Disabled non-text elements in the static if exist. + fitem.all(disabledNonTextElements).each(function(disabledElement) { + if (disabledElement.get('tagName').toUpperCase() === "A") { + disabledElement.addClass('disabled'); + } else { + disabledElement.setAttribute('disabled', 'disabled'); + } + }); + } else { + // Unmute the text inside the current static element. + fitem.removeClass('text-muted'); + // Enabled non-text elements in the static if exist. + fitem.all(disabledNonTextElements).each(function(disabledElement) { + if (disabledElement.get('tagName').toUpperCase() === "A") { + disabledElement.removeClass('disabled'); + } else { + disabledElement.removeAttribute('disabled', 'disabled'); + } + }); + } + } // Extra code to disable filepicker or filemanager form elements if (filepicker) { - var fitem = node.ancestor('.fitem'); if (fitem) { if (disabled) { fitem.addClass('disabled'); @@ -363,6 +399,28 @@ if (typeof M.form.dependencyManager === 'undefined') { return false; }, + /** + * Checks if a form element with the given name is static. + * + * @param {string} el - The name of the form element to check. + * @returns {boolean} - Returns true if the form element is static, otherwise false. + */ + isStaticElement: function(el) { + if (!this._staticElements) { + const staticElements = {}; + const els = this.get('form').all('.fitem [data-fieldtype="static"] .form-control-static'); + els.each(function(node) { + if (node.getData('name') === el) { + staticElements[node.getData('name')] = true; + } + }); + this._staticElements = staticElements; + } + if (({}).hasOwnProperty.call(this._staticElements, el)) { + return this._staticElements[el] || false; + } + return false; + }, _dependencyNotchecked: function(elements, value, isHide) { var lock = false; elements.each(function() { diff --git a/lib/form/templates/element-static.mustache b/lib/form/templates/element-static.mustache index 409cd95ea4c..e8fa064d9dd 100644 --- a/lib/form/templates/element-static.mustache +++ b/lib/form/templates/element-static.mustache @@ -32,12 +32,20 @@ Example context (json): { "label": "Example label", - "element": { "html": "Example HTML", "staticlabel": true } + "element": { + "html": "Example HTML", + "staticlabel": true, + "extraclasses": null, + "name": "static_name", + "id": "id_static", + "wrapperid": "fitem_id_static", + "iderror": "id_error_static" + } } }} {{< core_form/element-template }} {{$element}} -