2012-02-16 20:36:43 +00:00
|
|
|
/*\
|
|
|
|
title: js/Renderer.js
|
|
|
|
|
2012-03-29 11:23:34 +00:00
|
|
|
Renderer objects encapsulate a tree of nodes that are capable of rendering and selectively updating an
|
|
|
|
HTML representation of themselves. The following node types are defined:
|
|
|
|
* ''MacroNode'' - represents an invocation of a macro
|
|
|
|
* ''ElementNode'' - represents a single HTML element
|
|
|
|
* ''TextNode'' - represents an HTML text node
|
|
|
|
* ''EntityNode'' - represents an HTML entity node
|
|
|
|
* ''RawNode'' - represents a chunk of unparsed HTML text
|
|
|
|
|
|
|
|
These node types are implemented with prototypal inheritance from a base Node class. One
|
|
|
|
unusual convenience in the implementation is that the node constructors can be called without an explicit
|
|
|
|
`new` keyword: the constructors check for `this` not being an instance of themselves, and recursively invoke
|
|
|
|
themselves with `new` when required.
|
|
|
|
|
|
|
|
Convenience functions are provided to wrap up the construction of some common interface elements:
|
|
|
|
* ''ErrorNode''
|
|
|
|
* ''LabelNode''
|
|
|
|
* ''SplitLabelNode''
|
2012-02-16 20:36:43 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
2012-02-17 17:31:57 +00:00
|
|
|
/*jshint node: true, browser: true */
|
2012-02-16 20:36:43 +00:00
|
|
|
"use strict";
|
|
|
|
|
2012-02-20 18:04:12 +00:00
|
|
|
var utils = require("./Utils.js"),
|
2012-02-22 23:27:38 +00:00
|
|
|
ArgParser = require("./ArgParser.js").ArgParser,
|
2012-03-01 23:59:46 +00:00
|
|
|
Dependencies = require("./Dependencies.js").Dependencies,
|
|
|
|
esprima = require("esprima");
|
2012-02-16 20:36:43 +00:00
|
|
|
|
2012-03-29 11:23:34 +00:00
|
|
|
/*
|
|
|
|
Intialise the renderer object
|
|
|
|
nodes: an array of Node objects
|
|
|
|
dependencies: an optional Dependencies object
|
|
|
|
store: a reference to the WikiStore object to use for rendering these nodes
|
|
|
|
*/
|
2012-03-03 18:39:13 +00:00
|
|
|
var Renderer = function(nodes,dependencies,store) {
|
|
|
|
this.nodes = nodes;
|
2012-03-03 18:33:16 +00:00
|
|
|
this.dependencies = dependencies;
|
|
|
|
this.store = store;
|
|
|
|
};
|
|
|
|
|
2012-03-29 11:23:34 +00:00
|
|
|
/*
|
|
|
|
The base class for all types of renderer nodes
|
|
|
|
*/
|
2012-02-20 11:06:17 +00:00
|
|
|
var Node = function(children) {
|
|
|
|
if(this instanceof Node) {
|
|
|
|
this.children = children;
|
|
|
|
} else {
|
|
|
|
return new Node(children);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype.clone = function() {
|
|
|
|
// By default we don't actually clone nodes, we just re-use them (we do clone macros and elements)
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2012-03-13 15:12:23 +00:00
|
|
|
Node.prototype.broadcastEvent = function(event) {
|
|
|
|
return true;
|
2012-03-13 15:15:33 +00:00
|
|
|
};
|
2012-03-13 15:12:23 +00:00
|
|
|
|
2012-02-20 21:00:12 +00:00
|
|
|
Node.prototype.execute =
|
|
|
|
Node.prototype.render =
|
|
|
|
Node.prototype.renderInDom =
|
|
|
|
Node.prototype.refresh =
|
|
|
|
Node.prototype.refreshInDom = function() {
|
2012-02-21 21:57:30 +00:00
|
|
|
// All these methods are no-ops by default
|
2012-02-20 11:06:17 +00:00
|
|
|
};
|
|
|
|
|
2012-02-20 19:27:37 +00:00
|
|
|
/*
|
|
|
|
Construct a renderer node representing a macro invocation
|
|
|
|
macroName: name of the macro
|
2012-02-20 21:00:12 +00:00
|
|
|
srcParams: a hashmap of parameters (each can be a string, or a fn(tiddler,store,utils) for evaluated parameters)
|
2012-02-20 19:27:37 +00:00
|
|
|
children: optional array of child nodes
|
|
|
|
store: reference to the WikiStore associated with this macro
|
2012-03-29 11:23:34 +00:00
|
|
|
dependencies: optional Dependencies object representing the dependencies of this macro
|
|
|
|
|
|
|
|
Note that the dependencies will be evaluated if not provided.
|
2012-02-20 19:27:37 +00:00
|
|
|
*/
|
|
|
|
var MacroNode = function(macroName,srcParams,children,store,dependencies) {
|
2012-02-16 20:36:43 +00:00
|
|
|
if(this instanceof MacroNode) {
|
2012-02-20 19:27:37 +00:00
|
|
|
// Save the details
|
2012-02-16 20:36:43 +00:00
|
|
|
this.macroName = macroName;
|
|
|
|
this.macro = store.macros[macroName];
|
|
|
|
this.children = children;
|
|
|
|
this.store = store;
|
2012-02-22 23:27:38 +00:00
|
|
|
this.srcParams = typeof srcParams === "string" ? this.parseMacroParamString(srcParams) : srcParams;
|
2012-03-29 11:23:34 +00:00
|
|
|
// Evaluate the dependencies if required
|
|
|
|
this.dependencies = dependencies ? dependencies : this.evaluateDependencies();
|
2012-02-16 20:36:43 +00:00
|
|
|
} else {
|
2012-02-20 19:27:37 +00:00
|
|
|
return new MacroNode(macroName,srcParams,children,store,dependencies);
|
2012-02-16 20:36:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-20 11:06:17 +00:00
|
|
|
MacroNode.prototype = new Node();
|
|
|
|
MacroNode.prototype.constructor = MacroNode;
|
|
|
|
|
2012-03-29 11:23:34 +00:00
|
|
|
/*
|
2012-03-30 12:48:30 +00:00
|
|
|
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
|
2012-03-29 11:23:34 +00:00
|
|
|
*/
|
|
|
|
MacroNode.prototype.evaluateDependencies = function() {
|
|
|
|
if(this.srcParams && this.macro) {
|
2012-03-30 12:48:30 +00:00
|
|
|
if(this.macro.evaluateDependencies) {
|
|
|
|
// Call the evaluateDependencies method if the macro provides it
|
|
|
|
return this.macro.evaluateDependencies.call(this);
|
|
|
|
} else {
|
|
|
|
// Figure out the dependencies from the metadata and parameters
|
|
|
|
var dependencies = new Dependencies();
|
|
|
|
if(this.macro.dependentAll) {
|
|
|
|
dependencies.dependentAll = true;
|
|
|
|
}
|
|
|
|
if(this.macro.dependentOnContextTiddler) {
|
|
|
|
dependencies.dependentOnContextTiddler = true;
|
|
|
|
}
|
|
|
|
for(var m in this.macro.params) {
|
|
|
|
var paramInfo = this.macro.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);
|
|
|
|
}
|
2012-03-29 11:23:34 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-30 12:48:30 +00:00
|
|
|
return dependencies;
|
2012-03-29 11:23:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-22 23:27:38 +00:00
|
|
|
MacroNode.prototype.parseMacroParamString = function(paramString) {
|
2012-03-01 23:59:46 +00:00
|
|
|
/*jslint evil: true */
|
2012-02-22 23:27:38 +00:00
|
|
|
var params = {},
|
|
|
|
args = new ArgParser(paramString,{defaultName: "anon", cascadeDefaults: this.macro.cascadeDefaults}),
|
|
|
|
self = this,
|
|
|
|
insertParam = function(name,arg) {
|
|
|
|
if(arg.evaluated) {
|
2012-03-01 23:59:46 +00:00
|
|
|
params[name] = eval(esprima.generate( // (function(tiddler,store,utils) {return {paramOne: 1};})
|
2012-02-22 23:27:38 +00:00
|
|
|
{
|
2012-03-01 22:47:31 +00:00
|
|
|
"type": "Program",
|
|
|
|
"body": [
|
|
|
|
{
|
|
|
|
"type": "ExpressionStatement",
|
|
|
|
"expression": {
|
|
|
|
"type": "FunctionExpression",
|
|
|
|
"id": null,
|
|
|
|
"params": [
|
|
|
|
{
|
|
|
|
"type": "Identifier",
|
|
|
|
"name": "tiddler"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "Identifier",
|
|
|
|
"name": "store"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "Identifier",
|
|
|
|
"name": "utils"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"body": {
|
|
|
|
"type": "BlockStatement",
|
|
|
|
"body": [
|
|
|
|
{
|
|
|
|
"type": "ReturnStatement",
|
2012-03-01 23:59:46 +00:00
|
|
|
"argument": esprima.parse("(" + arg.string + ")").body[0].expression
|
2012-03-01 22:47:31 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2012-02-22 23:27:38 +00:00
|
|
|
}
|
2012-03-01 23:59:46 +00:00
|
|
|
));
|
2012-02-22 23:27:38 +00:00
|
|
|
} else {
|
|
|
|
params[name] = arg.string;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for(var m in this.macro.params) {
|
|
|
|
var param = this.macro.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;
|
|
|
|
};
|
|
|
|
|
2012-03-05 13:36:53 +00:00
|
|
|
MacroNode.prototype.hasParameter = function(name) {
|
|
|
|
return this.params.hasOwnProperty(name);
|
|
|
|
};
|
|
|
|
|
2012-02-16 20:36:43 +00:00
|
|
|
MacroNode.prototype.clone = function() {
|
2012-02-20 19:27:37 +00:00
|
|
|
return new MacroNode(this.macroName,this.srcParams,this.cloneChildren(),this.store,this.dependencies);
|
2012-02-16 20:36:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MacroNode.prototype.cloneChildren = function() {
|
|
|
|
var childClones;
|
|
|
|
if(this.children) {
|
|
|
|
childClones = [];
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
childClones.push(this.children[t].clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return childClones;
|
|
|
|
};
|
|
|
|
|
2012-03-29 13:30:22 +00:00
|
|
|
MacroNode.prototype.execute = function(parents,tiddlerTitle) {
|
2012-02-20 19:27:37 +00:00
|
|
|
// Evaluate macro parameters to get their values
|
|
|
|
this.params = {};
|
2012-03-29 13:30:22 +00:00
|
|
|
var tiddler = this.store.getTiddler(tiddlerTitle);
|
|
|
|
if(!tiddler) {
|
|
|
|
tiddler = {title: tiddlerTitle};
|
|
|
|
}
|
2012-02-20 19:27:37 +00:00
|
|
|
for(var p in this.srcParams) {
|
|
|
|
if(typeof this.srcParams[p] === "function") {
|
|
|
|
this.params[p] = this.srcParams[p](tiddler,this.store,utils);
|
|
|
|
} else {
|
|
|
|
this.params[p] = this.srcParams[p];
|
|
|
|
}
|
2012-02-16 20:36:43 +00:00
|
|
|
}
|
|
|
|
// Save the context tiddler
|
2012-03-29 13:30:22 +00:00
|
|
|
this.tiddlerTitle = tiddlerTitle;
|
2012-02-19 17:20:16 +00:00
|
|
|
// Save a reference to the array of parents
|
2012-02-20 18:04:12 +00:00
|
|
|
this.parents = parents || [];
|
2012-02-16 20:36:43 +00:00
|
|
|
// Render the macro to get its content
|
2012-02-21 21:57:30 +00:00
|
|
|
this.content = this.macro.execute.call(this);
|
2012-02-16 20:36:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MacroNode.prototype.render = function(type) {
|
|
|
|
var output = [];
|
|
|
|
for(var t=0; t<this.content.length; t++) {
|
|
|
|
output.push(this.content[t].render(type));
|
|
|
|
}
|
|
|
|
return output.join("");
|
|
|
|
};
|
|
|
|
|
2012-03-11 19:08:50 +00:00
|
|
|
MacroNode.prototype.renderInDom = function(parentDomNode,insertBefore) {
|
2012-02-16 20:36:43 +00:00
|
|
|
// Create the wrapper node for the macro
|
2012-02-17 12:34:56 +00:00
|
|
|
var macroContainer = document.createElement(this.macro.wrapperTag || "span");
|
2012-02-16 20:36:43 +00:00
|
|
|
this.domNode = macroContainer;
|
|
|
|
if(insertBefore) {
|
2012-03-11 19:08:50 +00:00
|
|
|
parentDomNode.insertBefore(macroContainer,insertBefore);
|
2012-02-16 20:36:43 +00:00
|
|
|
} else {
|
2012-03-11 19:08:50 +00:00
|
|
|
parentDomNode.appendChild(macroContainer);
|
2012-02-16 20:36:43 +00:00
|
|
|
}
|
|
|
|
// Add some debugging information to it
|
|
|
|
macroContainer.setAttribute("data-tw-macro",this.macroName);
|
2012-03-31 17:10:16 +00:00
|
|
|
// Ask the macro to add event handlers to the node
|
|
|
|
if(this.macro.addEventHandlers) {
|
|
|
|
this.macro.addEventHandlers.call(this);
|
|
|
|
} else {
|
|
|
|
for(var e in this.macro.events) {
|
|
|
|
// Register this macro node to handle the event via the handleEvent() method
|
|
|
|
macroContainer.addEventListener(e,this,false);
|
|
|
|
}
|
2012-02-16 20:36:43 +00:00
|
|
|
}
|
|
|
|
// Render the content of the macro
|
|
|
|
for(var t=0; t<this.content.length; t++) {
|
|
|
|
this.content[t].renderInDom(macroContainer);
|
|
|
|
}
|
2012-03-31 17:10:16 +00:00
|
|
|
// Call the macro renderInDom method if it has one
|
|
|
|
if(this.macro.renderInDom) {
|
|
|
|
this.macro.renderInDom.call(this);
|
|
|
|
}
|
2012-02-16 20:36:43 +00:00
|
|
|
};
|
|
|
|
|
2012-02-17 12:34:56 +00:00
|
|
|
MacroNode.prototype.refresh = function(changes) {
|
|
|
|
var t,
|
2012-02-17 14:11:25 +00:00
|
|
|
self = this;
|
2012-02-17 12:34:56 +00:00
|
|
|
// Check if any of the dependencies of this macro node have changed
|
2012-03-30 12:48:30 +00:00
|
|
|
if(this.dependencies.hasChanged(changes,this.tiddlerTitle)) {
|
2012-02-17 12:34:56 +00:00
|
|
|
// Re-execute the macro if so
|
2012-03-29 13:30:22 +00:00
|
|
|
this.execute(this.parents,this.tiddlerTitle);
|
2012-02-17 12:34:56 +00:00
|
|
|
} else {
|
|
|
|
// Refresh any children
|
|
|
|
for(t=0; t<this.content.length; t++) {
|
|
|
|
this.content[t].refresh(changes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-16 20:36:43 +00:00
|
|
|
MacroNode.prototype.refreshInDom = function(changes) {
|
|
|
|
var t,
|
2012-02-17 14:11:25 +00:00
|
|
|
self = this;
|
2012-03-30 15:45:24 +00:00
|
|
|
// Ask the macro to rerender itself if it can
|
|
|
|
if(this.macro.refreshInDom) {
|
|
|
|
this.macro.refreshInDom.call(this,changes);
|
|
|
|
} else {
|
|
|
|
// Check if any of the dependencies of this macro node have changed
|
|
|
|
if(this.dependencies.hasChanged(changes,this.tiddlerTitle)) {
|
2012-02-16 20:36:43 +00:00
|
|
|
// Manually reexecute and rerender this macro
|
|
|
|
while(this.domNode.hasChildNodes()) {
|
|
|
|
this.domNode.removeChild(this.domNode.firstChild);
|
|
|
|
}
|
2012-03-29 13:30:22 +00:00
|
|
|
this.execute(this.parents,this.tiddlerTitle);
|
2012-02-16 20:36:43 +00:00
|
|
|
for(t=0; t<this.content.length; t++) {
|
|
|
|
this.content[t].renderInDom(this.domNode);
|
|
|
|
}
|
2012-03-30 15:45:24 +00:00
|
|
|
} else {
|
|
|
|
// Refresh any children
|
|
|
|
for(t=0; t<this.content.length; t++) {
|
|
|
|
this.content[t].refreshInDom(changes);
|
|
|
|
}
|
2012-02-16 20:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-13 15:12:23 +00:00
|
|
|
MacroNode.prototype.handleEvent = function(event) {
|
|
|
|
return this.macro.events[event.type].call(this,event);
|
|
|
|
};
|
|
|
|
|
|
|
|
MacroNode.prototype.broadcastEvent = function(event) {
|
|
|
|
if(this.macro.events && this.macro.events.hasOwnProperty(event.type)) {
|
|
|
|
if(!this.handleEvent(event)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(this.content) {
|
|
|
|
for(var t=0; t<this.content.length; t++) {
|
|
|
|
if(!this.content[t].broadcastEvent(event)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2012-02-16 20:36:43 +00:00
|
|
|
var ElementNode = function(type,attributes,children) {
|
|
|
|
if(this instanceof ElementNode) {
|
|
|
|
this.type = type;
|
|
|
|
this.attributes = attributes;
|
|
|
|
this.children = children;
|
|
|
|
} else {
|
|
|
|
return new ElementNode(type,attributes,children);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-20 11:06:17 +00:00
|
|
|
ElementNode.prototype = new Node();
|
|
|
|
ElementNode.prototype.constructor = ElementNode;
|
|
|
|
|
2012-02-16 20:36:43 +00:00
|
|
|
ElementNode.prototype.clone = function() {
|
|
|
|
var childClones;
|
|
|
|
if(this.children) {
|
|
|
|
childClones = [];
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
childClones.push(this.children[t].clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new ElementNode(this.type,this.attributes,childClones);
|
|
|
|
};
|
|
|
|
|
2012-03-29 13:30:22 +00:00
|
|
|
ElementNode.prototype.execute = function(parents,tiddlerTitle) {
|
2012-02-16 20:36:43 +00:00
|
|
|
if(this.children) {
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
2012-03-29 13:30:22 +00:00
|
|
|
this.children[t].execute(parents,tiddlerTitle);
|
2012-02-16 20:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ElementNode.prototype.render = function(type) {
|
|
|
|
var isHtml = type === "text/html",
|
|
|
|
output = [];
|
|
|
|
if(isHtml) {
|
|
|
|
output.push("<",this.type);
|
|
|
|
if(this.attributes) {
|
|
|
|
for(var a in this.attributes) {
|
|
|
|
var v = this.attributes[a];
|
|
|
|
if(v !== undefined) {
|
|
|
|
if(v instanceof Array) {
|
|
|
|
v = v.join(" ");
|
|
|
|
} else if(typeof v === "object") {
|
|
|
|
var s = [];
|
|
|
|
for(var p in v) {
|
|
|
|
s.push(p + ":" + v[p] + ";");
|
|
|
|
}
|
|
|
|
v = s.join("");
|
|
|
|
}
|
|
|
|
output.push(" ",a,"='",utils.htmlEncode(v),"'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output.push(">");
|
|
|
|
}
|
|
|
|
if(this.children) {
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
output.push(this.children[t].render(type));
|
|
|
|
}
|
|
|
|
if(isHtml) {
|
|
|
|
output.push("</",this.type,">");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output.join("");
|
|
|
|
};
|
|
|
|
|
2012-03-11 19:08:50 +00:00
|
|
|
ElementNode.prototype.renderInDom = function(parentDomNode,insertBefore) {
|
2012-02-16 20:36:43 +00:00
|
|
|
var element = document.createElement(this.type);
|
|
|
|
if(this.attributes) {
|
|
|
|
for(var a in this.attributes) {
|
|
|
|
var v = this.attributes[a];
|
|
|
|
if(v !== undefined) {
|
|
|
|
if(v instanceof Array) { // Ahem, could there be arrays other than className?
|
|
|
|
element.className = v.join(" ");
|
|
|
|
} else if (typeof v === "object") { // ...or objects other than style?
|
|
|
|
for(var p in v) {
|
|
|
|
element.style[p] = v[p];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
element.setAttribute(a,v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-11 19:08:50 +00:00
|
|
|
if(insertBefore) {
|
|
|
|
parentDomNode.insertBefore(element,insertBefore);
|
|
|
|
} else {
|
|
|
|
parentDomNode.appendChild(element);
|
|
|
|
}
|
2012-03-05 13:36:53 +00:00
|
|
|
this.domNode = element;
|
2012-02-16 20:36:43 +00:00
|
|
|
if(this.children) {
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
this.children[t].renderInDom(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-17 12:34:56 +00:00
|
|
|
ElementNode.prototype.refresh = function(changes) {
|
|
|
|
if(this.children) {
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
this.children[t].refresh(changes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-16 20:36:43 +00:00
|
|
|
ElementNode.prototype.refreshInDom = function(changes) {
|
|
|
|
if(this.children) {
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
this.children[t].refreshInDom(changes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-13 15:12:23 +00:00
|
|
|
ElementNode.prototype.broadcastEvent = function(event) {
|
|
|
|
if(this.children) {
|
|
|
|
for(var t=0; t<this.children.length; t++) {
|
|
|
|
if(!this.children[t].broadcastEvent(event)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2012-02-16 20:36:43 +00:00
|
|
|
var TextNode = function(text) {
|
|
|
|
if(this instanceof TextNode) {
|
|
|
|
this.text = text;
|
|
|
|
} else {
|
|
|
|
return new TextNode(text);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-20 11:06:17 +00:00
|
|
|
TextNode.prototype = new Node();
|
|
|
|
TextNode.prototype.constructor = TextNode;
|
2012-02-16 20:36:43 +00:00
|
|
|
|
|
|
|
TextNode.prototype.render = function(type) {
|
|
|
|
return type === "text/html" ? utils.htmlEncode(this.text) : this.text;
|
|
|
|
};
|
|
|
|
|
|
|
|
TextNode.prototype.renderInDom = function(domNode) {
|
2012-03-05 13:36:53 +00:00
|
|
|
this.domNode = document.createTextNode(this.text);
|
|
|
|
domNode.appendChild(this.domNode);
|
2012-02-16 20:36:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var EntityNode = function(entity) {
|
|
|
|
if(this instanceof EntityNode) {
|
|
|
|
this.entity = entity;
|
|
|
|
} else {
|
|
|
|
return new EntityNode(entity);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-20 11:06:17 +00:00
|
|
|
EntityNode.prototype = new Node();
|
|
|
|
EntityNode.prototype.constructor = EntityNode;
|
2012-02-16 20:36:43 +00:00
|
|
|
|
|
|
|
EntityNode.prototype.render = function(type) {
|
|
|
|
return type === "text/html" ? this.entity : utils.entityDecode(this.entity);
|
|
|
|
};
|
|
|
|
|
|
|
|
EntityNode.prototype.renderInDom = function(domNode) {
|
2012-03-05 13:36:53 +00:00
|
|
|
this.domNode = document.createTextNode(utils.entityDecode(this.entity));
|
|
|
|
domNode.appendChild(this.domNode);
|
2012-02-16 20:36:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var RawNode = function(html) {
|
|
|
|
if(this instanceof RawNode) {
|
|
|
|
this.html = html;
|
|
|
|
} else {
|
|
|
|
return new RawNode(html);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-20 11:06:17 +00:00
|
|
|
RawNode.prototype = new Node();
|
|
|
|
RawNode.prototype.constructor = RawNode;
|
2012-02-16 20:36:43 +00:00
|
|
|
|
|
|
|
RawNode.prototype.render = function(type) {
|
|
|
|
return this.html;
|
|
|
|
};
|
|
|
|
|
|
|
|
RawNode.prototype.renderInDom = function(domNode) {
|
2012-03-05 13:36:53 +00:00
|
|
|
this.domNode = document.createElement("div");
|
|
|
|
this.domNode.innerHTML = this.html;
|
|
|
|
domNode.appendChild(this.domNode);
|
2012-02-16 20:36:43 +00:00
|
|
|
};
|
|
|
|
|
2012-03-02 12:09:06 +00:00
|
|
|
/*
|
|
|
|
Static method to construct an error message
|
|
|
|
*/
|
|
|
|
var ErrorNode = function(text) {
|
|
|
|
return new ElementNode("span",{
|
2012-03-05 15:13:25 +00:00
|
|
|
"class": ["label","label-important"]
|
2012-03-02 12:09:06 +00:00
|
|
|
},[
|
|
|
|
new TextNode(text)
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
2012-02-16 20:36:43 +00:00
|
|
|
/*
|
|
|
|
Static method to construct a label
|
|
|
|
*/
|
|
|
|
var LabelNode = function(type,value,classes) {
|
2012-03-01 22:47:31 +00:00
|
|
|
classes = (classes || []).slice(0);
|
|
|
|
classes.push("label");
|
|
|
|
return new ElementNode("span",{
|
|
|
|
"class": classes,
|
2012-02-16 20:36:43 +00:00
|
|
|
"data-tw-label-type": type
|
2012-03-01 22:47:31 +00:00
|
|
|
},value);
|
2012-02-16 20:36:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Static method to construct a split label
|
|
|
|
*/
|
|
|
|
var SplitLabelNode = function(type,left,right,classes) {
|
|
|
|
classes = (classes || []).slice(0);
|
|
|
|
classes.push("splitLabel");
|
2012-02-20 15:42:21 +00:00
|
|
|
return new ElementNode("span",{
|
2012-02-16 20:36:43 +00:00
|
|
|
"class": classes
|
|
|
|
},[
|
2012-02-20 15:42:21 +00:00
|
|
|
new ElementNode("span",{
|
2012-02-16 20:36:43 +00:00
|
|
|
"class": ["splitLabelLeft"],
|
|
|
|
"data-tw-label-type": type
|
|
|
|
},left),
|
2012-02-20 15:42:21 +00:00
|
|
|
new ElementNode("span",{
|
2012-02-16 20:36:43 +00:00
|
|
|
"class": ["splitLabelRight"]
|
|
|
|
},right)
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
2012-03-03 18:33:16 +00:00
|
|
|
Renderer.MacroNode = MacroNode;
|
|
|
|
Renderer.ElementNode = ElementNode;
|
|
|
|
Renderer.TextNode = TextNode;
|
|
|
|
Renderer.EntityNode = EntityNode;
|
|
|
|
Renderer.ErrorNode = ErrorNode;
|
|
|
|
Renderer.LabelNode = LabelNode;
|
|
|
|
Renderer.SplitLabelNode = SplitLabelNode;
|
2012-02-16 20:36:43 +00:00
|
|
|
|
|
|
|
exports.Renderer = Renderer;
|
|
|
|
|
|
|
|
})();
|