1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-10-30 15:13:00 +00:00

Refactor wiki.forEachTiddler() and wiki.getTiddlers() to take an options parameter

This commit is contained in:
Jermolene
2013-12-11 22:01:02 +00:00
parent b8897f86fb
commit 6298c7aa43

View File

@@ -255,14 +255,23 @@ exports.getModificationFields = function() {
}; };
/* /*
Return a sorted array of non-system tiddler titles, optionally filtered by a tag Return a sorted array of tiddler titles. Options include:
sortField: field to sort by
excludeTag: tag to exclude
includeSystem: whether to include system tiddlers (defaults to false)
*/ */
exports.getTiddlers = function(sortField,excludeTag) { exports.getTiddlers = function(options) {
sortField = sortField || "title"; options = options || {};
var tiddlers = [], t, titles = []; var self = this,
sortField = options.sortField || "title",
tiddlers = [], t, titles = [];
for(t in this.tiddlers) { for(t in this.tiddlers) {
if($tw.utils.hop(this.tiddlers,t) && !this.isSystemTiddler(t) && (!excludeTag || !this.tiddlers[t].hasTag(excludeTag))) { if($tw.utils.hop(this.tiddlers,t)) {
tiddlers.push(this.tiddlers[t]); if(options.includeSystem || !this.isSystemTiddler(t)) {
if(!options.excludeTag || !this.tiddlers[t].hasTag(excludeTag)) {
tiddlers.push(this.tiddlers[t]);
}
}
} }
} }
tiddlers.sort(function(a,b) { tiddlers.sort(function(a,b) {
@@ -285,7 +294,7 @@ exports.getTiddlers = function(sortField,excludeTag) {
}; };
exports.countTiddlers = function(excludeTag) { exports.countTiddlers = function(excludeTag) {
var tiddlers = this.getTiddlers(null,excludeTag); var tiddlers = this.getTiddlers({excludeTag: excludeTag});
return $tw.utils.count(tiddlers); return $tw.utils.count(tiddlers);
}; };
@@ -323,12 +332,17 @@ exports.sortTiddlers = function(titles,sortField,isDescending,isCaseSensitive) {
}); });
}; };
exports.forEachTiddler = function(/* [sortField,[excludeTag,]]callback */) { /*
For every tiddler invoke a callback(title,tiddler) with `this` set to the wiki object. Options include:
sortField: field to sort by
excludeTag: tag to exclude
includeSystem: whether to include system tiddlers (defaults to false)
*/
exports.forEachTiddler = function(/* [options,]callback */) {
var arg = 0, var arg = 0,
sortField = arguments.length > 1 ? arguments[arg++] : null, options = arguments.length >= 2 ? arguments[arg++] : {},
excludeTag = arguments.length > 2 ? arguments[arg++] : null,
callback = arguments[arg++], callback = arguments[arg++],
titles = this.getTiddlers(sortField,excludeTag), 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.tiddlers[titles[t]];