- md5
- 3d57b4a5aa660aeffd87ef355b3b1edc
- sha1
- 9e28ab186faed296c10e8c0b7ce9f4f532a8a260
- sha256
- b2cd6cad0a7dd256383788108a940b3e6b795a34de1fe8e7f1047d5b892457e8
"use strict";(function(a){'use strict';function b(a,b){for(let c;a;){if(c=a.parentElement,c&&c[e](b))return c;a=c}return null}function c(a){return a.ctrlKey||a.metaKey||a.shiftKey}const d={SPACE:32,ESC:27,ENTER:13};let e="matches";["matches","msMatchesSelector"].some(a=>"function"==typeof document.body[a]&&(e=a,!0));class f 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 c=0,d=a.length;c<d;c++)if(b(a[c],"joomla-field-subform")===this){this.containerWithRows=a[c];break}}this.template="",this.prepareTemplate(),(this.buttonAdd||this.buttonRemove)&&(this.addEventListener("click",c=>{let d=null,f=null;if(a.buttonAdd&&(d=c.target[e](a.buttonAdd)?c.target:b(c.target,a.buttonAdd)),a.buttonRemove&&(f=c.target[e](a.buttonRemove)?c.target:b(c.target,a.buttonRemove)),d&&b(d,"joomla-field-subform")===a){let e=b(d,a.repeatableElement);e=b(e,"joomla-field-subform")===a?e:null,a.addRow(e),c.preventDefault()}else if(f&&b(f,"joomla-field-subform")===a){const d=b(f,a.repeatableElement);a.removeRow(d),c.preventDefault()}}),this.addEventListener("keydown",c=>{if(c.keyCode!==d.SPACE)return;const f=a.buttonAdd&&c.target[e](a.buttonAdd),g=a.buttonRemove&&c.target[e](a.buttonRemove);if((f||g)&&b(c.target,"joomla-field-subform")===a){let d=b(c.target,a.repeatableElement);d=b(d,"joomla-field-subform")===a?d:null,g&&d?a.removeRow(d):f&&a.addRow(d),c.preventDefault()}})),this.buttonMove&&this.setUpDragSort()}getRows(){const a=this.containerWithRows.children,b=document.body.msMatchesSelector?"msMatchesSelector":"matches",c=[];for(let d=0,e=a.length;d<e;d++)a[d][b](this.repeatableElement)&&c.push(a[d]);return c}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,c){c=c||0;const d=a.getAttribute("data-group"),e=a.getAttribute("data-base-name"),f=e+c;a.setAttribute("data-group",f);let g=a.querySelectorAll("[name]");const h={};g=[].slice.call(g).filter(a=>b(a,"joomla-field-subform")===this);for(let e=0,i=g.length;e<i;e++){const c=g[e],i=c.getAttribute("name"),j=i.replace(/(\[\]$)/g,"").replace(/(\]\[)/g,"__").replace(/\[/g,"_").replace(/\]/g,""),k=i.replace(`[${d}][`,`[${f}][`);let l=j.replace(d,f),m=0,n=j;if("checkbox"===c.type&&i.match(/\[\]$/)){if(m=h[j]?h[j].length:0,!m){const d=b(c,"fieldset.checkboxes"),e=a.querySelector(`label[for="${j}"]`);d&&d.setAttribute("id",l),e&&(e.setAttribute("for",l),e.setAttribute("id",`${l}-lbl`))}n+=m,l+=m}else if("radio"===c.type){if(m=h[j]?h[j].length:0,!m){const d=b(c,"fieldset.radio"),e=a.querySelector(`label[for="${j}"]`);d&&d.setAttribute("id",l),e&&(e.setAttribute("for",l),e.setAttribute("id",`${l}-lbl`))}n+=m,l+=m}h[j]?h[j].push(!0):h[j]=[!0],c.name=k,c.id&&(c.id=l);const o=a.querySelector(`label[for="${n}"]`);o&&(o.setAttribute("for",l),o.setAttribute("id",`${l}-lbl`))}}setUpDragSort(){function a(a){return!a.form&&a[e](g.buttonMove)?a:b(a,g.buttonMove)}function f(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 g=this;let h=null,i=!1;const j=this.getRows();for(let a=0,b=j.length;a<b;a++){const b=j[a];b.setAttribute("draggable","false"),b.setAttribute("aria-grabbed","false"),b.setAttribute("tabindex","0")}this.addEventListener("touchstart",c=>{i=!0;const d=a(c.target),e=d?b(d,g.repeatableElement):null;e&&b(e,"joomla-field-subform")===g&&(h?(e!==h&&f(h,e),h.setAttribute("draggable","false"),h.setAttribute("aria-grabbed","false"),h=null):(e.setAttribute("draggable","true"),e.setAttribute("aria-grabbed","true"),h=e),c.preventDefault())}),this.addEventListener("mousedown",c=>{if(i)return;const d=a(c.target),e=d?b(d,g.repeatableElement):null;e&&b(e,"joomla-field-subform")===g&&(e.setAttribute("draggable","true"),e.setAttribute("aria-grabbed","true"),h=e)}),this.addEventListener("mouseup",()=>{h&&!i&&(h.setAttribute("draggable","false"),h.setAttribute("aria-grabbed","false"),h=null)}),this.addEventListener("keydown",a=>{if((a.keyCode===d.ESC||a.keyCode===d.SPACE||a.keyCode===d.ENTER)&&!a.target.form&&a.target[e](g.repeatableElement)){const e=a.target;if(e&&b(e,"joomla-field-subform")===g&&(a.keyCode===d.SPACE&&c(a)&&("true"===e.getAttribute("aria-grabbed")?(e.setAttribute("draggable","false"),e.setAttribute("aria-grabbed","false"),h=null):(h&&(h.setAttribute("draggable","false"),h.setAttribute("aria-grabbed","false"),h=null),e.setAttribute("draggable","true"),e.setAttribute("aria-grabbed","true"),h=e),a.preventDefault()),a.keyCode===d.ESC&&h&&(h.setAttribute("draggable","false"),h.setAttribute("aria-grabbed","false"),h=null),a.keyCode===d.ENTER&&h)){if(h.setAttribute("draggable","false"),h.setAttribute("aria-grabbed","false"),e===h)return void(h=null);f(h,e),a.preventDefault(),h=null}}}),this.addEventListener("dragstart",a=>{h&&(a.dataTransfer.effectAllowed="move",a.dataTransfer.setData("text",""))}),this.addEventListener("dragover",a=>{h&&a.preventDefault()}),this.addEventListener("dragenter",a=>{if(h&&(!g.rowsContainer||b(a.target,g.rowsContainer)===g.containerWithRows)){const c=a.target[e](g.repeatableElement)?a.target:b(a.target,g.repeatableElement);c&&f(h,c)}}),this.addEventListener("dragend",()=>{h&&(h.setAttribute("draggable","false"),h.setAttribute("aria-grabbed","false"),h=null)})}}a.define("joomla-field-subform",f)})(customElements);
The same content is in 3 releases, Joomla 4.0.0-alpha9 to 4.0.0-alpha11. See them all, or every version of this file.