Ticket #1104: ticket1104_jquery.twFile.patch

File ticket1104_jquery.twFile.patch, 5.8 KB (added by hugheth, 3 years ago)
  • .js

    old new  
    1 /* 
     1/* 
    22jQuery.twFile.js 
    33 
    44jQuery plugin for loading a file and saving data to a file 
     
    1010  http://www.opensource.org/licenses/mit-license.php 
    1111  http://www.gnu.org/licenses/gpl.html 
    1212*/ 
    13  
    1413(function($) { 
    1514        if(!$.twFile) { 
    1615                $.twFile = {}; 
     
    2019                currentDriver: null, 
    2120                driverList: ["activeX", "mozilla", "tiddlySaver", "javaLiveConnect"], 
    2221 
    23                 getDriver: function() { 
    24                         if(this.currentDriver === null) { 
    25                                 for(var t=0; t<this.driverList.length; t++) { 
    26                                         if(this.currentDriver === null && drivers[this.driverList[t]].isAvailable && drivers[this.driverList[t]].isAvailable()) 
    27                                                 this.currentDriver = drivers[this.driverList[t]]; 
    28                                 } 
    29                         } 
    30                         return this.currentDriver; 
    31                 }, 
    32                 init: function() { 
    33                         this.getDriver(); 
    34                 }, 
    3522                load: function(filePath) { 
    36                         return this.getDriver().loadFile(filePath); 
     23                        return this.getDriver().loadFile($.twFile.getFilePath(filePath)); 
    3724                }, 
    3825                save: function(filePath,content) { 
     26                        if(!content){content=filePath;filePath=$.twFile.getFilePath()}else{filePath=$.twFile.getFilePath(filePath)} 
    3927                        return this.getDriver().saveFile(filePath,content); 
    4028                }, 
    4129                copy: function(dest,source) { 
     
    4331                                return this.currentDriver.copyFile(dest,source); 
    4432                        else 
    4533                                return false; 
     34                }, 
     35                getDriver: function() { 
     36                        if(this.currentDriver === null) { 
     37                                for(var t=0; t<this.driverList.length; t++) { 
     38                                        if(this.currentDriver === null && drivers[this.driverList[t]].isAvailable && drivers[this.driverList[t]].isAvailable()) 
     39                                                this.currentDriver = drivers[this.driverList[t]]; 
     40                                } 
     41                        } 
     42                        return this.currentDriver; 
     43                }, 
     44                getFilePath: function(fPath){   // helper function to retrieve requested file's path 
     45                        // If blank then assume the file requested is the current file 
     46                        if(!fPath){ 
     47                                fPath = document.location.href; 
     48                        } 
     49                        fPath = unescape(fPath);                // Unescape the fPath 
     50                        //var origPath = convertUriToUTF8(fPath,config.options.txtFileSystemCharSet); 
     51                        var origPath = fPath; 
     52                        // Remove any location or query part of the URL 
     53                        var hashPos = fPath.indexOf("#"); 
     54                        var argPos = fPath.indexOf("?"); 
     55                        var nPos = Math.min((hashPos+1)?hashPos:fPath.length,(argPos+1)?argPos:fPath.length); 
     56                        var info = origPath.substr(nPos); 
     57                        origPath = origPath.substr(0,nPos); 
     58                        // Convert file://localhost/ to file:/// 
     59                        if(origPath.indexOf("file://localhost/") == 0) 
     60                        origPath = "file://" + origPath.substr(16); 
     61                        // Convert to a native file format 
     62                        var localPath = origPath; 
     63                        if(origPath.charAt(9) == ":") // pc local file 
     64                                localPath = origPath.substr(8).replace(new RegExp("/","g"),"\\"); 
     65                        else if(origPath.indexOf("file://///") == 0) // FireFox pc network file 
     66                                localPath = "\\\\" + origPath.substr(10).replace(new RegExp("/","g"),"\\"); 
     67                        else if(origPath.indexOf("file:///") == 0) // mac/unix local file 
     68                                localPath = origPath.substr(7); 
     69                        else if(origPath.indexOf("file:/") == 0) // mac/unix local file 
     70                                localPath = origPath.substr(5); 
     71                        else if(origPath.indexOf("//") == 0) // pc network file 
     72                                localPath = "\\\\" + unescape(origPath.substr(7)).replace(new RegExp("/","g"),"\\"); 
     73                        // If relative then append pathname 
     74                        if(localPath.indexOf("/")!=0&&(localPath.indexOf(":")==-1||localPath.indexOf(":")>2)){ 
     75                                var brPos = document.location.pathname.lastIndexOf("/"); 
     76                                localPath = "file://" + document.location.pathname.substr(0,brPos) + "/" + localPath; 
     77                        } 
     78                        // Append original information to the path 
     79                        // Return localPath 
     80                        return localPath; 
    4681                } 
    4782        }); 
    48  
     83         
    4984        // Deferred initialisation for any drivers that need it 
    5085        $(function() { 
    5186                for(var t in drivers) { 
     
    78113                                var content = file.ReadAll(); 
    79114                                file.Close(); 
    80115                        } catch(ex) { 
    81                                 //# alert("Exception while attempting to load\n\n" + ex.toString()); 
     116                                 //# alert("Exception while attempting to load\n\n" + ex.toString()); 
    82117                                return null; 
    83118                        } 
    84119                        return content; 
     
    186221                                        out.close(); 
    187222                                        return true; 
    188223                                } catch(ex) { 
    189                                         alert("Exception while attempting to save\n\n" + ex); 
     224                                        //# alert("Exception while attempting to save\n\n" + ex); 
    190225                                        return false; 
    191226                                } 
    192227                        } 
     
    210245                        var r; 
    211246                        try { 
    212247                                if(document.applets["TiddlySaver"]) { 
    213                                         r = document.applets["TiddlySaver"].loadFile(javaUrlToFilename(filePath),"UTF-8"); 
     248                                        r = document.applets["TiddlySaver"].loadFile(filePath,"UTF-8"); 
    214249                                        return (r === undefined || r === null) ? null : String(r); 
    215250                                } 
    216251                        } catch(ex) { 
     
    220255                saveFile: function(filePath,content) { 
    221256                        try { 
    222257                                if(document.applets["TiddlySaver"]) 
    223                                         return document.applets["TiddlySaver"].saveFile(javaUrlToFilename(filePath),"UTF-8",content); 
     258                                        return document.applets["TiddlySaver"].saveFile(filePath,"UTF-8",content); 
    224259                        } catch(ex) { 
    225260                        } 
    226261                        return null; 
     
    238273                        var r; 
    239274                        var content = []; 
    240275                        try { 
    241                                 r = new java.io.BufferedReader(new java.io.FileReader(javaUrlToFilename(filePath))); 
     276                                r = new java.io.BufferedReader(new java.io.FileReader(filePath)); 
    242277                                var line; 
    243278                                while((line = r.readLine()) != null) 
    244279                                        content.push(new String(line)); 
     
    250285                }, 
    251286                saveFile: function(filePath,content) { 
    252287                        try { 
    253                                 var s = new java.io.PrintStream(new java.io.FileOutputStream(javaUrlToFilename(filePath))); 
     288                                var s = new java.io.PrintStream(new java.io.FileOutputStream(filePath)); 
    254289                                s.print(content); 
    255290                                s.close(); 
    256291                        } catch(ex) { 
     
    260295                } 
    261296        } 
    262297 
    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  
    273298})(jQuery);