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