1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 12:07:19 +00:00

Add "listed" filter operator for retrieving the tiddlers that list a specified tiddler

This commit is contained in:
Jeremy Ruston 2013-08-16 09:31:05 +01:00
parent 6fc4e5db7c
commit b88079442c
2 changed files with 53 additions and 1 deletions

View File

@ -0,0 +1,37 @@
/*\
title: $:/core/modules/filters/listed.js
type: application/javascript
module-type: filteroperator
Filter operator returning all tiddlers that have the selected tiddlers in a list
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.listed = function(source,operator,options) {
var results = [];
// Function to check an individual title
function checkTiddler(title) {
$tw.utils.pushTop(results,options.wiki.findListingsOfTiddler(title));
}
// Iterate through the source tiddlers
if($tw.utils.isArray(source)) {
$tw.utils.each(source,function(title) {
checkTiddler(title);
});
} else {
$tw.utils.each(source,function(element,title) {
checkTiddler(title);
});
}
return results;
};
})();

View File

@ -415,13 +415,28 @@ exports.getTiddlersWithTag = function(tag) {
var titles = [];
for(var title in this.tiddlers) {
var tiddler = this.tiddlers[title];
if(tiddler.fields.tags && tiddler.fields.tags.indexOf(tag) !== -1) {
if($tw.utils.isArray(tiddler.fields.tags) && tiddler.fields.tags.indexOf(tag) !== -1) {
titles.push(title);
}
}
return this.sortByList(titles,tag);
};
/*
Lookup a given tiddler and return a list of all the tiddlers that include it in their list
*/
exports.findListingsOfTiddler = function(targetTitle) {
// Get the list associated with the tag
var titles = [];
for(var title in this.tiddlers) {
var tiddler = this.tiddlers[title];
if($tw.utils.isArray(tiddler.fields.list) && tiddler.fields.list.indexOf(targetTitle) !== -1) {
titles.push(title);
}
}
return titles;
};
/*
Sorts an array of tiddler titles according to an ordered list
*/