-
Notifications
You must be signed in to change notification settings - Fork 13
/
jquery.grewform.min.js
11 lines (11 loc) · 7.1 KB
/
jquery.grewform.min.js
1
2
3
4
5
6
7
8
9
10
11
/*
* jQuery GrewForm Plugin v0.4.1
*
* Copyright 2011-2014, Artem Suschev
* Grandcapital Ltd.
* Contributions: Andre_487 (https://github.com/Andre-487/)
*
* Licensed under the MIT license (license.txt)
*/
!function(){function a(){a.effects={show:"slideDown",hide:"slideUp"}}function b(){}function c(){jQuery.each(d.all,function(a,b){!function(a){var b=setInterval(function(){try{d.form.find("*").is(":animated")||(clearInterval(b),a.unmatches()&&a.run_unmatch_actions())}catch(c){clearInterval(b)}},200)}(b)}),jQuery.each(d.all,function(a,b){!function(a){var b=setInterval(function(){try{d.form.find("*").is(":animated")||(clearInterval(b),a.matches()&&a.run_match_actions())}catch(c){clearInterval(b)}},200)}(b)})}function d(a,c,f){"undefined"==typeof d.id&&(d.id=0,d.blip_ptr="hgo_grewform_rule",d.unmtach_by_blip=function(a){jQuery.each(d.all,function(b,c){c.blip===a&&c.run_unmatch_actions()})},d.all=[],d.form=c),this.id=d.id,d.id=d.id+1,this.blip=d.blip_ptr+this.id,this.selectors=g((""+a).split("AND")),jQuery.each(this.selectors,function(a){return function(b,c){a[b]=jQuery.trim(""+c)}}(this.selectors)),this.selector=h(this.selectors),this.trigged=!1,this.match_actions=[],this.unmatch_actions=[],this.raw=f,this.raw_selector=a;for(var i in f)e(i,c,this);this.matches=function(){for(var a=0;a<this.selectors.length;a++){var b=this.selectors[a];if(this.trigged)return!1;if(d.form.find(b).filter("option:first").length>0){if(0===d.form.find(b).parent("select:visible:first").length)return!1}else if(0===d.form.find(b).filter(":visible:first,input[type=hidden]:first").length)return!1}return!0},this.unmatches=function(){if(!this.trigged)return!1;for(var a=0;a<this.selectors.length;a++){var b=this.selectors[a];if(0===d.form.find(b+":first").length)return!0}return!1},this.run_match_actions=function(){b("match: "+this.raw_selector),this.trigged=!0,d.form.find(this.selector).addClass(this.blip);for(var a=0;a<this.match_actions.length;a++){var c=this.match_actions[a];c.call(this)}},this.run_match_actions.first_run=!0,this.run_unmatch_actions=function(){b("unmatch: "+this.raw_selector),this.trigged=!1,jQuery("."+this.blip).removeClass(this.blip);for(var a=0;a<this.unmatch_actions.length;a++){var c=this.unmatch_actions[a];c.call(this)}},d.all.push(this)}function e(b,c,e){switch(b){case"show":e.match_actions.push(function(b){return function(){b.is("option")?b.show():b[a.effects.show]()}}(c.find(e.raw[b]))),e.unmatch_actions.push(function(b){return function(){f(b),b.is("option")?b.hide():b[a.effects.hide]()}}(c.find(e.raw[b])));break;case"hide":e.match_actions.push(function(b){return function(){b.is("option")?b.hide():b[a.effects.hide]()}}(c.find(e.raw[b]))),e.unmatch_actions.push(function(b){return function(){f(b),b.is("option")?b.show():b[a.effects.show]()}}(c.find(e.raw[b])));break;case"set_value":e.match_actions.push(function(a){return function(){var b=this;jQuery.each(a,function(a,c){jQuery.each(d.form.find(a),function(){var d=jQuery(this).tagName;"select"==d?(b.run_match_actions.first_run&&b.unmatch_actions.push(function(a,b){return function(){f(a),b.attr("selected","selected")}}(elems.find("option [value="+c+"]"),elems.find("option:selected"))),elems.find("option").removeAttr("selected"),elems.find("option [value="+c+"]").attr("selected","selected")):(b.run_match_actions.first_run&&b.unmatch_actions.push(function(a,b){return function(){f(a),a.val(b)}}(jQuery(a),jQuery(a).val())),jQuery(a).val(c))})}),b.run_match_actions.first_run=!1}}(e.raw[b]));break;case"add_options":for(var g in e.raw[b]){var h=e.raw[b][g];if("function"==typeof h)e.match_actions.push(function(a,b,d){return function(){var e=d();for(var g in e){var h=a.find(b);jQuery("<option></option>").html(e[g]).val(g).appendTo(h)}if(this.run_match_actions.first_run){for(var g in e)this.unmatch_actions.push(function(a,b,c){return function(){f(a.children(b).children("option[value="+c+"]")),a.find(b).children().remove("option[value="+c+"]")}}(c,b,g));this.run_match_actions.first_run=!1}}}(c,g,h));else for(var i in h)e.match_actions.push(function(a,b,c,d){return function(){var e=a.find(b);jQuery("<option></option>").html(d).val(c).appendTo(e)}}(c,g,i,h[i])),e.unmatch_actions.push(function(a,b,c){return function(){f(a.children(b).children("option[value="+c+"]")),a.find(b).children().remove("option[value="+c+"]")}}(c,g,i))}break;case"disable":e.match_actions.push(function(a){return function(){a.attr("disabled","disabled")}}(c.find(e.raw[b]))),e.unmatch_actions.push(function(a){return function(){f(a),a.removeAttr("disabled")}}(c.find(e.raw[b])));break;case"enable":e.match_actions.push(function(a){return function(){a.removeAttr("disabled")}}(c.find(e.raw[b]))),e.unmatch_actions.push(function(a){return function(){f(a),a.attr("disabled","disabled")}}(c.find(e.raw[b])));break;case"check":e.match_actions.push(function(a){return function(){a.attr("checked","checked")}}(c.find(e.raw[b]))),e.unmatch_actions.push(function(a){return function(){f(a),a.removeAttr("checked")}}(c.find(e.raw[b])));break;case"uncheck":e.match_actions.push(function(a){return function(){a.removeAttr("checked")}}(c.find(e.raw[b]))),e.unmatch_actions.push(function(a){return function(){f(a),a.attr("checked","checked")}}(c.find(e.raw[b])));break;case"set_html":e.match_actions.push(function(a){return function(){var b=this;jQuery.each(a,function(a,c){jQuery.each(d.form.find(a),function(){b.run_match_actions.first_run&&b.unmatch_actions.push(function(a,b){return function(){f(a),a.html(b)}}(jQuery(a),jQuery(a).html())),jQuery(a).html(c)})}),b.run_match_actions.first_run=!1}}(e.raw[b]));break;case"custom":e.match_actions.push(function(a,b){return function(){a.call(b)}}(e.raw[b].match,jQuery(e.selector))),e.unmatch_actions.push(function(a,b){return function(){a.call(b)}}(e.raw[b].unmatch,jQuery(e.selector)));break;default:return}}function f(a){jQuery.each(a,function(){var a=jQuery(this);if(void 0!==a.attr("class")){var b=a.attr("class").split(" ");jQuery.each(b,function(a,b){d.unmtach_by_blip(b)})}f(a.children())})}function g(a){return a.constructor!==Array?[a]:a}function h(a){for(var b=[],c=0;c<a.length;c++)b.push(jQuery.trim(""+a[c]));return b.join(",")}function i(a){var b=0;if(document.selection){a.focus();var c=document.selection.createRange();c.moveStart("character",-a.value.length),b=Sel.text.length}else(a.selectionStart||"0"==a.selectionStart)&&(b=a.selectionStart);return b}function j(a,b){if(a.setSelectionRange)a.focus(),a.setSelectionRange(b,b);else if(a.createTextRange){var c=a.createTextRange();c.collapse(!0),c.moveEnd("character",b),c.moveStart("character",b),c.select()}}jQuery.fn.grewform=function(b,e){jQuery(document.body).on("keyup change","input,textarea",function(a){var b=[16,9,33,34,36,35,45,38,40,37,39];if(a.keyCode&&jQuery.inArray(a.keyCode,b)<0){var c=i(a.target);jQuery(a.target).attr("value",a.target.value),j(a.target,c)}});var f=this;for(var g in b)var h=new d(g,f,b[g]);if(f.find("*").keyup(function(){var a=setInterval(function(){clearInterval(a),c()},300)}),jQuery(document).ready(function(){c()}),a(),e&&e.effects)for(var k in e)a[k]=e[k];return f.find("*").change(function(){c()})},jQuery.fn.grewform.reset=function(){d.id=void 0,d.all=[]},jQuery.fn.grewform.runRules=function(){c()}}();
//# sourceMappingURL=jquery.grewform.min.js.map