Ticket #1104: ticket1104_jquery.twFile.3.patch
| File ticket1104_jquery.twFile.3.patch, 3.9 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 … … 43 43 return this.currentDriver.copyFile(dest,source); 44 44 else 45 45 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; 46 80 } 47 81 }); 48 82 … … 186 220 out.close(); 187 221 return true; 188 222 } catch(ex) { 189 alert("Exception while attempting to save\n\n" + ex);223 //# alert("Exception while attempting to save\n\n" + ex); 190 224 return false; 191 225 } 192 226 } … … 210 244 var r; 211 245 try { 212 246 if(document.applets["TiddlySaver"]) { 213 r = document.applets["TiddlySaver"].loadFile( javaUrlToFilename(filePath),"UTF-8");247 r = document.applets["TiddlySaver"].loadFile(filePath,"UTF-8"); 214 248 return (r === undefined || r === null) ? null : String(r); 215 249 } 216 250 } catch(ex) { … … 220 254 saveFile: function(filePath,content) { 221 255 try { 222 256 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); 224 258 } catch(ex) { 225 259 } 226 260 return null; … … 238 272 var r; 239 273 var content = []; 240 274 try { 241 r = new java.io.BufferedReader(new java.io.FileReader( javaUrlToFilename(filePath)));275 r = new java.io.BufferedReader(new java.io.FileReader(filePath)); 242 276 var line; 243 277 while((line = r.readLine()) != null) 244 278 content.push(new String(line)); … … 250 284 }, 251 285 saveFile: function(filePath,content) { 252 286 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)); 254 288 s.print(content); 255 289 s.close(); 256 290 } catch(ex) { … … 260 294 } 261 295 } 262 296 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 297 })(jQuery);
