added onNativeReady and onDOMContentLoaded events

This commit is contained in:
Dave Johnson
2010-07-22 13:05:41 -07:00
parent ca01781766
commit 8e5de2cb7b
4 changed files with 285 additions and 165 deletions
+136 -19
View File
@@ -11,10 +11,98 @@ PhoneGap = {
ready: true,
commands: [],
timer: null
},
_constructors: []
}
};
PhoneGap.Channel = function(type)
{
this.type = type;
this.handlers = {};
this.guid = 0;
this.fired = false;
this.enabled = true;
};
PhoneGap.Channel.prototype.sub = function(f, c, g)
{
// need a function to call
if (f == null)
return;
var func = f;
if (typeof c == "object" && f instanceof Function)
func = PhoneGap.close(c, f);
g = g || func.observer_guid || f.observer_guid || this.guid++;
func.observer_guid = g;
f.observer_guid = g;
this.handlers[g] = func;
return g;
};
PhoneGap.Channel.prototype.sob = function(f, c)
{
var g = null;
var _this = this;
var m = function() {
f.apply(c || null, arguments);
_this.dub(g);
}
if (this.fired) {
if (typeof c == "object" && f instanceof Function)
f = PhoneGap.close(c, f);
f.apply(this, this.fireArgs);
} else {
g = this.sub(m);
}
return g;
};
PhoneGap.Channel.prototype.dub = function(g)
{
if (g instanceof Function)
g = g.observer_guid;
this.handlers[g] = null;
delete this.handlers[g];
};
PhoneGap.Channel.prototype.fire = function(e)
{
if (this.enabled)
{
var fail = false;
for (var item in this.handlers) {
var handler = this.handlers[item];
if (handler instanceof Function) {
var rv = (handler.apply(this, arguments)==false);
fail = fail || rv;
}
}
this.fired = true;
this.fireArgs = arguments;
return !fail;
}
return true;
};
PhoneGap.Channel.merge = function(h, e) {
var i = e.length;
var f = function() {
if (!(--i)) h();
}
for (var j=0; j<i; j++) {
(!e[j].fired?e[j].sob(f):i--);
}
if (!i) h();
}
/**
* Boolean flag indicating if the PhoneGap API is available and initialized.
*/ // TODO: Remove this, it is unused here ... -jm
@@ -26,23 +114,9 @@ PhoneGap.available = DeviceInfo.uuid != undefined;
* @param {Function} func The function callback you want run once PhoneGap is initialized
*/
PhoneGap.addConstructor = function(func) {
var state = document.readyState;
if ( state == 'loaded' || state == 'complete' )
{
func();
}
else
{
PhoneGap._constructors.push(func);
}
};
document.addEventListener('DOMContentLoaded', function() {
// run our constructors list
while (PhoneGap._constructors.length > 0) {
var constructor = PhoneGap._constructors.shift();
PhoneGap.onDeviceReady.sob(function() {
try {
constructor();
func();
} catch(e) {
if (typeof(debug['log']) == 'function') {
debug.log("Failed to run constructor: " + debug.processMessage(e));
@@ -50,9 +124,41 @@ document.addEventListener('DOMContentLoaded', function() {
alert("Failed to run constructor: " + e.message);
}
}
}
});
};
PhoneGap.onDOMContentLoaded = new PhoneGap.Channel();
PhoneGap.onNativeReady = new PhoneGap.Channel();
if (_nativeReady) PhoneGap.onNativeReady.fire();
PhoneGap.onDeviceReady = new PhoneGap.Channel();
PhoneGap.Channel.merge(function() {
PhoneGap.onDeviceReady.fire();
}, [ PhoneGap.onDOMContentLoaded, PhoneGap.onNativeReady ]);
// Listen for DOMContentLoaded
document.addEventListener('DOMContentLoaded', function() {
PhoneGap.onDOMContentLoaded.fire();
});
// Intercept calls to document.addEventListener and watch for deviceready
PhoneGap._document_addEventListener = document.addEventListener;
document.addEventListener = function(evt, handler, capture) {
if (evt.toLowerCase() == 'deviceready') {
PhoneGap.onDeviceReady.sob(handler);
} else {
PhoneGap._document_addEventListener.call(document, evt, handler);
}
};
/**
* Execute a PhoneGap command in a queued fashion, to ensure commands do not
* execute with any race conditions, and only run when PhoneGap is ready to
@@ -181,3 +287,14 @@ PhoneGap.UUIDcreatePart = function(length) {
return uuidpart;
};
PhoneGap.close = function(context, func, params) {
if (null == params) {
return function() {
return func.apply(context, arguments);
}
} else {
return function() {
return func.apply(context, params);
}
}
}