2012-12-13 21:34:31 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widget/link.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Implements the link widget.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var isLinkExternal = function(to) {
|
|
|
|
var externalRegExp = /(?:file|http|https|mailto|ftp|irc|news|data):[^\s'"]+(?:\/|\b)/i;
|
|
|
|
return externalRegExp.test(to);
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
var LinkWidget = function(renderer) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Save state
|
|
|
|
this.renderer = renderer;
|
|
|
|
// Generate child nodes
|
2013-01-03 16:27:55 +00:00
|
|
|
this.generate();
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2013-01-03 16:27:55 +00:00
|
|
|
LinkWidget.prototype.generate = function() {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Get the parameters from the attributes
|
|
|
|
this.to = this.renderer.getAttribute("to");
|
|
|
|
this.hover = this.renderer.getAttribute("hover");
|
|
|
|
this.qualifyHoverTitle = this.renderer.getAttribute("qualifyHoverTitle");
|
|
|
|
// Determine the default link characteristics
|
|
|
|
this.isExternal = isLinkExternal(this.to);
|
|
|
|
if(!this.isExternal) {
|
|
|
|
this.isMissing = !this.renderer.renderTree.wiki.tiddlerExists(this.to);
|
|
|
|
}
|
|
|
|
// Compose the link
|
|
|
|
var classes = ["tw-tiddlylink"]
|
|
|
|
if(this.isExternal) {
|
|
|
|
$tw.utils.pushTop(classes,"tw-tiddlylink-external");
|
|
|
|
} else {
|
|
|
|
$tw.utils.pushTop(classes,"tw-tiddlylink-internal");
|
|
|
|
if(this.isMissing) {
|
|
|
|
$tw.utils.pushTop(classes,"tw-tiddlylink-missing");
|
|
|
|
} else {
|
|
|
|
$tw.utils.pushTop(classes,"tw-tiddlylink-resolves");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var events = [{name: "click", handlerObject: this, handlerMethod: "handleClickEvent"}];
|
|
|
|
if(this.hover) {
|
|
|
|
events.push({name: "mouseover", handlerObject: this, handlerMethod: "handleMouseOverOrOutEvent"});
|
|
|
|
events.push({name: "mouseout", handlerObject: this, handlerMethod: "handleMouseOverOrOutEvent"});
|
|
|
|
}
|
2013-01-03 16:27:55 +00:00
|
|
|
// Set up the element
|
|
|
|
this.tag = "a";
|
|
|
|
this.attributes = {
|
|
|
|
href: this.isExternal ? this.to : encodeURIComponent(this.to),
|
|
|
|
"class": classes.join(" ")
|
|
|
|
};
|
|
|
|
this.children = this.renderer.renderTree.createRenderers(this.renderer.renderContext,this.renderer.parseTreeNode.children);
|
|
|
|
this.events = events;
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
LinkWidget.prototype.handleClickEvent = function(event) {
|
2012-12-13 21:34:31 +00:00
|
|
|
if(isLinkExternal(this.to)) {
|
|
|
|
event.target.setAttribute("target","_blank");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
$tw.utils.dispatchCustomEvent(event.target,"tw-navigate",{
|
|
|
|
navigateTo: this.to,
|
|
|
|
navigateFromNode: this,
|
2013-01-03 16:27:55 +00:00
|
|
|
navigateFromClientRect: this.renderer.domNode.getBoundingClientRect()
|
2012-12-13 21:34:31 +00:00
|
|
|
});
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
LinkWidget.prototype.handleMouseOverOrOutEvent = function(event) {
|
2012-12-13 21:34:31 +00:00
|
|
|
if(this.hover) {
|
|
|
|
$tw.popup.triggerPopup({
|
2013-01-03 16:27:55 +00:00
|
|
|
title: this.hover,
|
|
|
|
domNode: this.renderer.domNode,
|
2012-12-13 21:34:31 +00:00
|
|
|
wiki: this.renderer.renderTree.wiki
|
|
|
|
});
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
LinkWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Set the class for missing tiddlers
|
2012-12-14 19:31:37 +00:00
|
|
|
if(this.targetTitle && changedTiddlers[this.targetTitle]) {
|
2013-01-03 16:27:55 +00:00
|
|
|
$tw.utils.toggleClass(this.renderer.domNode,"tw-tiddler-missing",!this.renderer.renderTree.wiki.tiddlerExists(this.targetTitle));
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
// Check if any of our attributes have changed, or if a tiddler we're interested in has changed
|
|
|
|
if(changedAttributes.to || changedAttributes.hover || (this.to && changedTiddlers[this.to]) || (this.hover && changedTiddlers[this.hover])) {
|
2013-01-03 16:27:55 +00:00
|
|
|
// Regenerate and rerender the widget and replace the existing DOM node
|
|
|
|
this.generate();
|
|
|
|
var oldDomNode = this.renderer.domNode,
|
|
|
|
newDomNode = this.renderer.renderInDom();
|
|
|
|
oldDomNode.parentNode.replaceChild(newDomNode,oldDomNode);
|
2012-12-13 21:34:31 +00:00
|
|
|
} else {
|
|
|
|
// We don't need to refresh ourselves, so just refresh any child nodes
|
|
|
|
$tw.utils.each(this.children,function(node) {
|
|
|
|
if(node.refreshInDom) {
|
|
|
|
node.refreshInDom(changedTiddlers);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
exports.link = LinkWidget;
|
|
|
|
|
2012-12-13 21:34:31 +00:00
|
|
|
})();
|