1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 05:19:57 +00:00

Make shadowTiddlers, pluginTiddlers and pluginInfo be private to the Wiki object constructor

This commit is contained in:
Jermolene 2014-03-17 10:50:18 +00:00
parent 279626a3e3
commit 9de17aa206
6 changed files with 193 additions and 163 deletions

View File

@ -798,39 +798,56 @@ $tw.modules.define("$:/boot/tiddlerfields/list","tiddlerfield",{
/////////////////////////// Barebones wiki store /////////////////////////// Barebones wiki store
/* /*
Construct a wiki store object Wiki constructor. State is stored in private members that only a small number of privileged accessor methods have direct access. Methods added via the prototype have to use these accessors and cannot access the state data directly.
options include:
shadowTiddlers: Array of shadow tiddlers to be added
*/ */
$tw.Wiki = function() { $tw.Wiki = function(options) {
options = options || {};
var self = this, var self = this,
tiddlers = {}; // Hashmap of tiddlers tiddlers = {}, // Hashmap of tiddlers
this.pluginTiddlers = []; // Array of tiddlers containing registered plugins, ordered by priority pluginTiddlers = [], // Array of tiddlers containing registered plugins, ordered by priority
this.pluginInfo = {}; // Hashmap of parsed plugin content pluginInfo = {}, // Hashmap of parsed plugin content
this.shadowTiddlers = {}; // Hashmap by title of {source:, tiddler:} shadowTiddlers = options.shadowTiddlers || {}; // Hashmap by title of {source:, tiddler:}
// Methods that need access to the private members
// Add a tiddler to the store
this.addTiddler = function(tiddler) { this.addTiddler = function(tiddler) {
if(!(tiddler instanceof $tw.Tiddler)) { if(!(tiddler instanceof $tw.Tiddler)) {
tiddler = new $tw.Tiddler(tiddler); tiddler = new $tw.Tiddler(tiddler);
} }
// Get the title
var title = tiddler.fields.title;
// Save the tiddler // Save the tiddler
if(tiddler) {
var title = tiddler.fields.title;
if(title) { if(title) {
tiddlers[title] = tiddler; tiddlers[title] = tiddler;
this.clearCache(title); this.clearCache(title);
this.clearGlobalCache(); this.clearGlobalCache();
this.enqueueTiddlerEvent(title); this.enqueueTiddlerEvent(title);
} }
}
}; };
// Delete a tiddler
this.deleteTiddler = function(title) {
delete tiddlers[title];
this.clearCache(title);
this.clearGlobalCache();
this.enqueueTiddlerEvent(title,true);
};
// Get a tiddler from the store
this.getTiddler = function(title) { this.getTiddler = function(title) {
var t = tiddlers[title]; var t = tiddlers[title];
if(t instanceof $tw.Tiddler) { if(t instanceof $tw.Tiddler) {
return t; return t;
} else if(title !== undefined && $tw.utils.hop(self.shadowTiddlers,title)) { } else if(title !== undefined && $tw.utils.hop(shadowTiddlers,title)) {
return self.shadowTiddlers[title].tiddler; return shadowTiddlers[title].tiddler;
} else { } else {
return undefined; return undefined;
} }
}; };
// Get a hashmap of all tiddler titles
this.getAllTitles = function() { this.getAllTitles = function() {
var results = {}; var results = {};
for(var title in tiddlers) { for(var title in tiddlers) {
@ -838,55 +855,57 @@ $tw.Wiki = function() {
} }
return results; return results;
}; };
// Iterate through all tiddler titles
this.each = function(callback) { this.each = function(callback) {
for(var title in tiddlers) { for(var title in tiddlers) {
callback(tiddlers[title],title); callback(tiddlers[title],title);
} }
}; };
// Iterate through all shadow tiddler titles
this.eachShadow = function(callback) {
for(var title in shadowTiddlers) {
var shadowInfo = shadowTiddlers[title];
callback(shadowInfo.tiddler,title);
}
};
// Test for the existence of a tiddler
this.tiddlerExists = function(title) { this.tiddlerExists = function(title) {
return !!$tw.utils.hop(tiddlers,title); return !!$tw.utils.hop(tiddlers,title);
}; };
this.deleteTiddler = function(title) {
delete tiddlers[title]; // Determines if a tiddler is a shadow tiddler, regardless of whether it has been overridden by a real tiddler
this.clearCache(title); this.isShadowTiddler = function(title) {
this.clearGlobalCache(); return $tw.utils.hop(shadowTiddlers,title);
this.enqueueTiddlerEvent(title,true);
}; };
};
// Dummy methods that will be filled in after boot this.getShadowSource = function(title) {
$tw.Wiki.prototype.clearCache = if($tw.utils.hop(shadowTiddlers,title)) {
$tw.Wiki.prototype.clearGlobalCache = return shadowTiddlers[title].source;
$tw.Wiki.prototype.enqueueTiddlerEvent = function() {};
$tw.Wiki.prototype.addTiddlers = function(tiddlers) {
for(var t=0; t<tiddlers.length; t++) {
this.addTiddler(tiddlers[t]);
} }
}; return null;
};
/* // Read plugin info for all plugins
Read plugin info for all plugins this.readPluginInfo = function() {
*/ for(var title in tiddlers) {
$tw.Wiki.prototype.readPluginInfo = function() { var tiddler = tiddlers[title];
var self = this; if(tiddler.fields.type === "application/json" && tiddler.hasField("plugin-type")) {
this.each(function(tiddler,title) { pluginInfo[tiddler.fields.title] = JSON.parse(tiddler.fields.text);
if(tiddler && tiddler.fields.type === "application/json" && tiddler.hasField("plugin-type")) {
self.pluginInfo[tiddler.fields.title] = JSON.parse(tiddler.fields.text);
} }
});
};
/* }
Register the plugin tiddlers of a particular type, optionally restricting registration to an array of tiddler titles. Return the array of titles affected };
*/
$tw.Wiki.prototype.registerPluginTiddlers = function(pluginType,titles) { // Register the plugin tiddlers of a particular type, optionally restricting registration to an array of tiddler titles. Return the array of titles affected
this.registerPluginTiddlers = function(pluginType,titles) {
var self = this, var self = this,
registeredTitles = []; registeredTitles = [],
// Go through the provided titles, or the entire tiddler list, looking for plugins of this type checkTiddler = function(tiddler) {
var checkTiddler = function(tiddler) {
if(tiddler && tiddler.fields.type === "application/json" && tiddler.fields["plugin-type"] === pluginType) { if(tiddler && tiddler.fields.type === "application/json" && tiddler.fields["plugin-type"] === pluginType) {
self.pluginTiddlers.push(tiddler); pluginTiddlers.push(tiddler);
registeredTitles.push(tiddler.fields.title); registeredTitles.push(tiddler.fields.title);
} }
}; };
@ -900,32 +919,28 @@ $tw.Wiki.prototype.registerPluginTiddlers = function(pluginType,titles) {
}); });
} }
return registeredTitles; return registeredTitles;
}; };
/* // Unregister the plugin tiddlers of a particular type, returning an array of the titles affected
Unregister the plugin tiddlers of a particular type, returning an array of the titles affected this.unregisterPluginTiddlers = function(pluginType) {
*/
$tw.Wiki.prototype.unregisterPluginTiddlers = function(pluginType) {
var self = this, var self = this,
titles = []; titles = [];
// Remove any previous registered plugins of this type // Remove any previous registered plugins of this type
for(var t=this.pluginTiddlers.length-1; t>=0; t--) { for(var t=pluginTiddlers.length-1; t>=0; t--) {
var tiddler = this.pluginTiddlers[t]; var tiddler = pluginTiddlers[t];
if(tiddler.fields["plugin-type"] === pluginType) { if(tiddler.fields["plugin-type"] === pluginType) {
titles.push(tiddler.fields.title); titles.push(tiddler.fields.title);
this.pluginTiddlers.splice(t,1); pluginTiddlers.splice(t,1);
} }
} }
return titles; return titles;
}; };
/* // Unpack the currently registered plugins, creating shadow tiddlers for their constituent tiddlers
Unpack the currently registered plugins, creating shadow tiddlers for their constituent tiddlers this.unpackPluginTiddlers = function() {
*/
$tw.Wiki.prototype.unpackPluginTiddlers = function() {
var self = this; var self = this;
// Sort the plugin titles by the `plugin-priority` field // Sort the plugin titles by the `plugin-priority` field
this.pluginTiddlers.sort(function(a,b) { pluginTiddlers.sort(function(a,b) {
if("plugin-priority" in a.fields && "plugin-priority" in b.fields) { if("plugin-priority" in a.fields && "plugin-priority" in b.fields) {
return a.fields["plugin-priority"] - b.fields["plugin-priority"]; return a.fields["plugin-priority"] - b.fields["plugin-priority"];
} else if("plugin-priority" in a.fields) { } else if("plugin-priority" in a.fields) {
@ -941,19 +956,33 @@ $tw.Wiki.prototype.unpackPluginTiddlers = function() {
} }
}); });
// Now go through the plugins in ascending order and assign the shadows // Now go through the plugins in ascending order and assign the shadows
this.shadowTiddlers = {}; shadowTiddlers = {};
$tw.utils.each(this.pluginTiddlers,function(tiddler) { $tw.utils.each(pluginTiddlers,function(tiddler) {
// Extract the constituent tiddlers // Extract the constituent tiddlers
$tw.utils.each(self.pluginInfo[tiddler.fields.title].tiddlers,function(constituentTiddler,constituentTitle) { $tw.utils.each(pluginInfo[tiddler.fields.title].tiddlers,function(constituentTiddler,constituentTitle) {
// Save the tiddler object // Save the tiddler object
if(constituentTitle) { if(constituentTitle) {
self.shadowTiddlers[constituentTitle] = { shadowTiddlers[constituentTitle] = {
source: tiddler.fields.title, source: tiddler.fields.title,
tiddler: new $tw.Tiddler(constituentTiddler,{title: constituentTitle}) tiddler: new $tw.Tiddler(constituentTiddler,{title: constituentTitle})
}; };
} }
}); });
}); });
};
};
// Dummy methods that will be filled in after boot
$tw.Wiki.prototype.clearCache =
$tw.Wiki.prototype.clearGlobalCache =
$tw.Wiki.prototype.enqueueTiddlerEvent = function() {};
// Add an array of tiddlers
$tw.Wiki.prototype.addTiddlers = function(tiddlers) {
for(var t=0; t<tiddlers.length; t++) {
this.addTiddler(tiddlers[t]);
}
}; };
/* /*
@ -985,14 +1014,12 @@ Register all the module tiddlers that have a module type
*/ */
$tw.Wiki.prototype.defineShadowModules = function() { $tw.Wiki.prototype.defineShadowModules = function() {
var self = this; var self = this;
$tw.utils.each(this.shadowTiddlers,function(element,title) { this.eachShadow(function(tiddler,title) {
var tiddler = self.getTiddler(title); // Don't define the module if it is overidden by an ordinary tiddler
if(!self.tiddlerExists(title)) { // Don't define the module if it is overidden by an ordinary tiddler if(!self.tiddlerExists(title) && tiddler.hasField("module-type")) {
if(tiddler.hasField("module-type")) {
// Define the module // Define the module
$tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],tiddler.fields.text); $tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],tiddler.fields.text);
} }
}
}); });
}; };

