Ticket #669: ticket669.patch

File ticket669.patch, 6.0 KB (added by MartinBudden, 4 years ago)
  • js/FileAdaptor.js

     
    22//-- Server adaptor for talking to static TiddlyWiki files 
    33//-- 
    44 
    5 function FileAdaptor() 
     5function AdaptorBase() 
    66{ 
    77        this.host = null; 
    88        this.store = null; 
    99        return this; 
    1010} 
    1111 
    12 FileAdaptor.serverType = 'file'; 
    13 FileAdaptor.serverLabel = 'TiddlyWiki'; 
    14  
    15 FileAdaptor.prototype.setContext = function(context,userParams,callback) 
     12AdaptorBase.prototype.close = function() 
    1613{ 
    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; 
    2715}; 
    2816 
    29 FileAdaptor.fullHostName = function(host) 
     17AdaptorBase.prototype.fullHostName = function(host) 
    3018{ 
    3119        if(!host) 
    3220                return ''; 
     21        host = host.trim(); 
    3322        if(!host.match(/:\/\//)) 
    3423                host = 'http://' + host; 
     24        if(host.substr(host.length-1) != '/') 
     25                host = host + '/'; 
    3526        return host; 
    3627}; 
    3728 
    38 FileAdaptor.minHostName = function(host) 
     29AdaptorBase.minHostName = function(host) 
    3930{ 
    4031        return host ? host.replace(/^http:\/\//,'').replace(/\/$/,'') : ''; 
    4132}; 
    4233 
     34AdaptorBase.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 
    4348// Open the specified host 
    44 //#   host - url of host (eg, "http://www.tiddlywiki.com/" or "www.tiddlywiki.com") 
     49//#   host - uri of host (eg, "http://www.tiddlywiki.com/" or "www.tiddlywiki.com") 
    4550//#   context is itself passed on as a parameter to the callback function 
    4651//#   userParams - user settable object object that is passed on unchanged to the callback function 
    4752//#   callback - optional function to be called on completion 
     
    5156//#   context.status - true if OK, string if error 
    5257//#   context.adaptor - reference to this adaptor object 
    5358//#   userParams - parameters as originally passed into the openHost function 
    54 FileAdaptor.prototype.openHost = function(host,context,userParams,callback) 
     59AdaptorBase.prototype.openHost = function(host,context,userParams,callback) 
    5560{ 
    5661        this.host = host; 
    5762        context = this.setContext(context,userParams,callback); 
     
    6166        return true; 
    6267}; 
    6368 
     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 
     81AdaptorBase.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 
     91function FileAdaptor() {} 
     92 
     93FileAdaptor.prototype = new AdaptorBase(); 
     94 
     95FileAdaptor.serverType = 'file'; 
     96FileAdaptor.serverLabel = 'TiddlyWiki'; 
     97 
    6498FileAdaptor.loadTiddlyWikiCallback = function(status,context,responseText,url,xhr) 
    6599{ 
    66100        context.status = status; 
     
    96130        return true; 
    97131}; 
    98132 
    99 // Open the specified workspace 
    100 //#   workspace - name of workspace to open 
    101 //#   context - passed on as a parameter to the callback function 
    102 //#   userParams - user settable object object that is passed on unchanged to the callback function 
    103 //#   callback - function to be called on completion 
    104 //# Return value is true if the request was successfully issued 
    105 //#   or an error description string if there was a problem 
    106 //# The callback parameters are callback(context,userParams) 
    107 //#   context.status - true if OK, false if error 
    108 //#   context.statusText - error message if there was an error 
    109 //#   context.adaptor - reference to this adaptor object 
    110 //#   userParams - parameters as originally passed into the openWorkspace function 
    111 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  
    121133// Gets the list of tiddlers within a given workspace 
    122134//#   context - passed on as a parameter to the callback function 
    123135//#   userParams - user settable object object that is passed on unchanged to the callback function 
     
    158170                } 
    159171                for(var i=0; i<context.tiddlers.length; i++) { 
    160172                        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); 
    162174                        context.tiddlers[i].fields['server.page.revision'] = context.tiddlers[i].modified.convertToYYYYMMDDHHMM(); 
    163175                } 
    164176                context.status = true; 
     
    203215{ 
    204216        var t = context.adaptor.store.fetchTiddler(context.title); 
    205217        t.fields['server.type'] = FileAdaptor.serverType; 
    206         t.fields['server.host'] = FileAdaptor.minHostName(context.host); 
     218        t.fields['server.host'] = AdaptorBase.minHostName(context.host); 
    207219        t.fields['server.page.revision'] = t.modified.convertToYYYYMMDDHHMM(); 
    208220        context.tiddler = t; 
    209221        context.status = true;