1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 18:17:20 +00:00

Start bringing tiddlers back from TiddlyWeb

This commit is contained in:
Jeremy Ruston 2012-11-18 10:24:20 +00:00
parent 4742ab14fc
commit a5d2a3a4e8
2 changed files with 52 additions and 16 deletions

View File

@ -13,3 +13,6 @@ TiddlyWeb [[login status|$:/plugins/tiddlyweb/IsLoggedIn]]: (($:/plugins/tiddlyw
TiddlyWeb [[username|$:/plugins/tiddlyweb/UserName]]: (($:/plugins/tiddlyweb/UserName)) TiddlyWeb [[username|$:/plugins/tiddlyweb/UserName]]: (($:/plugins/tiddlyweb/UserName))
All tiddlers:
<<list all>>

View File

@ -16,6 +16,7 @@ Main TiddlyWeb integration module
Creates a TiddlyWebSyncer object Creates a TiddlyWebSyncer object
*/ */
var TiddlyWebSyncer = function(options) { var TiddlyWebSyncer = function(options) {
this.wiki = options.wiki;
this.connection = undefined; this.connection = undefined;
}; };
@ -40,14 +41,16 @@ TiddlyWebSyncer.prototype.addConnection = function(connection) {
return Error("Missing connection data") return Error("Missing connection data")
} }
// Mark us as not logged in // Mark us as not logged in
$tw.wiki.addTiddler({ $tw.wiki.addTiddler({title: TiddlyWebSyncer.titleIsLoggedIn,text: "no"});
title: TiddlyWebSyncer.titleIsLoggedIn,
text: "no"
});
// Save and return the connection object // Save and return the connection object
this.connection = connection; this.connection = connection;
// Get the login status // 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 return ""; // We only support a single connection
}; };
@ -91,6 +94,9 @@ TiddlyWebSyncer.prototype.getStatus = function(callback) {
this.httpRequest({ this.httpRequest({
url: this.connection.host + "status", url: this.connection.host + "status",
callback: function(err,data) { callback: function(err,data) {
if(err) {
return callback(err);
}
// Decode the status JSON // Decode the status JSON
var json = null; var json = null;
try { try {
@ -101,22 +107,16 @@ TiddlyWebSyncer.prototype.getStatus = function(callback) {
// Check if we're logged in // Check if we're logged in
var isLoggedIn = json.username !== "GUEST"; var isLoggedIn = json.username !== "GUEST";
// Set the various status tiddlers // Set the various status tiddlers
$tw.wiki.addTiddler({ $tw.wiki.addTiddler({title: TiddlyWebSyncer.titleIsLoggedIn,text: isLoggedIn ? "yes" : "no"});
title: TiddlyWebSyncer.titleIsLoggedIn,
text: isLoggedIn ? "yes" : "no"
});
if(isLoggedIn) { if(isLoggedIn) {
$tw.wiki.addTiddler({ $tw.wiki.addTiddler({title: TiddlyWebSyncer.titleUserName,text: json.username});
title: TiddlyWebSyncer.titleUserName,
text: json.username
});
} else { } else {
$tw.wiki.deleteTiddler(TiddlyWebSyncer.titleUserName); $tw.wiki.deleteTiddler(TiddlyWebSyncer.titleUserName);
} }
} }
// Invoke the callback if present // Invoke the callback if present
if(callback) { if(callback) {
callback(isLoggedIn,json); callback(null,isLoggedIn,json);
} }
} }
}); });
@ -132,7 +132,9 @@ TiddlyWebSyncer.prototype.promptLogin = function() {
$tw.passwordPrompt.createPrompt({ $tw.passwordPrompt.createPrompt({
serviceName: "Login to TiddlySpace", serviceName: "Login to TiddlySpace",
callback: function(data) { 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 return true; // Get rid of the password prompt
} }
}); });
@ -162,7 +164,7 @@ TiddlyWebSyncer.prototype.login = function(username,password,callback) {
callback(err); callback(err);
} }
} else { } else {
self.getStatus(function(isLoggedIn,json) { self.getStatus(function(err,isLoggedIn,json) {
if(callback) { if(callback) {
callback(null,isLoggedIn); 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<json.length; t++) {
var jsonTiddler = json[t],
fields = {};
for(var f in jsonTiddler) {
switch(f) {
case "fields":
break;
default:
fields[f] = jsonTiddler[f];
break;
}
}
self.wiki.addTiddler(new $tw.Tiddler(fields));
}
}
});
};
/* /*
Some quick and dirty HTTP functions; to be refactored later. Options are: Some quick and dirty HTTP functions; to be refactored later. Options are:
url: URL to retrieve url: URL to retrieve