-
Notifications
You must be signed in to change notification settings - Fork 0
/
kloner.min.js
6 lines (6 loc) · 6.45 KB
/
kloner.min.js
1
2
3
4
5
6
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).kloner=t()}(this,function(){"use strict";
/*!
kloner v0.0.2 (https://kloner.js.org)
by Kodie Grantham (https://kodieg.com)
*/function s(e,t,u){t&&t.constructor===Object&&(u=t,t=null),e&&e.constructor===Object&&(u=e,e=null),u=Object.assign({},s.defaultOptions,u||{}),e&&(u.containerSelector=e),t&&(u.childSelector=t);var r=s.getInstances(u.containerSelector);if(r&&r.length)for(var n=0;n<r.length;n++)!function(r){var e={add:function(e,t){return s.add(r,e,t)},remove:function(e,t){return s.remove(r,e,t)}},t=Object.assign({},u,s.getAttributeOptions(r),e),e={};if(t.template){var n=document.createElement(null);n.innerHTML=t.template;var l=n.firstElementChild.cloneNode(!0);e.template=l,e.templateIndex=0,n.remove()}else{l=r.querySelector(t.childSelector);if(!l)return console.warn("[kloner] No template found:",t.childSelector);n=l.cloneNode(!0);e.template=n,e.templateIndex=Array.prototype.indexOf.call(r.children,l),l.matches("[data-kloner-template]")&&l.remove()}for(var a=r.querySelectorAll(t.childSelector),o=a.length,i=0;i<o;i++)s.replaceParameters(a[i],Object.assign({},t.parameters||{},{index:i,number:i+1}));if(e.count=o,e.constCount=o,r.kloner=t,r.klonerData=e,s.initializeTriggers(r),t.start&&(n=t.start,!Array.isArray(n)&&Number.isInteger(parseInt(n))&&(n=Array(parseInt(n)-r.klonerData.count||0).fill({})),Array.isArray(n))&&n.forEach(function(e){t.add(null,{parameters:e})}),t.min&&r.klonerData.count<parseInt(t.min))for(var c=0;c<parseInt(t.min)-r.klonerData.count;c++)t.add();if(t.max&&r.klonerData.count>parseInt(t.max))for(var d=0;d<r.klonerData.count-parseInt(t.max);d++)t.remove()}(r[n]);return!!r.length&&(1===r.length?r[0]:r)}return s.add=function(e,t,r){t&&t.constructor===Object&&(r=t,t=null),e&&e.constructor===Object&&(r=e,e=null),e=e||s.defaultOptions.containerSelector;var n,l,a,o,i,c=s.getInstances(e,!0);if(c&&c.length)for(var d=0;d<c.length;d++)n=c[d],i=o=a=l=void 0,a=Object.assign({},n.kloner,s.getAttributeOptions(n),r),o=n.klonerData.count,i=a.updateChildren?o:n.klonerData.constCount,a.max&&o>=parseInt(a.max)||(o=n.querySelectorAll(a.childSelector),l=n.klonerData.template.cloneNode(!0),s.replaceParameters(l,Object.assign({},a.parameters||{},{index:i,number:i+1})),a.beforeAdd&&!1===a.beforeAdd(n,l,a))||((o=null!=(i=null!=(i=t)?i:o.length)?i:n.klonerData.templateIndex)?o>=n.children.length?n.append(l):n.insertBefore(l,n.children[o]):n.prepend(l),n.klonerData.count++,n.klonerData.constCount++,a.updateChildren&&s.updateChildren(n),s.initializeTriggers(n),a.afterAdd&&a.afterAdd(n,l,a))},s.defaultOptions={afterAdd:null,afterChildUpdate:null,afterRemove:null,beforeAdd:null,beforeChildUpdate:null,beforeRemove:null,childSelector:"[data-kloner-template], :scope > *",containerSelector:"[data-kloner], .kloner",max:null,min:0,parameters:null,start:0,template:null,updateChildren:!1},s.getAttributeOptions=function(e){e=e||s.defaultOptions.containerSelector;var n,l,e=s.getInstances(e,!1,!0);return e?(n=Object.assign({},e.dataset),l=/kloner([A-Z].*)/,Object.keys(n).filter(function(e){return l.test(e)}).reduce(function(e,t){var r=t.match(l);return e[r[1][0].toLowerCase()+r[1].slice(1)]=n[t],e},{})):{}},s.getInstances=function(e,t,r){if("string"==typeof e?e=document.querySelectorAll(e):e instanceof HTMLElement&&(e=[e]),t){for(var n=[],l=0;l<e.length;l++)(a=e[l]).kloner?n.push(a):console.warn("Element doesn't appear to be a kloner instance:",a);return n}var a;return e.length&&r?e[0]:e},s.initializeTriggers=function(e){e=e||s.defaultOptions.containerSelector;var t=s.getInstances(e,!0);if(t&&t.length)for(var r=0;r<t.length;r++)!function(l){var a=l.querySelectorAll("[data-kloner-add]");if((a=l.id?Array.prototype.slice.call(a).concat(Array.prototype.slice.call(document.querySelectorAll('[data-kloner-add="'+l.id+'"]'))):a).length)for(var e=0;e<a.length;e++)!function(n){a[n].klonerData&&a[n].klonerData.trigger||(a[n].addEventListener("click",function(e){var t,r=null;a[n].dataset.klonerAdd.length||(t=a[n].closest(l.kloner.childSelector))&&(r=Array.prototype.indexOf.call(l.children,t)),l.kloner.add(r,s.getAttributeOptions(a[n]))}),a[n].klonerData||(a[n].klonerData={}),a[n].klonerData.trigger=!0)}(e);var o=l.querySelectorAll("[data-kloner-remove]");if((o=l.id?Array.prototype.slice.call(o).concat(Array.prototype.slice.call(document.querySelectorAll('[data-kloner-remove="'+l.id+'"]'))):o).length)for(var t=0;t<o.length;t++)!function(n){o[n].klonerData&&o[n].klonerData.trigger||(o[n].addEventListener("click",function(e){var t,r=null;o[n].dataset.klonerRemove.length||(t=o[n].closest(l.kloner.childSelector))&&(r=Array.prototype.indexOf.call(l.children,t)),l.kloner.remove(r,s.getAttributeOptions(o[n]))}),o[n].klonerData||(o[n].klonerData={}),o[n].klonerData.trigger=!0)}(t);var r=l.querySelectorAll("[data-kloner-count]");if((r=l.id?Array.prototype.slice.call(r).concat(Array.prototype.slice.call(document.querySelectorAll('[data-kloner-count="'+l.id+'"]'))):r).length)for(var n=0;n<r.length;n++)r[n].textContent=l.klonerData.count}(t[r])},s.remove=function(e,t,r){t&&t.constructor===Object&&(r=t,t=null),e&&e.constructor===Object&&(r=e,e=null),e=e||s.defaultOptions.containerSelector;var n,l,a,o,i=s.getInstances(e,!0);if(i&&i.length)for(var c=0;c<i.length;c++)n=i[c],o=a=l=void 0,a=Object.assign({},n.kloner,s.getAttributeOptions(n),r),(o=n.klonerData.count)<=0||a.min&&o<=parseInt(a.min)||(o=(o=n.querySelectorAll(a.childSelector))[l=(o=o.length-1)<(l=null!=(l=t)?l:o)?o:l],a.beforeRemove&&!1===a.beforeRemove(n,o,a))||(o.remove(),n.klonerData.count--,a.updateChildren&&s.updateChildren(n),s.initializeTriggers(n),a.afterRemove&&a.afterRemove(n,o,a))},s.replaceParameters=function(r,e){r.klonerData||(r.klonerData={}),r.klonerData.initialContent||(r.klonerData.initialContent=r.innerHTML),r.klonerData.parameters=Object.assign({},r.klonerData.parameters,e||{}),r.innerHTML=r.klonerData.initialContent.replaceAll(/\{kloner-(\w+)\}/g,function(e,t){return null!=(t=r.klonerData.parameters[t])?t:e})},s.updateChildren=function(e){e=e||s.defaultOptions.containerSelector;var t=s.getInstances(e,!0);if(t&&t.length)for(var r=0;r<t.length;r++){i=o=a=l=void 0;var n=t[r],l=n.kloner,a=n.querySelectorAll(l.childSelector);if(a)for(var o=0;o<a.length;o++){var i=a[o];l.beforeChildUpdate&&!1===l.beforeChildUpdate(n,i,l)||(s.replaceParameters(i,{index:o,number:o+1}),l.afterChildUpdate&&l.afterChildUpdate(n,i,l))}}},s});
//# sourceMappingURL=kloner.min.js.map