forked from webadvanced/takeCommand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
take-command.min.js
1 lines (1 loc) · 7.12 KB
/
take-command.min.js
1
(function(c,d,e){"use strict";var b,a={version:"1.0",init:function(){b()},groups:{},testMode:false};b=function(){if(typeof d.create!=="function")d.create=function(b){function a(){}a.prototype=b;return new a};if(typeof e.prototype.indexOf==="undefined")e.prototype.indexOf=function(b){for(var a=0;a<this.length;a++)return this[a]===b?a:-1}};if(!c.takeCommand)c.takeCommand=a;a.init()})(window,Object,Array);window.takeCommand.utils=function(f,e,c){"use strict";var a={chkArg:{}},d,b=function(a){throw a;};a.chkArg.isNotFalsy=function(c,a){!c&&b(a?"Argument cannot be falsy : "+a:"Argument cannot be falsy")};a.chkArg.isNotUndefined=function(c,a){c===d&&b(a?"Argument cannot be undefined : "+a:"Argument cannot be undefined")};a.chkArg.isNotEmpty=function(c,a){c===""&&b(a?"Argument cannot be empty : "+a:"Argument cannot be empty")};a.chkArg.isNotUndefinedOrEmpty=function(b,a){this.isNotUndefined(b,a);this.isNotEmpty(b,a)};a.chkArg.isFunction=function(c,a){typeof c!=="function"&&b(a?"Argument must be a function : "+a:"Argument must be a function")};a.makeArray=function(a){return f.prototype.slice.call(a,0)};a.pushToQueue=function(b,e,d,a){a=a||50;b=b||this;d=d||[];c.setTimeout(function(){e.apply(b,d)},a)};a.eachAsync=function(d,e,b){this.chkArg.isNotUndefined(d);this.chkArg.isNotUndefined(e);this.chkArg.isFunction(e);b=b||0;var f=e(d[b],b);if(f===false)return;b++;b<d.length&&c.setTimeout(function(){a.eachAsync(d,e,b)},10)};a.each=function(b,c){this.chkArg.isNotUndefined(b);this.chkArg.isNotUndefined(c);this.chkArg.isFunction(c);for(var a=0,d=b.length;a<d;a++)if(c(b[a],a)===false)break};a.isFunction=function(a){return e.prototype.toString.call(a)==="[object Function]"};return a}(Array,Object,window);window.takeCommand.Events=function(a){"use strict";return{subscribers:{},subscribe:function(e,c,b){var d=e.split(" ");a.each(d,this.proxy(function(a){if(b)a=this.keyEvent(a,b);(this.subscribers[a]||(this.subscribers[a]=[])).push(c)}))},publish:function(){var b=a.makeArray(arguments),f=b.shift(),c=b.shift(),d=this.subscribers,e=d[f];if(!d)return false;if(!e)return false;c=c||this;a.eachAsync(e,function(a){a.apply(c,b)});return true},forget:function(b,d,f){if(!b){this.subscribers={};return this}if(f)b=this.keyEvent(b,f);var e=this.subscribers,c=e[b];if(!e)return false;if(!c)return false;if(!d){delete this.subscribers[b];return this}a.each(c,this.proxy(function(a,b){if(d===a){c.splice(b,1);return false}}));return this},keyEvent:function(a,b){return a+":"+b}}}(window.takeCommand.utils);window.takeCommand.Module=function(d){"use strict";var a,b=["included","extended"],c=d.utils,e=c.chkArg;a={inherited:function(){},created:function(){},prototype:{init:function(){},initialized:function(){}},extend:function(c){var d=c.extended;for(var a in c)if(b.indexOf(a)===-1)this[a]=c[a];d&&d.apply(this);return this},include:function(c){var d=c.included;for(var a in c)if(b.indexOf(a)===-1)this.prototype[a]=c[a];d&&d.apply(this);return this},proxy:function(a){e.isNotUndefined(a);var b=this;return function(){a.apply(b,arguments)}},proxyAll:function(){for(var b=c.makeArray(arguments),a=0,d=b.length;a<d;a++)this[b[a]]=this.proxy(this[b[a]])},create:function(b,c){var a=Object.create(this);a.parent=this;a.prototype=a.fn=Object.create(this.prototype);b&&a.include(b);c&&a.extend(c);a.created();this.inherited(a);return a},init:function(){var a=Object.create(this.prototype);a.parent=this;a.init.apply(a,arguments);a.initialized.apply(a,arguments);return a}};a.prototype.proxy=a.proxy;a.prototype.proxyAll=a.proxyAll;a.base=a.create;return a}(window.takeCommand);window.takeCommand.Command=function(c,b,g){"use strict";var d=c.Module.base(null,c.Events),f={type:"GET",dataType:"JSON",contentType:"application/json; charset=utf-8",mock:{wasSuccess:true}},a=c.utils,e=a.chkArg;d.extend({clearProcessing:function(a){if(!a)throw"jQuery Object of form is required";a.find("input:submit").removeClass("processing-btn").attr("disabled",false)}});d.include({init:function(c,a,d){e.isNotFalsy(c,"command key");e.isNotFalsy(a,"command options");this.key=c;this.group=d;this.options={};b.extend(this.options,f,a)},initialized:function(){this.subscribe("send",this.proxy(function(e){if(e&&!e.currentTarget)this.options.data=e;this.publish("before");if(this.group.testMode||c.testMode){var d=this.options.mock;if(d.wasSuccess)this.publish("success",d.responseData);else this.publish("error",d.responseData);this.publish("always",d.responseData)}else{this.options.success=this.proxy(function(){var b=a.makeArray(arguments);b.unshift("success");this.publish.apply(this,b);b.length=0});this.options.error=this.proxy(function(){var b=a.makeArray(arguments);b.unshift("error");this.publish.apply(this,b);b.length=0});this.options.complete=this.proxy(function(){var b=a.makeArray(arguments);b.unshift("always");this.publish.apply(this,b);b.length=0});b.ajax(this.options)}}))},send:function(a){this.publish("send",a)},execute:function(a){this.publish("send",a)},off:function(e,d,c){a.chkArg.isNotUndefinedOrEmpty(e,"an event arg is required.");a.chkArg.isNotUndefinedOrEmpty(d,"a selector arg is required.");c=c||"body";b(c).undelegate(d,e)},on:function(l,k,f,i){a.chkArg.isNotUndefinedOrEmpty(l,"an event arg is required.");a.chkArg.isNotUndefinedOrEmpty(k,"a selector arg is required.");var c=this,h=c.options.data,e,j=true;if(a.isFunction(f)){i=f;f="body"}f=f||"body";b(f).delegate(k,l,function(k){var f=b(this);k.preventDefault();if(f.is("form"))e=f;if(e&&e.valid)j=e.valid();if(!j)return;if(e){h=e.serialize();e.find("input:submit").attr("disabled",true).addClass("processing-btn");c.always(function(){d.clearProcessing(e)})}h=a.isFunction(i)?i.apply(this,arguments):h;c.options.ctx=this;if(c.options.urlSelector)c.options.url=c.options.urlSelector==="window"?g.location.href:b(this).attr(c.options.urlSelector);a.pushToQueue(c,c.send,[h],150)});return this},before:function(a){this.wrap(a,"before");return this},success:function(a){this.wrap(a,"success");return this},always:function(a){this.wrap(a,"always");return this},error:function(a){this.wrap(a,"error");return this},wrap:function(c,b){a.chkArg.isFunction(c);a.chkArg.isNotUndefinedOrEmpty(b);this.subscribe(b,c)},subscribe:function(b,a){this.parent.subscribe(b,a,this.eventKey())},publish:function(){var e=a.makeArray(arguments),b=0,g=arguments.length,d=[],c=0,f;if(g===1)g++;for(;b<g;b++,c++){f=e[b];if(b===0)f=this.parent.keyEvent(e[0],this.eventKey());if(b===1){d[c]=this.options.ctx||this;c++}d[c]=f}this.parent.publish.apply(this.parent,d);e.length=0},clear:function(a){this.parent.forget(this.parent.keyEvent(a,this.eventKey()));return this},eventKey:function(){return this.key+":"+this.group.key}});return d}(window.takeCommand,window.jQuery,window);window.takeCommand.CommandGroup=function(a){"use strict";var b=a.Module.base(a.Events),c;b.include({testMode:false,init:function(b){this.key=b;a.groups[b]=this},initialized:function(){this.publish("initialized")},register:function(e,b,f){this.publish("beforeRegister");if(typeof b==="string"){var g=b;b={url:g}}if(f)b.type=f;c(b);var d=a.Command.init(e,b,this);this[e]=d;this.publish(e+":afterRegister",d);this.publish("afterRegister");return d}});c=function(a){if(a.url.charAt(0)===":"){a.urlSelector=a.url.substring(1);a.url=""}};return b}(window.takeCommand,window.jQuery)