1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +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))
All tiddlers:
<<list all>>

View File

@ -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<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:
url: URL to retrieve