Ticket #1104: ticket1104_jquery.twFile.2.2.patch

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

Resolved some minor layout issues

  • .js

    old new  
    1 /* 
     1/* 
    22jQuery.twFile.js 
    33 
    44jQuery plugin for loading a file and saving data to a file 
     
    3333                        this.getDriver(); 
    3434                }, 
    3535                load: function(filePath) { 
    36                         return this.getDriver().loadFile(filePath); 
     36                        return this.getDriver().loadFile($.twFile.getFilePath(filePath)); 
    3737                }, 
    3838                save: function(filePath,content) { 
     39                        if(!content){ 
     40                                content = filePath; 
     41                                filePath = $.twFile.getFilePath() 
     42                        } else { 
     43                                filePath = $.twFile.getFilePath(filePath) 
     44                        } 
    3945                        return this.getDriver().saveFile(filePath,content); 
    4046                }, 
    4147                copy: function(dest,source) { 
     
    4349                                return this.currentDriver.copyFile(dest,source); 
    4450                        else 
    4551                                return false; 
     52                }, 
     53                getFilePath: function(filePath){        // helper function to retrieve requested file's path 
     54                        // If blank then assume the file requested is the current file 
     55                        if(!filePath){ 
     56                                filePath = document.location.href; 
     57                        } 
     58                        filePath = unescape(filePath);          // Unescape the filePath 
     59                        // Remove any location or query part of the URL 
     60                        var hashPos = filePath.indexOf("#"); 
     61                        var argPos = filePath.indexOf("?"); 
     62                        var nPos = Math.min((hashPos+1) ? hashPos : filePath.length, (argPos+1) ? argPos : filePath.length); 
     63                        var info = filePath.substr(nPos); 
     64                        filePath = filePath.substr(0,nPos); 
     65                        // Convert file://localhost/ to file:/// 
     66                        if(filePath.indexOf("file://localhost/") == 0) 
     67                        filePath = "file://" + filePath.substr(16); 
     68                        // Convert to a native file format 
     69                        if(filePath.charAt(9) == ":") // pc local file 
     70                                filePath = filePath.substr(8).replace(new RegExp("/","g"),"\\"); 
     71                        else if(filePath.indexOf("file://///") == 0) // FireFox pc network file 
     72                                filePath = "\\\\" + filePath.substr(10).replace(new RegExp("/","g"),"\\"); 
     73                        else if(filePath.indexOf("file:///") == 0) // mac/unix local file 
     74                                filePath = filePath.substr(7); 
     75                        else if(filePath.indexOf("file:/") == 0) // mac/unix local file 
     76                                filePath = filePath.substr(5); 
     77                        else if(filePath.indexOf("//") == 0) // pc network file 
     78                                filePath = "\\\\" + filePath.substr(7).replace(new RegExp("/","g"),"\\"); 
     79                        // If relative then append pathname 
     80                        if(filePath.indexOf("/")!=0&&(filePath.indexOf(":")==-1||filePath.indexOf(":")>2)){ 
     81                                var brPos = document.location.pathname.lastIndexOf("/"); 
     82                                filePath = "file://" + document.location.pathname.substr(0,brPos) + "/" + filePath; 
     83                        } 
     84                        // Return filePath 
     85                        return filePath; 
    4686                } 
    4787        }); 
    4888 
     
    70110                        } 
    71111                        return true; 
    72112                }, 
     113 
    73114                loadFile: function(filePath) { 
    74115                        // Returns null if it can't do it, false if there's an error, or a string of the content if successful 
    75116                        try { 
     
    78119                                var content = file.ReadAll(); 
    79120                                file.Close(); 
    80121                        } catch(ex) { 
    81                                 //# alert("Exception while attempting to load\n\n" + ex.toString()); 
     122                                alert("Exception while attempting to load\n\n" + ex.toString()); 
    82123                                return null; 
    83124                        } 
    84125                        return content; 
     
    162203                                        inputStream.close(); 
    163204                                        return result.join(""); 
    164205                                } catch(ex) { 
    165                                         //# alert("Exception while attempting to load\n\n" + ex); 
     206                                        alert("Exception while attempting to load\n\n" + ex); 
    166207                                        return false; 
    167208                                } 
    168209                        } 
     
    186227                                        out.close(); 
    187228                                        return true; 
    188229                                } catch(ex) { 
    189                                         alert("Exception while attempting to save\n\n" + ex); 
     230                                        //# alert("Exception while attempting to save\n\n" + ex); 
    190231                                        return false; 
    191232                                } 
    192233                        } 
     
    210251                        var r; 
    211252                        try { 
    212253                                if(document.applets["TiddlySaver"]) { 
    213                                         r = document.applets["TiddlySaver"].loadFile(javaUrlToFilename(filePath),"UTF-8"); 
     254                                        r = document.applets["TiddlySaver"].loadFile(filePath,"UTF-8"); 
    214255                                        return (r === undefined || r === null) ? null : String(r); 
    215256                                } 
    216257                        } catch(ex) { 
     
    220261                saveFile: function(filePath,content) { 
    221262                        try { 
    222263                                if(document.applets["TiddlySaver"]) 
    223                                         return document.applets["TiddlySaver"].saveFile(javaUrlToFilename(filePath),"UTF-8",content); 
     264                                        return document.applets["TiddlySaver"].saveFile(filePath,"UTF-8",content); 
    224265                        } catch(ex) { 
    225266                        } 
    226267                        return null; 
     
    238279                        var r; 
    239280                        var content = []; 
    240281                        try { 
    241                                 r = new java.io.BufferedReader(new java.io.FileReader(javaUrlToFilename(filePath))); 
     282                                r = new java.io.BufferedReader(new java.io.FileReader(filePath)); 
    242283                                var line; 
    243284                                while((line = r.readLine()) != null) 
    244285                                        content.push(new String(line)); 
     
    250291                }, 
    251292                saveFile: function(filePath,content) { 
    252293                        try { 
    253                                 var s = new java.io.PrintStream(new java.io.FileOutputStream(javaUrlToFilename(filePath))); 
     294                                var s = new java.io.PrintStream(new java.io.FileOutputStream(filePath)); 
    254295                                s.print(content); 
    255296                                s.close(); 
    256297                        } catch(ex) { 
     
    260301                } 
    261302        } 
    262303 
    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  
    273304})(jQuery);