You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2012/04/26 21:03:30 UTC

git commit: [CB-298] Deprecate functions on the "PhoneGap" and "Cordova" globals

Updated Branches:
  refs/heads/master c1e7b2911 -> b80ff3407


[CB-298] Deprecate functions on the "PhoneGap" and "Cordova" globals


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/b80ff340
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/b80ff340
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/b80ff340

Branch: refs/heads/master
Commit: b80ff3407ee5de1736b26fd2f82a96c2dd8d34c5
Parents: c1e7b29
Author: Fil Maj <ma...@gmail.com>
Authored: Thu Apr 26 12:05:48 2012 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Thu Apr 26 12:05:48 2012 -0700

----------------------------------------------------------------------
 lib/cordova.js |  125 ++++++++++++++++++++++++++++++--------------------
 1 files changed, 75 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/b80ff340/lib/cordova.js
----------------------------------------------------------------------
diff --git a/lib/cordova.js b/lib/cordova.js
index 8389529..f6a754d 100644
--- a/lib/cordova.js
+++ b/lib/cordova.js
@@ -30,27 +30,27 @@ document.addEventListener = function(evt, handler, capture) {
     if (e == 'deviceready') {
         channel.onDeviceReady.subscribeOnce(handler);
     } else if (e == 'resume') {
-      channel.onResume.subscribe(handler);
-      // if subscribing listener after event has already fired, invoke the handler
-      if (channel.onResume.fired && handler instanceof Function) {
-          handler();
-      }
+        channel.onResume.subscribe(handler);
+        // if subscribing listener after event has already fired, invoke the handler
+        if (channel.onResume.fired && handler instanceof Function) {
+            handler();
+        }
     } else if (e == 'pause') {
-      channel.onPause.subscribe(handler);
+        channel.onPause.subscribe(handler);
     } else if (typeof documentEventHandlers[e] != 'undefined') {
-      documentEventHandlers[e].subscribe(handler);
+        documentEventHandlers[e].subscribe(handler);
     } else {
-      m_document_addEventListener.call(document, evt, handler, capture);
+        m_document_addEventListener.call(document, evt, handler, capture);
     }
 };
 
 window.addEventListener = function(evt, handler, capture) {
-  var e = evt.toLowerCase();
-  if (typeof windowEventHandlers[e] != 'undefined') {
-    windowEventHandlers[e].subscribe(handler);
-  } else {
-    m_window_addEventListener.call(window, evt, handler, capture);
-  }
+    var e = evt.toLowerCase();
+    if (typeof windowEventHandlers[e] != 'undefined') {
+        windowEventHandlers[e].subscribe(handler);
+    } else {
+        m_window_addEventListener.call(window, evt, handler, capture);
+    }
 };
 
 document.removeEventListener = function(evt, handler, capture) {
@@ -69,30 +69,29 @@ document.removeEventListener = function(evt, handler, capture) {
 };
 
 window.removeEventListener = function(evt, handler, capture) {
-  var e = evt.toLowerCase();
-  // If unsubcribing from an event that is handled by a plugin
-  if (typeof windowEventHandlers[e] != "undefined") {
-    windowEventHandlers[e].unsubscribe(handler);
-  } else {
-    m_window_removeEventListener.call(window, evt, handler, capture);
-  }
+    var e = evt.toLowerCase();
+    // If unsubcribing from an event that is handled by a plugin
+    if (typeof windowEventHandlers[e] != "undefined") {
+        windowEventHandlers[e].unsubscribe(handler);
+    } else {
+        m_window_removeEventListener.call(window, evt, handler, capture);
+    }
 };
 
 function createEvent(type, data) {
-  var event = document.createEvent('Events');
-  event.initEvent(type, false, false);
-  if (data) {
-    for (var i in data) {
-      if (data.hasOwnProperty(i)) {
-        event[i] = data[i];
-      }
+    var event = document.createEvent('Events');
+    event.initEvent(type, false, false);
+    if (data) {
+        for (var i in data) {
+            if (data.hasOwnProperty(i)) {
+                event[i] = data[i];
+            }
+        }
     }
-  }
-  return event;
+    return event;
 }
 
-if(typeof window.console === "undefined")
-{
+if(typeof window.console === "undefined") {
     window.console = {
         log:function(){}
     };
@@ -105,16 +104,16 @@ var cordova = {
      * Methods to add/remove your own addEventListener hijacking on document + window.
      */
     addWindowEventHandler:function(event, opts) {
-      return (windowEventHandlers[event] = channel.create(event, opts));
+        return (windowEventHandlers[event] = channel.create(event, opts));
     },
     addDocumentEventHandler:function(event, opts) {
-      return (documentEventHandlers[event] = channel.create(event, opts));
+        return (documentEventHandlers[event] = channel.create(event, opts));
     },
     removeWindowEventHandler:function(event) {
-      delete windowEventHandlers[event];
+        delete windowEventHandlers[event];
     },
     removeDocumentEventHandler:function(event) {
-      delete documentEventHandlers[event];
+        delete documentEventHandlers[event];
     },
     /**
      * Retreive original event handlers that were replaced by Cordova
@@ -129,20 +128,20 @@ var cordova = {
      * Method to fire event from native code
      */
     fireDocumentEvent: function(type, data) {
-      var evt = createEvent(type, data);
-      if (typeof documentEventHandlers[type] != 'undefined') {
-        documentEventHandlers[type].fire(evt);
-      } else {
-        document.dispatchEvent(evt);
-      }
+        var evt = createEvent(type, data);
+        if (typeof documentEventHandlers[type] != 'undefined') {
+            documentEventHandlers[type].fire(evt);
+        } else {
+            document.dispatchEvent(evt);
+        }
     },
     fireWindowEvent: function(type, data) {
-      var evt = createEvent(type,data);
-      if (typeof windowEventHandlers[type] != 'undefined') {
-        windowEventHandlers[type].fire(evt);
-      } else {
-        window.dispatchEvent(evt);
-      }
+        var evt = createEvent(type,data);
+        if (typeof windowEventHandlers[type] != 'undefined') {
+            windowEventHandlers[type].fire(evt);
+        } else {
+            window.dispatchEvent(evt);
+        }
     },
     // TODO: this is Android only; think about how to do this better
     shuttingDown:false,
@@ -248,15 +247,41 @@ var cordova = {
     }
 };
 
+// Adds deprecation warnings to functions of an object (but only logs a message once)
+function deprecateFunctions(obj, objLabel) {
+    var newObj = {};
+    var logHash = {};
+    for (var i in obj) {
+        if (obj.hasOwnProperty(i)) {
+            if (typeof obj[i] == 'function') {
+                newObj[i] = (function(prop){
+                    var oldFunk = obj[prop];
+                    var funkId = objLabel + '_' + prop;
+                    return function() {
+                        if (!logHash[funkId]) {
+                            console.log('[DEPRECATION NOTICE] The "' + objLabel + '" global will be removed in version 2.0, please use lowercase "cordova".');
+                            logHash[funkId] = true;
+                        }
+                        oldFunk.apply(obj, arguments);
+                    };
+                })(i);
+            } else {
+                newObj[i] = (function(prop) { return obj[prop]; })(i);
+            }
+        }
+    }
+    return newObj;
+}
+
 /**
  * Legacy variable for plugin support
  * TODO: remove in 2.0.
  */
 if (!window.PhoneGap) {
-    window.PhoneGap = cordova;
+    window.PhoneGap = deprecateFunctions(cordova, 'PhoneGap');
 }
 if (!window.Cordova) {
-    window.Cordova = cordova;
+    window.Cordova = deprecateFunctions(cordova, 'Cordova');
 }
 
 /**