Fixes for loading local XHR using file API, and still using default for remote 
XHR. responseXML returns document for local files.


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

Branch: refs/heads/master
Commit: 164b6c87984697bee548dde916bf2f07bb2f4046
Parents: 1254b23
Author: Jesse MacFadyen <purplecabb...@gmail.com>
Authored: Wed Jan 25 13:12:17 2012 -0800
Committer: Jesse MacFadyen <purplecabb...@gmail.com>
Committed: Wed Jan 25 13:12:17 2012 -0800

----------------------------------------------------------------------
 framework/js/PGXHR.js |  105 +++++++++++++++++++++++++++++---------------
 1 files changed, 69 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/164b6c87/framework/js/PGXHR.js
----------------------------------------------------------------------
diff --git a/framework/js/PGXHR.js b/framework/js/PGXHR.js
index 693f5e6..5742f71 100644
--- a/framework/js/PGXHR.js
+++ b/framework/js/PGXHR.js
@@ -12,8 +12,13 @@
        limitations under the License.
 */
                           
+/**
+ * @author purplecabbage
+ */
+                          
 (function(win,doc){
 
+
     doc.addEventListener("DOMContentLoaded",function()
     {
        var docDomain = null;
@@ -33,26 +38,34 @@
                
                        win.XMLHttpRequest = function(){};
                
-                       var UNSENT = 0;
-                       var OPENED = 1;
-                       var HEADERS_RECEIVED = 2;
-                       var LOADING = 3;
-                       var DONE = 4;
+                       win.XMLHttpRequest.UNSENT = 0;
+                       win.XMLHttpRequest.OPENED = 1;
+                       win.XMLHttpRequest.HEADERS_RECEIVED = 2;
+                       win.XMLHttpRequest.LOADING = 3;
+                       win.XMLHttpRequest.DONE = 4;
                  
                        win.XMLHttpRequest.prototype =
                        {
+                UNSENT:0,
+                OPENED:1,
+                HEADERS_RECEIVED:2,
+                LOADING:3,
+                DONE:4,
+
                                isAsync:false,
                                onreadystatechange:null,
-                               readyState:UNSENT,
+                               readyState:0,
+                _url:"",
                                open:function(reqType,uri,isAsync,user,password)
                                {
-                                       //console.log("XMLHttpRequest.open " + 
uri);
+                                       console.log("XMLHttpRequest.open " + 
uri);
                                        if(uri && uri.indexOf("http") == 0)
                                        {
                                                if(!this.wrappedXHR)
                                                {
-                                                       //console.log("using 
wrapped XHR");
                                                        this.wrappedXHR = new 
aliasXHR();
+                            var self = this;
+
                                                        Object.defineProperty( 
this, "status", { get: function() {
                                                                return 
this.wrappedXHR.status;                                                         
                 
                                                        }});
@@ -62,32 +75,40 @@
                                                        Object.defineProperty( 
this, "statusText", { get: function() {
                                                                return 
this.wrappedXHR.statusText;                                                     
                         
                                                        }});
-                                                       Object.defineProperty( 
this, "responseXML", { get: function() {
+
+                            Object.defineProperty( this, "responseXML", { get: 
function() {
                                                                return 
this.wrappedXHR.responseXML;                                                    
                         
-                                                       }});
-                                                       
-                                                       this.getResponseHeader 
= function() {
-                                                               return 
this.wrappedXHR.getResponseHeader.apply(this.wrappedXHR,arguments);
+                                                       }});    
+                        
+                                                       this.getResponseHeader 
= function(header) {
+                                                               return 
this.wrappedXHR.getResponseHeader(header);
                                                        };
                                                        
this.getAllResponseHeaders = function() {
-                                                               return 
this.wrappedXHR.getAllResponseHeaders.apply(this.wrappedXHR,arguments);
+                                                               return 
this.wrappedXHR.getAllResponseHeaders();
                                                        };
                                                        
-                                                       
this.wrappedXHR.onreadystatechange = this.onreadystatechange;
+                                                       
this.wrappedXHR.onreadystatechange = function()
+                            {
+                                
self.changeReadyState(self.wrappedXHR.readyState);
+                            };
                                                }
                                                return 
this.wrappedXHR.open(reqType,uri,isAsync,user,password);
                                        }
                                        else
                                        {
+                        // x-wmapp1://app/www/page2.html
                         // need to work some magic on the actual url/filepath
-                               var lastFileSlash = uri.lastIndexOf("\\");
-                               var newUrl =  "app/" + uri.substr(lastFileSlash 
+ 1);
+                               var newUrl =  uri;
+                        if(newUrl.indexOf(":/") > -1)
+                        {
+                            newUrl = newUrl.split(":/")[1];
+                        }
+
                                if(newUrl.lastIndexOf("/") === newUrl.length - 
1)
                                {
-                                   newUrl += "index.html"; // default page is 
index.html, when call is to a dir/
+                                   newUrl += "index.html"; // default page is 
index.html, when call is to a dir/ ( why not ...? )
                                }
-                                               
navigator.fileMgr.readAsText(newUrl,"UTF-8",this.onResult.bind(this),this.onError.bind(this));
-                               this.changeReadyState(OPENED);
+                        this._url = newUrl;
                                        }
                                },
                                statusText:"",
@@ -99,47 +120,59 @@
                                                this.onreadystatechange();      
                                        }
                                },
-                               getResponseHeader:function()
+                               getResponseHeader:function(header)
                                {
-                                       return "";
+                    return this.wrappedXHR ?  
this.wrappedXHR.getResponseHeader(header) : "";
                                },
                                getAllResponseHeaders:function()
                                {
-                                       return "";
+                                       return this.wrappedXHR ?  
this.wrappedXHR.getAllResponseHeaders() : "";
                                },
                                responseText:"",
-                               responseXML:function()
-                               {
-                                       return new Document(this.responseText);
-                               },
+                               responseXML:"",
                                onResult:function(res)
                                {
                                        this.status = 200;
                                        this.responseText = res;
-                                       this.changeReadyState(DONE);
+
+                    Object.defineProperty( this, "responseXML", { get: 
function() {
+                        var parser = new DOMParser();
+                                               return 
parser.parseFromString(this.responseText,"text/xml");                           
                                                 
+                                       }}); 
+                                       this.changeReadyState(this.DONE);
                                },
                                onError:function(err)
                                {
-                                       //console.log("Received Error from 
FileAPI :: " + err);
+                                       console.log("Wrapped XHR received Error 
from FileAPI :: " + err);
                                        this.status = 404;
-                                       this.changeReadyState(DONE);
+                                       this.changeReadyState(this.DONE);
                                },
+
+                abort:function()
+                {
+                                       if(this.wrappedXHR)
+                                       {
+                                               return this.wrappedXHR.abort();
+                                       }
+                },
                                
                                send:function(data)
                                {
                                        if(this.wrappedXHR)
                                        {
-                                               return 
this.wrappedXHR.send.apply(this.wrappedXHR,arguments);
+                                               return 
this.wrappedXHR.send(data);
                                        }
+                    else
+                    {
+                        this.changeReadyState(this.OPENED);
+                        
navigator.fileMgr.readAsText(this._url,"UTF-8",this.onResult.bind(this),this.onError.bind(this));
+                    }
                                },
-                               status:404,
-                               responseText:"empty"
+                               status:404
                        };                
            } // if doc domain 
 
     },false);// addEventListener
 
                  
-})(window,document);
-
-          
+})(window,document);
\ No newline at end of file

Reply via email to