mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-10-31 07:32:59 +00:00 
			
		
		
		
	Refactor wiki.filterTiddlers()
Now we pass a widget instead of the current tiddler title. We can use widget.getVariable(“currentTiddler”) to get the current tiddler.
This commit is contained in:
		| @@ -149,22 +149,22 @@ exports.getFilterOperators = function() { | |||||||
| 	return this.filterOperators; | 	return this.filterOperators; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| exports.filterTiddlers = function(filterString,currTiddlerTitle,source) { | exports.filterTiddlers = function(filterString,widget,source) { | ||||||
| 	var fn = this.compileFilter(filterString); | 	var fn = this.compileFilter(filterString); | ||||||
| 	return fn.call(this,source,currTiddlerTitle); | 	return fn.call(this,source,widget); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /* | /* | ||||||
| Compile a filter into a function with the signature fn(source,currTiddlerTitle) where: | Compile a filter into a function with the signature fn(source,widget) where: | ||||||
| source: an iterator function for the source tiddlers, called source(iterator), where iterator is called as iterator(tiddler,title) | source: an iterator function for the source tiddlers, called source(iterator), where iterator is called as iterator(tiddler,title) | ||||||
| currTiddlerTitle: the optional name of the current tiddler | widget: an optional widget node for retrieving the current tiddler etc. | ||||||
| */ | */ | ||||||
| exports.compileFilter = function(filterString) { | exports.compileFilter = function(filterString) { | ||||||
| 	var filterParseTree; | 	var filterParseTree; | ||||||
| 	try { | 	try { | ||||||
| 		filterParseTree = this.parseFilter(filterString); | 		filterParseTree = this.parseFilter(filterString); | ||||||
| 	} catch(e) { | 	} catch(e) { | ||||||
| 		return function(source,currTiddlerTitle) { | 		return function(source,widget) { | ||||||
| 			return ["Filter error: " + e]; | 			return ["Filter error: " + e]; | ||||||
| 		}; | 		}; | ||||||
| 	} | 	} | ||||||
| @@ -176,9 +176,10 @@ exports.compileFilter = function(filterString) { | |||||||
| 	var self = this; | 	var self = this; | ||||||
| 	$tw.utils.each(filterParseTree,function(operation) { | 	$tw.utils.each(filterParseTree,function(operation) { | ||||||
| 		// Create a function for the chain of operators in the operation | 		// Create a function for the chain of operators in the operation | ||||||
| 		var operationSubFunction = function(source,currTiddlerTitle) { | 		var operationSubFunction = function(source,widget) { | ||||||
| 			var accumulator = source, | 			var accumulator = source, | ||||||
| 				results = []; | 				results = [], | ||||||
|  | 				currTiddlerTitle = widget && widget.getVariable("currentTiddler"); | ||||||
| 			$tw.utils.each(operation.operators,function(operator) { | 			$tw.utils.each(operation.operators,function(operator) { | ||||||
| 				var operand = operator.operand, | 				var operand = operator.operand, | ||||||
| 					operatorFunction; | 					operatorFunction; | ||||||
| @@ -200,7 +201,7 @@ exports.compileFilter = function(filterString) { | |||||||
| 							regexp: operator.regexp | 							regexp: operator.regexp | ||||||
| 						},{ | 						},{ | ||||||
| 							wiki: self, | 							wiki: self, | ||||||
| 							currTiddlerTitle: currTiddlerTitle | 							widget: widget | ||||||
| 						}); | 						}); | ||||||
| 				accumulator = self.makeTiddlerIterator(results); | 				accumulator = self.makeTiddlerIterator(results); | ||||||
| 			}); | 			}); | ||||||
| @@ -210,25 +211,25 @@ exports.compileFilter = function(filterString) { | |||||||
| 		operationFunctions.push((function() { | 		operationFunctions.push((function() { | ||||||
| 			switch(operation.prefix || "") { | 			switch(operation.prefix || "") { | ||||||
| 				case "": // No prefix means that the operation is unioned into the result | 				case "": // No prefix means that the operation is unioned into the result | ||||||
| 					return function(results,source,currTiddlerTitle) { | 					return function(results,source,widget) { | ||||||
| 						$tw.utils.pushTop(results,operationSubFunction(source,currTiddlerTitle)); | 						$tw.utils.pushTop(results,operationSubFunction(source,widget)); | ||||||
| 					}; | 					}; | ||||||
| 				case "-": // The results of this operation are removed from the main result | 				case "-": // The results of this operation are removed from the main result | ||||||
| 					return function(results,source,currTiddlerTitle) { | 					return function(results,source,widget) { | ||||||
| 						$tw.utils.removeArrayEntries(results,operationSubFunction(source,currTiddlerTitle)); | 						$tw.utils.removeArrayEntries(results,operationSubFunction(source,widget)); | ||||||
| 					}; | 					}; | ||||||
| 				case "+": // This operation is applied to the main results so far | 				case "+": // This operation is applied to the main results so far | ||||||
| 					return function(results,source,currTiddlerTitle) { | 					return function(results,source,widget) { | ||||||
| 						// This replaces all the elements of the array, but keeps the actual array so that references to it are preserved | 						// This replaces all the elements of the array, but keeps the actual array so that references to it are preserved | ||||||
| 						source = self.makeTiddlerIterator(results); | 						source = self.makeTiddlerIterator(results); | ||||||
| 						results.splice(0,results.length); | 						results.splice(0,results.length); | ||||||
| 						$tw.utils.pushTop(results,operationSubFunction(source,currTiddlerTitle)); | 						$tw.utils.pushTop(results,operationSubFunction(source,widget)); | ||||||
| 					}; | 					}; | ||||||
| 			} | 			} | ||||||
| 		})()); | 		})()); | ||||||
| 	}); | 	}); | ||||||
| 	// Return a function that applies the operations to a source iterator of tiddler titles | 	// Return a function that applies the operations to a source iterator of tiddler titles | ||||||
| 	return $tw.perf.measure("filter",function filterFunction(source,currTiddlerTitle) { | 	return $tw.perf.measure("filter",function filterFunction(source,widget) { | ||||||
| 		if(!source) { | 		if(!source) { | ||||||
| 			source = self.each; | 			source = self.each; | ||||||
| 		} else if(typeof source === "object") { // Array or hashmap | 		} else if(typeof source === "object") { // Array or hashmap | ||||||
| @@ -236,7 +237,7 @@ exports.compileFilter = function(filterString) { | |||||||
| 		} | 		} | ||||||
| 		var results = []; | 		var results = []; | ||||||
| 		$tw.utils.each(operationFunctions,function(operationFunction) { | 		$tw.utils.each(operationFunctions,function(operationFunction) { | ||||||
| 			operationFunction(results,source,currTiddlerTitle); | 			operationFunction(results,source,widget); | ||||||
| 		}); | 		}); | ||||||
| 		return results; | 		return results; | ||||||
| 	}); | 	}); | ||||||
|   | |||||||
| @@ -16,8 +16,9 @@ Filter function for [all[current]] | |||||||
| Export our filter function | Export our filter function | ||||||
| */ | */ | ||||||
| exports.current = function(source,prefix,options) { | exports.current = function(source,prefix,options) { | ||||||
| 	if(options.currTiddlerTitle) { | 	var currTiddlerTitle = options.widget && options.widget.getVariable("currentTiddler"); | ||||||
| 		return [options.currTiddlerTitle]; | 	if(currTiddlerTitle) { | ||||||
|  | 		return [currTiddlerTitle]; | ||||||
| 	} else { | 	} else { | ||||||
| 		return []; | 		return []; | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -16,16 +16,17 @@ Filter function for [is[current]] | |||||||
| Export our filter function | Export our filter function | ||||||
| */ | */ | ||||||
| exports.current = function(source,prefix,options) { | exports.current = function(source,prefix,options) { | ||||||
| 	var results = []; | 	var results = [], | ||||||
|  | 		currTiddlerTitle = options.widget && options.widget.getVariable("currentTiddler"); | ||||||
| 	if(prefix === "!") { | 	if(prefix === "!") { | ||||||
| 		source(function(tiddler,title) { | 		source(function(tiddler,title) { | ||||||
| 			if(title !== options.currTiddlerTitle) { | 			if(title !== currTiddlerTitle) { | ||||||
| 				results.push(title); | 				results.push(title); | ||||||
| 			} | 			} | ||||||
| 		}); | 		}); | ||||||
| 	} else { | 	} else { | ||||||
| 		source(function(tiddler,title) { | 		source(function(tiddler,title) { | ||||||
| 			if(title === options.currTiddlerTitle) { | 			if(title === currTiddlerTitle) { | ||||||
| 				results.push(title); | 				results.push(title); | ||||||
| 			} | 			} | ||||||
| 		}); | 		}); | ||||||
|   | |||||||
| @@ -18,7 +18,8 @@ Export our filter function | |||||||
| exports.list = function(source,operator,options) { | exports.list = function(source,operator,options) { | ||||||
| 	var results = [], | 	var results = [], | ||||||
| 		tr = $tw.utils.parseTextReference(operator.operand), | 		tr = $tw.utils.parseTextReference(operator.operand), | ||||||
| 		list = options.wiki.getTiddlerList(tr.title || options.currTiddlerTitle,tr.field,tr.index); | 		currTiddlerTitle = options.widget && options.widget.getVariable("currentTiddler"), | ||||||
|  | 		list = options.wiki.getTiddlerList(tr.title || currTiddlerTitle,tr.field,tr.index); | ||||||
| 	if(operator.prefix === "!") { | 	if(operator.prefix === "!") { | ||||||
| 		source(function(tiddler,title) { | 		source(function(tiddler,title) { | ||||||
| 			if(list.indexOf(title) === -1) { | 			if(list.indexOf(title) === -1) { | ||||||
|   | |||||||
| @@ -43,7 +43,7 @@ CountWidget.prototype.execute = function() { | |||||||
| 	this.filter = this.getAttribute("filter"); | 	this.filter = this.getAttribute("filter"); | ||||||
| 	// Execute the filter | 	// Execute the filter | ||||||
| 	if(this.filter) { | 	if(this.filter) { | ||||||
| 		this.currentCount = this.wiki.filterTiddlers(this.filter,this.getVariable("currentTiddler")).length; | 		this.currentCount = this.wiki.filterTiddlers(this.filter,this).length; | ||||||
| 	} else { | 	} else { | ||||||
| 		this.currentCount = undefined; | 		this.currentCount = undefined; | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -60,14 +60,8 @@ EncryptWidget.prototype.execute = function() { | |||||||
| Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering | Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering | ||||||
| */ | */ | ||||||
| EncryptWidget.prototype.refresh = function(changedTiddlers) { | EncryptWidget.prototype.refresh = function(changedTiddlers) { | ||||||
| 	var changedAttributes = this.computeAttributes(), | 	// We don't need to worry about refreshing because the encrypt widget isn't for interactive use | ||||||
| 		affectedTiddlers = this.wiki.filterTiddlers(this.filter,null,changedTiddlers); |  | ||||||
| 	if(changedAttributes.filter || affectedTiddlers.length > 0) { |  | ||||||
| 		this.refreshSelf(); |  | ||||||
| 		return true; |  | ||||||
| 	} else { |  | ||||||
| 	return false; | 	return false; | ||||||
| 	} |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
| exports.encrypt = EncryptWidget; | exports.encrypt = EncryptWidget; | ||||||
|   | |||||||
| @@ -80,7 +80,7 @@ ListWidget.prototype.execute = function() { | |||||||
|  |  | ||||||
| ListWidget.prototype.getTiddlerList = function() { | ListWidget.prototype.getTiddlerList = function() { | ||||||
| 	var defaultFilter = "[!is[system]sort[title]]"; | 	var defaultFilter = "[!is[system]sort[title]]"; | ||||||
| 	return this.wiki.filterTiddlers(this.getAttribute("filter",defaultFilter),this.getVariable("currentTiddler")); | 	return this.wiki.filterTiddlers(this.getAttribute("filter",defaultFilter),this); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| ListWidget.prototype.getEmptyMessage = function() { | ListWidget.prototype.getEmptyMessage = function() { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jermolene
					Jermolene