2012-04-30 11:23:03 +00:00
|
|
|
/*\
|
2012-05-03 20:47:16 +00:00
|
|
|
title: $:/core/modules/macros/link.js
|
2012-04-30 11:23:03 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: macro
|
|
|
|
|
|
|
|
Implements the link macro.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
2012-05-04 17:49:04 +00:00
|
|
|
/*global $tw: false */
|
2012-04-30 11:23:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var isLinkExternal = function(to) {
|
|
|
|
var externalRegExp = /(?:file|http|https|mailto|ftp|irc|news|data):[^\s'"]+(?:\/|\b)/i;
|
|
|
|
return externalRegExp.test(to);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.info = {
|
|
|
|
name: "link",
|
|
|
|
params: {
|
|
|
|
to: {byName: "default", type: "tiddler", skinny: true},
|
2012-09-14 16:29:17 +00:00
|
|
|
throughField: {byname: true, type: "text"},
|
2012-04-30 11:23:03 +00:00
|
|
|
space: {byName: true, type: "text"}
|
2012-06-14 16:15:38 +00:00
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.handleEvent = function (event) {
|
2012-05-19 14:13:17 +00:00
|
|
|
if(event.type === "click") {
|
2012-10-11 13:56:22 +00:00
|
|
|
if(isLinkExternal(this.linkInfo.to)) {
|
2012-05-19 14:13:17 +00:00
|
|
|
event.target.setAttribute("target","_blank");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
var navEvent = document.createEvent("Event");
|
|
|
|
navEvent.initEvent("tw-navigate",true,true);
|
2012-09-14 16:29:17 +00:00
|
|
|
navEvent.navigateTo = this.linkInfo.to;
|
2012-10-26 21:12:40 +00:00
|
|
|
navEvent.navigateFromNode = this;
|
2012-11-03 12:34:36 +00:00
|
|
|
navEvent.navigateFromClientRect = this.child.domNode.getBoundingClientRect();
|
2012-05-19 14:13:17 +00:00
|
|
|
event.target.dispatchEvent(navEvent);
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.executeMacro = function() {
|
|
|
|
// Assemble the information about the link
|
2012-09-14 16:29:17 +00:00
|
|
|
this.linkInfo = {
|
2012-04-30 11:23:03 +00:00
|
|
|
space: this.params.space
|
|
|
|
};
|
2012-09-14 16:29:17 +00:00
|
|
|
if(this.hasParameter("to")) {
|
|
|
|
this.linkInfo.to = this.params.to;
|
|
|
|
} else if(this.hasParameter("throughField") && this.tiddlerTitle) {
|
|
|
|
var currTiddler = this.wiki.getTiddler(this.tiddlerTitle);
|
|
|
|
if(currTiddler && this.params.throughField in currTiddler.fields) {
|
|
|
|
this.linkInfo.to = currTiddler.fields[this.params.throughField];
|
|
|
|
}
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
// Generate the default link characteristics
|
2012-09-14 16:29:17 +00:00
|
|
|
this.linkInfo.isExternal = isLinkExternal(this.linkInfo.to);
|
|
|
|
if(!this.linkInfo.isExternal) {
|
|
|
|
this.linkInfo.isMissing = !this.wiki.tiddlerExists(this.linkInfo.to);
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
2012-09-14 16:29:17 +00:00
|
|
|
this.linkInfo.attributes = {
|
|
|
|
href: this.linkInfo.to
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
2012-09-14 16:29:17 +00:00
|
|
|
if(!this.linkInfo.isExternal) {
|
|
|
|
this.linkInfo.attributes.href = encodeURIComponent(this.linkInfo.to);
|
2012-05-02 16:24:51 +00:00
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
// Generate the default classes for the link
|
2012-09-14 16:29:17 +00:00
|
|
|
this.linkInfo.attributes["class"] = ["tw-tiddlylink"];
|
|
|
|
if(this.linkInfo.isExternal) {
|
|
|
|
this.linkInfo.attributes["class"].push("tw-tiddlylink-external");
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
2012-09-14 16:29:17 +00:00
|
|
|
this.linkInfo.attributes["class"].push("tw-tiddlylink-internal");
|
|
|
|
if(this.linkInfo.isMissing) {
|
|
|
|
this.linkInfo.attributes["class"].push("tw-tiddlylink-missing");
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
2012-09-14 16:29:17 +00:00
|
|
|
this.linkInfo.attributes["class"].push("tw-tiddlylink-resolves");
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-09 17:36:32 +00:00
|
|
|
if(this.classes) {
|
2012-09-14 16:29:17 +00:00
|
|
|
$tw.utils.pushTop(this.linkInfo.attributes["class"],this.classes);
|
2012-06-09 17:36:32 +00:00
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
// Create the link
|
2012-06-09 17:36:32 +00:00
|
|
|
var child;
|
2012-09-14 16:29:17 +00:00
|
|
|
if(this.linkInfo.suppressLink) {
|
2012-06-09 17:36:32 +00:00
|
|
|
child = $tw.Tree.Element("span",{},this.content);
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
2012-09-14 16:29:17 +00:00
|
|
|
child = $tw.Tree.Element("a",this.linkInfo.attributes,this.content,{events: ["click"], eventHandler: this});
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
2012-06-09 17:36:32 +00:00
|
|
|
child.execute(this.parents,this.tiddlerTitle);
|
|
|
|
return child;
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|