1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-10-03 01:10:45 +00:00

Refactored renderer logic to call refreshInDom on macros unconditionally

This allows the macro itself to decide whether it wants to perform an
update or not
This commit is contained in:
Jeremy Ruston 2012-03-30 16:45:24 +01:00
parent 1deb23b82d
commit a7b905cf88
4 changed files with 98 additions and 88 deletions

View File

@ -288,12 +288,12 @@ MacroNode.prototype.refresh = function(changes) {
MacroNode.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)) {
// Ask the macro to rerender itself if it can
if(this.macro.refreshInDom) {
this.macro.refreshInDom.call(this,changes);
} else {
// 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)) {
// Manually reexecute and rerender this macro
while(this.domNode.hasChildNodes()) {
this.domNode.removeChild(this.domNode.firstChild);
@ -302,11 +302,11 @@ MacroNode.prototype.refreshInDom = function(changes) {
for(t=0; t<this.content.length; t++) {
this.content[t].renderInDom(this.domNode);
}
}
} else {
// Refresh any children
for(t=0; t<this.content.length; t++) {
this.content[t].refreshInDom(changes);
} else {
// Refresh any children
for(t=0; t<this.content.length; t++) {
this.content[t].refreshInDom(changes);
}
}
}
};

View File

@ -91,17 +91,19 @@ exports.macro = {
return [editor];
},
refreshInDom: function(changes) {
// Don't refresh the editor if it contains the caret or selection
if(!window.getSelection().containsNode(this.domNode, true)) {
// Remove the previous content
while(this.domNode.hasChildNodes()) {
this.domNode.removeChild(this.domNode.firstChild);
}
// Execute the new content
this.execute(this.parents,this.tiddlerTitle);
// Render to the DOM
for(var t=0; t<this.content.length; t++) {
this.content[t].renderInDom(this.domNode);
if(this.dependencies.hasChanged(changes,this.tiddlerTitle)) {
// Don't refresh the editor if it contains the caret or selection
if(!window.getSelection().containsNode(this.domNode, true)) {
// Remove the previous content
while(this.domNode.hasChildNodes()) {
this.domNode.removeChild(this.domNode.firstChild);
}
// Execute the new content
this.execute(this.parents,this.tiddlerTitle);
// Render to the DOM
for(var t=0; t<this.content.length; t++) {
this.content[t].renderInDom(this.domNode);
}
}
}
}

View File

@ -145,27 +145,29 @@ exports.macro = {
return [content];
},
refreshInDom: function(changes) {
var needContentRefresh = true; // Avoid refreshing the content nodes if we don't need to
// If the state tiddler has changed then reset the open state
if(this.hasParameter("state") && changes.hasOwnProperty(this.params.state)) {
this.isOpen = getOpenState(this);
}
// Render the content if the slider is open and we don't have any content yet
if(this.isOpen && this.content[0].children[1].children.length === 0) {
// Remove the existing dom node for the body
this.content[0].domNode.removeChild(this.content[0].children[1].domNode);
// Get the slider content and execute it
this.content[0].children[1].children = getSliderContent(this);
this.content[0].children[1].execute(this.parents,this.tiddlerTitle);
this.content[0].children[1].renderInDom(this.content[0].domNode,null);
needContentRefresh = false; // Don't refresh the children if we've just created them
}
// Set the visibility of the slider content
this.content[0].children[1].domNode.style.display = this.isOpen ? "block" : "none";
// Refresh any children
if(needContentRefresh) {
for(var t=0; t<this.content.length; t++) {
this.content[t].refreshInDom(changes);
if(this.dependencies.hasChanged(changes,this.tiddlerTitle)) {
var needContentRefresh = true; // Avoid refreshing the content nodes if we don't need to
// If the state tiddler has changed then reset the open state
if(this.hasParameter("state") && changes.hasOwnProperty(this.params.state)) {
this.isOpen = getOpenState(this);
}
// Render the content if the slider is open and we don't have any content yet
if(this.isOpen && this.content[0].children[1].children.length === 0) {
// Remove the existing dom node for the body
this.content[0].domNode.removeChild(this.content[0].children[1].domNode);
// Get the slider content and execute it
this.content[0].children[1].children = getSliderContent(this);
this.content[0].children[1].execute(this.parents,this.tiddlerTitle);
this.content[0].children[1].renderInDom(this.content[0].domNode,null);
needContentRefresh = false; // Don't refresh the children if we've just created them
}
// Set the visibility of the slider content
this.content[0].children[1].domNode.style.display = this.isOpen ? "block" : "none";
// Refresh any children
if(needContentRefresh) {
for(var t=0; t<this.content.length; t++) {
this.content[t].refreshInDom(changes);
}
}
}
}

View File

@ -97,58 +97,64 @@ exports.macro = {
},
refreshInDom: function(changes) {
/*jslint browser: true */
// Get the tiddlers we're supposed to be displaying
var self = this,
story = JSON.parse(this.store.getTiddlerText(this.params.story)),
template = this.params.template,
t,n,domNode,
findTiddler = function (childIndex,tiddlerTitle,templateTitle) {
while(childIndex < self.content.length) {
var params = self.content[childIndex].params;
if(params.target === tiddlerTitle) {
if(!templateTitle || params.template === templateTitle) {
return childIndex;
if(this.dependencies.hasChanged(changes,this.tiddlerTitle)) {
// Get the tiddlers we're supposed to be displaying
var self = this,
story = JSON.parse(this.store.getTiddlerText(this.params.story)),
template = this.params.template,
t,n,domNode,
findTiddler = function (childIndex,tiddlerTitle,templateTitle) {
while(childIndex < self.content.length) {
var params = self.content[childIndex].params;
if(params.target === tiddlerTitle) {
if(!templateTitle || params.template === templateTitle) {
return childIndex;
}
}
childIndex++;
}
childIndex++;
}
return null;
};
for(t=0; t<story.tiddlers.length; t++) {
// See if the node we want is already there
var tiddlerNode = findTiddler(t,story.tiddlers[t].title,story.tiddlers[t].template);
if(tiddlerNode === null) {
// If not, render the tiddler
var m = Renderer.MacroNode("tiddler",
{target: story.tiddlers[t].title,template: story.tiddlers[t].template},
null,
this.store);
m.execute(this.parents,story.tiddlers[t].title);
m.renderInDom(this.domNode,this.domNode.childNodes[t]);
this.content.splice(t,0,m);
} else {
// Delete any nodes preceding the one we want
if(tiddlerNode > t) {
// First delete the DOM nodes
for(n=t; n<tiddlerNode; n++) {
domNode = this.content[n].domNode;
domNode.parentNode.removeChild(domNode);
return null;
};
for(t=0; t<story.tiddlers.length; t++) {
// See if the node we want is already there
var tiddlerNode = findTiddler(t,story.tiddlers[t].title,story.tiddlers[t].template);
if(tiddlerNode === null) {
// If not, render the tiddler
var m = Renderer.MacroNode("tiddler",
{target: story.tiddlers[t].title,template: story.tiddlers[t].template},
null,
this.store);
m.execute(this.parents,this.tiddlerTitle);
m.renderInDom(this.domNode,this.domNode.childNodes[t]);
this.content.splice(t,0,m);
} else {
// Delete any nodes preceding the one we want
if(tiddlerNode > t) {
// First delete the DOM nodes
for(n=t; n<tiddlerNode; n++) {
domNode = this.content[n].domNode;
domNode.parentNode.removeChild(domNode);
}
// Then delete the actual renderer nodes
this.content.splice(t,tiddlerNode-t);
}
// Then delete the actual renderer nodes
this.content.splice(t,tiddlerNode-t);
// Refresh the DOM node we're reusing
this.content[t].refreshInDom(changes);
}
// Refresh the DOM node we're reusing
}
// Remove any left over nodes
if(this.content.length > story.tiddlers.length) {
for(t=story.tiddlers.length; t<this.content.length; t++) {
domNode = this.content[t].domNode;
domNode.parentNode.removeChild(domNode);
}
this.content.splice(story.tiddlers.length,this.content.length-story.tiddlers.length);
}
} else {
for(t=0; t<this.content.length; t++) {
this.content[t].refreshInDom(changes);
}
}
// Remove any left over nodes
if(this.content.length > story.tiddlers.length) {
for(t=story.tiddlers.length; t<this.content.length; t++) {
domNode = this.content[t].domNode;
domNode.parentNode.removeChild(domNode);
}
this.content.splice(story.tiddlers.length,this.content.length-story.tiddlers.length);
}
}
};