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/03/15 20:36:25 UTC

[16/21] git commit: tweaked ios-specific FileReader/Entry objects to override the ios-specific methods over top common definitions for those objects

tweaked ios-specific FileReader/Entry objects to override the ios-specific methods over top common definitions for those objects


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/7547a9f4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/7547a9f4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/7547a9f4

Branch: refs/heads/master
Commit: 7547a9f4cbc374112c860d81d366406c2c246162
Parents: 7a27bef
Author: Fil Maj <ma...@gmail.com>
Authored: Sat Mar 10 12:07:17 2012 -0800
Committer: Fil Maj <ma...@gmail.com>
Committed: Thu Mar 15 10:14:41 2012 -0700

----------------------------------------------------------------------
 lib/platform/ios.js          |   16 +++++--
 lib/plugin/Entry.js          |    4 +-
 lib/plugin/FileReader.js     |    7 +--
 lib/plugin/ios/Entry.js      |    7 +++
 lib/plugin/ios/FileReader.js |   82 +++++++++++++++++++++++++++++++++++++
 5 files changed, 105 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/7547a9f4/lib/platform/ios.js
----------------------------------------------------------------------
diff --git a/lib/platform/ios.js b/lib/platform/ios.js
index 7f1779b..5a0118e 100644
--- a/lib/platform/ios.js
+++ b/lib/platform/ios.js
@@ -2,12 +2,20 @@ module.exports = {
     id: "ios",
     initialize:function() {
         // iOS doesn't allow reassigning / overriding navigator.geolocation object.
-        // So clobber it here :)
+        // So clobber its methods here instead :)
         var geo = require('cordova/plugin/geolocation');
         
         navigator.geolocation.getCurrentPosition = geo.getCurrentPosition;
         navigator.geolocation.watchPosition = geo.watchPosition;
         navigator.geolocation.clearWatch = geo.clearWatch;
+
+        // Override Entry's toURL method with iOS platform-specific version.
+        var Entry = require('cordova/plugin/Entry');
+        Entry.prototype.toURL = require('cordova/plugin/ios/Entry').toURL;
+
+        // Override FileReader's readAsText method with iOS-specific version.
+        var FileReader = require('cordova/plugin/FileReader');
+        FileReader.prototype.readAsText = require('cordova/plugin/ios/FileReader').readAsText;
     },
     objects: {
         navigator: {
@@ -18,10 +26,10 @@ module.exports = {
             }
         },
         File: { // exists natively , override
-      	  path: "cordova/plugin/File"
-    	},
+            path: "cordova/plugin/File"
+        },
         device: {
-          path: 'cordova/plugin/ios/device'
+            path: 'cordova/plugin/ios/device'
         },
         console: {
             path: 'cordova/plugin/ios/console'

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/7547a9f4/lib/plugin/Entry.js
----------------------------------------------------------------------
diff --git a/lib/plugin/Entry.js b/lib/plugin/Entry.js
index b3acd8a..f748dcc 100644
--- a/lib/plugin/Entry.js
+++ b/lib/plugin/Entry.js
@@ -152,9 +152,7 @@ Entry.prototype.copyTo = function(parent, newName, successCallback, errorCallbac
  */
 Entry.prototype.toURL = function() {
     // fullPath attribute contains the full URL
-    //return this.fullPath;
-    // *** iOS specific 
-    return "file://localhost" + this.fullPath;
+    return this.fullPath;
 };
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/7547a9f4/lib/plugin/FileReader.js
----------------------------------------------------------------------
diff --git a/lib/plugin/FileReader.js b/lib/plugin/FileReader.js
index df371ab..8b634d9 100644
--- a/lib/plugin/FileReader.js
+++ b/lib/plugin/FileReader.js
@@ -98,10 +98,9 @@ FileReader.prototype.readAsText = function(file, encoding) {
             if (me.readyState === FileReader.DONE) {
                 return;
             }
+
             // Save result
-            //me.result = r;
-            // *** iOS specific 
-            me.result = decodeURIComponent(r);
+            me.result = r;
 
             // If onload callback
             if (typeof me.onload === "function") {
@@ -234,7 +233,7 @@ FileReader.prototype.readAsDataURL = function(file) {
  */
 FileReader.prototype.readAsBinaryString = function(file) {
     // TODO - Can't return binary data to browser.
-    console.log('This method is not supported at this time.');
+    console.log('method "readAsBinaryString" is not supported at this time.');
 };
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/7547a9f4/lib/plugin/ios/Entry.js
----------------------------------------------------------------------
diff --git a/lib/plugin/ios/Entry.js b/lib/plugin/ios/Entry.js
new file mode 100644
index 0000000..230da8a
--- /dev/null
+++ b/lib/plugin/ios/Entry.js
@@ -0,0 +1,7 @@
+module.exports = {
+    toURL:function() {
+        // TODO: refactor path in a cross-platform way so we can eliminate 
+        // these kinds of platform-specific hacks.
+        return "file://localhost" + this.fullPath;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/7547a9f4/lib/plugin/ios/FileReader.js
----------------------------------------------------------------------
diff --git a/lib/plugin/ios/FileReader.js b/lib/plugin/ios/FileReader.js
new file mode 100644
index 0000000..86b0ae8
--- /dev/null
+++ b/lib/plugin/ios/FileReader.js
@@ -0,0 +1,82 @@
+module.exports = {
+    readAsText:function(file, encoding) {
+        // Figure out pathing
+        this.fileName = '';
+        if (typeof file.fullPath === 'undefined') {
+            this.fileName = file;
+        } else {
+            this.fileName = file.fullPath;
+        }
+
+        // Already loading something
+        if (this.readyState == FileReader.LOADING) {
+            throw new FileError(FileError.INVALID_STATE_ERR);
+        }
+
+        // LOADING state
+        this.readyState = FileReader.LOADING;
+
+        // If loadstart callback
+        if (typeof this.onloadstart === "function") {
+            this.onloadstart(new ProgressEvent("loadstart", {target:this}));
+        }
+
+        // Default encoding is UTF-8
+        var enc = encoding ? encoding : "UTF-8";
+
+        var me = this;
+
+        // Read file
+        exec(
+            // Success callback
+            function(r) {
+                // If DONE (cancelled), then don't do anything
+                if (me.readyState === FileReader.DONE) {
+                    return;
+                }
+
+                // Save result
+                me.result = decodeURIComponent(r);
+
+                // If onload callback
+                if (typeof me.onload === "function") {
+                    me.onload(new ProgressEvent("load", {target:me}));
+                }
+
+                // DONE state
+                me.readyState = FileReader.DONE;
+
+                // If onloadend callback
+                if (typeof me.onloadend === "function") {
+                    me.onloadend(new ProgressEvent("loadend", {target:me}));
+                }
+            },
+            // Error callback
+            function(e) {
+                // If DONE (cancelled), then don't do anything
+                if (me.readyState === FileReader.DONE) {
+                    return;
+                }
+
+                // DONE state
+                me.readyState = FileReader.DONE;
+
+                // null result
+                me.result = null;
+
+                // Save error
+                me.error = new FileError(e);
+
+                // If onerror callback
+                if (typeof me.onerror === "function") {
+                    me.onerror(new ProgressEvent("error", {target:me}));
+                }
+
+                // If onloadend callback
+                if (typeof me.onloadend === "function") {
+                    me.onloadend(new ProgressEvent("loadend", {target:me}));
+                }
+            }, 
+        "File", "readAsText", [this.fileName, enc]);
+    }
+}