2012-12-13 21:34:31 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/rendertree/wikirendertree.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: global
|
|
|
|
|
|
|
|
Wiki text render tree
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create a render tree object for a parse tree
|
|
|
|
*/
|
|
|
|
var WikiRenderTree = function(parser,options) {
|
|
|
|
this.parser = parser;
|
|
|
|
this.wiki = options.wiki;
|
2012-12-26 22:02:59 +00:00
|
|
|
// Hashmap of the renderer classes
|
|
|
|
if(!this.rendererClasses) {
|
|
|
|
WikiRenderTree.prototype.rendererClasses = $tw.modules.applyMethods("wikirenderer");
|
|
|
|
}
|
|
|
|
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Generate the full render tree for this parse tree
|
|
|
|
renderContext: see below
|
|
|
|
An renderContext consists of these fields:
|
|
|
|
tiddlerTitle: title of the tiddler providing the context
|
|
|
|
parentContext: reference back to previous context in the stack
|
|
|
|
*/
|
|
|
|
WikiRenderTree.prototype.execute = function(renderContext) {
|
2012-12-29 23:17:09 +00:00
|
|
|
renderContext = renderContext || {};
|
2012-12-13 21:34:31 +00:00
|
|
|
this.rendererTree = this.createRenderers(renderContext,this.parser.tree);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create an array of renderers for an array of parse tree nodes
|
|
|
|
*/
|
|
|
|
WikiRenderTree.prototype.createRenderers = function(renderContext,parseTreeNodes) {
|
|
|
|
var rendererNodes = [];
|
2012-12-15 17:34:48 +00:00
|
|
|
if(parseTreeNodes) {
|
|
|
|
for(var t=0; t<parseTreeNodes.length; t++) {
|
|
|
|
rendererNodes.push(this.createRenderer(renderContext,parseTreeNodes[t]));
|
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
return rendererNodes;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create a renderer node for a parse tree node
|
|
|
|
*/
|
|
|
|
WikiRenderTree.prototype.createRenderer = function(renderContext,parseTreeNode) {
|
2012-12-26 22:02:59 +00:00
|
|
|
var RenderNodeClass = this.rendererClasses[parseTreeNode.type];
|
2012-12-13 21:34:31 +00:00
|
|
|
return new RenderNodeClass(this,renderContext,parseTreeNode);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render as a string
|
|
|
|
*/
|
|
|
|
WikiRenderTree.prototype.render = function(type) {
|
|
|
|
var output = [];
|
|
|
|
$tw.utils.each(this.rendererTree,function(node) {
|
|
|
|
if(node.render) {
|
|
|
|
output.push(node.render(type));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return output.join("");
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render to the DOM
|
|
|
|
*/
|
|
|
|
WikiRenderTree.prototype.renderInDom = function(container) {
|
|
|
|
this.container = container;
|
|
|
|
$tw.utils.each(this.rendererTree,function(node) {
|
|
|
|
if(node.renderInDom) {
|
|
|
|
container.appendChild(node.renderInDom());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Update the DOM rendering in the light of a set of changes
|
|
|
|
*/
|
|
|
|
WikiRenderTree.prototype.refreshInDom = function(changes) {
|
|
|
|
$tw.utils.each(this.rendererTree,function(node) {
|
|
|
|
if(node.refreshInDom) {
|
|
|
|
node.refreshInDom(changes);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.WikiRenderTree = WikiRenderTree;
|
|
|
|
|
|
|
|
})();
|