From 4742ab14fcde7460b66ac4ed8f96b701a03679b1 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sat, 17 Nov 2012 21:15:19 +0000 Subject: [PATCH] Start using the server connection tiddler to store host details --- core/modules/wiki.js | 17 ++++++++- .../tw5tiddlyweb/wiki/TiddlyWebConnection.tid | 3 +- plugins/tiddlywiki/tiddlyweb/tiddlyweb.js | 35 +++++++++++++------ 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/core/modules/wiki.js b/core/modules/wiki.js index adc484c32..6ac5b2683 100644 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -711,7 +711,22 @@ exports.initServerConnections = function() { this.serverConnections = {}; var self = this; $tw.modules.forEachModuleOfType("serverconnection",function(title,module) { - self.serverConnections[title] = module; + // Get the associated syncer + if(module.syncer) { + var syncer = self.syncers[module.syncer]; + if(syncer) { + // Add the connection and save information about it + var connection = syncer.addConnection(module); + if(connection instanceof Error) { + console.log("Error adding connection: " + connection); + } else { + self.serverConnections[title] = { + syncer: syncer, + connection: connection + }; + } + } + } }); }; diff --git a/editions/tw5tiddlyweb/wiki/TiddlyWebConnection.tid b/editions/tw5tiddlyweb/wiki/TiddlyWebConnection.tid index f6578ef89..3fcc9b4e9 100644 --- a/editions/tw5tiddlyweb/wiki/TiddlyWebConnection.tid +++ b/editions/tw5tiddlyweb/wiki/TiddlyWebConnection.tid @@ -4,4 +4,5 @@ module-type: serverconnection syncer: tiddlywebsyncer # replace with a tiddlyspace to which you have write access -server: http://tw5tiddlyweb.tiddlyspace.com/ +host: http://tw5tiddlyweb.tiddlyspace.com/ +recipe: tw5tiddlyweb_public diff --git a/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js b/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js index c5af0c245..7524ecf61 100644 --- a/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js +++ b/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js @@ -16,13 +16,7 @@ Main TiddlyWeb integration module Creates a TiddlyWebSyncer object */ var TiddlyWebSyncer = function(options) { - // Mark us as not logged in - $tw.wiki.addTiddler({ - title: TiddlyWebSyncer.titleIsLoggedIn, - text: "no" - }); - // Get the login status - this.getStatus(); + this.connection = undefined; }; TiddlyWebSyncer.titleIsLoggedIn = "$:/plugins/tiddlyweb/IsLoggedIn"; @@ -36,6 +30,27 @@ TiddlyWebSyncer.prototype.showError = function(error) { console.log("TiddlyWeb error: " + error); }; +TiddlyWebSyncer.prototype.addConnection = function(connection) { + // Check if we've already got a connection + if(this.connection) { + return Error("TiddlyWebSyncer can only handle a single connection"); + } + // Check the connection has its constituent parts + if(!connection.host || !connection.recipe) { + return Error("Missing connection data") + } + // Mark us as not logged in + $tw.wiki.addTiddler({ + title: TiddlyWebSyncer.titleIsLoggedIn, + text: "no" + }); + // Save and return the connection object + this.connection = connection; + // Get the login status + this.getStatus(); + return ""; // We only support a single connection +}; + /* Handle syncer messages */ @@ -74,7 +89,7 @@ TiddlyWebSyncer.prototype.getCsrfToken = function() { TiddlyWebSyncer.prototype.getStatus = function(callback) { // Get status this.httpRequest({ - url: "http://tw5tiddlyweb.tiddlyspace.com/status", + url: this.connection.host + "status", callback: function(err,data) { // Decode the status JSON var json = null; @@ -134,7 +149,7 @@ Attempt to login to TiddlyWeb. TiddlyWebSyncer.prototype.login = function(username,password,callback) { var self = this; var httpRequest = this.httpRequest({ - url: "http://tw5tiddlyweb.tiddlyspace.com/challenge/tiddlywebplugins.tiddlyspace.cookie_form", + url: this.connection.host + "challenge/tiddlywebplugins.tiddlyspace.cookie_form", type: "POST", data: { user: username, @@ -164,7 +179,7 @@ TiddlyWebSyncer.prototype.logout = function(options) { options = options || {}; var self = this; var httpRequest = this.httpRequest({ - url: "http://tw5tiddlyweb.tiddlyspace.com/logout", + url: this.connection.host + "logout", type: "POST", data: { csrf_token: this.getCsrfToken(),