mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-24 00:50:28 +00:00
Introduce syncer mechanism
This commit is contained in:
parent
5c9b0d62c4
commit
e194aed449
@ -34,9 +34,10 @@ exports.startup = function() {
|
||||
$tw.wiki.initMacros();
|
||||
$tw.wiki.initEditors();
|
||||
$tw.wiki.initFieldViewers();
|
||||
$tw.wiki.initStoryViews();
|
||||
$tw.wiki.initListViews();
|
||||
$tw.wiki.initParsers();
|
||||
$tw.wiki.initSyncers();
|
||||
$tw.wiki.initServerConnections();
|
||||
// Set up the command modules
|
||||
$tw.Commander.initCommands();
|
||||
// Host-specific startup
|
||||
@ -57,6 +58,12 @@ exports.startup = function() {
|
||||
document.addEventListener("tw-modal",function(event) {
|
||||
$tw.modal.display(event.param);
|
||||
},false);
|
||||
// Install the syncer message mechanism
|
||||
var handleSyncerEvent = function(event) {
|
||||
$tw.wiki.handleSyncerEvent.call($tw.wiki,event);
|
||||
};
|
||||
document.addEventListener("tw-login",handleSyncerEvent,false);
|
||||
document.addEventListener("tw-logout",handleSyncerEvent,false);
|
||||
// Install the scroller
|
||||
$tw.scroller = new $tw.utils.Scroller();
|
||||
// Install the sprite factory
|
||||
@ -100,7 +107,7 @@ exports.startup = function() {
|
||||
$tw.renderer.refreshInDom(changes);
|
||||
});
|
||||
} else {
|
||||
// Start a commander with the command line arguments
|
||||
// On the server, start a commander with the command line arguments
|
||||
commander = new $tw.Commander(
|
||||
Array.prototype.slice.call(process.argv,2),
|
||||
function(err) {
|
||||
|
@ -557,18 +557,6 @@ exports.initListViews = function(moduleType) {
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
Install view modules for the story macro
|
||||
*/
|
||||
exports.initStoryViews = function(moduleType) {
|
||||
moduleType = moduleType || "storyview";
|
||||
var storyMacro = this.macros.story;
|
||||
if(storyMacro) {
|
||||
storyMacro.viewers = {};
|
||||
$tw.modules.applyMethods(moduleType,storyMacro.viewers);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
Select the appropriate saver modules and set them up
|
||||
*/
|
||||
@ -701,4 +689,39 @@ exports.search = function(text,options) {
|
||||
return results;
|
||||
};
|
||||
|
||||
/*
|
||||
Initialise syncers
|
||||
*/
|
||||
exports.initSyncers = function() {
|
||||
this.syncers = {};
|
||||
var self = this;
|
||||
$tw.modules.forEachModuleOfType("syncer",function(title,module) {
|
||||
if(module.name && module.syncer) {
|
||||
self.syncers[module.name] = new module.syncer({
|
||||
wiki: self
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/*
|
||||
Initialise server connections
|
||||
*/
|
||||
exports.initServerConnections = function() {
|
||||
this.serverConnections = {};
|
||||
var self = this;
|
||||
$tw.modules.forEachModuleOfType("serverconnection",function(title,module) {
|
||||
self.serverConnections[title] = module;
|
||||
});
|
||||
};
|
||||
|
||||
/*
|
||||
Handle a syncer message
|
||||
*/
|
||||
exports.handleSyncerEvent = function(event) {
|
||||
for(var syncer in this.syncers) {
|
||||
this.syncers[syncer].handleEvent(event);
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
||||
|
@ -3,10 +3,10 @@ title: HelloThere
|
||||
Experimenting with TiddlyWeb integration for TiddlyWiki5.
|
||||
|
||||
<<reveal state:[[$:/plugins/tiddlyweb/IsLoggedIn]] type:nomatch text:yes><
|
||||
Log in to TiddlyWeb: <<tiddlyweb.login><Login>>
|
||||
Log in to TiddlyWeb: <<button message:login><Login>>
|
||||
>>
|
||||
<<reveal state:[[$:/plugins/tiddlyweb/IsLoggedIn]] type:match text:yes><
|
||||
Log out of TiddlyWeb: <<tiddlyweb.logout><Logout>>
|
||||
Log out of TiddlyWeb: <<button message:logout><Logout>>
|
||||
>>
|
||||
|
||||
TiddlyWeb [[login status|$:/plugins/tiddlyweb/IsLoggedIn]]: (($:/plugins/tiddlyweb/IsLoggedIn))
|
||||
|
@ -2,5 +2,6 @@ title: TiddlyWebConnection
|
||||
type: application/x-tiddler-dictionary
|
||||
module-type: serverconnection
|
||||
|
||||
protocol: http
|
||||
host: tiddlyspace.com
|
||||
syncer: tiddlywebsyncer
|
||||
# replace with a tiddlyspace to which you have write access
|
||||
server: http://tw5tiddlyweb.tiddlyspace.com/
|
||||
|
@ -1,41 +0,0 @@
|
||||
/*\
|
||||
title: $:/plugins/tiddlywiki/tiddlyweb/loginmacro.js
|
||||
type: application/javascript
|
||||
module-type: macro
|
||||
|
||||
TiddlyWeb login plugin
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
exports.info = {
|
||||
name: "tiddlyweb.login",
|
||||
params: {}
|
||||
};
|
||||
|
||||
exports.handleEvent = function (event) {
|
||||
if(event.type === "click") {
|
||||
$tw.plugins.tiddlyweb.promptLogin();
|
||||
}
|
||||
};
|
||||
|
||||
exports.executeMacro = function() {
|
||||
// Create the link
|
||||
var child = $tw.Tree.Element(
|
||||
"a",
|
||||
null,
|
||||
this.content,
|
||||
{
|
||||
events: ["click"],
|
||||
eventHandler: this
|
||||
}
|
||||
);
|
||||
child.execute(this.parents,this.tiddlerTitle);
|
||||
return child;
|
||||
};
|
||||
|
||||
})();
|
@ -1,41 +0,0 @@
|
||||
/*\
|
||||
title: $:/plugins/tiddlywiki/tiddlyweb/logoutmacro.js
|
||||
type: application/javascript
|
||||
module-type: macro
|
||||
|
||||
TiddlyWeb logout plugin
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
exports.info = {
|
||||
name: "tiddlyweb.logout",
|
||||
params: {}
|
||||
};
|
||||
|
||||
exports.handleEvent = function (event) {
|
||||
if(event.type === "click") {
|
||||
$tw.plugins.tiddlyweb.logout();
|
||||
}
|
||||
};
|
||||
|
||||
exports.executeMacro = function() {
|
||||
// Create the link
|
||||
var child = $tw.Tree.Element(
|
||||
"a",
|
||||
null,
|
||||
this.content,
|
||||
{
|
||||
events: ["click"],
|
||||
eventHandler: this
|
||||
}
|
||||
);
|
||||
child.execute(this.parents,this.tiddlerTitle);
|
||||
return child;
|
||||
};
|
||||
|
||||
})();
|
@ -1,7 +1,7 @@
|
||||
/*\
|
||||
title: $:/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js
|
||||
type: application/javascript
|
||||
module-type: browser-startup
|
||||
module-type: syncer
|
||||
|
||||
Main TiddlyWeb integration module
|
||||
|
||||
@ -12,45 +12,55 @@ Main TiddlyWeb integration module
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
$tw.plugins.tiddlyweb = {
|
||||
titleIsLoggedIn: "$:/plugins/tiddlyweb/IsLoggedIn",
|
||||
titleUserName: "$:/plugins/tiddlyweb/UserName"
|
||||
};
|
||||
|
||||
/*
|
||||
Startup function that sets up TiddlyWeb and logs the user in. After login, any tiddlyweb-startup modules are executed.
|
||||
Creates a TiddlyWebSyncer object
|
||||
*/
|
||||
exports.startup = function() {
|
||||
if(!$tw.browser) {
|
||||
return;
|
||||
}
|
||||
var TiddlyWebSyncer = function(options) {
|
||||
// Mark us as not logged in
|
||||
$tw.wiki.addTiddler({
|
||||
title: $tw.plugins.tiddlyweb.titleIsLoggedIn,
|
||||
title: TiddlyWebSyncer.titleIsLoggedIn,
|
||||
text: "no"
|
||||
});
|
||||
// Get the login status
|
||||
$tw.plugins.tiddlyweb.getStatus();
|
||||
this.getStatus();
|
||||
};
|
||||
|
||||
TiddlyWebSyncer.titleIsLoggedIn = "$:/plugins/tiddlyweb/IsLoggedIn";
|
||||
TiddlyWebSyncer.titleUserName = "$:/plugins/tiddlyweb/UserName";
|
||||
|
||||
/*
|
||||
Error handling
|
||||
*/
|
||||
$tw.plugins.tiddlyweb.showError = function(error) {
|
||||
TiddlyWebSyncer.prototype.showError = function(error) {
|
||||
alert("TiddlyWeb error: " + error);
|
||||
console.log("TiddlyWeb error: " + error);
|
||||
};
|
||||
|
||||
/*
|
||||
Handle syncer messages
|
||||
*/
|
||||
TiddlyWebSyncer.prototype.handleEvent = function(event) {
|
||||
switch(event.type) {
|
||||
case "tw-login":
|
||||
this.promptLogin();
|
||||
break;
|
||||
case "tw-logout":
|
||||
this.logout();
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
Invoke any tiddlyweb-startup modules
|
||||
*/
|
||||
$tw.plugins.tiddlyweb.invokeTiddlyWebStartupModules = function(loggedIn) {
|
||||
TiddlyWebSyncer.prototype.invokeTiddlyWebStartupModules = function(loggedIn) {
|
||||
$tw.modules.forEachModuleOfType("tiddlyweb-startup",function(title,module) {
|
||||
module.startup(loggedIn);
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
$tw.plugins.tiddlyweb.getCsrfToken = function() {
|
||||
TiddlyWebSyncer.prototype.getCsrfToken = function() {
|
||||
var regex = /^(?:.*; )?csrf_token=([^(;|$)]*)(?:;|$)/,
|
||||
match = regex.exec(document.cookie),
|
||||
csrf = null;
|
||||
@ -58,11 +68,12 @@ $tw.plugins.tiddlyweb.getCsrfToken = function() {
|
||||
csrf = match[1];
|
||||
}
|
||||
return csrf;
|
||||
|
||||
};
|
||||
|
||||
$tw.plugins.tiddlyweb.getStatus = function(callback) {
|
||||
TiddlyWebSyncer.prototype.getStatus = function(callback) {
|
||||
// Get status
|
||||
$tw.plugins.tiddlyweb.httpRequest({
|
||||
this.httpRequest({
|
||||
url: "http://tw5tiddlyweb.tiddlyspace.com/status",
|
||||
callback: function(err,data) {
|
||||
// Decode the status JSON
|
||||
@ -76,16 +87,16 @@ $tw.plugins.tiddlyweb.getStatus = function(callback) {
|
||||
var isLoggedIn = json.username !== "GUEST";
|
||||
// Set the various status tiddlers
|
||||
$tw.wiki.addTiddler({
|
||||
title: $tw.plugins.tiddlyweb.titleIsLoggedIn,
|
||||
title: TiddlyWebSyncer.titleIsLoggedIn,
|
||||
text: isLoggedIn ? "yes" : "no"
|
||||
});
|
||||
if(isLoggedIn) {
|
||||
$tw.wiki.addTiddler({
|
||||
title: $tw.plugins.tiddlyweb.titleUserName,
|
||||
title: TiddlyWebSyncer.titleUserName,
|
||||
text: json.username
|
||||
});
|
||||
} else {
|
||||
$tw.wiki.deleteTiddler($tw.plugins.tiddlyweb.titleUserName);
|
||||
$tw.wiki.deleteTiddler(TiddlyWebSyncer.titleUserName);
|
||||
}
|
||||
}
|
||||
// Invoke the callback if present
|
||||
@ -99,13 +110,14 @@ $tw.plugins.tiddlyweb.getStatus = function(callback) {
|
||||
/*
|
||||
Dispay a password prompt and allow the user to login
|
||||
*/
|
||||
$tw.plugins.tiddlyweb.promptLogin = function() {
|
||||
$tw.plugins.tiddlyweb.getStatus(function(isLoggedIn,json) {
|
||||
TiddlyWebSyncer.prototype.promptLogin = function() {
|
||||
var self = this;
|
||||
this.getStatus(function(isLoggedIn,json) {
|
||||
if(!isLoggedIn) {
|
||||
$tw.passwordPrompt.createPrompt({
|
||||
serviceName: "Login to TiddlySpace",
|
||||
callback: function(data) {
|
||||
$tw.plugins.tiddlyweb.login(data.username,data.password);
|
||||
self.login(data.username,data.password);
|
||||
return true; // Get rid of the password prompt
|
||||
}
|
||||
});
|
||||
@ -119,8 +131,9 @@ Attempt to login to TiddlyWeb.
|
||||
password: password
|
||||
callback: invoked with arguments (err,isLoggedIn)
|
||||
*/
|
||||
$tw.plugins.tiddlyweb.login = function(username,password,callback) {
|
||||
var httpRequest = $tw.plugins.tiddlyweb.httpRequest({
|
||||
TiddlyWebSyncer.prototype.login = function(username,password,callback) {
|
||||
var self = this;
|
||||
var httpRequest = this.httpRequest({
|
||||
url: "http://tw5tiddlyweb.tiddlyspace.com/challenge/tiddlywebplugins.tiddlyspace.cookie_form",
|
||||
type: "POST",
|
||||
data: {
|
||||
@ -134,7 +147,7 @@ $tw.plugins.tiddlyweb.login = function(username,password,callback) {
|
||||
callback(err);
|
||||
}
|
||||
} else {
|
||||
$tw.plugins.tiddlyweb.getStatus(function(isLoggedIn,json) {
|
||||
self.getStatus(function(isLoggedIn,json) {
|
||||
if(callback) {
|
||||
callback(null,isLoggedIn);
|
||||
}
|
||||
@ -147,20 +160,21 @@ $tw.plugins.tiddlyweb.login = function(username,password,callback) {
|
||||
/*
|
||||
Attempt to log out of TiddlyWeb
|
||||
*/
|
||||
$tw.plugins.tiddlyweb.logout = function(options) {
|
||||
TiddlyWebSyncer.prototype.logout = function(options) {
|
||||
options = options || {};
|
||||
var httpRequest = $tw.plugins.tiddlyweb.httpRequest({
|
||||
var self = this;
|
||||
var httpRequest = this.httpRequest({
|
||||
url: "http://tw5tiddlyweb.tiddlyspace.com/logout",
|
||||
type: "POST",
|
||||
data: {
|
||||
csrf_token: $tw.plugins.tiddlyweb.getCsrfToken(),
|
||||
csrf_token: this.getCsrfToken(),
|
||||
tiddlyweb_redirect: "/status" // workaround to marginalize automatic subsequent GET
|
||||
},
|
||||
callback: function(err,data) {
|
||||
if(err) {
|
||||
console.log("logout error",err);
|
||||
} else {
|
||||
$tw.plugins.tiddlyweb.getStatus(function(isLoggedIn,json) {
|
||||
self.getStatus(function(isLoggedIn,json) {
|
||||
console.log("after logout, isLoggedIn",isLoggedIn);
|
||||
});
|
||||
console.log("Result of logout",data,httpRequest);
|
||||
@ -175,7 +189,7 @@ Some quick and dirty HTTP functions; to be refactored later. Options are:
|
||||
type: GET, PUT, POST etc
|
||||
callback: function invoked with (err,data)
|
||||
*/
|
||||
$tw.plugins.tiddlyweb.httpRequest = function(options) {
|
||||
TiddlyWebSyncer.prototype.httpRequest = function(options) {
|
||||
var type = options.type || "GET",
|
||||
client = new XMLHttpRequest(),
|
||||
data = "",
|
||||
@ -212,4 +226,10 @@ $tw.plugins.tiddlyweb.httpRequest = function(options) {
|
||||
return client;
|
||||
};
|
||||
|
||||
// Only export anything on the browser
|
||||
if($tw.browser) {
|
||||
exports.name = "tiddlywebsyncer";
|
||||
exports.syncer = TiddlyWebSyncer;
|
||||
}
|
||||
|
||||
})();
|
||||
|
Loading…
Reference in New Issue
Block a user