-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction-v0.4.4.min.js
5 lines (5 loc) · 7.55 KB
/
action-v0.4.4.min.js
1
2
3
4
5
/****************************************
Action! v0.4.4 Akshay Kumar
https://github.com/designfrontier/Action
****************************************/
!function(e){"use strict";var t={eventMe:function(e){var n=e;return n.emitterId=Math.ceil(1e4*Math.random()),n.eventStore={},n.emit=function(e,n,o){var r,a,i=this,c="undefined"!=typeof o&&o;if(r=c?i.eventStore[e]:t.eventStore[e],"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(e,r[a].call,!0,c)},n.emitLocal=function(e,t){var n=this;n.emit(e,t,!0)},n.listen=function(e,n,o,r,a){var i,c,s,l=this,f=!0,u=e,d=n,p=o,y=a,h=r;if("object"==typeof e&&(u=e.eventName,d=e.handler,p=e.scope,h=e.once,y=e.local),c="undefined"!=typeof y&&y?l.eventStore[e]:t.eventStore[e],s="undefined"!=typeof y&&y?l:t,"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(e,n,o,r){var a,i,c,s=this,l=!0,f=e,u=n,d=o,p=r;if("object"==typeof e&&(f=e.eventName,u=e.handler,d=e.scope,p=e.local),"undefined"!=typeof p&&p?(i=s.eventStore[f],c=s):(i=t.eventStore[f],c=t),"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[e]=[],c.eventStore[e].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(e,n,o,r,a){var i,c,s=this,l=!1,f=e,u=n,d=o,p=r,y=a;if("object"==typeof e&&(f=e.eventName,u=e.handler,d=e.once,p=e.scope,y=e.local),c="undefined"!=typeof y&&y?s:t,"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(e){var n=this;n.emitterId===e&&(t.traced=n)},n),"function"==typeof n.init&&n.init.apply(n),n},modelMe:function(e){var n=this,o=n.eventMe({}),r={},a=[];return o.super={},o.get=function(e){return r[e]},o.set=function(e,n){var o,a=this;if("object"==typeof e)for(o in e)e.hasOwnProperty(o)&&("destroy"!==o&&"fetch"!==o&&"save"!==o&&"function"!=typeof e[o]?("object"==typeof n?(r[e]=Array.isArray(e[o])?[]:{},t.clone(r[e],e[o])):r[o]=e[o],a.emitLocal("attribute:changed",o)):("function"!=typeof a[o]||a.super[o]||(a.super[o]=a[o].bind(a)),a[o]=e[o]));else"destroy"!==e&&"fetch"!==e&&"save"!==e?("object"==typeof n?(r[e]=Array.isArray(n)?[]:{},t.clone(r[e],n)):r[e]=n,a.emitLocal("attribute:changed",e)):("function"==typeof a[e]&&(a.super[e]=a[e].bind(a)),a[e]=n)},o.flatten=function(){return r},o.fetch=function(e,n,o,r){var a=this,i=a.get("url"),c=a.get("cacheLocal")&&t.useLocalCache&&!r;"undefined"!=typeof i?c?window.localforage.getItem(window.btoa(a.get("url")),function(t){null===t?a.ajaxGet(e,function(e){var t=e,n=a.get("url");window.localforage.setItem(window.btoa(n),t,function(){})}):a.emit(a.get("dataEvent"),t)}):a.ajaxGet(e,n):(a.emit("global:error",new t.Error("http","No URL defined",a)),"function"==typeof o&&o.apply(a))},o.ajaxGet=function(e,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 e?o.set(e,r):o.set(r),"function"==typeof n&&n.apply(o,[r])):400===this.status||500===this.status&&o.emit("global:error",new t.Error("http","Error in request",o))},a.onerror=function(e,n,r){o.emit("global:error",new t.Error("http","Error in request type: "+n,o,r))},a.open("get",r,!0),a.send()},o.save=function(){var e=this,n=e.get("url"),o=e.get("id"),r="undefined"==typeof o?"post":"put",a=new XMLHttpRequest;"undefined"!=typeof n?(a.onload=function(){200===this.status||302===this.status?(e.clearChanges(),e.set(data),e.emit(e.get("dataEvent"),data)):(500===this.status||400===this.status)&&e.emit("global:error",new t.Error("http","Error in request",e))},a.submittedData=e.flatten(),a.open(r,n,!0),a.send()):t.emit("global:error",new t.Error("http","No URL defined",e))},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(e),o.listenLocal("attribute:changed",function(e){a.push(e)},o),o.listen(o.get("requestEvent"),function(){this.fetch()},o),"function"==typeof o.init&&o.init.apply(o),o},viewMe:function(e){var t=this,n=t.eventMe(e);if("undefined"==typeof n.render)throw"render is required for a view";return n.stateReady=function(){n.render.apply(n)},n.requiredEvent("data:set:"+n.dataID,function(e){this.viewData=e},n),n.requiredEvent("template:set:"+n.templateID,function(e){this.template=e},n),"undefined"==typeof n.destroy&&(n.destroy=function(){}),n.listen("state:change",function(e){var t=this;(e===t.stateEvent||e.replace("/","")===t.stateEvent)&&(t.emit("template:get",t.templateID),t.emit("data:get:"+t.dataID))},n),n},routeMe:function(){var e=this,t=e.eventMe({}),n=function(){var e=(document.querySelectorAll("a"),document);e.addEventListener("click",function(e){var n,o=e.toElement;if("a"===o.tagName.toLowerCase()){if(n=o.attributes.href.textContent,n.match(/http:/))return{};t.emit("state:change",n),e.preventDefault()}})};return n(),{}},Error:function(e,t,n,o){return{type:e,message:t,createdBy:n,errorObject:o}},clone:function(e,n){var o;for(o in n)n.hasOwnProperty(o)&&("object"==typeof n[o]?(e[o]=Array.isArray(n[o])?[]:{},t.clone(e[o],n[o])):e[o]=n[o])},eventStore:{}};t=t.eventMe(t),t.listen("template:get",function(e){t.emit("template:set:"+e,t.templates[e])}),t.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=t}("undefined"==typeof exports?this.action={}:exports);