1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 18:17:20 +00:00

Refactor widget.getVariable() to have better behaved parameters

This commit is contained in:
Jeremy Ruston 2013-10-25 22:16:15 +01:00
parent 46bdc52261
commit 8fbc5759ad
4 changed files with 12 additions and 8 deletions

View File

@ -50,7 +50,7 @@ ElementWidget.prototype.execute = function() {
if(this.namespace) {
this.setVariable("namespace",this.namespace);
} else {
this.namespace = this.getVariable("namespace",null,"http://www.w3.org/1999/xhtml");
this.namespace = this.getVariable("namespace",{defaultValue: "http://www.w3.org/1999/xhtml"});
}
// Make the child widgets
this.makeChildWidgets();

View File

@ -43,7 +43,7 @@ MacroCallWidget.prototype.execute = function() {
params.push({name: name, value: attribute});
});
// Get the macro value
var text = this.getVariable(this.parseTreeNode.name || this.getAttribute("$name"),params);
var text = this.getVariable(this.parseTreeNode.name || this.getAttribute("$name"),{params: params});
// Parse the macro
var parser = this.wiki.new_parseText("text/vnd.tiddlywiki",text,
{parseAsInline: !this.parseTreeNode.isBlock}),

View File

@ -66,7 +66,7 @@ Compose a string comprising the title, field and/or index to identify this trans
TranscludeWidget.prototype.makeRecursionMarker = function() {
var output = [];
output.push("{");
output.push(this.getVariable("tiddlerTitle",""));
output.push(this.getVariable("tiddlerTitle",{defaultValue: ""}));
output.push("|");
output.push(this.transcludeTitle || "");
output.push("|");

View File

@ -72,10 +72,14 @@ Widget.prototype.execute = function() {
/*
Get the prevailing value of a context variable
name: name of variable
options: see below
Options include
params: array of {name:, value:} for each parameter
defaultValue: default value if the variable is not defined
*/
Widget.prototype.getVariable = function(name,actualParams,defaultValue) {
actualParams = actualParams || [];
Widget.prototype.getVariable = function(name,options) {
options = options || {};
var actualParams = options.params || [];
// Search up the widget tree for the variable name
var node = this;
while(node && !$tw.utils.hop(node.variables,name)) {
@ -83,7 +87,7 @@ Widget.prototype.getVariable = function(name,actualParams,defaultValue) {
}
// If we get to the root then look for a macro module
if(!node) {
return this.evaluateMacroModule(name,actualParams,defaultValue);
return this.evaluateMacroModule(name,actualParams,options.defaultValue);
}
// Get the value
var value = node.variables[name].value;
@ -126,7 +130,7 @@ Widget.prototype.substituteVariableParameters = function(text,formalParams,actua
Widget.prototype.substituteVariableReferences = function(text) {
var self = this;
return text.replace(/\$\(([^\)\$]+)\)\$/g,function(match,p1,offset,string) {
return self.getVariable(p1,null,"");
return self.getVariable(p1,{defaultValue: ""});
});
};
@ -215,7 +219,7 @@ Widget.prototype.computeAttributes = function() {
if(attribute.type === "indirect") {
value = self.wiki.getTextReference(attribute.textReference,"",self.getVariable("tiddlerTitle"));
} else if(attribute.type === "macro") {
value = self.getVariable(attribute.value.name,attribute.value.params);
value = self.getVariable(attribute.value.name,{params: attribute.value.params});
} else { // String attribute
value = attribute.value;
}