@arnellebalane/master-tab
Version:
Controls which tab should be the master tab when opening a page in multiple browser tabs
3 lines (2 loc) • 1.42 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.MasterTab=e()}(this,function(){var t,e=new(function(t){var e=function(){this.$listeners={}};e.prototype.on=function(t,e){this.$listeners[t]||(this.$listeners[t]=[]),this.$listeners[t].push(e)},e.prototype.off=function(t,e){if(this.$listeners[t]){if(e instanceof Function){var n=this.$listeners[t].indexOf(e);this.$listeners[t].splice(n,1)}this.$listeners[t].length&&e||delete this.$listeners[t]}},e.prototype.emit=function(t,e){this.$listeners[t]&&this.$listeners[t].forEach(function(t){return t(e)})},t.exports=e}(t={exports:{}},t.exports),t.exports),n=e.STORAGE_KEY||"mastertab",i=e.INSTANCE_ID||Math.floor(65536*Math.random()).toString(16),s=0,o=1,r=2,f=function(){var t=localStorage.getItem(n);return t?t.split(";"):[]},c=function(t){localStorage.setItem(n,t.join(";"))},a=function(t){e.emit("change",t[0]===i)};return["pageshow","focus","visibilitychange"].forEach(function(t){window.addEventListener(t,function(t){if(("hidden"===document.visibilityState?r:document.hasFocus()?s:o)!==r){var e=f(),n=e.indexOf(i);n<0&&(n=e.length),e.splice(n,1),e.unshift(i),c(e),a(e)}})}),window.addEventListener("unload",function(){var t=f(),e=t.indexOf(i);t.splice(e,1),c(t),a(t)}),window.addEventListener("storage",function(t){t.key===n&&a(f())}),e});
//# sourceMappingURL=master-tab.umd.js.map