View File

@ -34,7 +34,7 @@ exports.shadow = function(source,prefix,options) {
}); });
} else { } else {
if(prefix !== "!") { if(prefix !== "!") {
$tw.utils.each(options.wiki.shadowTiddlers,function(tiddler,title) { options.wiki.eachShadow(function(tiddler,title) {
results.push(title); results.push(title);
}); });
} else { } else {

View File

@ -18,9 +18,9 @@ Export our filter function
exports.shadowsource = function(source,operator,options) { exports.shadowsource = function(source,operator,options) {
var results = [], var results = [],
pushShadowSource = function(title) { pushShadowSource = function(title) {
var shadowInfo = options.wiki.shadowTiddlers[title]; var source = options.wiki.getShadowSource(title);
if(shadowInfo) { if(source) {
$tw.utils.pushTop(results,shadowInfo.source); $tw.utils.pushTop(results,source);
} }
}; };
// Iterate through the source tiddlers // Iterate through the source tiddlers

View File

@ -62,8 +62,9 @@ PluginSwitcher.prototype.switchPlugins = function() {
var unregisteredTiddlers = $tw.wiki.unregisterPluginTiddlers(this.pluginType); var unregisteredTiddlers = $tw.wiki.unregisterPluginTiddlers(this.pluginType);
// Accumulate the titles of shadow tiddlers that have changed as a result of this switch // Accumulate the titles of shadow tiddlers that have changed as a result of this switch
var changedTiddlers = {}; var changedTiddlers = {};
$tw.utils.each(this.wiki.shadowTiddlers,function(shadowInfo,title) { this.wiki.eachShadow(function(tiddler,title) {
if(unregisteredTiddlers.indexOf(shadowInfo.source) !== -1) { var source = self.wiki.getShadowSource(title);
if(unregisteredTiddlers.indexOf(source) !== -1) {
changedTiddlers[title] = true; // isDeleted? changedTiddlers[title] = true; // isDeleted?
} }
}); });
@ -72,8 +73,9 @@ PluginSwitcher.prototype.switchPlugins = function() {
// Unpack the current theme tiddlers // Unpack the current theme tiddlers
$tw.wiki.unpackPluginTiddlers(); $tw.wiki.unpackPluginTiddlers();
// Accumulate the affected shadow tiddlers // Accumulate the affected shadow tiddlers
$tw.utils.each(this.wiki.shadowTiddlers,function(shadowInfo,title) { this.wiki.eachShadow(function(tiddler,title) {
if(registeredTiddlers.indexOf(shadowInfo.source) !== -1) { var source = self.wiki.getShadowSource(title);
if(registeredTiddlers.indexOf(source) !== -1) {
changedTiddlers[title] = false; // isDeleted? changedTiddlers[title] = false; // isDeleted?
} }
}); });

View File

@ -178,13 +178,6 @@ exports.isTemporaryTiddler = function(title) {
return title.indexOf("$:/temp/") === 0; return title.indexOf("$:/temp/") === 0;
}; };
/*
Determines if a tiddler is a shadow tiddler, regardless of whether it has been overridden by a real tiddler
*/
exports.isShadowTiddler = function(title) {
return $tw.utils.hop(this.shadowTiddlers,title);
};
exports.isImageTiddler = function(title) { exports.isImageTiddler = function(title) {
var tiddler = this.getTiddler(title); var tiddler = this.getTiddler(title);
if(tiddler) { if(tiddler) {
@ -453,12 +446,12 @@ exports.getTagMap = function() {
}, },
title, tiddler; title, tiddler;
// Collect up all the tags // Collect up all the tags
for(title in self.shadowTiddlers) { self.eachShadow(function(tiddler,title) {
if(!self.tiddlerExists(title)) { if(!self.tiddlerExists(title)) {
tiddler = self.shadowTiddlers[title].tiddler; tiddler = self.getTiddler(title);
storeTags(tiddler.fields.tags,title); storeTags(tiddler.fields.tags,title);
} }
} });
self.each(function(tiddler,title) { self.each(function(tiddler,title) {
storeTags(tiddler.fields.tags,title); storeTags(tiddler.fields.tags,title);
}); });

View File

@ -15,13 +15,36 @@ Tests the filtering mechanism.
describe("Filter tests", function() { describe("Filter tests", function() {
// Create a wiki // Create a wiki
var wiki = new $tw.Wiki(); var wiki = new $tw.Wiki({
shadowTiddlers: {
// Some helpers "$:/TiddlerFive": {
var addShadowTiddler = function(fields) { tiddler: new $tw.Tiddler({title: "$:/TiddlerFive",
var tiddler = new $tw.Tiddler(fields); text: "Everything in federation",
wiki.shadowTiddlers[tiddler.fields.title] = {tiddler: tiddler}; tags: ["two"]
}; }),
},
"TiddlerSix": {
tiddler: new $tw.Tiddler({title: "TiddlerSix",
text: "Missing inaction from TiddlerOne",
tags: []
}),
},
"TiddlerSeventh": {
tiddler: new $tw.Tiddler({title: "TiddlerSeventh",
text: "",
list: "TiddlerOne [[Tiddler Three]] [[a fourth tiddler]] MissingTiddler",
tags: []
}),
},
"Tiddler8": {
tiddler: new $tw.Tiddler({title: "Tiddler8",
text: "Tidd",
tags: [],
"test-field": "JoeBloggs"
})
}
}
});
// Add a few tiddlers // Add a few tiddlers
wiki.addTiddler({ wiki.addTiddler({
@ -52,28 +75,13 @@ describe("Filter tests", function() {
text: "This is the text of tiddler [[one]]", text: "This is the text of tiddler [[one]]",
list: "[[Tiddler Three]] [[TiddlerOne]]", list: "[[Tiddler Three]] [[TiddlerOne]]",
modifier: "JohnDoe"}); modifier: "JohnDoe"});
// And some shadows
addShadowTiddler({
title: "$:/TiddlerFive",
text: "Everything in federation",
tags: ["two"]});
addShadowTiddler({
title: "TiddlerSix",
text: "Missing inaction from TiddlerOne",
tags: []});
addShadowTiddler({
title: "TiddlerSeventh",
text: "",
list: "TiddlerOne [[Tiddler Three]] [[a fourth tiddler]] MissingTiddler",
tags: []});
addShadowTiddler({
title: "Tiddler8",
text: "Tidd",
tags: [],
"test-field": "JoeBloggs"});
// Our tests // Our tests
it("should retrieve shadow tiddlers", function() {
expect(wiki.getTiddlerText("Tiddler8")).toBe("Tidd");
});
it("should handle the title operator", function() { it("should handle the title operator", function() {
expect(wiki.filterTiddlers("TiddlerOne [title[$:/TiddlerTwo]] [[Tiddler Three]]").join(",")).toBe("TiddlerOne,$:/TiddlerTwo,Tiddler Three"); expect(wiki.filterTiddlers("TiddlerOne [title[$:/TiddlerTwo]] [[Tiddler Three]]").join(",")).toBe("TiddlerOne,$:/TiddlerTwo,Tiddler Three");
expect(wiki.filterTiddlers("[!title[Tiddler Three]]").join(",")).toBe("TiddlerOne,$:/TiddlerTwo,a fourth tiddler,one"); expect(wiki.filterTiddlers("[!title[Tiddler Three]]").join(",")).toBe("TiddlerOne,$:/TiddlerTwo,a fourth tiddler,one");