mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-11-16 23:37:19 +00:00
Medium-sized refactoring of macro architecture
Now event handlers are attached to element nodes, not to macro nodes.
This commit is contained in:
@@ -14,13 +14,16 @@ Element nodes
|
||||
|
||||
var Node = require("./node.js").Node;
|
||||
|
||||
var Element = function(type,attributes,children) {
|
||||
var Element = function(type,attributes,children,options) {
|
||||
options = options || {};
|
||||
if(this instanceof Element) {
|
||||
this.type = type;
|
||||
this.attributes = attributes || {};
|
||||
this.children = children;
|
||||
this.events = options.events;
|
||||
this.eventHandler = options.eventHandler;
|
||||
} else {
|
||||
return new Element(type,attributes,children);
|
||||
return new Element(type,attributes,children,options);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -35,7 +38,10 @@ Element.prototype.clone = function() {
|
||||
childClones.push(this.children[t].clone());
|
||||
}
|
||||
}
|
||||
return new Element(this.type,this.attributes,childClones);
|
||||
return new Element(this.type,this.attributes,childClones,{
|
||||
events: this.events,
|
||||
eventHandler: this.eventHandler
|
||||
});
|
||||
};
|
||||
|
||||
Element.prototype.execute = function(parents,tiddlerTitle) {
|
||||
@@ -87,7 +93,9 @@ Element.prototype.render = function(type) {
|
||||
};
|
||||
|
||||
Element.prototype.renderInDom = function(parentDomNode,insertBefore) {
|
||||
// Create the element
|
||||
var element = document.createElement(this.type);
|
||||
// Assign the attributes
|
||||
if(this.attributes) {
|
||||
for(var a in this.attributes) {
|
||||
var v = this.attributes[a];
|
||||
@@ -104,12 +112,21 @@ Element.prototype.renderInDom = function(parentDomNode,insertBefore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Insert it into the DOM tree
|
||||
if(insertBefore) {
|
||||
parentDomNode.insertBefore(element,insertBefore);
|
||||
} else {
|
||||
parentDomNode.appendChild(element);
|
||||
}
|
||||
// Register event handlers
|
||||
if(this.events) {
|
||||
for(var e=0; e<this.events.length; e++) {
|
||||
element.addEventListener(this.events[e],this.eventHandler,false);
|
||||
}
|
||||
}
|
||||
// Save a reference to the DOM element
|
||||
this.domNode = element;
|
||||
// Render any child nodes
|
||||
if(this.children) {
|
||||
for(var t=0; t<this.children.length; t++) {
|
||||
this.children[t].renderInDom(element);
|
||||
|
||||
@@ -175,27 +175,13 @@ Macro.prototype.render = function(type) {
|
||||
};
|
||||
|
||||
Macro.prototype.renderInDom = function(parentDomNode,insertBefore) {
|
||||
this.parentDomNode = parentDomNode;
|
||||
if(this.child) {
|
||||
this.child.renderInDom(parentDomNode,insertBefore);
|
||||
this.domNode = this.child.domNode;
|
||||
this.addEventHandlers();
|
||||
this.postRenderInDom();
|
||||
}
|
||||
};
|
||||
|
||||
Macro.prototype.addEventHandlers = function() {
|
||||
if(this.info.events && this.child) {
|
||||
for(var t=0; t<this.info.events.length; t++) {
|
||||
// Register this macro node to handle the event via the handleEvent() method
|
||||
var info = this.info.events[t];
|
||||
if(typeof info === "string") {
|
||||
info = {type: info};
|
||||
}
|
||||
this.child.domNode.addEventListener(info.type,this,info.capture);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Macro.prototype.postRenderInDom = function() {
|
||||
// Do nothing, individual macros can override
|
||||
};
|
||||
@@ -215,23 +201,12 @@ Macro.prototype.refresh = function(changes) {
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
Macros that need special refreshing should override this function
|
||||
*/
|
||||
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
|
||||
var parent = this.child.domNode.parentNode,
|
||||
nextSibling = this.child.domNode.nextSibling;
|
||||
parent.removeChild(this.child.domNode);
|
||||
this.execute(this.parents,this.tiddlerTitle);
|
||||
this.child.renderInDom(parent,nextSibling);
|
||||
this.domNode = this.child.domNode;
|
||||
this.addEventHandlers();
|
||||
} else {
|
||||
if(this.child) {
|
||||
this.child.refreshInDom(changes);
|
||||
}
|
||||
if(this.child) {
|
||||
this.child.refreshInDom(changes);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user