mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-04-12 05:43:16 +00:00
Support for saving result lists in a variable
Extend let filter run prefix to store list of results, and add varlist operator for accessing variables as a list. We already had partial support for variables returning a list of values in order for functions to work, now we extend it so that any variable can be used to store a list We should extend the set widget so that it returns a result list that can be accessed with the varlist operator
This commit is contained in:
parent
e7b713c277
commit
7397f4fa3a
core/modules
editions/test/tiddlers/tests/data/let-filter-prefix
@ -25,7 +25,7 @@ exports.let = function(operationSubFunction,options) {
|
||||
var inputSource = widget.wiki.makeTiddlerIterator(results.toArray());
|
||||
// Assign the result of the subfunction to the variable
|
||||
var variables = {};
|
||||
variables[name] = operationSubFunction(inputSource,widget)[0] || "";
|
||||
variables[name] = operationSubFunction(inputSource,widget);
|
||||
// Clear the results
|
||||
results.clear();
|
||||
// Return the variables
|
||||
|
30
core/modules/filters/varlist.js
Normal file
30
core/modules/filters/varlist.js
Normal file
@ -0,0 +1,30 @@
|
||||
/*\
|
||||
title: $:/core/modules/filters/varlist.js
|
||||
type: application/javascript
|
||||
module-type: filteroperator
|
||||
|
||||
Retrieve the value of a variable as a list
|
||||
|
||||
[all[shadows+tiddlers]]
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
/*
|
||||
Export our filter function
|
||||
*/
|
||||
exports.varlist = function(source,operator,options) {
|
||||
// Check for common optimisations
|
||||
var variableInfo = options.widget.getVariableInfo(operator.operand);
|
||||
if(variableInfo) {
|
||||
return options.wiki.makeTiddlerIterator(variableInfo.resultList);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
@ -83,7 +83,7 @@ Widget.prototype.execute = function() {
|
||||
/*
|
||||
Set the value of a context variable
|
||||
name: name of the variable
|
||||
value: value of the variable
|
||||
value: value of the variable, can be a string or an array
|
||||
params: array of {name:, default:} for each parameter
|
||||
isMacroDefinition: true if the variable is set via a \define macro pragma (and hence should have variable substitution performed)
|
||||
options includes:
|
||||
@ -93,8 +93,10 @@ options includes:
|
||||
*/
|
||||
Widget.prototype.setVariable = function(name,value,params,isMacroDefinition,options) {
|
||||
options = options || {};
|
||||
var valueIsArray = $tw.utils.isArray(value);
|
||||
this.variables[name] = {
|
||||
value: value,
|
||||
value: valueIsArray ? (value[0] || "") : value,
|
||||
resultList: valueIsArray ? value : [value],
|
||||
params: params,
|
||||
isMacroDefinition: !!isMacroDefinition,
|
||||
isFunctionDefinition: !!options.isFunctionDefinition,
|
||||
@ -164,6 +166,9 @@ Widget.prototype.getVariableInfo = function(name,options) {
|
||||
resultList = this.wiki.filterTiddlers(value,this.makeFakeWidgetWithVariables(variables),options.source);
|
||||
value = resultList[0] || "";
|
||||
} else {
|
||||
if(variable.resultList) {
|
||||
resultList = variable.resultList;
|
||||
}
|
||||
params = variable.params;
|
||||
}
|
||||
return {
|
||||
@ -313,7 +318,7 @@ Widget.prototype.getStateQualifier = function(name) {
|
||||
};
|
||||
|
||||
/*
|
||||
Make a fake widget with specified variables, suitable for variable lookup in filters
|
||||
Make a fake widget with specified variables, suitable for variable lookup in filters. Each variable can be a string or an array of strings
|
||||
*/
|
||||
Widget.prototype.makeFakeWidgetWithVariables = function(variables) {
|
||||
var self = this,
|
||||
@ -321,7 +326,12 @@ Widget.prototype.makeFakeWidgetWithVariables = function(variables) {
|
||||
return {
|
||||
getVariable: function(name,opts) {
|
||||
if($tw.utils.hop(variables,name)) {
|
||||
return variables[name];
|
||||
var value = variables[name];
|
||||
if($tw.utils.isArray(value)) {
|
||||
return value[0];
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
} else {
|
||||
opts = opts || {};
|
||||
opts.variables = variables;
|
||||
@ -330,9 +340,18 @@ Widget.prototype.makeFakeWidgetWithVariables = function(variables) {
|
||||
},
|
||||
getVariableInfo: function(name,opts) {
|
||||
if($tw.utils.hop(variables,name)) {
|
||||
return {
|
||||
text: variables[name]
|
||||
};
|
||||
var value = variables[name];
|
||||
if($tw.utils.isArray(value)) {
|
||||
return {
|
||||
text: value[0],
|
||||
resultList: value
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
text: value,
|
||||
resultList: [value]
|
||||
};
|
||||
}
|
||||
} else {
|
||||
opts = opts || {};
|
||||
opts.variables = $tw.utils.extend({},variables,opts.variables);
|
||||
|
@ -0,0 +1,12 @@
|
||||
title: LetFilterRunPrefix/ResultList
|
||||
description: Using the "let" filter run prefix to store result lists, not just single values
|
||||
type: text/vnd.tiddlywiki-multiple
|
||||
tags: [[$:/tags/wiki-test-spec]]
|
||||
|
||||
title: Output
|
||||
|
||||
<$text text={{{ :let:varname[all[tiddlers]] [varlist[varname]sort[]join[,]] }}}/>
|
||||
+
|
||||
title: ExpectedResult
|
||||
|
||||
<p>$:/core,ExpectedResult,Output</p>
|
Loading…
x
Reference in New Issue
Block a user