Refactor link widget with tidier event handler

print-window-tiddler
Jeremy Ruston 2013-10-25 23:22:46 +01:00
rodzic ad518c181d
commit 068befb24f
1 zmienionych plików z 18 dodań i 14 usunięć

Wyświetl plik

@ -74,24 +74,28 @@ LinkWidget.prototype.renderLink = function(parent,nextSibling) {
wikiLinkText = wikiLinkText.replace("$uri_doubleencoded$",encodeURIComponent(encodeURIComponent(this.to))); wikiLinkText = wikiLinkText.replace("$uri_doubleencoded$",encodeURIComponent(encodeURIComponent(this.to)));
domNode.setAttribute("href",wikiLinkText); domNode.setAttribute("href",wikiLinkText);
// Add a click event handler // Add a click event handler
domNode.addEventListener("click",function (event) { $tw.utils.addEventListeners(domNode,[
{name: "click", handlerObject: this, handlerMethod: "handleClickEvent"}
]);
// Insert the link into the DOM and render any children
parent.insertBefore(domNode,nextSibling);
this.renderChildren(domNode,null);
this.domNodes.push(domNode);
};
LinkWidget.prototype.handleClickEvent = function (event) {
// Send the click on it's way as a navigate event // Send the click on it's way as a navigate event
var bounds = domNode.getBoundingClientRect(); var bounds = this.domNodes[0].getBoundingClientRect();
self.dispatchEvent({ this.dispatchEvent({
type: "tw-navigate", type: "tw-navigate",
navigateTo: self.to, navigateTo: this.to,
navigateFromNode: self, navigateFromNode: this,
navigateFromClientRect: { top: bounds.top, left: bounds.left, width: bounds.width, right: bounds.right, bottom: bounds.bottom, height: bounds.height navigateFromClientRect: { top: bounds.top, left: bounds.left, width: bounds.width, right: bounds.right, bottom: bounds.bottom, height: bounds.height
} }
}); });
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
return false; return false;
},false);
// Insert the link into the DOM and render any children
parent.insertBefore(domNode,nextSibling);
this.renderChildren(domNode,null);
this.domNodes.push(domNode);
}; };
/* /*