diff --git a/media_source/system/js/fields/joomla-field-subform.w-c.es6.js b/media_source/system/js/fields/joomla-field-subform.w-c.es6.js index e437f665f333..a61de9eb5dad 100644 --- a/media_source/system/js/fields/joomla-field-subform.w-c.es6.js +++ b/media_source/system/js/fields/joomla-field-subform.w-c.es6.js @@ -19,6 +19,41 @@ function hasModifier(event) { return (event.ctrlKey || event.metaKey || event.shiftKey); } +/** + * Make an id from the field name + * @param {string} name + * + * @returns {string} + */ +function nameToId(name) { + return name.replace(/(\]\[)/g, '__').replace(/\[/g, '_').replace(/\]/g, '').replace(/\W/g, '_'); +} + +/** + * Replace the name prefix in an attribute value, the value may hold several ids like aria-describedby + * @param {string} value + * @param {string} oldName + * @param {string} newName + * + * @returns {string} + */ +function replacePrefix(value, oldName, newName) { + const oldId = nameToId(oldName); + const newId = nameToId(newName); + + return value.split(' ').map((part) => { + if (part.startsWith(oldName)) { + return newName + part.substring(oldName.length); + } + + if (part.startsWith(oldId)) { + return newId + part.substring(oldId.length); + } + + return part; + }).join(' '); +} + class JoomlaFieldSubform extends HTMLElement { // Attribute getters get buttonAdd() { return this.getAttribute('button-add'); } @@ -38,10 +73,15 @@ class JoomlaFieldSubform extends HTMLElement { get name() { return this.getAttribute('name'); } set name(value) { + const oldName = this.name; + // Update the template - this.template = this.template.replace(new RegExp(` name="${this.name.replace(/[[\]]/g, '\\$&')}`, 'g'), ` name="${value}`); + this.template = this.template.replace(/(\s)(name|id|for|aria-describedby)="([^"]*)"/g, (match, space, attr, attrValue) => `${space}${attr}="${replacePrefix(attrValue, oldName, value)}"`); this.setAttribute('name', value); + + // Update existing rows + this.getRows().forEach((row) => this.fixNamePrefix(row, oldName, value)); } constructor() { @@ -279,7 +319,7 @@ class JoomlaFieldSubform extends HTMLElement { const nameNew = name.replace(`[${group}][`, `[${groupnew}][`); // New name let idNew = id.replace(group, groupnew).replace(/\W/g, '_'); // Count new id let countMulti = 0; // count for multiple radio/checkboxes - const forOldAttr = $el.id; // Fix "for" in the labels + const forOldAttr = $el.id || id; // Fix "for" in the labels, nested subform has no id if ($el.type === 'checkbox' && name.match(/\[\]$/)) { // fix countMulti = ids[id] ? ids[id].length : 0; @@ -377,6 +417,44 @@ class JoomlaFieldSubform extends HTMLElement { }); } + /** + * Fix name and id for fields that are in the row, after the subform name has changed + * @param {HTMLElement} row + * @param {String} oldName + * @param {String} newName + */ + fixNamePrefix(row, oldName, newName) { + let elements = row.querySelectorAll('[name], [id], [for], [aria-describedby]'); + + // Filter out nested + elements = [].slice.call(elements).filter((el) => { + if (el.nodeName === 'JOOMLA-FIELD-SUBFORM') { + // Skip self in .closest() call + return el.parentElement.closest('joomla-field-subform') === this; + } + + return el.closest('joomla-field-subform') === this; + }); + + elements.forEach((elem) => { + const $el = elem; + + // Nested subform will fix own fields + if ($el.nodeName === 'JOOMLA-FIELD-SUBFORM') { + $el.name = newName + $el.name.substring(oldName.length); + return; + } + + ['name', 'id', 'for', 'aria-describedby'].forEach((attr) => { + const value = $el.getAttribute(attr); + + if (value) { + $el.setAttribute(attr, replacePrefix(value, oldName, newName)); + } + }); + }); + } + /** * Use of HTML Drag and Drop API * https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API