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,26 +74,30 @@ LinkWidget.prototype.renderLink = function(parent,nextSibling) {
wikiLinkText = wikiLinkText.replace("$uri_doubleencoded$",encodeURIComponent(encodeURIComponent(this.to)));
domNode.setAttribute("href",wikiLinkText);
// Add a click event handler
domNode.addEventListener("click",function (event) {
// Send the click on it's way as a navigate event
var bounds = domNode.getBoundingClientRect();
self.dispatchEvent({
type: "tw-navigate",
navigateTo: self.to,
navigateFromNode: self,
navigateFromClientRect: { top: bounds.top, left: bounds.left, width: bounds.width, right: bounds.right, bottom: bounds.bottom, height: bounds.height
}
});
event.preventDefault();
event.stopPropagation();
return false;
},false);
$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
var bounds = this.domNodes[0].getBoundingClientRect();
this.dispatchEvent({
type: "tw-navigate",
navigateTo: this.to,
navigateFromNode: this,
navigateFromClientRect: { top: bounds.top, left: bounds.left, width: bounds.width, right: bounds.right, bottom: bounds.bottom, height: bounds.height
}
});
event.preventDefault();
event.stopPropagation();
return false;
};
/*
Compute the internal state of the widget
*/