You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2012/02/22 09:15:23 UTC
[10/29] Moar renames
http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/f95d8786/framework/js/cordova.js.base
----------------------------------------------------------------------
diff --git a/framework/js/cordova.js.base b/framework/js/cordova.js.base
new file mode 100644
index 0000000..6a39183
--- /dev/null
+++ b/framework/js/cordova.js.base
@@ -0,0 +1,639 @@
+
+/**
+ * The order of events during page load and Cordova startup is as follows:
+ *
+ * onDOMContentLoaded Internal event that is received when the web page is loaded and parsed.
+ * window.onload Body onload event.
+ * onNativeReady Internal event that indicates the Cordova native side is ready.
+ * onCordovaInit Internal event that kicks off creation of all Cordova JavaScript objects (runs constructors).
+ * onCordovaReady Internal event fired when all Cordova JavaScript objects have been created
+ * onCordovaInfoReady Internal event fired when device properties are available
+ * onDeviceReady User event fired to indicate that Cordova is ready
+ * onResume User event fired to indicate a start/resume lifecycle event
+ * onPause User event fired to indicate a pause lifecycle event
+ * onDestroy Internal event fired when app is being destroyed (User should use window.onunload event, not this one).
+ *
+ * The only Cordova events that user code should register for are:
+ * onDeviceReady
+ * onResume
+ *
+ * Listeners can be registered as:
+ * document.addEventListener("deviceready", myDeviceReadyListener, false);
+ * document.addEventListener("resume", myResumeListener, false);
+ * document.addEventListener("pause", myPauseListener, false);
+ */
+
+ if (typeof(DeviceInfo) !== 'object') {
+ var DeviceInfo = {};
+}
+
+var Cordova = {
+ queue: {
+ ready: true,
+ commands: [],
+ timer: null
+ },
+ available:false,
+ callbackId:0,
+ callbacks:{},
+ resources:{}
+};
+
+Cordova.callbackStatus = {
+ NO_RESULT: 0,
+ OK: 1,
+ CLASS_NOT_FOUND_EXCEPTION: 2,
+ ILLEGAL_ACCESS_EXCEPTION: 3,
+ INSTANTIATION_EXCEPTION: 4,
+ MALFORMED_URL_EXCEPTION: 5,
+ IO_EXCEPTION: 6,
+ INVALID_ACTION: 7,
+ JSON_EXCEPTION: 8,
+ ERROR: 9
+};
+
+/**
+ * Determine if resource has been loaded by Cordova
+ *
+ * @param name
+ * @return
+ */
+Cordova.hasResource = function(name) {
+ return Cordova.resources[name];
+};
+
+/**
+ * Add a resource to list of loaded resources by Cordova
+ *
+ * @param name
+ */
+Cordova.addResource = function(name) {
+ Cordova.resources[name] = true;
+};
+
+Cordova.exec = function(success, fail, service, action, args)
+{
+
+ var callbackId = service + Cordova.callbackId++;
+ if (typeof success == "function" || typeof fail == "function")
+ {
+ Cordova.callbacks[callbackId] = {success:success, fail:fail};
+ }
+
+ // generate a new command string, ex. DebugConsole/log/DebugConsole23/{"message":"wtf dude?"}
+ var command = service + "/" + action + "/" + callbackId + "/" + JSON.stringify(args);
+ // pass it on to Notify
+ window.external.Notify(command);
+};
+
+CordovaCommandResult = function(status,callbackId,args,cast)
+{
+ if(status === "backbutton") {
+
+ Cordova.fireEvent(document,"backbutton");
+ return "true";
+
+ } else if(status === "resume") {
+
+ Cordova.onResume.fire();
+ return "true";
+
+ } else if(status === "pause") {
+
+ Cordova.onPause.fire();
+ return "true";
+ }
+
+ var safeStatus = parseInt(status);
+ if(safeStatus === Cordova.callbackStatus.NO_RESULT ||
+ safeStatus === Cordova.callbackStatus.OK) {
+ Cordova.CallbackSuccess(callbackId,args,cast);
+ }
+ else
+ {
+ Cordova.CallbackError(callbackId,args,cast);
+ }
+};
+
+/**
+ * Called by native code when returning successful result from an action.
+ *
+ * @param callbackId
+ * @param args
+ * @param cast
+ */
+Cordova.CallbackSuccess = function(callbackId, args, cast)
+{
+
+ var commandResult;
+ try
+ {
+ commandResult = JSON.parse(args);
+
+ if (typeof cast !== 'undefined')
+ {
+ eval('commandResult = ' + cast + '(commandResult);');
+ }
+
+ }
+ catch(exception)
+ {
+ return exception.message;
+ }
+
+ if (Cordova.callbacks[callbackId] ) {
+
+ // If result is to be sent to callback
+ if (commandResult.status === Cordova.callbackStatus.OK) {
+ try {
+ if (Cordova.callbacks[callbackId].success) {
+ result = Cordova.callbacks[callbackId].success(commandResult.message);
+ }
+ }
+ catch (e) {
+ console.log("Error in success callback: "+callbackId+" = " + e.message);
+ }
+ }
+
+ // Clear callback if not expecting any more results
+ if (!commandResult.keepCallback) {
+ delete Cordova.callbacks[callbackId];
+ }
+ }
+ // Note that in WP7, this method can return a value to the native calling code
+ return "";
+};
+
+/**
+ * Called by native code when returning error result from an action.
+ *
+ * @param callbackId
+ * @param args
+ * @param cast - not supported
+ */
+Cordova.CallbackError = function (callbackId, args, cast) {
+
+ var commandResult;
+ try
+ {
+ commandResult = JSON.parse(args);
+ }
+ catch(exception)
+ {
+ return exception.message;
+ }
+
+ if (Cordova.callbacks[callbackId]) {
+ try {
+ if (Cordova.callbacks[callbackId].fail) {
+ Cordova.callbacks[callbackId].fail(commandResult.message);
+ }
+ }
+ catch (e) {
+ console.log("Error in error callback: "+callbackId+" = "+e);
+ }
+
+ // Clear callback if not expecting any more results
+ if (!args.keepCallback) {
+ delete Cordova.callbacks[callbackId];
+ }
+ }
+};
+
+/**
+ * Create a UUID
+ *
+ * @return {String}
+ */
+Cordova.createUUID = function() {
+ return Cordova.UUIDcreatePart(4) + '-' +
+ Cordova.UUIDcreatePart(2) + '-' +
+ Cordova.UUIDcreatePart(2) + '-' +
+ Cordova.UUIDcreatePart(2) + '-' +
+ Cordova.UUIDcreatePart(6);
+};
+
+Cordova.UUIDcreatePart = function(length) {
+ var uuidpart = "";
+ var i, uuidchar;
+ for (i=0; i<length; i++) {
+ uuidchar = parseInt((Math.random() * 256),0).toString(16);
+ if (uuidchar.length === 1) {
+ uuidchar = "0" + uuidchar;
+ }
+ uuidpart += uuidchar;
+ }
+ return uuidpart;
+};
+
+/**
+ * Does a deep clone of the object.
+ *
+ * @param obj
+ * @return {Object}
+ */
+Cordova.clone = function(obj) {
+ var i, retVal;
+ if(!obj) {
+ return obj;
+ }
+
+ if(obj instanceof Array){
+ retVal = [];
+ for(i = 0; i < obj.length; ++i){
+ retVal.push(Cordova.clone(obj[i]));
+ }
+ return retVal;
+ }
+
+ if (typeof obj === "function") {
+ return obj;
+ }
+
+ if(!(obj instanceof Object)){
+ return obj;
+ }
+
+ if (obj instanceof Date) {
+ return obj;
+ }
+
+ retVal = {};
+ for(i in obj){
+ if(!(i in retVal) || retVal[i] !== obj[i]) {
+ retVal[i] = Cordova.clone(obj[i]);
+ }
+ }
+ return retVal;
+};
+
+/*Clones object, but catches exception*/
+Cordova.safeClone = function(obj)
+{
+ try
+ {
+ return Cordova.clone(obj);
+ }
+ catch(e)
+ {
+ console.log("CloneError::" + e.message);
+ }
+ return null;
+};
+
+
+/**
+ * Custom pub-sub channel that can have functions subscribed to it
+ * @constructor
+ */
+Cordova.Channel = function(type)
+{
+ this.type = type;
+ this.handlers = {};
+ this.guid = 0;
+ this.fired = false;
+ this.enabled = true;
+};
+
+/**
+ * Subscribes the given function to the channel. Any time that
+ * Channel.fire is called so too will the function.
+ * Optionally specify an execution context for the function
+ * and a guid that can be used to stop subscribing to the channel.
+ * Returns the guid.
+ */
+Cordova.Channel.prototype.subscribe = function(f, c, g) {
+ // need a function to call
+ if (f === null) { return; }
+
+ var func = f;
+ if (typeof c === "object" && typeof f === "function") { func = Cordova.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;
+};
+
+/**
+ * Like subscribe but the function is only called once and then it
+ * auto-unsubscribes itself.
+ */
+Cordova.Channel.prototype.subscribeOnce = function(f, c) {
+ var g = null;
+ var _this = this;
+ var m = function() {
+ f.apply(c || null, arguments);
+ _this.unsubscribe(g);
+ };
+ if (this.fired) {
+ if (typeof c === "object" && typeof f === "function") { f = Cordova.close(c, f); }
+ f.apply(this, this.fireArgs);
+ } else {
+ g = this.subscribe(m);
+ }
+ return g;
+};
+
+/**
+ * Unsubscribes the function with the given guid from the channel.
+ */
+Cordova.Channel.prototype.unsubscribe = function(g) {
+ if (typeof g === "function") { g = g.observer_guid; }
+ this.handlers[g] = null;
+ delete this.handlers[g];
+};
+
+/**
+ * Calls all functions subscribed to this channel.
+ */
+Cordova.Channel.prototype.fire = function(e) {
+ if (this.enabled) {
+ var fail = false;
+ var item, handler, rv;
+ for (item in this.handlers) {
+ if (this.handlers.hasOwnProperty(item)) {
+ handler = this.handlers[item];
+ if (typeof handler === "function") {
+ rv = (handler.apply(this, arguments) === false);
+ fail = fail || rv;
+ }
+ }
+ }
+ this.fired = true;
+ this.fireArgs = arguments;
+ return !fail;
+ }
+ return true;
+};
+
+/**
+ * Calls the provided function only after all of the channels specified
+ * have been fired.
+ */
+Cordova.Channel.join = function(h, c) {
+ var i = c.length;
+ var f = function() {
+ if (!(--i)) {
+ h();
+ }
+ };
+ var len = i;
+ var j;
+ for (j=0; j<len; j++) {
+ if (!c[j].fired) {
+ c[j].subscribeOnce(f);
+ }
+ else {
+ i--;
+ }
+ }
+ if (!i) {
+ h();
+ }
+};
+
+/**
+ * Boolean flag indicating if the Cordova API is available and initialized.
+ */ // TODO: Remove this, it is unused here ... -jm
+Cordova.available = DeviceInfo.uuid !== undefined;
+
+/**
+ * Add an initialization function to a queue that ensures it will run and initialize
+ * application constructors only once Cordova has been initialized.
+ * @param {Function} func The function callback you want run once Cordova is initialized
+ */
+Cordova.addConstructor = function(func)
+{
+ Cordova.onCordovaInit.subscribeOnce(function() {
+ try {
+ func();
+ } catch(e) {
+ console.log("Failed to run constructor: " + e);
+ }
+ });
+};
+
+/**
+ * Plugins object
+ */
+if (!window.plugins) {
+ window.plugins = {};
+};
+
+/**
+ * Adds a plugin object to window.plugins.
+ * The plugin is accessed using window.plugins.<name>
+ *
+ * @param name The plugin name
+ * @param obj The plugin object
+ */
+Cordova.addPlugin = function(name, obj) {
+ if (!window.plugins[name]) {
+ window.plugins[name] = obj;
+ }
+ else {
+ console.log("Error: Plugin "+name+" already exists.");
+ }
+};
+
+/**
+ * onDOMContentLoaded channel is fired when the DOM content
+ * of the page has been parsed.
+ */
+Cordova.onDOMContentLoaded = new Cordova.Channel('onDOMContentLoaded');
+
+/**
+ * onNativeReady channel is fired when the Cordova native code
+ * has been initialized.
+ */
+Cordova.onNativeReady = new Cordova.Channel('onNativeReady');
+
+/**
+ * onCordovaInit channel is fired when the web page is fully loaded and
+ * Cordova native code has been initialized.
+ */
+Cordova.onCordovaInit = new Cordova.Channel('onCordovaInit');
+
+/**
+ * onCordovaReady channel is fired when the JS Cordova objects have been created.
+ */
+Cordova.onCordovaReady = new Cordova.Channel('onCordovaReady');
+
+/**
+ * onCordovaInfoReady channel is fired when the Cordova device properties
+ * has been set.
+ */
+Cordova.onCordovaInfoReady = new Cordova.Channel('onCordovaInfoReady');
+
+/**
+ * onCordovaConnectionReady channel is fired when the Cordova connection properties
+ * has been set.
+ */
+Cordova.onCordovaConnectionReady = new Cordova.Channel('onCordovaConnectionReady');
+
+/**
+ * onResume channel is fired when the Cordova native code
+ * resumes.
+ */
+Cordova.onResume = new Cordova.Channel('onResume');
+
+/**
+ * onPause channel is fired when the Cordova native code
+ * pauses.
+ */
+Cordova.onPause = new Cordova.Channel('onPause');
+
+/**
+ * onDestroy channel is fired when the Cordova native code
+ * is destroyed. It is used internally.
+ * Window.onunload should be used by the user.
+ */
+Cordova.onDestroy = new Cordova.Channel('onDestroy');
+Cordova.onDestroy.subscribeOnce(function() {
+ Cordova.shuttingDown = true;
+});
+Cordova.shuttingDown = false;
+
+// _nativeReady is global variable that the native side can set
+// to signify that the native code is ready. It is a global since
+// it may be called before any Cordova JS is ready.
+if (typeof _nativeReady !== 'undefined') { Cordova.onNativeReady.fire(); }
+
+/**
+ * onDeviceReady is fired only after all Cordova objects are created and
+ * the device properties are set.
+ */
+Cordova.onDeviceReady = new Cordova.Channel('onDeviceReady');
+
+
+// Array of channels that must fire before "deviceready" is fired
+Cordova.deviceReadyChannelsArray = [ Cordova.onCordovaReady, Cordova.onCordovaInfoReady, Cordova.onCordovaConnectionReady];
+
+// Hashtable of user defined channels that must also fire before "deviceready" is fired
+Cordova.deviceReadyChannelsMap = {};
+
+/**
+ * Indicate that a feature needs to be initialized before it is ready to be used.
+ * This holds up Cordova's "deviceready" event until the feature has been initialized
+ * and Cordova.initComplete(feature) is called.
+ *
+ * @param feature {String} The unique feature name
+ */
+Cordova.waitForInitialization = function(feature) {
+ if (feature) {
+ var channel = new Cordova.Channel(feature);
+ Cordova.deviceReadyChannelsMap[feature] = channel;
+ Cordova.deviceReadyChannelsArray.push(channel);
+ }
+};
+
+/**
+ * Indicate that initialization code has completed and the feature is ready to be used.
+ *
+ * @param feature {String} The unique feature name
+ */
+Cordova.initializationComplete = function(feature) {
+ var channel = Cordova.deviceReadyChannelsMap[feature];
+ if (channel) {
+ channel.fire();
+ }
+};
+
+/**
+ * Create all Cordova objects once page has fully loaded and native side is ready.
+ */
+Cordova.Channel.join(
+ function()
+ {
+
+ setTimeout(function()
+ {
+
+ Cordova.UsePolling = false;
+ //Cordova.JSCallback();
+ },1);
+
+ // Run Cordova constructors
+ Cordova.onCordovaInit.fire();
+
+ // Fire event to notify that all objects are created
+ Cordova.onCordovaReady.fire();
+
+ // Fire onDeviceReady event once all constructors have run and Cordova info has been
+ // received from native side, and any user defined initialization channels.
+ Cordova.Channel.join(function() {
+ Cordova.onDeviceReady.fire();
+
+ // Fire the onresume event, since first one happens before JavaScript is loaded
+ Cordova.onResume.fire();
+ }, Cordova.deviceReadyChannelsArray);
+
+ },
+ [ Cordova.onDOMContentLoaded ]);
+
+
+
+// Listen for DOMContentLoaded and notify our channel subscribers
+document.addEventListener('DOMContentLoaded', function() {
+ Cordova.onDOMContentLoaded.fire();
+}, false);
+
+Cordova.m_document_addEventListener = document.addEventListener;
+document.addEventListener = function(evt, handler, capture)
+{
+ console.log("document.addEventListener event named " + evt);
+
+ var e = evt.toLowerCase();
+ if (e === 'deviceready')
+ {
+ Cordova.onDeviceReady.subscribeOnce(handler);
+ }
+ else if (e === 'resume')
+ {
+ Cordova.onResume.subscribe(handler);
+ if (Cordova.onDeviceReady.fired)
+ {
+ Cordova.onResume.fire();
+ }
+ }
+ else if (e === 'pause')
+ {
+ Cordova.onPause.subscribe(handler);
+ }
+ else
+ {
+
+ if (e === 'backbutton')
+ {
+ Cordova.exec(null, null, "CoreEvents", "overrideBackbutton", [true]);
+ }
+ Cordova.m_document_addEventListener.call(document, evt, handler, capture);
+ }
+};
+
+Cordova.m_document_removeEventListener = document.removeEventListener;
+document.removeEventListener = function(evt, handler, capture)
+{
+ console.log("document.removeEventListener event named " + evt);
+
+ var e = evt.toLowerCase();
+
+ if (e === 'backbutton')
+ {
+ Cordova.exec(null, null, "CoreEvents", "overrideBackbutton", [false]);
+ }
+ Cordova.m_document_removeEventListener.call(document, evt, handler, capture);
+
+}
+
+
+Cordova.fireEvent = function(_targ,evtName)
+{
+ var target = _targ || window;
+ var eventObj = document.createEvent('MouseEvents');
+ eventObj.initEvent( evtName, true, false );
+ target.dispatchEvent( eventObj );
+}
+