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 /* 2 2 jQuery.twFile.js 3 3 4 4 jQuery plugin for loading a file and saving data to a file … … 10 10 http://www.opensource.org/licenses/mit-license.php 11 11 http://www.gnu.org/licenses/gpl.html 12 12 */ 13 14 13 (function($) { 15 14 if(!$.twFile) { 16 15 $.twFile = {}; … … 20 19 currentDriver: null, 21 20 driverList: ["activeX", "mozilla", "tiddlySaver", "javaLiveConnect"], 22 21 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 },35 22 load: function(filePath) { 36 return this.getDriver().loadFile( filePath);23 return this.getDriver().loadFile($.twFile.getFilePath(filePath)); 37 24 }, 38 25 save: function(filePath,content) { 26 if(!content){content=filePath;filePath=$.twFile.getFilePath()}else{filePath=$.twFile.getFilePath(filePath)} 39 27 return this.getDriver().saveFile(filePath,content); 40 28 }, 41 29 copy: function(dest,source) { … … 43 31 return this.currentDriver.copyFile(dest,source); 44 32 else 45 33 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; 46 81 } 47 82 }); 48 83 49 84 // Deferred initialisation for any drivers that need it 50 85 $(function() { 51 86 for(var t in drivers) { … … 78 113 var content = file.ReadAll(); 79 114 file.Close(); 80 115 } catch(ex) { 81 //# alert("Exception while attempting to load\n\n" + ex.toString());116 //# alert("Exception while attempting to load\n\n" + ex.toString()); 82 117 return null; 83 118 } 84 119 return content; … … 186 221 out.close(); 187 222 return true; 188 223 } catch(ex) { 189 alert("Exception while attempting to save\n\n" + ex);224 //# alert("Exception while attempting to save\n\n" + ex); 190 225 return false; 191 226 } 192 227 } … … 210 245 var r; 211 246 try { 212 247 if(document.applets["TiddlySaver"]) { 213 r = document.applets["TiddlySaver"].loadFile( javaUrlToFilename(filePath),"UTF-8");248 r = document.applets["TiddlySaver"].loadFile(filePath,"UTF-8"); 214 249 return (r === undefined || r === null) ? null : String(r); 215 250 } 216 251 } catch(ex) { … … 220 255 saveFile: function(filePath,content) { 221 256 try { 222 257 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); 224 259 } catch(ex) { 225 260 } 226 261 return null; … … 238 273 var r; 239 274 var content = []; 240 275 try { 241 r = new java.io.BufferedReader(new java.io.FileReader( javaUrlToFilename(filePath)));276 r = new java.io.BufferedReader(new java.io.FileReader(filePath)); 242 277 var line; 243 278 while((line = r.readLine()) != null) 244 279 content.push(new String(line)); … … 250 285 }, 251 286 saveFile: function(filePath,content) { 252 287 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)); 254 289 s.print(content); 255 290 s.close(); 256 291 } catch(ex) { … … 260 295 } 261 296 } 262 297 263 // Private utilities264 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 273 298 })(jQuery);
