Ticket #1104: ticket1104_jquery.twFile.2.2.patch
| File ticket1104_jquery.twFile.2.2.patch, 5.1 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 … … 33 33 this.getDriver(); 34 34 }, 35 35 load: function(filePath) { 36 return this.getDriver().loadFile( filePath);36 return this.getDriver().loadFile($.twFile.getFilePath(filePath)); 37 37 }, 38 38 save: function(filePath,content) { 39 if(!content){ 40 content = filePath; 41 filePath = $.twFile.getFilePath() 42 } else { 43 filePath = $.twFile.getFilePath(filePath) 44 } 39 45 return this.getDriver().saveFile(filePath,content); 40 46 }, 41 47 copy: function(dest,source) { … … 43 49 return this.currentDriver.copyFile(dest,source); 44 50 else 45 51 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; 46 86 } 47 87 }); 48 88 … … 70 110 } 71 111 return true; 72 112 }, 113 73 114 loadFile: function(filePath) { 74 115 // Returns null if it can't do it, false if there's an error, or a string of the content if successful 75 116 try { … … 78 119 var content = file.ReadAll(); 79 120 file.Close(); 80 121 } catch(ex) { 81 //#alert("Exception while attempting to load\n\n" + ex.toString());122 alert("Exception while attempting to load\n\n" + ex.toString()); 82 123 return null; 83 124 } 84 125 return content; … … 162 203 inputStream.close(); 163 204 return result.join(""); 164 205 } catch(ex) { 165 //#alert("Exception while attempting to load\n\n" + ex);206 alert("Exception while attempting to load\n\n" + ex); 166 207 return false; 167 208 } 168 209 } … … 186 227 out.close(); 187 228 return true; 188 229 } catch(ex) { 189 alert("Exception while attempting to save\n\n" + ex);230 //# alert("Exception while attempting to save\n\n" + ex); 190 231 return false; 191 232 } 192 233 } … … 210 251 var r; 211 252 try { 212 253 if(document.applets["TiddlySaver"]) { 213 r = document.applets["TiddlySaver"].loadFile( javaUrlToFilename(filePath),"UTF-8");254 r = document.applets["TiddlySaver"].loadFile(filePath,"UTF-8"); 214 255 return (r === undefined || r === null) ? null : String(r); 215 256 } 216 257 } catch(ex) { … … 220 261 saveFile: function(filePath,content) { 221 262 try { 222 263 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); 224 265 } catch(ex) { 225 266 } 226 267 return null; … … 238 279 var r; 239 280 var content = []; 240 281 try { 241 r = new java.io.BufferedReader(new java.io.FileReader( javaUrlToFilename(filePath)));282 r = new java.io.BufferedReader(new java.io.FileReader(filePath)); 242 283 var line; 243 284 while((line = r.readLine()) != null) 244 285 content.push(new String(line)); … … 250 291 }, 251 292 saveFile: function(filePath,content) { 252 293 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)); 254 295 s.print(content); 255 296 s.close(); 256 297 } catch(ex) { … … 260 301 } 261 302 } 262 303 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 304 })(jQuery);
