1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-28 15:08:46 +00:00

Use prototypal inheritance to speed up the inheritance of widget variables

Now we reuse the prototype mechanism to let javascript do the work of
searching up the parent chain.
This commit is contained in:
Jermolene 2013-11-13 21:25:45 +00:00
parent a8129874fb
commit fc6dd83f2f

View File

@ -38,8 +38,10 @@ Widget.prototype.initialise = function(parseTreeNode,options) {
// Save widget info // Save widget info
this.parseTreeNode = parseTreeNode; this.parseTreeNode = parseTreeNode;
this.wiki = options.wiki; this.wiki = options.wiki;
this.variables = options.variables || {};
this.parentWidget = options.parentWidget; this.parentWidget = options.parentWidget;
this.variablesConstructor = function() {};
this.variablesConstructor.prototype = this.parentWidget ? this.parentWidget.variables : {};
this.variables = new this.variablesConstructor();
this.document = options.document; this.document = options.document;
this.attributes = {}; this.attributes = {};
this.children = []; this.children = [];
@ -67,6 +69,16 @@ Widget.prototype.execute = function() {
this.makeChildWidgets(); this.makeChildWidgets();
}; };
/*
Set the value of a context variable
name: name of the variable
value: value of the variable
params: array of {name:, default:} for each parameter
*/
Widget.prototype.setVariable = function(name,value,params) {
this.variables[name] = {value: value, params: params};
};
/* /*
Get the prevailing value of a context variable Get the prevailing value of a context variable
name: name of variable name: name of variable
@ -78,19 +90,14 @@ defaultValue: default value if the variable is not defined
Widget.prototype.getVariable = function(name,options) { Widget.prototype.getVariable = function(name,options) {
options = options || {}; options = options || {};
var actualParams = options.params || []; var actualParams = options.params || [];
// Search up the widget tree for the variable name // If the variable doesn't exist then look for a macro module
var node = this; if(!(name in this.variables)) {
while(node && !$tw.utils.hop(node.variables,name)) {
node = node.parentWidget;
}
// If we get to the root then look for a macro module
if(!node) {
return this.evaluateMacroModule(name,actualParams,options.defaultValue); return this.evaluateMacroModule(name,actualParams,options.defaultValue);
} }
// Get the value var variable = this.variables[name],
var value = node.variables[name].value || ""; value = variable.value || "";
// Substitute any parameters specified in the definition // Substitute any parameters specified in the definition
value = this.substituteVariableParameters(value,node.variables[name].params,actualParams); value = this.substituteVariableParameters(value,variable.params,actualParams);
value = this.substituteVariableReferences(value); value = this.substituteVariableReferences(value);
return value; return value;
}; };
@ -166,16 +173,6 @@ Widget.prototype.evaluateMacroModule = function(name,actualParams,defaultValue)
} }
}; };
/*
Set the value of a context variable
name: name of the variable
value: value of the variable
params: array of {name:, default:} for each parameter
*/
Widget.prototype.setVariable = function(name,value,params) {
this.variables[name] = {value: value, params: params};
};
/* /*
Check whether a given context variable value exists in the parent chain Check whether a given context variable value exists in the parent chain
*/ */