From 3365dba87078817232c49788457eb8f2e73cb45f Mon Sep 17 00:00:00 2001 From: Dave Johnson Date: Tue, 20 Jul 2010 11:25:55 -0700 Subject: [PATCH] fixed up things so that deviceready now uses DOMContentLoaded and if DCL fires before something is attached to deviceready the function will be called immediately --- droidgap | 1 + example/phonegap.js | 126 +++++++++++++++++++++++++-- framework/assets/js/channel.js | 81 +++++++++++++++++ framework/assets/js/phonegap.js.base | 2 - 4 files changed, 201 insertions(+), 9 deletions(-) create mode 100644 framework/assets/js/channel.js diff --git a/droidgap b/droidgap index 7c9d9d1e..928054a7 100755 --- a/droidgap +++ b/droidgap @@ -7,6 +7,7 @@ class Build def initialize(*a) @android_sdk_path, @name, @pkg, @www, @path = a + @android_sdk_path = "/Users/davejohnson/Sdk/android-sdk-mac_86" @android_dir = File.expand_path(File.dirname(__FILE__)) @framework_dir = File.join(@android_dir, "framework") end diff --git a/example/phonegap.js b/example/phonegap.js index dfd6f458..37080a15 100644 --- a/example/phonegap.js +++ b/example/phonegap.js @@ -186,8 +186,6 @@ document.addEventListener = function(evt, handler, capture) { } }; - - /** * 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 @@ -331,6 +329,8 @@ PhoneGap.close = function(context, func, params) { this.y = y; this.z = z; this.timestamp = new Date().getTime(); + this.win = null; + this.fail = null; } /** @@ -364,7 +364,6 @@ Accelerometer.ERROR_MSG = ["Not running", "Starting", "", "Failed to start"]; * @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. */ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, errorCallback, options) { - // successCallback required if (typeof successCallback != "function") { console.log("Accelerometer Error: successCallback is not a function"); @@ -427,6 +426,24 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error } } + +Accelerometer.prototype.gotCurrentAcceleration = function(key, x, y, z) +{ + var a = new Acceleration(x,y,z); + a.x = x; + a.y = y; + a.z = z; + a.win = accelListeners[key].win; + a.fail = accelListeners[key].fail; + this.timestamp = new Date().getTime(); + this.lastAcceleration = a; + accelListeners[key] = a; + if (typeof a.win == "function") { + a.win(a); + } +} + + /** * Asynchronously aquires the acceleration repeatedly at a given interval. * @@ -436,7 +453,6 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error * @return String The watch id that must be passed to #clearWatch to stop watching. */ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallback, options) { - // Default interval (10 sec) var frequency = (options != undefined)? options.frequency : 10000; @@ -550,7 +566,87 @@ Camera.prototype.fail = function(err) PhoneGap.addConstructor(function() { if (typeof navigator.camera == "undefined") navigator.camera = new Camera(); }); -/** +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