forked from mdelrosso/sheepit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.sheepItPlugin.min.js
1 lines (1 loc) · 13.6 KB
/
jquery.sheepItPlugin.min.js
1
(function(a){jQuery.fn.sheepIt=function(N){function P(){var ax;if(typeof N.beforeClone==="function"){N.beforeClone(ao,C)}ax=C.cloneWithAttribut(true);if(typeof N.afterClone==="function"){N.afterClone(ao,ax)}ax.getSource=function(){return ao};return ax}function p(ax){ax.preventDefault();ac()}function d(ax){ax.preventDefault();if(K.value!==""){y(K.attr("value"))}}function r(ax){ax.preventDefault();if(typeof N.beforeRemoveCurrent==="function"){N.beforeRemoveCurrent(ao)}if(N.removeCurrentConfirmation){if(confirm(N.removeCurrentConfirmationMsg)){aj(a(this).data("removableClone"))}}else{aj(a(this).data("removableClone"))}if(typeof N.afterRemoveCurrent==="function"){N.afterRemoveCurrent(ao)}}function f(ax){ax.preventDefault();if(N.removeLastConfirmation){if(confirm(N.removeLastConfirmationMsg)){ae()}}else{ae()}}function af(ax){ax.preventDefault();if(N.removeAllConfirmation){if(confirm(N.removeAllConfirmationMsg)){aq()}}else{aq()}}function am(ay,aA){var az=ay.attr(aA+"template");if(az){return unescape(az)}var ax=ay.attr(aA);ay.attr(aA+"template",escape(ax));return ax}function L(ay,ax){ay.find(v).each(function(){var aC=a(this),aB=am(aC,"id"),aA=am(aC,"name"),aD=aC.attr("id"),az=aC.attr("name");newNameAttr=aA.replace(N.indexFormat,ax);aC.attr("name",newNameAttr);newIdAttr=aB.replace(N.indexFormat,ax);ay.find("label[for='"+aD+"']").each(function(){a(this).attr("for",newIdAttr)});aC.attr("id",newIdAttr)})}function aw(ay,ax){H(ay,ax+1)}function H(ay,ax){ay.find(N.labelSelector).html(ax);return true}function w(ax){return ax.find(N.labelSelector).html()}function T(){if(at()){if(B()==1){ab.hideIf();ad.showIf()}else{ab.showIf();ad.showIf()}var ax="";if(N.allowRemoveCurrent){ax=ao.find(N.removeCurrentSelector);if(b()){ax.show()}else{ax.hide()}}else{ax=ao.find(N.removeCurrentSelector);ax.hide()}}else{ad.hideIf();ab.hideIf()}if(!R()){o.hideIf();ag.hideIf()}else{o.showIf();ag.showIf()}if(!b()){ad.hideIf();ab.hideIf()}if(o.css("display")!="none"||ag.css("display")!="none"||ab.css("display")!="none"||ad.css("display")!="none"){i.show()}else{i.hide()}}function J(){if(at()){h.hide();if(N.continuousIndex){var ax=0,ay=Q();do{g(ay,ax);ax++;ay=av(ay)}while(ay!=false)}}else{h.show()}}function g(aB,ay){if(typeof ay=="undefined"){ay=l()}var ax=am(aB,"id");if(aB.attr("id")){aB.attr("id",ax+ay)}L(aB,ay);aw(aB,ay);if(aB.html().indexOf(N.indexFormat)!=-1){var az=new RegExp(N.indexFormat,"ig");aB.html(aB.html().replace(az,ay))}var aA=aB.find(N.removeCurrentSelector);(N.allowRemoveCurrent)?aA.show():aA.hide();return aB}function A(){J();T()}function ac(az,ay){if(typeof az=="undefined"){az=true}if(typeof ay=="undefined"){ay=false}if(typeof N.beforeAdd==="function"){N.beforeAdd(ao)}var aC=false;if(ay){if(typeof(ay)=="string"){aC=a("#"+ay)}else{if(typeof(ay)=="object"){aC=ay}else{return false}}aC.remove()}else{aC=P()}if(R()&&aC){aC=g(aC);var aA=aC.find(N.removeCurrentSelector).first();aA.click(r);aA.data("removableClone",aC);aC.data("formIndex",l());aC.data("previousSeparator",false);aC.data("nextSeparator",false);aC.data("previousForm",false);aC.data("nextForm",false);if(at()){var aF=ap();aF.data("nextForm",aC);aC.data("previousForm",aF);if(N.separator){var aB=an();aB.insertAfter(aF);aF.data("nextSeparator",aB);aC.data("previousSeparator",aB)}}(N.insertNewForms=="after")?aC.insertBefore(h):aC.insertAfter(h);if(N.nestedForms.length>0){var aD=0;var aG=[];for(aD in N.nestedForms){if(typeof(N.nestedForms[aD].id)!="undefined"&&typeof(N.nestedForms[aD].options)!="undefined"){N.nestedForms[aD].isNestedForm=true;N.nestedForms[aD].parentForm=ao;var ax=N.nestedForms[aD].id.replace(N.indexFormat,aC.data("formIndex"));var aE=a("#"+ax).sheepIt(N.nestedForms[aD].options);aG.push(aE)}}aC.data("nestedForms",aG)}u(aC);ai.push(aC);if(az||N.continuousIndex){A()}if(typeof N.afterAdd==="function"){N.afterAdd(ao,aC)}return true}else{return false}}function y(az,ay){if(typeof az!="undefined"){az=parseFloat(az);var ax=1;for(ax=1;ax<=az;ax++){ac(ay)}}}function ae(ax){if(typeof ax=="undefined"){ax=true}if(b()){x();if(ax){A()}return true}else{return false}}function aq(ay){if(typeof ay=="undefined"){ay=true}if(m()){var ax=[];for(ax in ai){if(ai[ax]){x(ai[ax])}}if(ay){A()}return true}else{return false}}function aj(ax,ay){if(typeof ay=="undefined"){ay=true}if(b()){x(ax);if(ay){A()}return true}else{return false}}function x(ax){if(typeof ax=="undefined"){ax=ap()}index=ax.data("formIndex");if(ax.data("previousSeparator")&&ax.data("nextSeparator")){ax.data("previousSeparator").remove();ax.data("previousForm").data("nextSeparator",ax.data("nextSeparator"))}else{if(ax.data("previousSeparator")&&!ax.data("nextSeparator")){ax.data("previousSeparator").remove();ax.data("previousForm").data("nextSeparator",false)}else{if(!ax.data("previousSeparator")&&ax.data("nextSeparator")){ax.data("nextSeparator").remove();ax.data("nextForm").data("previousSeparator",false)}}}if(ax.data("previousForm")){ax.data("previousForm").data("nextForm",ax.data("nextForm"))}if(ax.data("nextForm")){ax.data("nextForm").data("previousForm",ax.data("previousForm"))}ai[index]=false;ax.remove();return true}function E(){return z}function S(){if(z!==false){if(ai.length>1){var ax=0;var ay=parseFloat(z+1);for(ax=ay;ax<ai.length;ax++){if(ai[ax]){z=ax;return true}}return false}else{return false}}else{return false}}function j(){if(z!==false){if(ai.length>1){var ax=0;var ay=parseFloat(z-1);for(ax=ay;ax>=0;ax--){if(ai[ax]){z=ax;return true}}return false}else{return false}}else{return false}}function Y(){z=false;if(ai.length>0){var ax=0;for(ax in ai){if(ai[ax]){z=ax;return true}}return false}else{return false}}function I(){z=false;if(ai.length>0){if(ai[ai.length-1]){z=ai.length-1;return true}else{var ax=0;for(ax=(ai.length-1);ax>=0;ax--){if(ai[ax]){z=ax;return true}}return false}}else{return false}}function k(){if(ai.length>0){var ay=0;var ax=[];for(ax in ai){if(ai[ax]){ay++}}return ay}else{return 0}}function D(ax){if(typeof ax!="undefined"){z=e(ax);if(z!==false){return true}else{return false}}else{return false}}function e(ay){var ax=0;var aA=0;var az=false;for(ax in ai){if(ai[ax]){aA++;if(ay==aA){az=ax}}}return az}function q(az){var ay=0;var ax=0;for(ay=0;ay<=az;ay++){if(ai[ay]){ax++}}return ax}function l(){return ai.length}function B(){return k()}function Q(){if(Y()!==false){return al()}else{return false}}function ap(){if(I()!==false){return al()}else{return false}}function av(ax){if(ax){return ax.data("nextForm")}else{if(E()!==false){if(S()!==false){return al()}else{return false}}else{return false}}}function M(ax){if(ax){return ax.data("previousForm")}else{if(E()!==false){if(j()!==false){return al()}else{return false}}else{return false}}}function al(){if(E()!==false){return ai[E()]}else{return false}}function ak(ax){if(at()){if(typeof(ax)!="undefined"){D(ax);return al()}else{return ap()}}else{return false}}function c(){if(at()){Y();var ax=0;var ay=[];for(ax=0;ax<B();ax++){ay.push(al());S()}return ay}else{return false}}function at(){return(B()>0)?true:false}function R(){if(N.maxFormsCount==0){return true}else{return(B()<N.maxFormsCount)?true:false}}function b(){return(B()>N.minFormsCount)?true:false}function m(){return(N.minFormsCount==0)?true:false}function W(ax){if(a("#"+ax.attr("id")).length>0){return true}else{return false}}function ah(ay,ax){var az="";if(typeof(ay)=="number"){ay++;if((ay)>B()){ac()}az=ak(ay);V(az,ax)}else{if(typeof(ay)=="string"){az=a("#"+ay);V(az,ax)}}if(typeof N.afterFill==="function"){N.afterFill(ao,az,ax)}}function V(ay,az){var ax=0;a.each(az,function(aD,aF){var aH=ao.attr("id");var aB=ay.data("formIndex");if(aD.indexOf("#form#")!=-1||aD.indexOf("#index#")!=-1){aD=aD.replace("#form#",aH);aD=aD.replace("#index#",aB)}else{aD=aH+"_"+aB+"_"+aD}var aG=ay.find(':input[id="'+aD+'"]');if(aG.length==0){aG=ay.find(':input[name="'+aD+'"]');if(aG.length==0){aG=ay.find(':input[name="'+aD+'[]"]')}}if(aG.length>0){var aA=false;if(typeof(aF)=="object"){aA=true}var aE=false;if(aG.length>1){aE=true}if(aE){if(aA){var aC=[];aC.fields=[];aC.values=[];ax=0;for(ax in aF){aC.fields.push(aG.filter('[value="'+aF[ax]+'"]'));aC.values.push(aF[ax])}ax=0;for(ax in aC.fields){aa(aC.fields[ax],aC.values[ax])}}else{aa(aG.filter('[value="'+aF+'"]',aF))}}else{if(aA){ax=0;for(ax in aF){aa(aG,aF[ax])}}else{aa(aG,aF)}}}else{if(typeof(ay.data("nestedForms"))!="undefined"){if(ay.data("nestedForms").length>0){ax=0;for(ax in ay.data("nestedForms")){if(aD==ay.data("nestedForms")[ax].attr("id")&&typeof(aF)=="object"){ay.data("nestedForms")[ax].inject(aF)}}}}}})}function aa(az,ay){var ax=az.attr("type");if(ax=="text"||ax=="hidden"||ax=="password"){az.attr("value",ay);return true}else{if(ax=="textarea"){az.text(ay);return true}else{if(ax=="checkbox"||ax=="radio"){az.attr("checked","checked");return true}else{if(ax=="select-one"||ax=="select-multiple"){az.find("option").each(function(){if(a(this).text()==ay||a(this).attr("value")==ay){a(this).attr("selected","selected")}});return true}else{return false}}}}}function ar(){if(N.separator!=""){return true}else{return false}}function an(){if(ar()){return a(N.separator)}else{return false}}function U(ax){N=[];N=a.extend(G,ax);s(N)}function t(){return N}function Z(){ao.hide();o=a(N.addSelector);ag=a(N.addNSelector);K=a(N.addNInputSelector);au=a(N.addNButtonSelector);ad=a(N.removeLastSelector);O=a(N.removeCurrentSelector);ab=a(N.removeAllSelector);i=a(N.controlsSelector);if(o.length==0){N.allowAdd=false}if(ag.length==0){N.allowAddN=false}if(ad.length==0){N.allowRemoveLast=false}if(ab.length==0){N.allowRemoveAll=false}F(o,N.allowAdd,p);F(ag,N.allowAddN,d,au);F(ad,N.allowRemoveLast,f);F(ab,N.allowRemoveAll,af);o.init();ag.init();ad.init();ab.init();n=a(N.formTemplateSelector);h=a(N.noFormsTemplateSelector);C=n.cloneWithAttribut(true);n.remove();var ay=0;if(N.pregeneratedForms.length>0){ay=0;for(ay in N.pregeneratedForms){ac(false,N.pregeneratedForms[ay])}}if(N.iniFormsCount>B()){ay=0;var ax=N.iniFormsCount-B();for(ay=1;ay<=ax;ay++){ac(false)}}if(N.data){ao.inject(N.data)}A();ao.show()}function F(az,ax,ay,aA){if(typeof(aA)=="undefined"){aA=false}a.extend(az,{hideIf:function(aB,aC){if(ax){az.hide(aB,aC)}},showIf:function(aB,aC){if(ax){az.show(aB,aC)}},init:function(){if(ax){if(aA){aA.click(ay)}else{az.click(ay)}az.show()}else{az.hide()}}})}function X(ax){a.extend(ax,{getAddControl:function(){return o},getAddNControl:function(){return ag},getRemoveLastControl:function(){return ad},getRemoveAllControl:function(){return ab},getOptions:function(){return t()},getOption:function(ay){return N[ay]},setOption:function(ay,az){if(typeof(ay)!="undefined"&&typeof(az)!="undefined"){N[ay]=az;return N[ay]}else{return false}},getForms:function(){return c()},getAllForms:function(){return c()},getForm:function(ay){if(typeof(ay)!="undefined"){ay++}return ak(ay)},getLastForm:function(){return ak()},getFirstForm:function(){Y();return al()},addForm:function(){return ac()},addNForms:function(ay){return y(ay)},getFormsCount:function(){return B()},hasForms:function(){return at()},canAddForm:function(){return R()},canRemoveAllForms:function(){return m()},canRemoveForm:function(){return b()},removeAllForms:function(){return aq()},removeLastForm:function(){return ae()},removeFirstForm:function(){Y();return x(al())},removeForm:function(ay){if(typeof(ay)!="undefined"){ay++}return x(ak(ay))},inject:function(ay){a.each(ay,a.proxy(ah,ax))}})}function u(ax){a.extend(ax,{setLabel:function(ay){return H(ax,ay)},getLabel:function(){return w(ax)},inject:function(ay){V(ax,ay)},getNestedForms:function(){return ax.data("nestedForms")},getNestedForm:function(ay){return ax.data("nestedForms")[ay]},getPosition:function(){return q(ax.data("formIndex"))},getPreviousForm:function(){return M(ax)},getNextForm:function(){return av(ax)},removeForm:function(){return x(ax)}})}function s(ax){if(ax.maxFormsCount>0){if(ax.maxFormsCount<ax.minFormsCount){ax.maxFormsCount=ax.minFormsCount}if(ax.iniFormsCount<ax.minFormsCount||ax.iniFormsCount>ax.maxFormsCount){ax.iniFormsCount=ax.minFormsCount}}else{if(ax.iniFormsCount<ax.minFormsCount){ax.iniFormsCount=ax.minFormsCount}}if(!m()){ax.allowRemoveAll=false}}var ao=a(this).first();X(ao);var o,ag,K,au,ad,O,ab,i,C,n,h,v="input, checkbox, select, textarea",ai=[],z=false,G={addSelector:"#"+a(this).attr("id")+"_add",addNSelector:"#"+a(this).attr("id")+"_add_n",addNInputSelector:"#"+a(this).attr("id")+"_add_n_input",addNButtonSelector:"#"+a(this).attr("id")+"_add_n_button",removeLastSelector:"#"+a(this).attr("id")+"_remove_last",removeCurrentSelector:"#"+a(this).attr("id")+"_remove_current",removeAllSelector:"#"+a(this).attr("id")+"_remove_all",controlsSelector:"#"+a(this).attr("id")+"_controls",labelSelector:"#"+a(this).attr("id")+"_label",allowRemoveLast:true,allowRemoveCurrent:true,allowRemoveAll:false,allowAdd:true,allowAddN:false,removeLastConfirmation:false,removeCurrentConfirmation:false,removeAllConfirmation:true,removeLastConfirmationMsg:"Are you sure?",removeCurrentConfirmationMsg:"Are you sure?",removeAllConfirmationMsg:"Are you sure?",formTemplateSelector:"#"+a(this).attr("id")+"_template",noFormsTemplateSelector:"#"+a(this).attr("id")+"_noforms_template",separator:'<div style="width:100%; border-top:1px solid #ff0000; margin: 10px 0px;"></div>',iniFormsCount:1,maxFormsCount:20,minFormsCount:1,incrementCount:1,noFormsMsg:"No forms to display",indexFormat:"#index#",data:[],pregeneratedForms:[],nestedForms:[],isNestedForm:false,parentForm:{},beforeClone:function(){},afterClone:function(){},beforeAdd:function(){},afterAdd:function(){},afterFill:function(){},afterRemoveCurrent:function(){},beforeRemoveCurrent:function(){},insertNewForms:"after",continuousIndex:true};U(N);Z();return ao};jQuery.fn.cloneWithAttribut=function(b){if(jQuery.support.noCloneEvent){return a(this).clone(b)}else{a(this).find("*").each(function(){a(this).data("name",a(this).attr("name"))});var c=a(this).clone(b);c.find("*").each(function(){a(this).attr("name",a(this).data("name"))});return c}}})(jQuery);