2017-10-04 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @function Phaser.Boot.VisibilityHandler
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-01-12 17:09:09 +00:00
|
|
|
* @param {Phaser.Events.EventDispatcher} eventEmitter - The EventDispatcher that will dispatch the visibility events.
|
2017-10-04 22:48:16 +00:00
|
|
|
*/
|
2018-01-12 17:09:09 +00:00
|
|
|
var VisibilityHandler = function (eventEmitter)
|
2017-05-09 00:24:46 +00:00
|
|
|
{
|
|
|
|
var hiddenVar;
|
|
|
|
|
|
|
|
if (document.hidden !== undefined)
|
|
|
|
{
|
|
|
|
hiddenVar = 'visibilitychange';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var vendors = [ 'webkit', 'moz', 'ms' ];
|
|
|
|
|
|
|
|
vendors.forEach(function (prefix)
|
|
|
|
{
|
|
|
|
if (document[prefix + 'Hidden'] !== undefined)
|
|
|
|
{
|
|
|
|
document.hidden = function ()
|
|
|
|
{
|
|
|
|
return document[prefix + 'Hidden'];
|
|
|
|
};
|
|
|
|
|
|
|
|
hiddenVar = prefix + 'visibilitychange';
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var onChange = function (event)
|
|
|
|
{
|
|
|
|
if (document.hidden || event.type === 'pause')
|
|
|
|
{
|
2018-01-12 17:09:09 +00:00
|
|
|
eventEmitter.emit('hidden');
|
2017-05-09 00:24:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-12 17:09:09 +00:00
|
|
|
eventEmitter.emit('visible');
|
2017-05-09 00:24:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Does browser support it?
|
|
|
|
// If not (like in IE9 or old Android) we need to fall back to blur / focus
|
|
|
|
if (hiddenVar)
|
|
|
|
{
|
|
|
|
document.addEventListener(hiddenVar, onChange, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
console.log('Fallback TODO');
|
|
|
|
}
|
2017-05-09 14:39:30 +00:00
|
|
|
|
|
|
|
window.onblur = function ()
|
|
|
|
{
|
2018-01-12 17:09:09 +00:00
|
|
|
eventEmitter.emit('blur');
|
2017-05-09 14:39:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
window.onfocus = function ()
|
|
|
|
{
|
2018-01-12 17:09:09 +00:00
|
|
|
eventEmitter.emit('focus');
|
2017-05-09 14:39:30 +00:00
|
|
|
};
|
2017-05-09 00:24:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = VisibilityHandler;
|