- md5
- cf8bbbbd4f07da2803d2f28edefa29d5
- sha1
- 69ba40b503513db814df108c601aa384fa48beed
- sha256
- ced68c7036734d33823912b8e6150b7554328d481c0f45901c5dc9a69d4798f9
"use strict";(a=>{'use strict';function b(a){return a.ctrlKey||a.metaKey||a.shiftKey}const c={SPACE:32,ESC:27,ENTER:13};class d extends HTMLElement{get buttonAdd(){return this.getAttribute("button-add")}get buttonRemove(){return this.getAttribute("button-remove")}get buttonMove(){return this.getAttribute("button-move")}get rowsContainer(){return this.getAttribute("rows-container")}get repeatableElement(){return this.getAttribute("repeatable-element")}get minimum(){return this.getAttribute("minimum")}get maximum(){return this.getAttribute("maximum")}get name(){return this.getAttribute("name")}set name(a){return this.template=this.template.replace(new RegExp(` name="${this.name.replace(/[\[\]]/g,"\\$&")}`,"g"),` name="${a}`),this.setAttribute("name",a)}constructor(){super();const a=this;if(this.containerWithRows=this,this.rowsContainer){const a=this.querySelectorAll(this.rowsContainer);for(let b=0,c=a.length;b<c;b++)if(a[b].closest("joomla-field-subform")===this){this.containerWithRows=a[b];break}}this.lastRowIndex=this.getRows().length-1,this.template="",this.prepareTemplate(),(this.buttonAdd||this.buttonRemove)&&(this.addEventListener("click",b=>{let c=null,d=null;if(a.buttonAdd&&(c=b.target.matches(a.buttonAdd)?b.target:b.target.closest(a.buttonAdd)),a.buttonRemove&&(d=b.target.matches(a.buttonRemove)?b.target:b.target.closest(a.buttonRemove)),c&&c.closest("joomla-field-subform")===a){let d=c.closest("joomla-field-subform");d=d.closest(a.repeatableElement)===a?d:null,a.addRow(d),b.preventDefault()}else if(d&&d.closest("joomla-field-subform")===a){const c=d.closest(a.repeatableElement);a.removeRow(c),b.preventDefault()}}),this.addEventListener("keydown",b=>{if(b.keyCode!==c.SPACE)return;const d=a.buttonAdd&&b.target.matches(a.buttonAdd),e=a.buttonRemove&&b.target.matches(a.buttonRemove);if((d||e)&&b.target.closest("joomla-field-subform")===a){let c=b.target.closest("joomla-field-subform");c=c.closest(a.repeatableElement)===a?c:null,e&&c?a.removeRow(c):d&&a.addRow(c),b.preventDefault()}})),this.buttonMove&&this.setUpDragSort()}getRows(){const a=this.containerWithRows.children,b=[];for(let c=0,d=a.length;c<d;c++)a[c].matches(this.repeatableElement)&&b.push(a[c]);return b}prepareTemplate(){const a=[].slice.call(this.children).filter(a=>a.classList.contains("subform-repeatable-template-section"));if(a[0]&&(this.template=a[0].innerHTML),!this.template)throw new Error("The row template are required to subform element to work")}addRow(a){const b=this.getRows().length;if(b>=this.maximum)return null;let c;c="TBODY"===this.containerWithRows.nodeName||"TABLE"===this.containerWithRows.nodeName?document.createElement("tbody"):document.createElement("div"),c.innerHTML=this.template;const d=c.children[0];return a?a.parentNode.insertBefore(d,a.nextSibling):this.containerWithRows.append(d),this.buttonMove&&(d.setAttribute("draggable","false"),d.setAttribute("aria-grabbed","false"),d.setAttribute("tabindex","0")),d.setAttribute("data-new","1"),this.fixUniqueAttributes(d,b),this.dispatchEvent(new CustomEvent("subform-row-add",{detail:{row:d},bubbles:!0})),window.Joomla&&Joomla.Event.dispatch(d,"joomla:updated"),d}removeRow(a){const b=this.getRows().length;b<=this.minimum||(this.dispatchEvent(new CustomEvent("subform-row-remove",{detail:{row:a},bubbles:!0})),window.Joomla&&Joomla.Event.dispatch(a,"joomla:removed"),a.parentNode.removeChild(a))}fixUniqueAttributes(a,b){b=b||0;const c=a.getAttribute("data-group"),d=a.getAttribute("data-base-name"),e=Math.max(this.lastRowIndex,b),f=d+e;this.lastRowIndex=e+1,a.setAttribute("data-group",f);let g=a.querySelectorAll("[name]");const h={};g=[].slice.call(g).filter(a=>a.closest("joomla-field-subform")===this);for(let d=0,e=g.length;d<e;d++){const b=g[d],e=b.getAttribute("name"),i=e.replace(/(\[\]$)/g,"").replace(/(\]\[)/g,"__").replace(/\[/g,"_").replace(/\]/g,""),j=e.replace(`[${c}][`,`[${f}][`);let k=i.replace(c,f).replace(/\W/g,"_"),l=0,m=i;if("checkbox"===b.type&&e.match(/\[\]$/)){if(l=h[i]?h[i].length:0,!l){const c=b.closest("fieldset.checkboxes"),d=a.querySelector(`label[for="${i}"]`);c&&c.setAttribute("id",k),d&&(d.setAttribute("for",k),d.setAttribute("id",`${k}-lbl`))}m+=l,k+=l}else if("radio"===b.type){if(l=h[i]?h[i].length:0,!l){const c=b.closest("fieldset.radio"),d=a.querySelector(`label[for="${i}"]`);c&&c.setAttribute("id",k),d&&(d.setAttribute("for",k),d.setAttribute("id",`${k}-lbl`))}m+=l,k+=l}h[i]?h[i].push(!0):h[i]=[!0],b.name=j,b.id&&(b.id=k);const n=a.querySelector(`label[for="${m}"]`);n&&(n.setAttribute("for",k),n.setAttribute("id",`${k}-lbl`))}}setUpDragSort(){function a(a){return!a.form&&a.matches(e.buttonMove)?a:a.closest(e.buttonMove)}function d(a,b){let c=!1;if(a.parentNode===b.parentNode)for(let d=a;d;d=d.previousSibling)if(d===b){c=!0;break}c?b.parentNode.insertBefore(a,b):b.parentNode.insertBefore(a,b.nextSibling)}const e=this;let f=null,g=!1;const h=this.getRows();for(let a=0,b=h.length;a<b;a++){const b=h[a];b.setAttribute("draggable","false"),b.setAttribute("aria-grabbed","false"),b.setAttribute("tabindex","0")}this.addEventListener("touchstart",b=>{g=!0;const c=a(b.target),h=c?c.closest(e.repeatableElement):null;h&&h.closest("joomla-field-subform")===e&&(f?(h!==f&&d(f,h),f.setAttribute("draggable","false"),f.setAttribute("aria-grabbed","false"),f=null):(h.setAttribute("draggable","true"),h.setAttribute("aria-grabbed","true"),f=h),b.preventDefault())}),this.addEventListener("mousedown",({target:b})=>{if(g)return;const c=a(b),d=c?c.closest(e.repeatableElement):null;d&&d.closest("joomla-field-subform")===e&&(d.setAttribute("draggable","true"),d.setAttribute("aria-grabbed","true"),f=d)}),this.addEventListener("mouseup",()=>{f&&!g&&(f.setAttribute("draggable","false"),f.setAttribute("aria-grabbed","false"),f=null)}),this.addEventListener("keydown",a=>{if((a.keyCode===c.ESC||a.keyCode===c.SPACE||a.keyCode===c.ENTER)&&!a.target.form&&a.target.matches(e.repeatableElement)){const g=a.target;if(g&&g.closest("joomla-field-subform")===e&&(a.keyCode===c.SPACE&&b(a)&&("true"===g.getAttribute("aria-grabbed")?(g.setAttribute("draggable","false"),g.setAttribute("aria-grabbed","false"),f=null):(f&&(f.setAttribute("draggable","false"),f.setAttribute("aria-grabbed","false"),f=null),g.setAttribute("draggable","true"),g.setAttribute("aria-grabbed","true"),f=g),a.preventDefault()),a.keyCode===c.ESC&&f&&(f.setAttribute("draggable","false"),f.setAttribute("aria-grabbed","false"),f=null),a.keyCode===c.ENTER&&f)){if(f.setAttribute("draggable","false"),f.setAttribute("aria-grabbed","false"),g===f)return void(f=null);d(f,g),a.preventDefault(),f=null}}}),this.addEventListener("dragstart",({dataTransfer:a})=>{f&&(a.effectAllowed="move",a.setData("text",""))}),this.addEventListener("dragover",a=>{f&&a.preventDefault()}),this.addEventListener("dragenter",({target:a})=>{if(f&&(!e.rowsContainer||a.closest(e.rowsContainer)===e.containerWithRows)){const b=a.matches(e.repeatableElement)?a:a.closest(e.repeatableElement);b&&d(f,b)}}),this.addEventListener("dragend",()=>{f&&(f.setAttribute("draggable","false"),f.setAttribute("aria-grabbed","false"),f=null)})}}a.define("joomla-field-subform",d)})(customElements);
The same content is in 4 releases, Joomla 4.0.0-beta to 4.0.0-beta4. See them all, or every version of this file.