jquery-cloner
Version:
A jQuery plugin to clone HTML content
1 lines • 5.79 kB
JavaScript
!function(e,n){"use strict";var t={init:function(n,t){var o=this;return o.elem=t,o.$elem=e(o.elem),o.options=e.extend({},e.fn.cloner.options,n),o.$container=o.$elem,o.$clonables=o.$container.closestChild(o.options.clonable),o.$closeButton=o.$container.closestChild(o.options.closeButton),o.$clonables.addClass(o.options.sourceName),o.$closeButton.first().hide(),this.debug("--------------------------------"),this.debug("[Cloner]: initialized"),o},toggle:function(e,n){this.debug("start click--------------------------------");var t=n.$clonables,o=t.length,l=t.last(),r=l.clone(!0);l[0].hasAttribute("data-clone-number")||l.attr("data-clone-number",o);var s=+l.attr("data-clone-number");return r.removeClass(n.options.sourceName).addClass(n.options.cloneName),n.$last=l,n.$clone=r,n.cloneNumber=s,!(!n.options.limitCloneNumbers||!n.$clone.hasClass(n.options.clonableCloneNumberDecrement)||1!==n.cloneNumber)||(1==n.cloneNumber?this.cloneNumberHandler(n.cloneNumber,n.$clone,"increment"):n.cloneNumber>1&&this.cloneNumberHandler(n.cloneNumber,n.$clone,"decrement"),n.debug("[Cloner]: start `beforeToggle` method"),n.options.beforeToggle(r,o,n),n.debug("[Cloner]: end `beforeToggle` method"),n.debug("[Cloner]: start `toggle` method"),n.options.clearValueOnClone&&(r.find("input, select").val(""),r.find("textarea").text("")),r.closestChild(n.options.closeButton).show(),this.increment(r,o,n),this.decrement(r,o,n),this.nestedClonesHandler(r,o,n),n.debug("[Cloner]: start clone append"),l.after(r),n.debug("[Cloner]: end clone append"),r.find(n.options.focusableElement).focus(),n.debug("[Cloner]: end `toggle` method"),n.debug("[Cloner]: start `afterToggle` method"),n.options.afterToggle(r,o,n),n.debug("[Cloner]: end `afterToggle` method"),this.debug("end click--------------------------------"),!0)},cloneNumberHandler:function(e,n,t){"increment"==t?n.attr("data-clone-number",e+1):"decrement"==t&&n.attr("data-clone-number",e-1)},increment:function(n,t,o){var l=this,r=n.find('[class*="'+o.options.incrementName+'"]').filter(function(t,l){return e(l).closest(o.options.clonable).get(0)==n.get(0)}),s=n[0].hasAttribute("clone-number")?n.data("clone-number"):t;l.debug("[Cloner]: start increment | Clone number: "+s+" | Index: "+t),r.each(function(){for(var n=e(this).attr("class").split(" "),t=n.length-1;t>=0;t--){var r=new RegExp(o.options.incrementName,"g");if(r.test(n[t])){var c=n[t].split(o.options.incrementName);switch(c=c[1].replace("-","")){case"value":var a=e(this).val(),i=a.replace(/-?\d+/g,function(e){return++e});e(this).val(i);break;case"html":var a=e(this).html(),i=a.replace(/-?\d+/g,function(e){return++e});e(this).html(i);break;case"text":var a=e(this).text(),i=a.replace(/-?\d+/g,function(e){return++e});e(this).text(i);break;case"for":case"id":case"class":default:if(!e(this)[0].hasAttribute(c))break;var a=e(this).attr(c),i=a.replace(/-?\d+/g,function(e){return++e});e(this).attr(c,i)}}}l.debug("[Cloner]: incrementing values... | Clone Number: "+s)}),l.debug("[Cloner]: end increment ")},decrement:function(n,t,o){var l=this,r=n.find('[class*="'+o.options.decrementName+'"]').filter(function(t,l){return e(l).closest(o.options.clonable).get(0)==n.get(0)}),s=n[0].hasAttribute("clone-number")?n.data("clone-number"):t;l.debug("[Cloner]: start increment | Clone number: "+s+" | Index: "+t),r.each(function(){for(var n=e(this).attr("class").split(" "),t=n.length-1;t>=0;t--){var r=new RegExp(o.options.decrementName,"g");if(r.test(n[t])){var c=n[t].split(o.options.decrementName);switch(c=c[1].replace("-","")){case"value":var a=e(this).val(),i=a.replace(/-?\d+/g,function(e){return console.log(e),--e});e(this).val(i);break;case"html":var a=e(this).html(),i=a.replace(/-?\d+/g,function(e){return console.log(e),--e});e(this).html(i);break;case"text":var a=e(this).text(),i=a.replace(/-?\d+/g,function(e){return console.log(e),--e});e(this).text(i);break;case"for":case"id":case"class":default:var a=e(this).attr(c),i=a.replace(/-?\d+/g,function(e){return console.log(e),--e});e(this).attr(i)}}}l.debug("[Cloner]: incrementing values... | Clone Number: "+s)}),l.debug("[Cloner]: end increment ")},nestedClonesHandler:function(e,n,t){if(t.options.removeNestedClonablesOnClone){var o=e.closestChild(t.options.clonable);o.not("."+t.options.sourceName).remove()}return t},destroy:function(){this.destroy(),this.element.unbind(this.eventNamespace),this.bindings.unbind(this.eventNamespace)},remove:function(e){var n=this;return n.$clonables=n.$container.find(n.options.clonable),e(n),!0},debug:function(e){var n=this;n.options.debug&&console.log(e)}};e.fn.cloner=function(o){var l=Object.create(t);return this.each(function(){var t=l.init(o,this),r=t.$elem.closestChild(t.options.addButton);e(r).on("click",function(n){t.$clonables=e(this).closest(t.options.clonableContainer).closestChild(t.options.clonable),l.toggle(t.options,t),n.preventDefault()}),l.remove(function(t){return e(n).on("click",t.options.closeButton,function(n){e(this).closest(t.options.clonable).remove()}),!0})})},e.fn.closestChild=function(n){var t,o;return t=this.children(),0===t.length?e():(o=t.filter(n),o.length>0?o:t.closestChild(n))},e.fn.cloner.options={clonableContainer:".clonable-block",clonable:".clonable",addButton:".clonable-button-add",closeButton:".clonable-button-close",focusableElement:":input:visible:enabled:first",clearValueOnClone:!0,removeNestedClonablesOnClone:!0,limitCloneNumbers:!0,debug:!1,cloneName:"clonable-clone",sourceName:"clonable-source",clonableCloneNumberDecrement:"clonable-clone-number-decrement",incrementName:"clonable-increment",decrementName:"clonable-decrement",beforeToggle:function(e,n,t){},afterToggle:function(e,n,t){}},e(n).find("[data-toggle=cloner]").each(function(){e(this).cloner(Object.assign({},e(this).data("options")||{}))})}(jQuery,document);