2012-12-13 21:34:31 +00:00
|
|
|
/*\
|
2013-05-31 12:23:25 +00:00
|
|
|
title: $:/core/modules/widgets/link.js
|
2012-12-13 21:34:31 +00:00
|
|
|
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
|
2013-06-09 18:24:45 +00:00
|
|
|
this.to = this.renderer.getAttribute("to",this.renderer.tiddlerTitle);
|
2012-12-13 21:34:31 +00:00
|
|
|
this.hover = this.renderer.getAttribute("hover");
|
2013-03-05 11:55:25 +00:00
|
|
|
this.qualifyHoverTitles = this.renderer.getAttribute("qualifyHoverTitles");
|
|
|
|
// Qualify the hover tiddler title if needed
|
|
|
|
if(this.qualifyHoverTitles) {
|
2013-05-15 16:32:17 +00:00
|
|
|
this.hover = this.hover + "-" + this.renderer.renderTree.getContextScopeId(this.renderer.parentRenderer);
|
2013-03-05 11:55:25 +00:00
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
// Determine the default link characteristics
|
|
|
|
this.isExternal = isLinkExternal(this.to);
|
|
|
|
if(!this.isExternal) {
|
|
|
|
this.isMissing = !this.renderer.renderTree.wiki.tiddlerExists(this.to);
|
2013-04-03 13:29:12 +00:00
|
|
|
this.isShadow = this.renderer.renderTree.wiki.isShadowTiddler(this.to);
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
// 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");
|
2013-04-03 13:29:12 +00:00
|
|
|
if(this.isShadow) {
|
|
|
|
$tw.utils.pushTop(classes,"tw-tiddlylink-shadow");
|
|
|
|
}
|
|
|
|
if(this.isMissing && !this.isShadow) {
|
2012-12-13 21:34:31 +00:00
|
|
|
$tw.utils.pushTop(classes,"tw-tiddlylink-missing");
|
|
|
|
} else {
|
2013-04-03 13:29:12 +00:00
|
|
|
if(!this.isMissing) {
|
|
|
|
$tw.utils.pushTop(classes,"tw-tiddlylink-resolves");
|
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-08 17:47:46 +00:00
|
|
|
var events = [
|
|
|
|
{name: "click", handlerObject: this, handlerMethod: "handleClickEvent"},
|
|
|
|
{name: "dragstart", handlerObject: this, handlerMethod: "handleDragStartEvent"},
|
|
|
|
{name: "dragend", handlerObject: this, handlerMethod: "handleDragEndEvent"}
|
|
|
|
];
|
2012-12-13 21:34:31 +00:00
|
|
|
if(this.hover) {
|
|
|
|
events.push({name: "mouseover", handlerObject: this, handlerMethod: "handleMouseOverOrOutEvent"});
|
|
|
|
events.push({name: "mouseout", handlerObject: this, handlerMethod: "handleMouseOverOrOutEvent"});
|
|
|
|
}
|
2013-03-01 18:04:53 +00:00
|
|
|
// Get the value of the tw-wikilinks configuration macro
|
2013-05-15 16:32:17 +00:00
|
|
|
var wikiLinksMacro = this.renderer.renderTree.findMacroDefinition(this.renderer.parentRenderer,"tw-wikilinks"),
|
2013-03-01 18:04:53 +00:00
|
|
|
useWikiLinks = wikiLinksMacro ? !(wikiLinksMacro.text.trim() === "no") : true;
|
2013-01-03 16:27:55 +00:00
|
|
|
// Set up the element
|
2013-03-01 18:04:53 +00:00
|
|
|
if(useWikiLinks) {
|
|
|
|
this.tag = "a";
|
|
|
|
this.attributes = {
|
|
|
|
"class": classes.join(" ")
|
|
|
|
};
|
2013-03-04 11:13:10 +00:00
|
|
|
if(this.isExternal) {
|
|
|
|
this.attributes.href = this.to;
|
|
|
|
} else {
|
2013-05-15 16:32:17 +00:00
|
|
|
var wikiLinkTemplateMacro = this.renderer.renderTree.findMacroDefinition(this.renderer.parentRenderer,"tw-wikilink-template"),
|
2013-06-27 08:03:59 +00:00
|
|
|
wikiLinkTemplate = wikiLinkTemplateMacro ? wikiLinkTemplateMacro.text.trim() : "#$uri_encoded$";
|
2013-04-29 15:03:35 +00:00
|
|
|
this.wikiLinkText = wikiLinkTemplate.replace("$uri_encoded$",encodeURIComponent(this.to));
|
|
|
|
this.wikiLinkText = this.wikiLinkText.replace("$uri_doubleencoded$",encodeURIComponent(encodeURIComponent(this.to)));
|
|
|
|
this.attributes.href = this.wikiLinkText;
|
2013-03-04 11:13:10 +00:00
|
|
|
}
|
2013-04-08 17:47:46 +00:00
|
|
|
this.events = events;
|
2013-03-01 18:04:53 +00:00
|
|
|
} else {
|
|
|
|
this.tag = "span";
|
|
|
|
}
|
2013-05-15 16:32:17 +00:00
|
|
|
this.children = this.renderer.renderTree.createRenderers(this.renderer,this.renderer.parseTreeNode.children);
|
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 {
|
2013-07-25 15:58:42 +00:00
|
|
|
var bounds = this.renderer.domNode.getBoundingClientRect();
|
2012-12-13 21:34:31 +00:00
|
|
|
$tw.utils.dispatchCustomEvent(event.target,"tw-navigate",{
|
|
|
|
navigateTo: this.to,
|
|
|
|
navigateFromNode: this,
|
2013-07-25 15:58:42 +00:00
|
|
|
navigateFromClientRect: {
|
|
|
|
top: bounds.top,
|
|
|
|
left: bounds.left,
|
|
|
|
width: bounds.width,
|
|
|
|
right: bounds.right,
|
|
|
|
bottom: bounds.bottom,
|
|
|
|
height: bounds.height
|
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
});
|
|
|
|
event.preventDefault();
|
2013-04-29 15:03:35 +00:00
|
|
|
event.stopPropagation();
|
2012-12-13 21:34:31 +00:00
|
|
|
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-04-08 17:47:46 +00:00
|
|
|
LinkWidget.prototype.handleDragStartEvent = function(event) {
|
|
|
|
if(this.to) {
|
|
|
|
// Set the dragging class on the element being dragged
|
|
|
|
$tw.utils.addClass(event.target,"tw-tiddlylink-dragging");
|
2013-08-08 20:46:38 +00:00
|
|
|
// Create the drag image elements
|
2013-06-05 14:38:06 +00:00
|
|
|
this.dragImage = this.renderer.renderTree.document.createElement("div");
|
2013-04-08 17:47:46 +00:00
|
|
|
this.dragImage.className = "tw-tiddler-dragger";
|
2013-08-08 20:46:38 +00:00
|
|
|
var inner = this.renderer.renderTree.document.createElement("div");
|
|
|
|
inner.className = "tw-tiddler-dragger-inner";
|
|
|
|
inner.appendChild(this.renderer.renderTree.document.createTextNode(this.to));
|
|
|
|
this.dragImage.appendChild(inner);
|
2013-06-05 14:38:06 +00:00
|
|
|
this.renderer.renderTree.document.body.appendChild(this.dragImage);
|
2013-08-08 20:46:38 +00:00
|
|
|
// Astoundingly, we need to cover the dragger up: http://www.kryogenix.org/code/browser/custom-drag-image.html
|
|
|
|
var bounds = this.dragImage.firstChild.getBoundingClientRect(),
|
|
|
|
cover = this.renderer.renderTree.document.createElement("div");
|
|
|
|
cover.className = "tw-tiddler-dragger-cover";
|
|
|
|
cover.style.left = (bounds.left - 8) + "px";
|
|
|
|
cover.style.top = (bounds.top - 8) + "px";
|
|
|
|
cover.style.width = (bounds.width + 16) + "px";
|
|
|
|
cover.style.height = (bounds.height + 16) + "px";
|
|
|
|
this.dragImage.appendChild(cover);
|
2013-04-08 17:47:46 +00:00
|
|
|
// Set the data transfer properties
|
|
|
|
var dataTransfer = event.dataTransfer;
|
|
|
|
dataTransfer.effectAllowed = "copy";
|
2013-08-08 20:46:38 +00:00
|
|
|
dataTransfer.setDragImage(this.dragImage.firstChild,-16,-16);
|
2013-04-08 17:47:46 +00:00
|
|
|
dataTransfer.clearData();
|
|
|
|
dataTransfer.setData("text/vnd.tiddler",this.renderer.renderTree.wiki.getTiddlerAsJson(this.to));
|
|
|
|
dataTransfer.setData("text/plain",this.renderer.renderTree.wiki.getTiddlerText(this.to,""));
|
|
|
|
event.stopPropagation();
|
|
|
|
} else {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
LinkWidget.prototype.handleDragEndEvent = function(event) {
|
|
|
|
// Remove the dragging class on the element being dragged
|
|
|
|
$tw.utils.removeClass(event.target,"tw-tiddlylink-dragging");
|
|
|
|
// Delete the drag image element
|
|
|
|
if(this.dragImage) {
|
|
|
|
this.dragImage.parentNode.removeChild(this.dragImage);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
LinkWidget.prototype.postRenderInDom = function() {
|
|
|
|
// Add the draggable attribute to links (we don't include it in the static HTML representation)
|
|
|
|
if(this.renderer.domNode.tagName === "A") {
|
|
|
|
this.renderer.domNode.setAttribute("draggable",true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
})();
|