/
duel.min.js
2 lines (2 loc) · 6.37 KB
/
duel.min.js
1
2
!function(e,t){"use strict";"function"==typeof define&&define.amd?define(function(){return t(e)}):"object"==typeof exports?module.exports=t(e):e.duel=t(e)}("undefined"!=typeof window?window:this,function(e){var t={};return t.useStorageEvent=!/trident|MSIE/i.test(navigator.userAgent),t.noWarnings=!0,t.isLocalStorageAvailable=function(){try{var e="undefined"!=typeof localStorage&&"setItem"in localStorage&&localStorage.setItem;if(!e)return!1;var t=new Date;return localStorage.setItem(t,t),localStorage.removeItem(t),!0}catch(n){return!1}},t.clone=function(e){var n;if(null==e||"object"!=typeof e)return e;if(e instanceof Object){n={};for(var r in e)e.hasOwnProperty(r)&&(n[r]=t.clone(e[r]));return n}throw new Error("Unable to copy obj! Its type isn't supported.")},t.DuelAbstractChannel=function(){var e=function(){};return e.prototype.getName=function(){return this._name},e.prototype.setCurrentWindowAsMaster=function(){return!0},e.prototype.currentWindowIsMaster=function(){return!0},e.prototype.broadcast=function(e){this.currentWindowIsMaster()&&this.executeTrigger({name:e,args:Array.prototype.slice.call(arguments,1)})},e.prototype.emit=function(){this.broadcast.apply(this,arguments)},e.prototype.getItem=function(e){return e+=":dueljs:variable:"+this.getName(),this.__storage?"object"==typeof angular?angular.fromJson(this.__storage.getItem(e)):JSON.parse(this.__storage.getItem(e),!0):null},e.prototype.setItem=function(e,t){this.__storage&&(e+=":dueljs:variable:"+this.getName(),this.__storage.setItem(e,"object"==typeof angular?angular.toJson(t):JSON.stringify(t)))},e.prototype.removeItem=function(e){this.__storage&&(e+=":dueljs:variable:"+this.getName(),this.__storage.removeItem(e))},e.prototype.executeTrigger=function(e,n){if(n=n||!1,!(e instanceof Object))throw"triggerDetails should be an Object";if(!this.currentWindowIsMaster()||n)try{this._triggers[e.name]instanceof Array?(this._triggers[e.name][0].apply(this,e.args),delete this._triggers[e.name]):this._triggers[e.name].apply(this,e.args)}catch(r){t.noWarnings||("object"==typeof angular?console.warn("DuelJS caught exception, maybe you didn't know that the functions inside the dueljs events use a their own scopes",r):console.warn("DuelJS caught exception",r))}},e.prototype.on=function(e,t){this._triggers||(this._triggers={}),this._triggers[e]=t},e.prototype.once=function(e,t){this.on(e,[t])},e.prototype.off=function(e){try{delete this._triggers[e]}catch(t){}},e}(),t._generateWindowID=function(){return this._windowID=+Math.random().toString().split(".")[1],this._windowID},t.getWindowID=function(){return this._windowID?this._windowID:this._generateWindowID()},t.DuelLocalStorageChannel=function(t){this._name=t,this.__storage=e.localStorage,this.setCurrentWindowAsMaster()},t.DuelLocalStorageChannel.prototype=t.clone(t.DuelAbstractChannel.prototype),t.DuelLocalStorageChannel.prototype.setCurrentWindowAsMaster=function(){var e,n,r,a,o=t.getWindowID(),i="dueljs_channel_"+this.getName();if(r=this.__storage.getItem(i)){for(r=JSON.parse(r),a=-1,e=0,n=r.length;n>e;e++)r[e].master=!1,r[e].id===o&&(a=e);-1===a?r.push({id:o,master:!0}):r[a].master=!0,this.__storage.setItem(i,JSON.stringify(r))}else this.__storage.setItem(i,JSON.stringify([{id:o,master:!0}]));return!0},t.DuelLocalStorageChannel.prototype.currentWindowIsMaster=function(){var e,n,r,a,o=t.getWindowID(),i="dueljs_channel_"+this.getName();if(r=this.__storage.getItem(i)){for(r=JSON.parse(r),a=-1,e=0,n=r.length;n>e;e++)if(r[e].id===o){a=e;break}return-1===a?!1:r[a].master}return!1},t.DuelLocalStorageChannel.prototype.broadcast=function(e){this.currentWindowIsMaster()&&(this.__storage.setItem("dueljs_trigger",JSON.stringify({channelName:this.getName(),triggerDetails:{name:e,args:Array.prototype.slice.call(arguments,1)}})),this.__storage.setItem("dueljs_trigger_event_key",+Math.random().toString().split(".")[1]))},t.DuelFakeChannel=function(e){this._name=e,t.noWarnings||console.error("DuelJS warning: instanceof DuelFakeChannel was created, check the localStorage support in your browser")},t.DuelFakeChannel.prototype=t.clone(t.DuelAbstractChannel.prototype),t.activeChannels=[],t.channel=function(e){for(var n in t.activeChannels)if(t.activeChannels.hasOwnProperty(n)&&t.activeChannels[n].getName()==e)return t.activeChannels[n];var r=this.isLocalStorageAvailable()?new this.DuelLocalStorageChannel(e):new this.DuelFakeChannel(e);return t.activeChannels.push(r),r},t.makeCurrentWindowMaster=function(){for(var e=t.activeChannels.length-1;e>=0;e--)try{t.activeChannels[e].setCurrentWindowAsMaster()}catch(n){}},e.onfocus=function(){t.makeCurrentWindowMaster()},e.isMaster=function(){return t.activeChannels.length?t.activeChannels[0].currentWindowIsMaster():!0},t.addEvent=function(){return document.addEventListener?function(t,n,r){if(t&&t.nodeName||t===e)t.addEventListener(n,r,!1);else if(t&&t.length)for(var a=0;a<t.length;a++)addEvent(t[a],n,r)}:function(t,n,r){if(t&&t.nodeName||t===e)t.attachEvent("on"+n,function(){return r.call(t,e.event)});else if(t&&t.length)for(var a=0;a<t.length;a++)addEvent(t[a],n,r)}}(),t.addEvent(e,"visibilitychange",function(){document.hidden||t.makeCurrentWindowMaster()}),t.storageEvent=function(e){if("dueljs_trigger"==e.key)for(var n=JSON.parse(e.newValue),r=t.activeChannels.length-1;r>=0;r--)try{t.activeChannels[r].getName()==n.channelName&&t.activeChannels[r].executeTrigger(n.triggerDetails)}catch(a){}},t.isLocalStorageAvailable()&&(t.storageOldTriggerValue=e.localStorage.getItem("dueljs_trigger_event_key"),t.useStorageEvent?t.addEvent(e,"storage",function(n){var r=n||r||e.event;"dueljs_trigger_event_key"==r.key&&r.newValue!=t.storageOldTriggerValue&&(t.storageOldTriggerValue=e.localStorage.getItem("dueljs_trigger_event_key"),t.storageEvent({key:"dueljs_trigger",newValue:e.localStorage.getItem("dueljs_trigger")}))}):setInterval(function(){e.localStorage.getItem("dueljs_trigger_event_key")!=t.storageOldTriggerValue&&(t.storageOldTriggerValue=e.localStorage.getItem("dueljs_trigger_event_key"),t.storageEvent({key:"dueljs_trigger",newValue:e.localStorage.getItem("dueljs_trigger")}))},100),t.addEvent(e,"unload",function(){var n,r,a,o,i,s,l=t.getWindowID();for(a=t.activeChannels.length-1;a>=0;a--)try{if(i="dueljs_channel_"+t.activeChannels[a].getName(),n=e.localStorage.getItem(i))for(n=JSON.parse(n),s=-1,o=0,r=n.length;r>o;o++)if(n[o].id===l){n[o].master&&(n[o].master=!1,n[0].master=!0),n.splice(o,1),e.localStorage.setItem(i,JSON.stringify(n));break}}catch(g){}})),t});
//# sourceMappingURL=duel.min.js.map