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

Starting to make the members of $tw.Wiki be private

We want to avoid plugins from directly accessing the tiddlers hashmap.
Later we’ll do pluginTiddlers, pluginInfo and shadowTiddlers.
This commit is contained in:
Jermolene 2014-03-16 21:23:10 +00:00
parent 499730c458
commit 721e333a20
3 changed files with 84 additions and 96 deletions

View File

@ -674,7 +674,7 @@ Apply a callback to each module of a particular type
*/ */
$tw.modules.forEachModuleOfType = function(moduleType,callback) { $tw.modules.forEachModuleOfType = function(moduleType,callback) {
var modules = $tw.modules.types[moduleType]; var modules = $tw.modules.types[moduleType];
$tw.utils.each(modules,function(element,title,object) { $tw.utils.each(modules,function(element,title) {
callback(title,$tw.modules.execute(title)); callback(title,$tw.modules.execute(title));
}); });
}; };
@ -795,20 +795,63 @@ $tw.modules.define("$:/boot/tiddlerfields/list","tiddlerfield",{
Construct a wiki store object Construct a wiki store object
*/ */
$tw.Wiki = function() { $tw.Wiki = function() {
this.tiddlers = {}; var self = this,
tiddlers = {}; // Hashmap of tiddlers
this.pluginTiddlers = []; // Array of tiddlers containing registered plugins, ordered by priority this.pluginTiddlers = []; // Array of tiddlers containing registered plugins, ordered by priority
this.pluginInfo = {}; // Hashmap of parsed plugin content this.pluginInfo = {}; // Hashmap of parsed plugin content
this.shadowTiddlers = {}; // Hashmap by title of {source:, tiddler:} this.shadowTiddlers = {}; // Hashmap by title of {source:, tiddler:}
// Methods that need access to the private members
this.addTiddler = function(tiddler) {
if(!(tiddler instanceof $tw.Tiddler)) {
tiddler = new $tw.Tiddler(tiddler);
}
// Get the title
var title = tiddler.fields.title;
// Save the tiddler
if(title) {
tiddlers[title] = tiddler;
this.clearCache(title);
this.clearGlobalCache();
this.enqueueTiddlerEvent(title);
}
};
this.getTiddler = function(title) {
var t = tiddlers[title];
if(t instanceof $tw.Tiddler) {
return t;
} else if(title !== undefined && $tw.utils.hop(self.shadowTiddlers,title)) {
return self.shadowTiddlers[title].tiddler;
} else {
return undefined;
}
};
this.getAllTitles = function() {
var results = {};
for(var title in tiddlers) {
results[title] = true;
}
return results;
};
this.each = function(callback) {
for(var title in tiddlers) {
callback(tiddlers[title],title);
}
};
this.tiddlerExists = function(title) {
return !!$tw.utils.hop(tiddlers,title);
};
this.deleteTiddler = function(title) {
delete tiddlers[title];
this.clearCache(title);
this.clearGlobalCache();
this.enqueueTiddlerEvent(title,true);
};
}; };
$tw.Wiki.prototype.addTiddler = function(tiddler) { // Dummy methods that will be filled in after boot
if(!(tiddler instanceof $tw.Tiddler)) { $tw.Wiki.prototype.clearCache =
tiddler = new $tw.Tiddler(tiddler); $tw.Wiki.prototype.clearGlobalCache =
} $tw.Wiki.prototype.enqueueTiddlerEvent = function() {};
if(tiddler.fields.title) {
this.tiddlers[tiddler.fields.title] = tiddler;
}
};
$tw.Wiki.prototype.addTiddlers = function(tiddlers) { $tw.Wiki.prototype.addTiddlers = function(tiddlers) {
for(var t=0; t<tiddlers.length; t++) { for(var t=0; t<tiddlers.length; t++) {
@ -821,7 +864,7 @@ Read plugin info for all plugins
*/ */
$tw.Wiki.prototype.readPluginInfo = function() { $tw.Wiki.prototype.readPluginInfo = function() {
var self = this; var self = this;
$tw.utils.each(this.tiddlers,function(tiddler,title) { this.each(function(tiddler,title) {
if(tiddler && tiddler.fields.type === "application/json" && tiddler.hasField("plugin-type")) { if(tiddler && tiddler.fields.type === "application/json" && tiddler.hasField("plugin-type")) {
self.pluginInfo[tiddler.fields.title] = JSON.parse(tiddler.fields.text); self.pluginInfo[tiddler.fields.title] = JSON.parse(tiddler.fields.text);
} }
@ -846,7 +889,7 @@ $tw.Wiki.prototype.registerPluginTiddlers = function(pluginType,titles) {
checkTiddler(self.getTiddler(title)); checkTiddler(self.getTiddler(title));
}); });
} else { } else {
$tw.utils.each(this.tiddlers,function(tiddler,title) { this.each(function(tiddler,title) {
checkTiddler(tiddler); checkTiddler(tiddler);
}); });
} }
@ -911,7 +954,7 @@ $tw.Wiki.prototype.unpackPluginTiddlers = function() {
Define all modules stored in ordinary tiddlers Define all modules stored in ordinary tiddlers
*/ */
$tw.Wiki.prototype.defineTiddlerModules = function() { $tw.Wiki.prototype.defineTiddlerModules = function() {
$tw.utils.each(this.tiddlers,function(tiddler,title,object) { this.each(function(tiddler,title) {
if(tiddler.hasField("module-type")) { if(tiddler.hasField("module-type")) {
switch (tiddler.fields.type) { switch (tiddler.fields.type) {
case "application/javascript": case "application/javascript":
@ -938,7 +981,7 @@ $tw.Wiki.prototype.defineShadowModules = function() {
var self = this; var self = this;
$tw.utils.each(this.shadowTiddlers,function(element,title) { $tw.utils.each(this.shadowTiddlers,function(element,title) {
var tiddler = self.getTiddler(title); var tiddler = self.getTiddler(title);
if(!$tw.utils.hop(self.tiddlers,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(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);
@ -947,17 +990,6 @@ $tw.Wiki.prototype.defineShadowModules = function() {
}); });
}; };
$tw.Wiki.prototype.getTiddler = function(title) {
var t = this.tiddlers[title];
if(t instanceof $tw.Tiddler) {
return t;
} else if(title !== undefined && $tw.utils.hop(this.shadowTiddlers,title)) {
return this.shadowTiddlers[title].tiddler;
} else {
return undefined;
}
};
/* /*
Extracts tiddlers from a typed block of text, specifying default field values Extracts tiddlers from a typed block of text, specifying default field values
*/ */

View File

@ -151,7 +151,7 @@ exports.getFilterOperators = function() {
exports.filterTiddlers = function(filterString,currTiddlerTitle,tiddlerList) { exports.filterTiddlers = function(filterString,currTiddlerTitle,tiddlerList) {
var fn = this.compileFilter(filterString); var fn = this.compileFilter(filterString);
return fn.call(this,tiddlerList || this.tiddlers,currTiddlerTitle); return fn.call(this,tiddlerList,currTiddlerTitle);
}; };
exports.compileFilter = function(filterString) { exports.compileFilter = function(filterString) {
@ -219,7 +219,7 @@ exports.compileFilter = function(filterString) {
}); });
// Return a function that applies the operations to a source array/hashmap of tiddler titles // Return a function that applies the operations to a source array/hashmap of tiddler titles
return function(source,currTiddlerTitle) { return function(source,currTiddlerTitle) {
source = source || self.tiddlers; source = source || self.getAllTitles();
var results = []; var results = [];
$tw.utils.each(operationFunctions,function(operationFunction) { $tw.utils.each(operationFunctions,function(operationFunction) {
operationFunction(results,source,currTiddlerTitle); operationFunction(results,source,currTiddlerTitle);

View File

@ -155,17 +155,6 @@ exports.getChangeCount = function(title) {
} }
}; };
exports.deleteTiddler = function(title) {
delete this.tiddlers[title];
this.clearCache(title);
this.clearGlobalCache();
this.enqueueTiddlerEvent(title,true);
};
exports.tiddlerExists = function(title) {
return !!$tw.utils.hop(this.tiddlers,title);
};
/* /*
Generate an unused title from the specified base Generate an unused title from the specified base
*/ */
@ -206,22 +195,6 @@ exports.isImageTiddler = function(title) {
} }
}; };
exports.addTiddler = function(tiddler) {
// Check if we're passed a fields hashmap instead of a tiddler
if(!(tiddler instanceof $tw.Tiddler)) {
tiddler = new $tw.Tiddler(tiddler);
}
// Get the title
var title = tiddler.fields.title;
// Save the tiddler
if(title) {
this.tiddlers[title] = tiddler;
this.clearCache(title);
this.clearGlobalCache();
this.enqueueTiddlerEvent(title);
}
};
/* /*
Like addTiddler() except it will silently reject any plugin tiddlers that are older than the currently loaded version. Returns true if the tiddler was imported Like addTiddler() except it will silently reject any plugin tiddlers that are older than the currently loaded version. Returns true if the tiddler was imported
*/ */
@ -277,15 +250,13 @@ exports.getTiddlers = function(options) {
var self = this, var self = this,
sortField = options.sortField || "title", sortField = options.sortField || "title",
tiddlers = [], t, titles = []; tiddlers = [], t, titles = [];
for(t in this.tiddlers) { this.each(function(tiddler,title) {
if($tw.utils.hop(this.tiddlers,t)) { if(options.includeSystem || !self.isSystemTiddler(title)) {
if(options.includeSystem || !this.isSystemTiddler(t)) { if(!options.excludeTag || !tiddler.hasTag(options.excludeTag)) {
if(!options.excludeTag || !this.tiddlers[t].hasTag(options.excludeTag)) { tiddlers.push(tiddler);
tiddlers.push(this.tiddlers[t]);
}
} }
} }
} });
tiddlers.sort(function(a,b) { tiddlers.sort(function(a,b) {
var aa = a.fields[sortField].toLowerCase() || "", var aa = a.fields[sortField].toLowerCase() || "",
bb = b.fields[sortField].toLowerCase() || ""; bb = b.fields[sortField].toLowerCase() || "";
@ -363,7 +334,7 @@ exports.forEachTiddler = function(/* [options,]callback */) {
titles = this.getTiddlers(options), titles = this.getTiddlers(options),
t, tiddler; t, tiddler;
for(t=0; t<titles.length; t++) { for(t=0; t<titles.length; t++) {
tiddler = this.tiddlers[titles[t]]; tiddler = this.getTiddler(titles[t]);
if(tiddler) { if(tiddler) {
callback.call(this,tiddler.fields.title,tiddler); callback.call(this,tiddler.fields.title,tiddler);
} }
@ -450,26 +421,6 @@ exports.getOrphanTitles = function() {
return orphans; // Todo return orphans; // Todo
}; };
exports.getSystemTitles = function() {
var titles = [];
for(var title in this.tiddlers) {
if(this.isSystemTiddler(title)) {
titles.push(title);
}
}
titles.sort();
return titles;
};
exports.getShadowTitles = function() {
var titles = [];
for(var title in this.shadowTiddlers) {
titles.push(title);
}
titles.sort();
return titles;
};
/* /*
Retrieves a list of the tiddler titles that are tagged with a given tag Retrieves a list of the tiddler titles that are tagged with a given tag
*/ */
@ -488,7 +439,7 @@ exports.getTagMap = function() {
var self = this; var self = this;
return this.getGlobalCache("tagmap",function() { return this.getGlobalCache("tagmap",function() {
var tags = {}, var tags = {},
storeTags = function(tagArray) { storeTags = function(tagArray,title) {
if(tagArray) { if(tagArray) {
for(var index=0; index<tagArray.length; index++) { for(var index=0; index<tagArray.length; index++) {
var tag = tagArray[index]; var tag = tagArray[index];
@ -503,15 +454,14 @@ exports.getTagMap = function() {
title, tiddler; title, tiddler;
// Collect up all the tags // Collect up all the tags
for(title in self.shadowTiddlers) { for(title in self.shadowTiddlers) {
if(!$tw.utils.hop(self.tiddlers,title)) { if(!self.tiddlerExists(title)) {
tiddler = self.shadowTiddlers[title].tiddler; tiddler = self.shadowTiddlers[title].tiddler;
storeTags(tiddler.fields.tags); storeTags(tiddler.fields.tags,title);
} }
} }
for(title in self.tiddlers) { self.each(function(tiddler,title) {
tiddler = self.tiddlers[title]; storeTags(tiddler.fields.tags,title);
storeTags(tiddler.fields.tags); });
}
return tags; return tags;
}); });
}; };
@ -522,12 +472,11 @@ Lookup a given tiddler and return a list of all the tiddlers that include it in
exports.findListingsOfTiddler = function(targetTitle) { exports.findListingsOfTiddler = function(targetTitle) {
// Get the list associated with the tag // Get the list associated with the tag
var titles = []; var titles = [];
for(var title in this.tiddlers) { this.each(function(tiddler,title) {
var tiddler = this.tiddlers[title];
if($tw.utils.isArray(tiddler.fields.list) && tiddler.fields.list.indexOf(targetTitle) !== -1) { if($tw.utils.isArray(tiddler.fields.list) && tiddler.fields.list.indexOf(targetTitle) !== -1) {
titles.push(title); titles.push(title);
} }
} });
return titles; return titles;
}; };
@ -998,11 +947,18 @@ exports.search = function(text,options) {
} }
} }
} else { } else {
var source = options.titles || this.tiddlers; if(options.titles) {
for(t in source) { for(var title in options.titles) {
if(!!searchTiddler(t) === !options.invert) { if(!!searchTiddler(title) === !options.invert) {
results.push(t); results.push(title);
}
} }
} else {
this.each(function(tiddler,title) {
if(!!searchTiddler(title) === !options.invert) {
results.push(title);
}
});
} }
} }
// Remove any of the results we have to exclude // Remove any of the results we have to exclude