-
Notifications
You must be signed in to change notification settings - Fork 7
/
jquery.metalClone.min.js
1 lines (1 loc) · 9.85 KB
/
jquery.metalClone.min.js
1
!function(e){e.fn.metalClone=function(n,t){var o=cloned=e.extend({},e.fn.metalClone.defaults,n),l=clonedElement=this;return l.each(function(n,t){function a(n,t,o,l){var a,r=t,s="",m="",f=[];if(n=0==n?1:n,e.inArray(g,ee)!==-1&&e.inArray(M,ee)!==-1)for(var u=0;u<n;u++){if(a=p())return;if(s=r.clone(),Y&&(s.children("td").wrapInner('<div class="'+$+'"></div>'),s.children("td").each(function(n,t){e(t).children("div").slideDown()})),"after"===l?s.insertAfter(o.find("tr").last()):s.insertAfter(o.find("tr").first()),Y){var v=s.children("td").length,h=0;s.children("td").children("div").hide().slideDown(function(){s.children("td").children("."+$).contents().unwrap(),s.find("td").last().append(d("table")),T||i(s),function(n){z&&1==n&&h==v-1&&e(document).triggerHandler("metal-event:scrollTop",{cloned_element:s,scroll_top_speed:G})}(u),h++})}else s.find("td").last().append(d("table")),function(n){z&&0==n&&e(document).triggerHandler("metal-event:scrollTop",{cloned_element:s,scroll_top_speed:G})}(u),T||i(s);f.push(s)}else if(e.inArray(g,ee)==-1&&e.inArray(M,ee)==-1)for(var u=0;u<n;u++){if(a=p())return;s=r.clone(),Y?"after"===l?(o.append(s.append(d("div"))),o.children().last().hide().slideDown(q)):(o.prepend(s.append(d("div"))),o.children().first().hide().slideDown(q)):o.prepend(s.append(d("div"))),z&&!function(n){0==n&&e(document).triggerHandler("metal-event:scrollTop",{cloned_element:s,scroll_top_speed:G})}(u),T||i(s),f.push(s)}else if(e.inArray(g,ee)==-1&&e.inArray(M,ee)!==-1)for(var u=0;u<n;u++){if(a=p())return;s=r.clone(),Y?"after"===l?(o.append(s.append(d("div"))),o.children().last().hide().slideDown(q)):(o.prepend(s.append(d("div"))),o.children().first().hide().slideDown(q)):"after"===l?o.append(s.append(d("div"))):o.prepend(s.append(d("div"))),function(n){z&&0==n&&e(document).triggerHandler("metal-event:scrollTop",{cloned_element:s,scroll_top_speed:G})}(u),T||i(s),f.push(s)}else if(e.inArray(g,ee)!==-1&&e.inArray(M,ee)==-1)return console.error("MetalClone Error: Destination provided is not table element. Table rows should exist inside table element"),!1;return e.isArray(D)&&e.isEmptyObject(D)?m=e.map(f,function(n,t){return e(n).get(0)}):e.isArray(D)&&!e.isEmptyObject(D)&&(m=c(D)),m}function r(n,t,o,l){var a,r=t,s="",m="",f=[];if(n=0==n?1:n,e.inArray(g,ee)!==-1)for(var u=0;u<n;u++){if(a=p())return;if(s=r.clone(),Y&&(s.children("td").wrapInner('<div class="'+$+'"></div>'),s.children("td").each(function(n,t){e(t).children("div").slideDown()})),"after"===l?s.insertAfter(o):s.insertBefore(o),Y){var v=s.children("td").length,h=0;s.children("td").children("div").hide().slideDown(function(){s.children("td").children("."+$).contents().unwrap(),s.find("td").last().append(d("table")),T||i(s),function(n){z&&1==n&&h==v-1&&e(document).triggerHandler("metal-event:scrollTop",{cloned_element:s,scroll_top_speed:G})}(u),h++})}else s.find("td").last().append(d("table")),function(n){z&&0==n&&e(document).triggerHandler("metal-event:scrollTop",{cloned_element:s,scroll_top_speed:G})}(u),T||i(s);f.push(s)}else if(e.inArray(g,ee)==-1)for(var u=0;u<n;u++){if(a=p())return;s=r.clone(),"after"===l?s.insertAfter(o):s.insertBefore(o),Y?!function(n){s.hide().slideDown(q,function(){z&&0==n&&e(document).triggerHandler("metal-event:scrollTop",{cloned_element:s,scroll_top_speed:G})}).append(d("div"))}(u):(s.append(d("div")),function(n){z&&0==n&&e(document).triggerHandler("metal-event:scrollTop",{cloned_element:s,scroll_top_speed:G})}(u)),T||i(s),f.push(s)}return e.isArray(D)&&e.isEmptyObject(D)?m=e.map(f,function(n,t){return e(n).get(0)}):e.isArray(D)&&!e.isEmptyObject(D)&&(m=c(D)),m}function i(n){n.find('input:not("input[type=button], input[type=submit], input[type=checkbox], input[type=radio]"), textarea, select').each(function(){e(this).not(R).val("")})}function d(e){var n="",t="";switch(j&&(n='<i class="'+s()+" "+I+'" data-fa-transform="'+U+'" data-fa-mask="'+V+'"></i> '),e){case"table":t='<div class="operation-container"><div class="operations"><div class="'+w+' metal-btn-remove operationsImg" data-metal-ref="'+y+'">'+n+"<span>"+x+"</span></div></div></div>";break;default:t='<button type="button" class="'+w+" metal-btn-remove "+(o.btnRemoveClass&&"number"!=typeof o.btnRemoveClass?o.btnRemoveClass:"")+'" style="margin-bottom: 20px" data-metal-ref="'+y+'">'+n+x+"</button>"}return t}function s(){if(e.inArray(B,J)===-1)return"far";var n;switch(B){case"solid":n="fas";break;case"brand":n="fab";break;case"light":n="fal";break;case"basic":n="fa";break;default:n="far"}return n}function c(n){var t,o=[];return t=e.inArray("*",n)?"*":n.join(","),e(y).not(":first").each(function(n,l){e(this).find(t).each(function(t,o){if(e(this).attr("id")){var l=e(this).attr("id"),a=l.replace(/\d+/g,"");e(this).attr("id",a+parseInt(n))}}),o.push(e(this).get(0))}),o}function m(){var n;return n=e(y).length,"infinity"!=H&&"number"==typeof H?!(E>H)&&n:"no limit"}function f(){var e;return e=ne?y.replace(".",""):y.replace("#","")}function p(){var n,t=m(),o=!1;if(t?n="no limit"==t?0:t:(console.error("MetalClone Error: numberToClone option defined is more than cloneLimit option"),alert("MetalClone Error: numberToClone option defined is more than cloneLimit option"),o=!0),n>H){if(console.log("Can't clone more than limit provided"),e(te).next().is("span"))e(te).next().html(L);else{var l=f();e("<span/>",{"data-clone-reference":l,"class":"metal-error-limit"+(S&&"number"!=typeof S?" "+S:""),text:L}).insertAfter(te)}o=!0}return o}var u=e(t).attr("id")||e(t).attr("class");u||(u=t);var v=Math.floor(99999999999*Math.random()+1);if(e(t).data("metalCloneRef",v),e(t).addClass("metalElement"+v),void 0==e(t).data("metalClone-"+u)){e(t).data("metalClone-"+u,"metalClone");var h,b,C,A=l.selector||"."+u,y=".metalElement"+v,w=y.replace(".","")+"BtnRemove",g=l[0].nodeName,T=o.copyValue,R=o.clearExceptions,_=o.position,E=o.numberToClone,k=o.destination,D=o.ids,x=o.btnRemoveText,M=k?e(k)[0].nodeName:"none",H=o.cloneLimit,L=o.cloneLimitText,S=o.cloneLimitClass,j=o.enableIcon,B=o.fontAwesomeTheme,I=o.fontAwesomeRemoveClass,O=o.fontAwesomeAddClass,F=o.fontAwesomeAddDataTransform,N=o.fontAwesomeAddDataMask,U=o.fontAwesomeRemoveDataTransform,V=o.fontAwesomeRemoveDataMask,Q=o.enableConfirmMessage,W=o.confirmMessageText,Y=o.enableAnimation,q=o.animationSpeed,z=o.enableScrollTop,G=o.scrollTopSpeed,J=["regular","solid","brand","light","basic"],K=o.onStart,P=o.onClone,X=o.onComplete,Z=o.onClonedRemoved,$="metalRowElementWrapper",ee=["TABLE","TR","TD","TBODY","TFOOT","THEAD","TH"],ne=!1;A.match(/[.]/)?(ne=!0,h=e(this).first()):h=e(this),e.isFunction(K)&&K.call(l,l),e.inArray(g,ee)!==-1&&(b=h.closest("table"),C=b.find("tr").first());var te,oe;if(null===o.btnClone?(te="metalBtnClone"+Math.floor(99999999999*Math.random()+1),e.inArray(g,ee)!==-1&&e.inArray(M,ee)==-1?oe=b:e.inArray(g,ee)!==-1&&e.inArray(M,ee)!==-1?oe=b:e.inArray(g,ee)==-1&&e.inArray(M,ee)==-1?oe=t:e.inArray(g,ee)==-1&&e.inArray(M,ee)!==-1&&(oe=t),e("<button/>",{type:"button",text:o.btnCloneText,"class":te+(o.btnCloneClass&&"number"!=typeof o.btnCloneClass?" "+o.btnCloneClass:""),css:{marginTop:"10px"}}).insertAfter(oe),te="."+te):te=o.btnClone,j){var le='<i class="'+s()+" "+O+'" data-fa-transform="'+F+'" data-fa-mask="'+N+'"></i> ';e(te).get(0).insertAdjacentHTML("afterbegin",le)}e(te).data("metalCloneButtonRef",v),e(document).on({mouseenter:function(){e(this).find(".operations").css({display:"block"})},mouseleave:function(){e(this).find(".operations").css({display:"none"})}},y),e(document).on("click",te,function(){var n,i=(e(this),"");return function(e){o.cancelClone=function(n){n&&(window[e+"cancelClone"]=n)},o.removeCloned=function(n){n&&(window[e+"removeCloned"]=n)}}(y),e.isFunction(P)&&P.call(l,l,cloned),window[y+"cancelClone"]&&void 0!==typeof window[y+"cancelClone"]?void delete window[y+"cancelClone"]:(k!==!1?(n=e(k),i="after"===_?a(E,h,n,_):a(E,h,n,_)):(n=e(y),i="after"===_?r(E,h,n.last(),_):r(E,h,n.first(),_)),e.isFunction(X)&&X.call(l,e(t),cloned,i),void(window[y+"removeCloned"]&&void 0!==typeof window[y+"removeCloned"]&&(delete window[y+"removeCloned"],e(i).remove(),e.isFunction(Z)&&Z.call(l,i))))}),e(document).on("click","button."+w+",div."+w,function(){var n,t=e(this).data("metalRef").replace(".",""),a=e(this).closest("tr."+t);return!("boolean"==typeof Q&&Q&&!confirm(W))&&(n=f(),a.length?o.enableAnimation?(a.children("td").wrapInner("<div></div>"),a.children("td").children("div").each(function(n,t){e(t).slideUp(q,function(){n===a.children("td").children("div").length-1&&e(document).triggerHandler("metal-event:onClonedRemoved",{base:l,callback:Z,toRemoveElement:a})})})):e(document).triggerHandler("metal-event:onClonedRemoved",{base:l,callback:Z,toRemoveElement:a}):o.enableAnimation?e(this).closest(y).slideUp(q,function(){e(document).triggerHandler("metal-event:onClonedRemoved",{base:l,callback:Z,toRemoveElement:e(this).closest(y)})}):e(document).triggerHandler("metal-event:onClonedRemoved",{base:l,callback:Z,toRemoveElement:e(this).closest(y)}),void e("body").find('[data-clone-reference="'+n+'"]').remove())})}})},e.fn.metalClone.defaults={destination:!1,position:"after",numberToClone:1,ids:[],btnClone:null,copyValue:!1,clearExceptions:"",btnRemoveText:"Remove",btnRemoveClass:null,btnCloneText:"Create New Element",btnCloneClass:null,cloneLimit:"infinity",cloneLimitText:"Clone limit reached",cloneLimitClass:null,onStart:null,onClone:null,onComplete:null,onClonedRemoved:null,enableIcon:!0,fontAwesomeTheme:"solid",fontAwesomeRemoveClass:"fa-trash-alt",fontAwesomeAddClass:"fa-plus",fontAwesomeAddDataTransform:"",fontAwesomeAddDataMask:"",fontAwesomeRemoveDataTransform:"",fontAwesomeRemoveDataMask:"",enableConfirmMessage:!0,confirmMessageText:"Are you sure?",enableAnimation:!0,animationSpeed:400,enableScrollTop:!0,scrollTopSpeed:1e3},e(document).on("metal-event:onClonedRemoved",function(n,t){t.toRemoveElement.remove(),e.isFunction(t.callback)&&t.callback.call(t.base,t.toRemoveElement)}),e(document).on("metal-event:scrollTop",function(n,t){e("html,body").animate({scrollTop:e(t.cloned_element).offset().top},t.scroll_top_speed)})}(jQuery);