2016-11-28 16:55:13 +00:00
|
|
|
var OS = require('../device/OS');
|
|
|
|
|
2016-11-25 02:08:33 +00:00
|
|
|
var isBooted = false;
|
|
|
|
|
2017-10-12 14:09:52 +00:00
|
|
|
/**
|
2018-01-26 03:40:49 +00:00
|
|
|
* Inspects the readyState of the document. If the document is already complete then it invokes the given callback.
|
|
|
|
* If not complete it sets up several event listeners such as `deviceready`, and once those fire, it invokes the callback.
|
|
|
|
* Called automatically by the Phaser.Game instance. Should not usually be access directly.
|
2017-10-12 14:09:52 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Dom.DOMContentLoaded
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {function} callback - The callback to be invoked when the device is ready and the DOM content is loaded.
|
|
|
|
*
|
|
|
|
* @return {boolean} Returns `false` if the document is already loaded, otherwise `true` if the callback is pending.
|
|
|
|
*/
|
2017-01-09 23:54:14 +00:00
|
|
|
var DOMContentLoaded = function (callback)
|
2016-11-25 02:08:33 +00:00
|
|
|
{
|
|
|
|
if (isBooted)
|
|
|
|
{
|
2017-10-12 14:09:52 +00:00
|
|
|
return false;
|
2016-11-25 02:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (document.readyState === 'complete' || document.readyState === 'interactive')
|
|
|
|
{
|
|
|
|
isBooted = true;
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
2017-10-12 14:09:52 +00:00
|
|
|
return true;
|
2016-11-25 02:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var check = function ()
|
|
|
|
{
|
|
|
|
isBooted = true;
|
|
|
|
|
|
|
|
document.removeEventListener('deviceready', check, true);
|
|
|
|
document.removeEventListener('DOMContentLoaded', check, true);
|
|
|
|
window.removeEventListener('load', check, true);
|
|
|
|
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!document.body)
|
|
|
|
{
|
|
|
|
window.setTimeout(check, 20);
|
|
|
|
}
|
2016-11-25 04:33:48 +00:00
|
|
|
else if (OS.cordova && !OS.cocoonJS)
|
|
|
|
{
|
|
|
|
// Ref. http://docs.phonegap.com/en/3.5.0/cordova_events_events.md.html#deviceready
|
|
|
|
document.addEventListener('deviceready', check, false);
|
|
|
|
}
|
2016-11-25 02:08:33 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
document.addEventListener('DOMContentLoaded', check, true);
|
|
|
|
window.addEventListener('load', check, true);
|
|
|
|
}
|
2017-10-12 14:09:52 +00:00
|
|
|
|
|
|
|
return true;
|
2017-01-09 23:54:14 +00:00
|
|
|
};
|
2016-11-25 02:08:33 +00:00
|
|
|
|
|
|
|
module.exports = DOMContentLoaded;
|