2012-04-30 11:23:03 +00:00
|
|
|
/*\
|
2012-05-03 20:47:16 +00:00
|
|
|
title: $:/core/modules/treenodes/macro.js
|
2012-04-30 11:23:03 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: treenode
|
|
|
|
|
|
|
|
Macro node, used as the base class for macros
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jshint node: true, browser: true */
|
2012-05-04 17:49:04 +00:00
|
|
|
/*global $tw: false */
|
2012-04-30 11:23:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Node = require("./node.js").Node;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Construct a renderer node representing a macro invocation
|
|
|
|
macroName: name of the macro
|
2012-05-28 14:51:52 +00:00
|
|
|
options: see below
|
|
|
|
|
|
|
|
The options available are:
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
srcParams: a string or a hashmap of parameters (each can be a string, or a fn(tiddler,wiki) for evaluated parameters)
|
2012-05-28 14:51:52 +00:00
|
|
|
content: optional array of child nodes
|
|
|
|
wiki: reference to the WikiStore associated with this macro
|
2012-04-30 11:23:03 +00:00
|
|
|
dependencies: optional Dependencies object representing the dependencies of this macro
|
2012-05-28 14:51:52 +00:00
|
|
|
isBlock: true if this macro is being used as an HTML block
|
2012-06-04 11:07:39 +00:00
|
|
|
classes: array of classes to be assigned to the macro
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
Note that the dependencies will be evaluated if not provided.
|
|
|
|
*/
|
2012-05-28 14:51:52 +00:00
|
|
|
var Macro = function(macroName,options) {
|
|
|
|
options = options || {};
|
|
|
|
var MacroClass = options.wiki ? options.wiki.macros[macroName] : null; // Get the macro class
|
2012-04-30 11:23:03 +00:00
|
|
|
if(this instanceof Macro) {
|
|
|
|
// Save the details
|
|
|
|
this.macroName = macroName;
|
2012-05-28 14:51:52 +00:00
|
|
|
this.srcParams = options.srcParams || {};
|
|
|
|
this.content = options.content || [];
|
|
|
|
this.wiki = options.wiki;
|
|
|
|
this.dependencies = options.dependencies;
|
|
|
|
this.isBlock = options.isBlock;
|
2012-06-04 11:07:39 +00:00
|
|
|
this.classes = options.classes;
|
2012-04-30 11:23:03 +00:00
|
|
|
// Parse the macro parameters if required
|
|
|
|
if(typeof this.srcParams === "string") {
|
2012-05-28 14:51:52 +00:00
|
|
|
this.srcParams = this.parseMacroParamString(this.srcParams);
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
// Evaluate the dependencies if required
|
|
|
|
if(macroName && !this.dependencies) {
|
|
|
|
this.dependencies = this.evaluateDependencies();
|
|
|
|
}
|
|
|
|
// Get a reference to the static information about this macro
|
2012-05-28 14:51:52 +00:00
|
|
|
if(MacroClass) {
|
2012-04-30 11:23:03 +00:00
|
|
|
this.MacroClass = MacroClass;
|
|
|
|
this.info = MacroClass.prototype.info;
|
|
|
|
}
|
|
|
|
} else {
|
2012-05-04 17:49:04 +00:00
|
|
|
// If Macro() has been called without 'new' then instantiate the right macro class
|
2012-05-05 10:21:01 +00:00
|
|
|
if(!MacroClass) {
|
|
|
|
throw "Unknown macro '" + macroName + "'";
|
|
|
|
}
|
2012-05-28 14:51:52 +00:00
|
|
|
return new MacroClass(macroName,options);
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype = new Node();
|
|
|
|
Macro.prototype.constructor = Macro;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Evaluate the dependencies of this macro invocation. If the macro provides an `evaluateDependencies` method
|
|
|
|
then it is invoked to evaluate the dependencies. Otherwise it generates the dependencies based on the
|
|
|
|
macro parameters provided
|
|
|
|
*/
|
|
|
|
Macro.prototype.evaluateDependencies = function() {
|
|
|
|
// Figure out the dependencies from the metadata and parameters
|
|
|
|
var dependencies = new $tw.Dependencies();
|
|
|
|
if(this.info.dependentAll) {
|
|
|
|
dependencies.dependentAll = true;
|
|
|
|
}
|
|
|
|
if(this.info.dependentOnContextTiddler) {
|
|
|
|
dependencies.dependentOnContextTiddler = true;
|
|
|
|
}
|
|
|
|
for(var m in this.info.params) {
|
|
|
|
var paramInfo = this.info.params[m];
|
|
|
|
if(m in this.srcParams && paramInfo.type === "tiddler") {
|
|
|
|
if(typeof this.srcParams[m] === "function") {
|
|
|
|
dependencies.dependentAll = true;
|
|
|
|
} else {
|
|
|
|
dependencies.addDependency(this.srcParams[m],!paramInfo.skinny);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dependencies;
|
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.parseMacroParamString = function(paramString) {
|
|
|
|
/*jslint evil: true */
|
|
|
|
var params = {},
|
|
|
|
args = new $tw.utils.ArgParser(paramString,{defaultName: "anon", cascadeDefaults: this.info.cascadeDefaults}),
|
|
|
|
self = this,
|
|
|
|
insertParam = function(name,arg) {
|
|
|
|
if(arg.evaluated) {
|
|
|
|
params[name] = eval("(function(tiddler,wiki) {return " + arg.string + ";})");
|
|
|
|
} else {
|
|
|
|
params[name] = arg.string;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for(var m in this.info.params) {
|
|
|
|
var param = this.info.params[m],
|
|
|
|
arg;
|
|
|
|
if("byPos" in param && args.byPos[param.byPos] && (args.byPos[param.byPos].n === "anon" || args.byPos[param.byPos].n === m)) {
|
|
|
|
arg = args.byPos[param.byPos].v;
|
|
|
|
insertParam(m,arg);
|
|
|
|
} else {
|
|
|
|
arg = args.getValueByName(m);
|
|
|
|
if(!arg && param.byName === "default") {
|
|
|
|
arg = args.getValueByName("anon");
|
|
|
|
}
|
|
|
|
if(arg) {
|
|
|
|
insertParam(m,arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.hasParameter = function(name) {
|
2012-05-09 07:51:16 +00:00
|
|
|
return $tw.utils.hop(this.params,name);
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.cloneContent = function() {
|
|
|
|
var contentClones = [];
|
|
|
|
for(var t=0; t<this.content.length; t++) {
|
|
|
|
contentClones.push(this.content[t].clone());
|
|
|
|
}
|
|
|
|
return contentClones;
|
2012-05-04 17:49:04 +00:00
|
|
|
};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
Macro.prototype.clone = function() {
|
2012-05-28 14:51:52 +00:00
|
|
|
return new this.MacroClass(this.macroName,{
|
|
|
|
srcParams: this.srcParams,
|
|
|
|
content: this.cloneContent(),
|
|
|
|
wiki: this.wiki,
|
|
|
|
isBlock: this.isBlock,
|
2012-06-04 11:07:39 +00:00
|
|
|
dependencies: this.dependencies,
|
|
|
|
classes: this.classes
|
2012-05-28 14:51:52 +00:00
|
|
|
});
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.execute = function(parents,tiddlerTitle) {
|
|
|
|
parents = parents || [];
|
|
|
|
// Evaluate macro parameters to get their values
|
|
|
|
this.params = {};
|
|
|
|
var tiddler = this.wiki.getTiddler(tiddlerTitle);
|
|
|
|
if(!tiddler) {
|
|
|
|
tiddler = {title: tiddlerTitle};
|
|
|
|
}
|
|
|
|
for(var p in this.srcParams) {
|
|
|
|
if(typeof this.srcParams[p] === "function") {
|
2012-05-02 16:27:13 +00:00
|
|
|
this.params[p] = this.srcParams[p](tiddler.fields,this.wiki);
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
|
|
|
this.params[p] = this.srcParams[p];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Save the context info for use when we're refreshing it
|
|
|
|
this.tiddlerTitle = tiddlerTitle;
|
|
|
|
this.parents = parents;
|
2012-06-09 17:36:32 +00:00
|
|
|
// Execute the macro to generate its node and children, and recursively execute them
|
|
|
|
this.child = this.executeMacro.call(this);
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.render = function(type) {
|
2012-06-09 17:36:32 +00:00
|
|
|
return this.child ? this.child.render(type) : "";
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.renderInDom = function(parentDomNode,insertBefore) {
|
2012-06-09 17:36:32 +00:00
|
|
|
if(this.child) {
|
|
|
|
this.child.renderInDom(parentDomNode,insertBefore);
|
|
|
|
this.postRenderInDom();
|
2012-06-04 11:07:39 +00:00
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.postRenderInDom = function() {
|
|
|
|
// Do nothing, individual macros can override
|
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.refresh = function(changes) {
|
|
|
|
var t,
|
|
|
|
self = this;
|
|
|
|
// Check if any of the dependencies of this macro node have changed
|
|
|
|
if(this.dependencies.hasChanged(changes,this.tiddlerTitle)) {
|
|
|
|
// Re-execute the macro if so
|
|
|
|
this.execute(this.parents,this.tiddlerTitle);
|
|
|
|
} else {
|
2012-06-09 17:36:32 +00:00
|
|
|
// Refresh the child node
|
|
|
|
if(this.child) {
|
|
|
|
this.child.refresh(changes);
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-14 16:15:38 +00:00
|
|
|
/*
|
|
|
|
Macros that need special refreshing should override this function
|
|
|
|
*/
|
2012-04-30 11:23:03 +00:00
|
|
|
Macro.prototype.refreshInDom = function(changes) {
|
2012-06-19 15:47:10 +00:00
|
|
|
// Check if any of the dependencies of this macro node have changed
|
|
|
|
if(this.dependencies.hasChanged(changes,this.tiddlerTitle)) {
|
|
|
|
// Re-execute the macro if so
|
2012-06-19 17:01:39 +00:00
|
|
|
// Macros can only auto-refresh if on of their descendent child has a DOM node
|
2012-06-19 15:56:55 +00:00
|
|
|
var child = this.child;
|
|
|
|
while(!child.domNode && child.child) {
|
|
|
|
child = child.child;
|
|
|
|
}
|
|
|
|
var parentDomNode = child.domNode.parentNode,
|
|
|
|
insertBefore = child.domNode.nextSibling;
|
|
|
|
parentDomNode.removeChild(child.domNode);
|
2012-06-19 15:47:10 +00:00
|
|
|
this.execute(this.parents,this.tiddlerTitle);
|
|
|
|
this.renderInDom(parentDomNode,insertBefore);
|
|
|
|
} else {
|
|
|
|
// Refresh any child
|
|
|
|
if(this.child) {
|
|
|
|
this.child.refreshInDom(changes);
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-04 11:07:39 +00:00
|
|
|
Macro.prototype.addClass = function(className) {
|
|
|
|
this.classes = this.classes || [];
|
|
|
|
$tw.utils.pushTop(this.classes,className.split(" "));
|
|
|
|
};
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
exports.Macro = Macro;
|
|
|
|
|
|
|
|
})();
|