From 4d6f8ab8eb27103dfeab2a1599c5a4709be0a0cf Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Mon, 20 Feb 2012 11:06:17 +0000 Subject: [PATCH] Introduced new base class for Renderer nodes Making the code a bit simpler --- js/Renderer.js | 88 +++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 48 deletions(-) diff --git a/js/Renderer.js b/js/Renderer.js index bd859409e..2bd36176a 100644 --- a/js/Renderer.js +++ b/js/Renderer.js @@ -11,6 +11,34 @@ Renderer objects var utils = require("./Utils.js"); +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; +}; + +Node.prototype.execute = function(parents,tiddler) { +}; + +Node.prototype.render = function(type) { +}; + +Node.prototype.renderInDom = function(domNode) { +}; + +Node.prototype.refresh = function(changes) { +}; + +Node.prototype.refreshInDom = function(changes) { +}; + var MacroNode = function(macroName,paramFn,children,dependencies,store) { if(this instanceof MacroNode) { this.macroName = macroName; @@ -24,6 +52,9 @@ var MacroNode = function(macroName,paramFn,children,dependencies,store) { } }; +MacroNode.prototype = new Node(); +MacroNode.prototype.constructor = MacroNode; + MacroNode.prototype.clone = function() { return new MacroNode(this.macroName,this.paramFn,this.cloneChildren(),this.dependencies,this.store); }; @@ -140,6 +171,9 @@ var ElementNode = function(type,attributes,children) { } }; +ElementNode.prototype = new Node(); +ElementNode.prototype.constructor = ElementNode; + ElementNode.prototype.clone = function() { var childClones; if(this.children) { @@ -244,14 +278,8 @@ var TextNode = function(text) { } }; -TextNode.prototype.clone = function() { - // We don't actually clone text nodes, instead we re-use them - return this; -}; - -TextNode.prototype.execute = function(parents,tiddler) { - // Text nodes don't need executing -}; +TextNode.prototype = new Node(); +TextNode.prototype.constructor = TextNode; TextNode.prototype.render = function(type) { return type === "text/html" ? utils.htmlEncode(this.text) : this.text; @@ -261,14 +289,6 @@ TextNode.prototype.renderInDom = function(domNode) { domNode.appendChild(document.createTextNode(this.text)); }; -TextNode.prototype.refresh = function(changes) { - // Text nodes don't need refreshing -}; - -TextNode.prototype.refreshInDom = function(changes) { - // Text nodes don't need refreshing -}; - var EntityNode = function(entity) { if(this instanceof EntityNode) { this.entity = entity; @@ -277,14 +297,8 @@ var EntityNode = function(entity) { } }; -EntityNode.prototype.clone = function() { - // We don't actually clone entity nodes, instead we re-use them - return this; -}; - -EntityNode.prototype.execute = function(parents,tiddler) { - // Entity nodes don't need executing -}; +EntityNode.prototype = new Node(); +EntityNode.prototype.constructor = EntityNode; EntityNode.prototype.render = function(type) { return type === "text/html" ? this.entity : utils.entityDecode(this.entity); @@ -294,14 +308,6 @@ EntityNode.prototype.renderInDom = function(domNode) { domNode.appendChild(document.createTextNode(utils.entityDecode(this.entity))); }; -EntityNode.prototype.refresh = function(changes) { - // Entity nodes don't need refreshing -}; - -EntityNode.prototype.refreshInDom = function(changes) { - // Entity nodes don't need refreshing -}; - var RawNode = function(html) { if(this instanceof RawNode) { this.html = html; @@ -310,14 +316,8 @@ var RawNode = function(html) { } }; -RawNode.prototype.clone = function() { - // We don't actually clone raw nodes, instead we re-use them - return this; -}; - -RawNode.prototype.execute = function(parents,tiddler) { - // Raw nodes don't need executing -}; +RawNode.prototype = new Node(); +RawNode.prototype.constructor = RawNode; RawNode.prototype.render = function(type) { return this.html; @@ -329,14 +329,6 @@ RawNode.prototype.renderInDom = function(domNode) { domNode.appendChild(div); }; -RawNode.prototype.refresh = function(changes) { - // Raw nodes don't need refreshing -}; - -RawNode.prototype.refreshInDom = function(changes) { - // Raw nodes don't need refreshing -}; - /* Static method to construct a label */