-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction-v0.4.1.min.js
5 lines (5 loc) · 6.52 KB
/
action-v0.4.1.min.js
1
2
3
4
5
/****************************************
Action! v0.4.1 Akshay Kumar
https://github.com/designfrontier/Action
****************************************/
var action=function(){"use strict";var e={eventMe:function(t){var n=t;return n.emitterId=Math.ceil(1e4*Math.random()),n.eventStore={},n.emit=function(t,n,o){var r,a,i=this,c="undefined"!=typeof o&&o;if(r=c?i.eventStore[t]:e.eventStore[t],"undefined"!=typeof r)for(a=0;a<r.length;a++)"undefined"!=typeof r[a].scope?r[a].call.apply(r[a].scope,[n,i.emitterId]):r[a].call(n,i.emitterId),r[a].once&&i.silence(t,r[a].call,!0,c)},n.emitLocal=function(e,t){var n=this;n.emit(e,t,!0)},n.listen=function(t,n,o,r,a){var i,c,s,l=this,f=!0,u=t,d=n,p=o,y=a,h=r;if("object"==typeof t&&(u=t.eventName,d=t.handler,p=t.scope,h=t.once,y=t.local),c="undefined"!=typeof y&&y?l.eventStore[t]:e.eventStore[t],s="undefined"!=typeof y&&y?l:e,"undefined"!=typeof c){for(i=0;i<c.length;i++)if(c[i].call===d&&c[i].once===!1){f=!1;break}f&&"undefined"!=typeof o?c.push({once:!1,call:d,scope:p}):f&&c.push({once:!1,call:d})}else s.eventStore[u]=[],s.eventStore[u].push("undefined"!=typeof o?{once:!1,call:d,scope:p}:{once:!1,call:d})},n.listenLocal=function(e,t,n,o){var r=this;"object"==typeof e?(e.local=!0,r.listen(e)):r.listen({eventName:e,handler:t,scope:n,once:o,local:!0})},n.listenOnce=function(t,n,o,r){var a,i,c,s=this,l=!0,f=t,u=n,d=o,p=r;if("object"==typeof t&&(f=t.eventName,u=t.handler,d=t.scope,p=t.local),"undefined"!=typeof p&&p?(i=s.eventStore[f],c=s):(i=e.eventStore[f],c=e),"undefined"!=typeof i){for(a=0;a<i.length;a++)if(i[a].call===u&&i[a].once===!0){l=!1;break}l&&i.push({once:!0,call:u,scope:d})}else c.eventStore[t]=[],c.eventStore[t].push({once:!0,call:u,scope:d})},n.listenOnceLocal=function(e,t,n){var o=this;"object"==typeof e?(e.local=!0,o.listenLocal(e)):o.listenLocal({eventName:e,handler:t,scope:n,local:!0})},n.silence=function(t,n,o,r,a){var i,c,s=this,l=!1,f=t,u=n,d=o,p=r,y=a;if("object"==typeof t&&(f=t.eventName,u=t.handler,d=t.once,p=t.scope,y=t.local),c="undefined"!=typeof y&&y?s:e,"undefined"!=typeof c.eventStore[f])for(i=0;i<c.eventStore[f].length;i++){if(l=!1,"undefined"==typeof u){c.eventStore[f]=[];break}l="undefined"!=typeof p?"boolean"==typeof d?u===c.eventStore[f][i].call&&p===c.eventStore[f][i].scope&&d===c.eventStore[f][i].once:u===c.eventStore[f][i].call&&p===c.eventStore[f][i].scope:"boolean"==typeof d?u===c.eventStore[f][i].call&&c.eventStore[f][i].once===d:u===c.eventStore[f][i].call,l&&c.eventStore[f].splice(i,1)}},n.silenceLocal=function(e,t,n,o){var r=this;"object"==typeof e?(e.local=!0,r.silence(e)):r.silence({eventName:e,handler:t,once:n,scope:o,local:!0})},n.requiredEvent=function(e,t,n,o){var r,a=this;a._fireMultiple="undefined"!=typeof o?o:!1,"undefined"==typeof a.stateEvents&&(a.stateEvents={}),"undefined"==typeof a._triggeredStateReady&&(a._triggeredStateReady=!1),a.stateEvents[e]=!1,r=a.stateUpdate(e,a.stateEvents),a.listen(e,t,n),a.listen(e,r,a)},n.stateUpdate=function(e,t){var n=e,o=t,r=this;return function(){var e,t=!0;if("undefined"!=typeof o[n]){o[n]=!0;for(e in o)t=t&&o[e];t&&(!r._triggeredStateReady||r._fireMultiple)&&(setTimeout(r.stateReady.apply(r),100),r._triggeredStateReady=!0)}}},n.stateReady=function(){console.log("ready!")},n.listen("system:trace",function(t){var n=this;n.emitterId===t&&(e.traced=n)},n),"function"==typeof n.init&&n.init.apply(n),n},modelMe:function(t){var n=this,o=n.eventMe({}),r={},a=[];return o.super={},o.get=function(e){return r[e]},o.set=function(t,n){var o,a=this;if("object"==typeof t)for(o in t)t.hasOwnProperty(o)&&("destroy"!==o&&"fetch"!==o&&"save"!==o&&"function"!=typeof t[o]?("object"==typeof n?(r[t]=Array.isArray(t[o])?[]:{},e.clone(r[t],t[o])):r[o]=t[o],a.emitLocal("attribute:changed",o)):("function"!=typeof a[o]||a.super[o]||(a.super[o]=a[o].bind(a)),a[o]=t[o]));else"destroy"!==t&&"fetch"!==t&&"save"!==t?("object"==typeof n?(r[t]=Array.isArray(n)?[]:{},e.clone(r[t],n)):r[t]=n,a.emitLocal("attribute:changed",t)):("function"==typeof a[t]&&(a.super[t]=a[t].bind(a)),a[t]=n)},o.flatten=function(){return r},o.fetch=function(t,n,o,r){var a=this,i=a.get("url"),c=e.useLocalCache&&!r;"undefined"!=typeof i?c?window.localforage.getItem(window.btoa(a.get("url")),function(e){null===e?a.ajaxGet(t,function(e){var t=e,n=a.get("url");window.localforage.setItem(window.btoa(n),t,function(){})}):a.emit(a.get("dataEvent"),e)}):a.ajaxGet(t,n):(a.emit("global:error",new e.Error("http","No URL defined",a)),"function"==typeof o&&o.apply(a))},o.ajaxGet=function(t,n){var o=this,r=o.get("url"),a=new XMLHttpRequest;a.onload=function(){var r=JSON.parse(this.responseText);200===this.status||302===this.status?(o.emit(o.get("dataEvent"),r),"string"==typeof t?o.set(t,r):o.set(r),"function"==typeof n&&n.apply(o,[r])):400===this.status||500===this.status&&o.emit("global:error",new e.Error("http","Error in request",o))},a.onerror=function(t,n,r){o.emit("global:error",new e.Error("http","Error in request type: "+n,o,r))},a.open("get",r,!0),a.send()},o.save=function(){var t=this,n=t.get("url"),o=t.get("id"),r="undefined"==typeof o?"post":"put",a=new XMLHttpRequest;"undefined"!=typeof n?(a.onload=function(){200===this.status||302===this.status?(t.clearChanges(),t.set(data),t.emit(t.get("dataEvent"),data)):(500===this.status||400===this.status)&&t.emit("global:error",new e.Error("http","Error in request",t))},a.submittedData=t.flatten(),a.open(r,n,!0),a.send()):e.emit("global:error",new e.Error("http","No URL defined",t))},o.clearChanges=function(){a=[]},o.getChanges=function(){return a},o.clear=function(){r={}},o.destroy=function(){var e,t=this;setTimeout(function(){},0);for(e in t);},o.set(t),o.listenLocal("attribute:changed",function(e){a.push(e)},this),"function"==typeof o.init&&o.init.apply(o),o},Error:function(e,t,n,o){return{type:e,message:t,createdBy:n,errorObject:o}},clone:function(t,n){var o;for(o in n)n.hasOwnProperty(o)&&("object"==typeof n[o]?(t[o]=Array.isArray(n[o])?[]:{},e.clone(t[o],n[o])):t[o]=n[o])},eventStore:{}};return e=e.eventMe(e),e.listen("global:error",function(e){console.group("An Error occured in an object with emitterid: "+e.createdBy.emitterId),console.log("It was a "+e.type+"error."),"string"==typeof e.errorObject?(console.log("It says: "+e.errorObject),console.log("and: "+e.message)):console.log("It says: "+e.message),console.log("The Whole Enchilada (object that caused this mess):"),console.dir(e.createdBy),"function"==typeof e.createdBy.flatten&&(console.log("Just the Lettuce (attributes):"),console.dir(e.createdBy.flatten())),"object"==typeof e.errorObject&&(console.log("Oh look an Error Object!:"),console.dir(e.errorObject)),console.groupEnd()}),e}(this);