Ticket #1104: ticket1104_jquery.twFile.3.patch

File ticket1104_jquery.twFile.3.patch, 3.9 KB (added by hugheth, 3 years ago)

The new getFilePath function must now be called by the user

  • .js

    old new  
    1 /* 
     1/* 
    22jQuery.twFile.js 
    33 
    44jQuery plugin for loading a file and saving data to a file 
     
    4343                                return this.currentDriver.copyFile(dest,source); 
    4444                        else 
    4545                                return false; 
     46                }, 
     47                getFilePath: function(filePath){        // helper function to retrieve requested file's path 
     48                        // If blank then assume the file requested is the current file 
     49                        if(!filePath){ 
     50                                filePath = document.location.href; 
     51                        } 
     52                        filePath = unescape(filePath);          // Unescape the filePath 
     53                        // Remove any location or query part of the URL 
     54                        var hashPos = filePath.indexOf("#"); 
     55                        var argPos = filePath.indexOf("?"); 
     56                        var nPos = Math.min((hashPos+1) ? hashPos : filePath.length, (argPos+1) ? argPos : filePath.length); 
     57                        var info = filePath.substr(nPos); 
     58                        filePath = filePath.substr(0,nPos); 
     59                        // Convert file://localhost/ to file:/// 
     60                        if(filePath.indexOf("file://localhost/") == 0) 
     61                        filePath = "file://" + filePath.substr(16); 
     62                        // Convert to a native file format 
     63                        if(filePath.charAt(9) == ":") // pc local file 
     64                                filePath = filePath.substr(8).replace(new RegExp("/","g"),"\\"); 
     65                        else if(filePath.indexOf("file://///") == 0) // FireFox pc network file 
     66                                filePath = "\\\\" + filePath.substr(10).replace(new RegExp("/","g"),"\\"); 
     67                        else if(filePath.indexOf("file:///") == 0) // mac/unix local file 
     68                                filePath = filePath.substr(7); 
     69                        else if(filePath.indexOf("file:/") == 0) // mac/unix local file 
     70                                filePath = filePath.substr(5); 
     71                        else if(filePath.indexOf("//") == 0) // pc network file 
     72                                filePath = "\\\\" + filePath.substr(7).replace(new RegExp("/","g"),"\\"); 
     73                        // If relative then append pathname 
     74                        if(filePath.indexOf("/")!=0&&(filePath.indexOf(":")==-1||filePath.indexOf(":")>2)){ 
     75                                var brPos = document.location.pathname.lastIndexOf("/"); 
     76                                filePath = "file://" + document.location.pathname.substr(0,brPos) + "/" + filePath; 
     77                        } 
     78                        // Return filePath 
     79                        return filePath; 
    4680                } 
    4781        }); 
    4882 
     
    186220                                        out.close(); 
    187221                                        return true; 
    188222                                } catch(ex) { 
    189                                         alert("Exception while attempting to save\n\n" + ex); 
     223                                        //# alert("Exception while attempting to save\n\n" + ex); 
    190224                                        return false; 
    191225                                } 
    192226                        } 
     
    210244                        var r; 
    211245                        try { 
    212246                                if(document.applets["TiddlySaver"]) { 
    213                                         r = document.applets["TiddlySaver"].loadFile(javaUrlToFilename(filePath),"UTF-8"); 
     247                                        r = document.applets["TiddlySaver"].loadFile(filePath,"UTF-8"); 
    214248                                        return (r === undefined || r === null) ? null : String(r); 
    215249                                } 
    216250                        } catch(ex) { 
     
    220254                saveFile: function(filePath,content) { 
    221255                        try { 
    222256                                if(document.applets["TiddlySaver"]) 
    223                                         return document.applets["TiddlySaver"].saveFile(javaUrlToFilename(filePath),"UTF-8",content); 
     257                                        return document.applets["TiddlySaver"].saveFile(filePath,"UTF-8",content); 
    224258                        } catch(ex) { 
    225259                        } 
    226260                        return null; 
     
    238272                        var r; 
    239273                        var content = []; 
    240274                        try { 
    241                                 r = new java.io.BufferedReader(new java.io.FileReader(javaUrlToFilename(filePath))); 
     275                                r = new java.io.BufferedReader(new java.io.FileReader(filePath)); 
    242276                                var line; 
    243277                                while((line = r.readLine()) != null) 
    244278                                        content.push(new String(line)); 
     
    250284                }, 
    251285                saveFile: function(filePath,content) { 
    252286                        try { 
    253                                 var s = new java.io.PrintStream(new java.io.FileOutputStream(javaUrlToFilename(filePath))); 
     287                                var s = new java.io.PrintStream(new java.io.FileOutputStream(filePath)); 
    254288                                s.print(content); 
    255289                                s.close(); 
    256290                        } catch(ex) { 
     
    260294                } 
    261295        } 
    262296 
    263         // Private utilities 
    264  
    265         function javaUrlToFilename(url) { 
    266                 var f = "//localhost"; 
    267                 if(url.indexOf(f) == 0) 
    268                         return url.substring(f.length); 
    269                 var i = url.indexOf(":"); 
    270                 return i > 0 ? url.substring(i-1) : url; 
    271         } 
    272  
    273297})(jQuery);