1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-06-05 16:14:07 +00:00

Minor refactorings and reorderings

This commit is contained in:
Jeremy Ruston 2012-11-19 14:18:21 +00:00
parent b8fcc9f0c0
commit e2d6c16f50

View File

@ -83,26 +83,19 @@ TiddlyWebSyncer.prototype.handleEvent = function(event) {
}; };
/* /*
Invoke any tiddlyweb-startup modules Lazily load a skinny tiddler if we can
*/ */
TiddlyWebSyncer.prototype.invokeTiddlyWebStartupModules = function(loggedIn) { TiddlyWebSyncer.prototype.lazyLoad = function(connection,title,tiddler) {
$tw.modules.forEachModuleOfType("tiddlyweb-startup",function(title,module) { // Queue up a sync task to load this tiddler
module.startup(loggedIn); this.enqueueSyncTask({
type: "load",
title: title
}); });
};
TiddlyWebSyncer.prototype.getCsrfToken = function() {
var regex = /^(?:.*; )?csrf_token=([^(;|$)]*)(?:;|$)/,
match = regex.exec(document.cookie),
csrf = null;
if (match && (match.length === 2)) {
csrf = match[1];
}
return csrf;
}; };
/*
Get the current status of the TiddlyWeb connection
*/
TiddlyWebSyncer.prototype.getStatus = function(callback) { TiddlyWebSyncer.prototype.getStatus = function(callback) {
// Get status // Get status
var self = this; var self = this;
@ -113,14 +106,15 @@ TiddlyWebSyncer.prototype.getStatus = function(callback) {
return callback(err); return callback(err);
} }
// Decode the status JSON // Decode the status JSON
var json = null; var json = null,
isLoggedIn = false;
try { try {
json = JSON.parse(data); json = JSON.parse(data);
} catch (e) { } catch (e) {
} }
if(json) { if(json) {
// Check if we're logged in // Check if we're logged in
var isLoggedIn = json.username !== "GUEST"; isLoggedIn = json.username !== "GUEST";
// Set the various status tiddlers // Set the various status tiddlers
self.wiki.addTiddler({title: TiddlyWebSyncer.titleIsLoggedIn,text: isLoggedIn ? "yes" : "no"}); self.wiki.addTiddler({title: TiddlyWebSyncer.titleIsLoggedIn,text: isLoggedIn ? "yes" : "no"});
if(isLoggedIn) { if(isLoggedIn) {
@ -212,45 +206,6 @@ TiddlyWebSyncer.prototype.logout = function(options) {
}); });
}; };
/*
Convert a TiddlyWeb JSON tiddler into a TiddlyWiki5 tiddler and save it in the store. Returns true if the tiddler was actually stored
*/
TiddlyWebSyncer.prototype.storeTiddler = function(tiddlerFields,revision) {
var self = this,
result = {};
// Don't update if we've already got this revision
if(this.tiddlerInfo[tiddlerFields.title] && this.tiddlerInfo[tiddlerFields.title].revision === revision) {
return false;
}
// Transfer the fields, pulling down the `fields` hashmap
$tw.utils.each(tiddlerFields,function(element,title,object) {
switch(title) {
case "fields":
$tw.utils.each(element,function(element,subTitle,object) {
result[subTitle] = element;
});
break;
default:
result[title] = tiddlerFields[title];
break;
}
});
// Some unholy freaking of content types
if(result.type === "text/javascript") {
result.type = "application/javascript";
} else if(!result.type || result.type === "None") {
result.type = "text/vnd.tiddlywiki2";
}
// Save the tiddler
self.wiki.addTiddler(new $tw.Tiddler(self.wiki.getTiddler(result.title),result));
// Save the tiddler revision and changeCount details
self.tiddlerInfo[result.title] = {
revision: revision,
changeCount: self.wiki.getChangeCount(result.title)
};
return true;
};
/* /*
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 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
*/ */
@ -469,6 +424,42 @@ TiddlyWebSyncer.prototype.dispatchTask = function(task,callback) {
} }
}; };
/*
Convert a TiddlyWeb JSON tiddler into a TiddlyWiki5 tiddler and save it in the store. Returns true if the tiddler was actually stored
*/
TiddlyWebSyncer.prototype.storeTiddler = function(tiddlerFields,revision) {
var self = this,
result = {};
// Don't update if we've already got this revision
if(this.tiddlerInfo[tiddlerFields.title] && this.tiddlerInfo[tiddlerFields.title].revision === revision) {
return false;
}
// Transfer the fields, pulling down the `fields` hashmap
$tw.utils.each(tiddlerFields,function(element,title,object) {
if(title === "fields") {
$tw.utils.each(element,function(element,subTitle,object) {
result[subTitle] = element;
});
} else {
result[title] = tiddlerFields[title];
}
});
// Some unholy freaking of content types
if(result.type === "text/javascript") {
result.type = "application/javascript";
} else if(!result.type || result.type === "None") {
result.type = "text/vnd.tiddlywiki2";
}
// Save the tiddler
self.wiki.addTiddler(new $tw.Tiddler(self.wiki.getTiddler(result.title),result));
// Save the tiddler revision and changeCount details
self.tiddlerInfo[result.title] = {
revision: revision,
changeCount: self.wiki.getChangeCount(result.title)
};
return true;
};
/* /*
Convert a tiddler to a field set suitable for PUTting to TiddlyWeb Convert a tiddler to a field set suitable for PUTting to TiddlyWeb
*/ */
@ -506,17 +497,6 @@ TiddlyWebSyncer.prototype.getRevisionFromEtag = function(request) {
} }
}; };
/*
Lazily load a skinny tiddler if we can
*/
TiddlyWebSyncer.prototype.lazyLoad = function(connection,title,tiddler) {
// Queue up a sync task to load this tiddler
this.enqueueSyncTask({
type: "load",
title: title
});
};
/* /*
A quick and dirty HTTP function; to be refactored later. Options are: A quick and dirty HTTP function; to be refactored later. Options are:
url: URL to retrieve url: URL to retrieve
@ -535,29 +515,29 @@ TiddlyWebSyncer.prototype.httpRequest = function(options) {
data = options.data; data = options.data;
} else { // A hashmap of strings } else { // A hashmap of strings
results = []; results = [];
$tw.utils.each(options.data,function(element,title,object) { $tw.utils.each(options.data,function(dataItem,dataItemTitle) {
results.push(title + "=" + encodeURIComponent(element)); results.push(dataItemTitle + "=" + encodeURIComponent(dataItem));
}); });
data = results.join("&") data = results.join("&");
} }
} }
// Set up the state change handler // Set up the state change handler
request.onreadystatechange = function() { request.onreadystatechange = function() {
if(this.readyState === 4) { if(this.readyState === 4) {
if(this.status === 200) { if(this.status === 200) {
// success! // Success!
options.callback(null,this.responseText,this); options.callback(null,this.responseText,this);
return; return;
} }
// something went wrong // Something went wrong
options.callback(new Error("XMLHttpRequest error: " + this.status)); options.callback(new Error("XMLHttpRequest error: " + this.status));
} }
}; };
// Make the request // Make the request
request.open(type,options.url,true); request.open(type,options.url,true);
if(headers) { if(headers) {
$tw.utils.each(headers,function(element,title,object) { $tw.utils.each(headers,function(header,headerTitle,object) {
request.setRequestHeader(title,element); request.setRequestHeader(headerTitle,header);
}); });
} }
if(data && !$tw.utils.hop(headers,"Content-type")) { if(data && !$tw.utils.hop(headers,"Content-type")) {
@ -567,6 +547,20 @@ TiddlyWebSyncer.prototype.httpRequest = function(options) {
return request; return request;
}; };
/*
Retrieve the CSRF token from its cookie
*/
TiddlyWebSyncer.prototype.getCsrfToken = function() {
var regex = /^(?:.*; )?csrf_token=([^(;|$)]*)(?:;|$)/,
match = regex.exec(document.cookie),
csrf = null;
if (match && (match.length === 2)) {
csrf = match[1];
}
return csrf;
};
// Only export anything on the browser // Only export anything on the browser
if($tw.browser) { if($tw.browser) {
exports.name = "tiddlywebsyncer"; exports.name = "tiddlywebsyncer";