2013-05-27 16:59:03 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/filters/each.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: filteroperator
|
|
|
|
|
2015-01-26 12:39:04 +00:00
|
|
|
Filter operator that selects one tiddler for each unique value of the specified field.
|
|
|
|
With suffix "list", selects all tiddlers that are values in a specified list field.
|
2013-05-27 16:59:03 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Export our filter function
|
|
|
|
*/
|
|
|
|
exports.each = function(source,operator,options) {
|
2015-01-27 18:31:23 +00:00
|
|
|
var results =[] ,
|
2018-02-17 18:51:18 +00:00
|
|
|
value,values = {},
|
|
|
|
field = operator.operand || "title";
|
|
|
|
if(operator.suffix === "value" && field === "title") {
|
2015-01-27 00:17:14 +00:00
|
|
|
source(function(tiddler,title) {
|
2018-02-17 18:51:18 +00:00
|
|
|
if(!$tw.utils.hop(values,title)) {
|
|
|
|
values[title] = true;
|
|
|
|
results.push(title);
|
2015-01-27 00:17:14 +00:00
|
|
|
}
|
2015-02-11 18:35:05 +00:00
|
|
|
});
|
2018-02-17 18:51:18 +00:00
|
|
|
} else if(operator.suffix !== "list-item") {
|
|
|
|
if(field === "title") {
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
if(tiddler && !$tw.utils.hop(values,title)) {
|
|
|
|
values[title] = true;
|
|
|
|
results.push(title);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
if(tiddler) {
|
|
|
|
value = tiddler.getFieldString(field);
|
|
|
|
if(!$tw.utils.hop(values,value)) {
|
|
|
|
values[value] = true;
|
|
|
|
results.push(title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-01-27 00:17:14 +00:00
|
|
|
} else {
|
|
|
|
source(function(tiddler,title) {
|
|
|
|
if(tiddler) {
|
|
|
|
$tw.utils.each(
|
|
|
|
options.wiki.getTiddlerList(title,field),
|
|
|
|
function(value) {
|
2015-02-11 18:35:05 +00:00
|
|
|
if(!$tw.utils.hop(values,value)) {
|
|
|
|
values[value] = true;
|
|
|
|
results.push(value);
|
|
|
|
}
|
2015-01-27 00:17:14 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-05-27 16:59:03 +00:00
|
|
|
return results;
|
|
|
|
};
|
|
|
|
|
2015-01-28 23:45:58 +00:00
|
|
|
})();
|