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;
|
|
|
|
// Execute the macro to generate its children, and recursively execute them
|
|
|
|
this.children = this.executeMacro.call(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.render = function(type) {
|
|
|
|
var output = [];
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
output.push(this.children[t].render(type));
|
|
|
|
}
|
|
|
|
return output.join("");
|
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.renderInDom = function(parentDomNode,insertBefore) {
|
|
|
|
// Create the wrapper node for the macro
|
2012-05-28 14:51:52 +00:00
|
|
|
var domNode = document.createElement(this.isBlock ? "div" : "span");
|
2012-04-30 11:23:03 +00:00
|
|
|
this.domNode = domNode;
|
|
|
|
if(insertBefore) {
|
|
|
|
parentDomNode.insertBefore(domNode,insertBefore);
|
|
|
|
} else {
|
|
|
|
parentDomNode.appendChild(domNode);
|
|
|
|
}
|
2012-06-04 11:07:39 +00:00
|
|
|
// Add classes and some debugging information to it
|
|
|
|
if(this.classes) {
|
|
|
|
domNode.setAttribute("class",this.classes.join(" "));
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
domNode.setAttribute("data-tw-macro",this.macroName);
|
|
|
|
// Ask the macro to add event handlers to the node
|
|
|
|
this.addEventHandlers();
|
|
|
|
// Render the content of the macro
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
this.children[t].renderInDom(domNode);
|
|
|
|
}
|
|
|
|
this.postRenderInDom();
|
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.addEventHandlers = function() {
|
|
|
|
if(this.info.events) {
|
|
|
|
for(var t=0; t<this.info.events.length; t++) {
|
|
|
|
// Register this macro node to handle the event via the handleEvent() method
|
|
|
|
this.domNode.addEventListener(this.info.events[t],this,false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.broadcastEvent = function(event) {
|
|
|
|
if(!this.handleEvent(event)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
if(!this.children[t].broadcastEvent(event)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
|
|
|
// Refresh any children
|
|
|
|
for(t=0; t<this.children.length; t++) {
|
|
|
|
this.children[t].refresh(changes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Macro.prototype.refreshInDom = 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)) {
|
|
|
|
// Manually reexecute and rerender this macro
|
|
|
|
while(this.domNode.hasChildNodes()) {
|
|
|
|
this.domNode.removeChild(this.domNode.firstChild);
|
|
|
|
}
|
|
|
|
this.execute(this.parents,this.tiddlerTitle);
|
|
|
|
for(t=0; t<this.children.length; t++) {
|
|
|
|
this.children[t].renderInDom(this.domNode);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Refresh any children
|
|
|
|
for(t=0; t<this.children.length; t++) {
|
|
|
|
this.children[t].refreshInDom(changes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
})();
|