Ticket #669: ticket669.patch
| File ticket669.patch, 6.0 KB (added by MartinBudden, 4 years ago) |
|---|
-
js/FileAdaptor.js
2 2 //-- Server adaptor for talking to static TiddlyWiki files 3 3 //-- 4 4 5 function FileAdaptor()5 function AdaptorBase() 6 6 { 7 7 this.host = null; 8 8 this.store = null; 9 9 return this; 10 10 } 11 11 12 FileAdaptor.serverType = 'file'; 13 FileAdaptor.serverLabel = 'TiddlyWiki'; 14 15 FileAdaptor.prototype.setContext = function(context,userParams,callback) 12 AdaptorBase.prototype.close = function() 16 13 { 17 if(!context) context = {}; 18 context.userParams = userParams; 19 if(callback) context.callback = callback; 20 context.adaptor = this; 21 if(!context.host) 22 context.host = this.host; 23 context.host = FileAdaptor.fullHostName(context.host); 24 if(!context.workspace) 25 context.workspace = this.workspace; 26 return context; 14 return true; 27 15 }; 28 16 29 FileAdaptor.fullHostName = function(host)17 AdaptorBase.prototype.fullHostName = function(host) 30 18 { 31 19 if(!host) 32 20 return ''; 21 host = host.trim(); 33 22 if(!host.match(/:\/\//)) 34 23 host = 'http://' + host; 24 if(host.substr(host.length-1) != '/') 25 host = host + '/'; 35 26 return host; 36 27 }; 37 28 38 FileAdaptor.minHostName = function(host)29 AdaptorBase.minHostName = function(host) 39 30 { 40 31 return host ? host.replace(/^http:\/\//,'').replace(/\/$/,'') : ''; 41 32 }; 42 33 34 AdaptorBase.prototype.setContext = function(context,userParams,callback) 35 { 36 if(!context) context = {}; 37 context.userParams = userParams; 38 if(callback) context.callback = callback; 39 context.adaptor = this; 40 if(!context.host) 41 context.host = this.host; 42 context.host = this.fullHostName(context.host); 43 if(!context.workspace) 44 context.workspace = this.workspace; 45 return context; 46 }; 47 43 48 // Open the specified host 44 //# host - ur lof host (eg, "http://www.tiddlywiki.com/" or "www.tiddlywiki.com")49 //# host - uri of host (eg, "http://www.tiddlywiki.com/" or "www.tiddlywiki.com") 45 50 //# context is itself passed on as a parameter to the callback function 46 51 //# userParams - user settable object object that is passed on unchanged to the callback function 47 52 //# callback - optional function to be called on completion … … 51 56 //# context.status - true if OK, string if error 52 57 //# context.adaptor - reference to this adaptor object 53 58 //# userParams - parameters as originally passed into the openHost function 54 FileAdaptor.prototype.openHost = function(host,context,userParams,callback)59 AdaptorBase.prototype.openHost = function(host,context,userParams,callback) 55 60 { 56 61 this.host = host; 57 62 context = this.setContext(context,userParams,callback); … … 61 66 return true; 62 67 }; 63 68 69 // Open the specified workspace 70 //# workspace - name of workspace to open 71 //# context - passed on as a parameter to the callback function 72 //# userParams - user settable object object that is passed on unchanged to the callback function 73 //# callback - function to be called on completion 74 //# Return value is true if the request was successfully issued 75 //# or an error description string if there was a problem 76 //# The callback parameters are callback(context,userParams) 77 //# context.status - true if OK, false if error 78 //# context.statusText - error message if there was an error 79 //# context.adaptor - reference to this adaptor object 80 //# userParams - parameters as originally passed into the openWorkspace function 81 AdaptorBase.prototype.openWorkspace = function(workspace,context,userParams,callback) 82 { 83 this.workspace = workspace; 84 context = this.setContext(context,userParams,callback); 85 context.status = true; 86 if(callback) 87 window.setTimeout(function() {callback(context,userParams);},10); 88 return true; 89 }; 90 91 function FileAdaptor() {} 92 93 FileAdaptor.prototype = new AdaptorBase(); 94 95 FileAdaptor.serverType = 'file'; 96 FileAdaptor.serverLabel = 'TiddlyWiki'; 97 64 98 FileAdaptor.loadTiddlyWikiCallback = function(status,context,responseText,url,xhr) 65 99 { 66 100 context.status = status; … … 96 130 return true; 97 131 }; 98 132 99 // Open the specified workspace100 //# workspace - name of workspace to open101 //# context - passed on as a parameter to the callback function102 //# userParams - user settable object object that is passed on unchanged to the callback function103 //# callback - function to be called on completion104 //# Return value is true if the request was successfully issued105 //# or an error description string if there was a problem106 //# The callback parameters are callback(context,userParams)107 //# context.status - true if OK, false if error108 //# context.statusText - error message if there was an error109 //# context.adaptor - reference to this adaptor object110 //# userParams - parameters as originally passed into the openWorkspace function111 FileAdaptor.prototype.openWorkspace = function(workspace,context,userParams,callback)112 {113 this.workspace = workspace;114 context = this.setContext(context,userParams,callback);115 context.status = true;116 if(callback)117 window.setTimeout(function() {callback(context,userParams);},10);118 return true;119 };120 121 133 // Gets the list of tiddlers within a given workspace 122 134 //# context - passed on as a parameter to the callback function 123 135 //# userParams - user settable object object that is passed on unchanged to the callback function … … 158 170 } 159 171 for(var i=0; i<context.tiddlers.length; i++) { 160 172 context.tiddlers[i].fields['server.type'] = FileAdaptor.serverType; 161 context.tiddlers[i].fields['server.host'] = FileAdaptor.minHostName(context.host);173 context.tiddlers[i].fields['server.host'] = AdaptorBase.minHostName(context.host); 162 174 context.tiddlers[i].fields['server.page.revision'] = context.tiddlers[i].modified.convertToYYYYMMDDHHMM(); 163 175 } 164 176 context.status = true; … … 203 215 { 204 216 var t = context.adaptor.store.fetchTiddler(context.title); 205 217 t.fields['server.type'] = FileAdaptor.serverType; 206 t.fields['server.host'] = FileAdaptor.minHostName(context.host);218 t.fields['server.host'] = AdaptorBase.minHostName(context.host); 207 219 t.fields['server.page.revision'] = t.modified.convertToYYYYMMDDHHMM(); 208 220 context.tiddler = t; 209 221 context.status = true;
