1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 12:07:19 +00:00

Start using the server connection tiddler to store host details

This commit is contained in:
Jeremy Ruston 2012-11-17 21:15:19 +00:00
parent 5d10cd2ae3
commit 4742ab14fc
3 changed files with 43 additions and 12 deletions

View File

@ -711,7 +711,22 @@ exports.initServerConnections = function() {
this.serverConnections = {}; this.serverConnections = {};
var self = this; var self = this;
$tw.modules.forEachModuleOfType("serverconnection",function(title,module) { $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
};
}
}
}
}); });
}; };

View File

@ -4,4 +4,5 @@ module-type: serverconnection
syncer: tiddlywebsyncer syncer: tiddlywebsyncer
# replace with a tiddlyspace to which you have write access # replace with a tiddlyspace to which you have write access
server: http://tw5tiddlyweb.tiddlyspace.com/ host: http://tw5tiddlyweb.tiddlyspace.com/
recipe: tw5tiddlyweb_public

View File

@ -16,13 +16,7 @@ Main TiddlyWeb integration module
Creates a TiddlyWebSyncer object Creates a TiddlyWebSyncer object
*/ */
var TiddlyWebSyncer = function(options) { var TiddlyWebSyncer = function(options) {
// Mark us as not logged in this.connection = undefined;
$tw.wiki.addTiddler({
title: TiddlyWebSyncer.titleIsLoggedIn,
text: "no"
});
// Get the login status
this.getStatus();
}; };
TiddlyWebSyncer.titleIsLoggedIn = "$:/plugins/tiddlyweb/IsLoggedIn"; TiddlyWebSyncer.titleIsLoggedIn = "$:/plugins/tiddlyweb/IsLoggedIn";
@ -36,6 +30,27 @@ TiddlyWebSyncer.prototype.showError = function(error) {
console.log("TiddlyWeb error: " + 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 Handle syncer messages
*/ */
@ -74,7 +89,7 @@ TiddlyWebSyncer.prototype.getCsrfToken = function() {
TiddlyWebSyncer.prototype.getStatus = function(callback) { TiddlyWebSyncer.prototype.getStatus = function(callback) {
// Get status // Get status
this.httpRequest({ this.httpRequest({
url: "http://tw5tiddlyweb.tiddlyspace.com/status", url: this.connection.host + "status",
callback: function(err,data) { callback: function(err,data) {
// Decode the status JSON // Decode the status JSON
var json = null; var json = null;
@ -134,7 +149,7 @@ Attempt to login to TiddlyWeb.
TiddlyWebSyncer.prototype.login = function(username,password,callback) { TiddlyWebSyncer.prototype.login = function(username,password,callback) {
var self = this; var self = this;
var httpRequest = this.httpRequest({ 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", type: "POST",
data: { data: {
user: username, user: username,
@ -164,7 +179,7 @@ TiddlyWebSyncer.prototype.logout = function(options) {
options = options || {}; options = options || {};
var self = this; var self = this;
var httpRequest = this.httpRequest({ var httpRequest = this.httpRequest({
url: "http://tw5tiddlyweb.tiddlyspace.com/logout", url: this.connection.host + "logout",
type: "POST", type: "POST",
data: { data: {
csrf_token: this.getCsrfToken(), csrf_token: this.getCsrfToken(),