Created
May 23, 2013 12:02
-
-
Save pete-otaqui/5635596 to your computer and use it in GitHub Desktop.
Visibility API "polyfill-of-sorts"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Visibility API polyfill-of-sorts | |
This is wrapped as an AMD module for convenience, and assumes | |
that you are using a 'pubusb' library of some kind. That's | |
all fairly easy to remove (amd and the dependency). | |
*/ | |
define(['pubsub'], function (pubsub) { | |
'use strict'; | |
var current_state; | |
var hidden = 'hidden'; | |
// Standards: | |
if (hidden in document) { | |
document.addEventListener('visibilitychange', onVisibilityChange); | |
} else if ('mozHidden' in document) { | |
hidden = 'mozHidden'; | |
document.addEventListener('mozvisibilitychange', onVisibilityChange); | |
} else if ('webkitHidden' in document) { | |
hidden = 'webkitHidden'; | |
document.addEventListener('webkitvisibilitychange', onVisibilityChange); | |
} else if ('msHidden' in document) { | |
hidden = 'msHidden'; | |
document.addEventListener('msvisibilitychange', onVisibilityChange); | |
} | |
/* Generalised "Visibility API" polyfill */ | |
function onVisibilityChange (evt) { | |
var state = 'unknown', | |
v = 'visible', h = 'hidden', | |
evtMap = { | |
focus:v, | |
focusin:v, | |
pageshow:v, | |
blur:h, | |
focusout:h, | |
pagehide:h | |
}; | |
evt = evt || window.event; | |
if (evt.type in evtMap) { | |
state = evtMap[evt.type]; | |
} else { | |
state = document[hidden] ? h : v; | |
} | |
// (nb - might be triggered twice, so we track "current_state") | |
if (state !== current_state) { | |
current_state = state; | |
// this is the bit you would change if you aren't using | |
// amd or a pubsub library | |
pubsub.publish('visibility-change', [state]); | |
} | |
} | |
// let's do this too in for other browsers | |
window.addEventListener('pageshow', onVisibilityChange, false); | |
window.addEventListener('pagehide', onVisibilityChange, false); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment