From a5d2a3a4e893c139f3a563bb3f8f19c87106a187 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sun, 18 Nov 2012 10:24:20 +0000 Subject: [PATCH] Start bringing tiddlers back from TiddlyWeb --- editions/tw5tiddlyweb/tiddlers/HelloThere.tid | 3 + plugins/tiddlywiki/tiddlyweb/tiddlyweb.js | 65 ++++++++++++++----- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/editions/tw5tiddlyweb/tiddlers/HelloThere.tid b/editions/tw5tiddlyweb/tiddlers/HelloThere.tid index 190f2c72b..5d46c86a5 100644 --- a/editions/tw5tiddlyweb/tiddlers/HelloThere.tid +++ b/editions/tw5tiddlyweb/tiddlers/HelloThere.tid @@ -13,3 +13,6 @@ TiddlyWeb [[login status|$:/plugins/tiddlyweb/IsLoggedIn]]: (($:/plugins/tiddlyw TiddlyWeb [[username|$:/plugins/tiddlyweb/UserName]]: (($:/plugins/tiddlyweb/UserName)) +All tiddlers: + +<> diff --git a/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js b/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js index 7524ecf61..12f24f151 100644 --- a/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js +++ b/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js @@ -16,6 +16,7 @@ Main TiddlyWeb integration module Creates a TiddlyWebSyncer object */ var TiddlyWebSyncer = function(options) { + this.wiki = options.wiki; this.connection = undefined; }; @@ -40,14 +41,16 @@ TiddlyWebSyncer.prototype.addConnection = function(connection) { return Error("Missing connection data") } // Mark us as not logged in - $tw.wiki.addTiddler({ - title: TiddlyWebSyncer.titleIsLoggedIn, - text: "no" - }); + $tw.wiki.addTiddler({title: TiddlyWebSyncer.titleIsLoggedIn,text: "no"}); // Save and return the connection object this.connection = connection; // Get the login status - this.getStatus(); + var self = this; + this.getStatus(function (err,isLoggedIn,json) { + if(isLoggedIn) { + self.syncFromServer(); + } + }); return ""; // We only support a single connection }; @@ -91,6 +94,9 @@ TiddlyWebSyncer.prototype.getStatus = function(callback) { this.httpRequest({ url: this.connection.host + "status", callback: function(err,data) { + if(err) { + return callback(err); + } // Decode the status JSON var json = null; try { @@ -101,22 +107,16 @@ TiddlyWebSyncer.prototype.getStatus = function(callback) { // Check if we're logged in var isLoggedIn = json.username !== "GUEST"; // Set the various status tiddlers - $tw.wiki.addTiddler({ - title: TiddlyWebSyncer.titleIsLoggedIn, - text: isLoggedIn ? "yes" : "no" - }); + $tw.wiki.addTiddler({title: TiddlyWebSyncer.titleIsLoggedIn,text: isLoggedIn ? "yes" : "no"}); if(isLoggedIn) { - $tw.wiki.addTiddler({ - title: TiddlyWebSyncer.titleUserName, - text: json.username - }); + $tw.wiki.addTiddler({title: TiddlyWebSyncer.titleUserName,text: json.username}); } else { $tw.wiki.deleteTiddler(TiddlyWebSyncer.titleUserName); } } // Invoke the callback if present if(callback) { - callback(isLoggedIn,json); + callback(null,isLoggedIn,json); } } }); @@ -132,7 +132,9 @@ TiddlyWebSyncer.prototype.promptLogin = function() { $tw.passwordPrompt.createPrompt({ serviceName: "Login to TiddlySpace", callback: function(data) { - self.login(data.username,data.password); + self.login(data.username,data.password,function(err,isLoggedIn) { + self.syncFromServer(); + }); return true; // Get rid of the password prompt } }); @@ -162,7 +164,7 @@ TiddlyWebSyncer.prototype.login = function(username,password,callback) { callback(err); } } else { - self.getStatus(function(isLoggedIn,json) { + self.getStatus(function(err,isLoggedIn,json) { if(callback) { callback(null,isLoggedIn); } @@ -195,6 +197,37 @@ TiddlyWebSyncer.prototype.logout = function(options) { }); }; +/* +Synchronise from the server by reading the tiddler list from the recipe and queuing up GETs for any tiddlers that we don't already have +*/ +TiddlyWebSyncer.prototype.syncFromServer = function() { + var self = this; + this.httpRequest({ + url: this.connection.host + "recipes/" + this.connection.recipe + "/tiddlers.json", + callback: function(err,data) { + if(err) { +console.log("error in syncFromServer",err); + return; + } + var json = JSON.parse(data); + for(var t=0; t