-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiform.min.js
1 lines (1 loc) · 4.34 KB
/
multiform.min.js
1
Array.prototype.includes||Object.defineProperty(Array.prototype,"includes",{value:function(t,e){if(null==this)throw new TypeError('"this" is null or not defined');var n=Object(this),i=n.length>>>0;if(0===i)return!1;var r,o,l=0|e,s=Math.max(l>=0?l:i-Math.abs(l),0);for(;s<i;){if((r=n[s])===(o=t)||"number"==typeof r&&"number"==typeof o&&isNaN(r)&&isNaN(o))return!0;s++}return!1}});var FORM_ELEMENTS=["DATALIST","INPUT","OPTGROUP","SELECT","TEXTAREA"];function cloneFormNodes(t,e){for(var n=new Array,i=0;i<t.length;i++){if(t[i].nodeName.includes("#"))n[i]=t[i].cloneNode(!0);else{n[i]=document.createElement(t[i].nodeName);for(var r=0,o=t[i].attributes.length;r<o;r++)n[i].setAttribute(t[i].attributes[r].name,t[i].attributes[r].value);FORM_ELEMENTS.includes(t[i].nodeName)?(n[i].name=e+t[i].name,n[i].id=e+t[i].id):"LABEL"==t[i].nodeName?n[i].setAttribute("for",e+t[i].getAttribute("for")):(t[i].id&&(n[i].id=e+t[i].id),t[i].name&&(n[i].name=e+t[i].name))}if(t[i].childNodes.length>0)for(var l=cloneFormNodes(t[i].childNodes,e),s=0,d=l.length;s<d;s++)n[i].appendChild(l[s])}return n}function Template(t,e,n,i){this.nodes=Array(),this.prefix=e,this.currentIteration=0,this.classList=[],this.removeButton=n||null,this.removeButtonContainer=i||null;for(var r=0,o=t.classList.length;r<o;r++)this.classList.push(t.classList[r]);for(var l=0,s=t.childNodes.length;l<s;l++)this.nodes[l]=t.childNodes[0].cloneNode(!0),t.removeChild(t.childNodes[0]);this.createInstance=function(){for(var t,e="",n=document.createElement("div"),i=this.removeButton||document.createElement("div"),r=null,o=0,l=this.classList.length;o<l;o++)n.classList.add(this.classList[o]);n.classList.remove("multiform","multiform-item"),this.removeButton?i=this.removeButton.cloneNode(!0):i.innerHTML="Remove",i.getAttribute("type")||i.setAttribute("type","button"),i.classList.add("btn"),0==$(i).filter("*[class^='btn-']").length&&i.classList.add("btn-danger"),i.classList.add("multiform-remove"),this.prefix&&(e=this.prefix+"_"),e+=this.currentIteration.toString()+"-",n.id=e+"item_container",i.setAttribute("data-itemcontainer",n.id),t=cloneFormNodes(this.nodes,e);for(var s in t)n.appendChild(t[s]);return this.removeButtonContainer?(r=this.removeButtonContainer.cloneNode(!0),n.appendChild(r),r.appendChild(i)):n.appendChild(i),this.currentIteration++,n}}function MultiformContainer(t,e,n){this.container=t,this.controlsContainer=n||null,this.addButton=null,e?$(t).find(e).length?this.addButton=e.cloneNode(!0):this.addButton=e:(this.addButton=document.createElement("div"),this.addButton.innerHTML="Add"),this.addButton.getAttribute("type")||this.addButton.setAttribute("type","button"),this.addButton.classList.add("btn"),0==$(this.addButton).filter("*[class^='btn-']").length&&this.addButton.classList.add("btn-success"),this.addButton.setAttribute("id","multiform-add"),this.controlsContainer||(this.controlsContainer=document.createElement("div"),this.container.appendChild(this.controlsContainer)),this.controlsContainer.setAttribute("id","multiform-controls"),$(this.controlsContainer).find(this.addButton)||this.controlsContainer.appendChild(this.addButton),this.appendChild=function(t){this.container.appendChild(t),$(".multiform-remove").unbind("click"),$(".multiform-remove").click(function(){$("#"+$(this).data("itemcontainer")).remove()})}}!function(t){t.fn.multiForm=function(e,n){var i=this.filter(".multiform-item"),r=Array(),o=n||null,l=t(this.not(".multiform-item"))[0],s=t("#multiform-controls-container")[0]||null,d=t("#multiform-add")[0]||null,a=null,u=t(l).children("#multiform-remove-container")[0]||null,m=null,c=null,h=null;c=u?t(u).children("#multiform-remove")[0]||null:t(l).children("#multiform-remove")[0]||null,d&&(t(l).find(d).length?(a=d.cloneNode(!0),l.removeChild(d)):a=d),c&&(h=c.cloneNode(!0),u?u.removeChild(c):l.removeChild(c)),u&&(m=u.cloneNode(!0),l.removeChild(u));var f=new Template(this.not(".multiform-item")[0],e,h,m),p=new MultiformContainer(this.not(".multiform-item")[0],a,s);p.container.classList=[],p.container.id="multiform-container";var v=0;for(i.length;v<i.length;v++)itemsArrayIndex=r.length,r[itemsArrayIndex]=new Template(i[v],e,h,m),r[itemsArrayIndex].currentIteration=v,p.appendChild(r[itemsArrayIndex].createInstance()),i[v].parentElement.removeChild(i[v]);return f.currentIteration=i.length,p.appendChild(f.createInstance()),t("#multiform-add").click(function(){p.appendChild(f.createInstance()),o&&o()}),this}}(jQuery);