1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-26 19:47:20 +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) {
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));
});
};
@ -795,20 +795,63 @@ $tw.modules.define("$:/boot/tiddlerfields/list","tiddlerfield",{
Construct a wiki store object
*/
$tw.Wiki = function() {
this.tiddlers = {};
var self = this,
tiddlers = {}; // Hashmap of tiddlers
this.pluginTiddlers = []; // Array of tiddlers containing registered plugins, ordered by priority
this.pluginInfo = {}; // Hashmap of parsed plugin content
this.shadowTiddlers = {}; // Hashmap by title of {source:, tiddler:}
};
$tw.Wiki.prototype.addTiddler = function(tiddler) {
// Methods that need access to the private members
this.addTiddler = function(tiddler) {
if(!(tiddler instanceof $tw.Tiddler)) {
tiddler = new $tw.Tiddler(tiddler);
}
if(tiddler.fields.title) {
this.tiddlers[tiddler.fields.title] = 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);
};
};
// Dummy methods that will be filled in after boot
$tw.Wiki.prototype.clearCache =
$tw.Wiki.prototype.clearGlobalCache =
$tw.Wiki.prototype.enqueueTiddlerEvent = function() {};
$tw.Wiki.prototype.addTiddlers = function(tiddlers) {
for(var t=0; t<tiddlers.length; t++) {
@ -821,7 +864,7 @@ Read plugin info for all plugins
*/
$tw.Wiki.prototype.readPluginInfo = function() {
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")) {
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));
});
} else {
$tw.utils.each(this.tiddlers,function(tiddler,title) {
this.each(function(tiddler,title) {
checkTiddler(tiddler);
});
}
@ -911,7 +954,7 @@ $tw.Wiki.prototype.unpackPluginTiddlers = function() {
Define all modules stored in ordinary tiddlers
*/
$tw.Wiki.prototype.defineTiddlerModules = function() {
$tw.utils.each(this.tiddlers,function(tiddler,title,object) {
this.each(function(tiddler,title) {
if(tiddler.hasField("module-type")) {
switch (tiddler.fields.type) {
case "application/javascript":
@ -938,7 +981,7 @@ $tw.Wiki.prototype.defineShadowModules = function() {
var self = this;
$tw.utils.each(this.shadowTiddlers,function(element,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")) {
// Define the module
$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
*/

View File

@ -151,7 +151,7 @@ exports.getFilterOperators = function() {
exports.filterTiddlers = function(filterString,currTiddlerTitle,tiddlerList) {
var fn = this.compileFilter(filterString);
return fn.call(this,tiddlerList || this.tiddlers,currTiddlerTitle);
return fn.call(this,tiddlerList,currTiddlerTitle);
};
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 function(source,currTiddlerTitle) {
source = source || self.tiddlers;
source = source || self.getAllTitles();
var results = [];
$tw.utils.each(operationFunctions,function(operationFunction) {
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
*/
@ -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
*/
@ -277,15 +250,13 @@ exports.getTiddlers = function(options) {
var self = this,
sortField = options.sortField || "title",
tiddlers = [], t, titles = [];
for(t in this.tiddlers) {
if($tw.utils.hop(this.tiddlers,t)) {
if(options.includeSystem || !this.isSystemTiddler(t)) {
if(!options.excludeTag || !this.tiddlers[t].hasTag(options.excludeTag)) {
tiddlers.push(this.tiddlers[t]);
}
}
this.each(function(tiddler,title) {
if(options.includeSystem || !self.isSystemTiddler(title)) {
if(!options.excludeTag || !tiddler.hasTag(options.excludeTag)) {
tiddlers.push(tiddler);
}
}
});
tiddlers.sort(function(a,b) {
var aa = a.fields[sortField].toLowerCase() || "",
bb = b.fields[sortField].toLowerCase() || "";
@ -363,7 +334,7 @@ exports.forEachTiddler = function(/* [options,]callback */) {
titles = this.getTiddlers(options),
t, tiddler;
for(t=0; t<titles.length; t++) {
tiddler = this.tiddlers[titles[t]];
tiddler = this.getTiddler(titles[t]);
if(tiddler) {
callback.call(this,tiddler.fields.title,tiddler);
}
@ -450,26 +421,6 @@ exports.getOrphanTitles = function() {
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
*/
@ -488,7 +439,7 @@ exports.getTagMap = function() {
var self = this;
return this.getGlobalCache("tagmap",function() {
var tags = {},
storeTags = function(tagArray) {
storeTags = function(tagArray,title) {
if(tagArray) {
for(var index=0; index<tagArray.length; index++) {
var tag = tagArray[index];
@ -503,15 +454,14 @@ exports.getTagMap = function() {
title, tiddler;
// Collect up all the tags
for(title in self.shadowTiddlers) {
if(!$tw.utils.hop(self.tiddlers,title)) {
if(!self.tiddlerExists(title)) {
tiddler = self.shadowTiddlers[title].tiddler;
storeTags(tiddler.fields.tags);
storeTags(tiddler.fields.tags,title);
}
}
for(title in self.tiddlers) {
tiddler = self.tiddlers[title];
storeTags(tiddler.fields.tags);
}
self.each(function(tiddler,title) {
storeTags(tiddler.fields.tags,title);
});
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) {
// Get the list associated with the tag
var titles = [];
for(var title in this.tiddlers) {
var tiddler = this.tiddlers[title];
this.each(function(tiddler,title) {
if($tw.utils.isArray(tiddler.fields.list) && tiddler.fields.list.indexOf(targetTitle) !== -1) {
titles.push(title);
}
}
});
return titles;
};
@ -998,12 +947,19 @@ exports.search = function(text,options) {
}
}
} else {
var source = options.titles || this.tiddlers;
for(t in source) {
if(!!searchTiddler(t) === !options.invert) {
results.push(t);
if(options.titles) {
for(var title in options.titles) {
if(!!searchTiddler(title) === !options.invert) {
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
if(options.exclude) {