mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-10-26 05:07:39 +00:00
Support for lazy loaded tiddlers
This commit is contained in:
@@ -585,7 +585,7 @@ $tw.Wiki.prototype.getTiddler = function(title) {
|
|||||||
} else if($tw.utils.hop(this.bundledTiddlers,title)) {
|
} else if($tw.utils.hop(this.bundledTiddlers,title)) {
|
||||||
return this.bundledTiddlers[title];
|
return this.bundledTiddlers[title];
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return undefined;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,11 @@ exports.executeMacro = function() {
|
|||||||
value;
|
value;
|
||||||
// Get the value to display
|
// Get the value to display
|
||||||
if(tiddler) {
|
if(tiddler) {
|
||||||
value = tiddler.fields[field];
|
if(field === "text") {
|
||||||
|
value = this.wiki.getTiddlerText(this.tiddlerTitle);
|
||||||
|
} else {
|
||||||
|
value = tiddler.fields[field];
|
||||||
|
}
|
||||||
} else { // Use a special value if the tiddler is missing
|
} else { // Use a special value if the tiddler is missing
|
||||||
switch(field) {
|
switch(field) {
|
||||||
case "text":
|
case "text":
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ exports.isTemporary = function() {
|
|||||||
exports.getFieldString = function(field) {
|
exports.getFieldString = function(field) {
|
||||||
var value = this.fields[field];
|
var value = this.fields[field];
|
||||||
// Check for a missing field
|
// Check for a missing field
|
||||||
if(value === undefined) {
|
if(value === undefined || value === null) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
// Parse the field with the associated module (if any)
|
// Parse the field with the associated module (if any)
|
||||||
|
|||||||
@@ -730,6 +730,17 @@ exports.initServerConnections = function() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Invoke all the active server connections
|
||||||
|
*/
|
||||||
|
exports.invokeServerConnections = function(method /* ,args */) {
|
||||||
|
var args = Array.prototype.slice.call(arguments,1);
|
||||||
|
for(var title in this.serverConnections) {
|
||||||
|
var connection = this.serverConnections[title];
|
||||||
|
connection.syncer[method].apply(connection.syncer,[connection.connection].concat(args));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Handle a syncer message
|
Handle a syncer message
|
||||||
*/
|
*/
|
||||||
@@ -739,4 +750,24 @@ exports.handleSyncerEvent = function(event) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Trigger a load for a tiddler if it is skinny. Returns the text, or undefined if the tiddler is missing, null if the tiddler is being lazily loaded.
|
||||||
|
*/
|
||||||
|
exports.getTiddlerText = function(title) {
|
||||||
|
var tiddler = this.getTiddler(title);
|
||||||
|
// Return undefined if the tiddler isn't found
|
||||||
|
if(!tiddler) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
if(tiddler.fields.text) {
|
||||||
|
// Just return the text if we've got it
|
||||||
|
return tiddler.fields.text;
|
||||||
|
} else {
|
||||||
|
// Ask all the server connections to load the tiddler if they can
|
||||||
|
this.invokeServerConnections("lazyLoad",title,tiddler);
|
||||||
|
// Indicate that the text is being loaded
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -197,6 +197,26 @@ TiddlyWebSyncer.prototype.logout = function(options) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Convert a TiddlyWeb JSON tiddler into a TiddlyWiki5 tiddler
|
||||||
|
*/
|
||||||
|
TiddlyWebSyncer.prototype.convertTiddler = function(tiddlerFields) {
|
||||||
|
var result = {};
|
||||||
|
for(var f in tiddlerFields) {
|
||||||
|
switch(f) {
|
||||||
|
case "fields":
|
||||||
|
for(var ff in tiddlerFields[f]) {
|
||||||
|
result[ff] = tiddlerFields[f][ff];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
result[f] = tiddlerFields[f];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
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
|
||||||
*/
|
*/
|
||||||
@@ -211,34 +231,42 @@ console.log("error in syncFromServer",err);
|
|||||||
}
|
}
|
||||||
var json = JSON.parse(data);
|
var json = JSON.parse(data);
|
||||||
for(var t=0; t<json.length; t++) {
|
for(var t=0; t<json.length; t++) {
|
||||||
var jsonTiddler = json[t],
|
self.wiki.addTiddler(new $tw.Tiddler(self.convertTiddler(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:
|
Lazily load a skinny tiddler if we can
|
||||||
|
*/
|
||||||
|
TiddlyWebSyncer.prototype.lazyLoad = function(connection,title,tiddler) {
|
||||||
|
var self = this;
|
||||||
|
this.httpRequest({
|
||||||
|
url: this.connection.host + "recipes/" + this.connection.recipe + "/tiddlers/" + title,
|
||||||
|
callback: function(err,data) {
|
||||||
|
if(err) {
|
||||||
|
console.log("error in lazyLoad",err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.wiki.addTiddler(new $tw.Tiddler(self.convertTiddler(JSON.parse(data))));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
A quick and dirty HTTP function; to be refactored later. Options are:
|
||||||
url: URL to retrieve
|
url: URL to retrieve
|
||||||
type: GET, PUT, POST etc
|
type: GET, PUT, POST etc
|
||||||
callback: function invoked with (err,data)
|
callback: function invoked with (err,data)
|
||||||
*/
|
*/
|
||||||
TiddlyWebSyncer.prototype.httpRequest = function(options) {
|
TiddlyWebSyncer.prototype.httpRequest = function(options) {
|
||||||
var type = options.type || "GET",
|
var type = options.type || "GET",
|
||||||
|
headers = options.headers || {accept: "application/json"},
|
||||||
client = new XMLHttpRequest(),
|
client = new XMLHttpRequest(),
|
||||||
data = "",
|
data = "",
|
||||||
f,results;
|
f,results;
|
||||||
|
// Massage the data hashmap into a string
|
||||||
if(options.data) {
|
if(options.data) {
|
||||||
if(typeof options.data === "string") { // Already a string
|
if(typeof options.data === "string") { // Already a string
|
||||||
data = options.data;
|
data = options.data;
|
||||||
@@ -252,6 +280,7 @@ TiddlyWebSyncer.prototype.httpRequest = function(options) {
|
|||||||
data = results.join("&")
|
data = results.join("&")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Set up the state change handler
|
||||||
client.onreadystatechange = function() {
|
client.onreadystatechange = function() {
|
||||||
if(this.readyState === 4) {
|
if(this.readyState === 4) {
|
||||||
if(this.status === 200) {
|
if(this.status === 200) {
|
||||||
@@ -263,7 +292,13 @@ TiddlyWebSyncer.prototype.httpRequest = function(options) {
|
|||||||
options.callback(new Error("XMLHttpRequest error: " + this.status));
|
options.callback(new Error("XMLHttpRequest error: " + this.status));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
// Make the request
|
||||||
client.open(type,options.url,true);
|
client.open(type,options.url,true);
|
||||||
|
if(headers) {
|
||||||
|
for(var h in headers) {
|
||||||
|
client.setRequestHeader(h,headers[h]);
|
||||||
|
}
|
||||||
|
}
|
||||||
if(data) {
|
if(data) {
|
||||||
client.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");
|
client.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user