1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-09 19:39:57 +00:00
TiddlyWiki5/core/modules/macros/link.js

98 lines
2.8 KiB
JavaScript
Raw Normal View History

/*\
title: $:/core/modules/macros/link.js
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 */
"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},
throughField: {byname: true, type: "text"},
space: {byName: true, type: "text"}
}
};
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);
navEvent.navigateTo = this.linkInfo.to;
navEvent.navigateFromNode = this;
navEvent.navigateFromClientRect = this.child.domNode.getBoundingClientRect();
2012-05-19 14:13:17 +00:00
event.target.dispatchEvent(navEvent);
event.preventDefault();
return false;
}
}
};
exports.executeMacro = function() {
// Assemble the information about the link
this.linkInfo = {
space: this.params.space
};
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];
}
}
// Generate the default link characteristics
this.linkInfo.isExternal = isLinkExternal(this.linkInfo.to);
if(!this.linkInfo.isExternal) {
this.linkInfo.isMissing = !this.wiki.tiddlerExists(this.linkInfo.to);
}
this.linkInfo.attributes = {
href: this.linkInfo.to
};
if(!this.linkInfo.isExternal) {
this.linkInfo.attributes.href = encodeURIComponent(this.linkInfo.to);
}
// Generate the default classes for the link
this.linkInfo.attributes["class"] = ["tw-tiddlylink"];
if(this.linkInfo.isExternal) {
this.linkInfo.attributes["class"].push("tw-tiddlylink-external");
} else {
this.linkInfo.attributes["class"].push("tw-tiddlylink-internal");
if(this.linkInfo.isMissing) {
this.linkInfo.attributes["class"].push("tw-tiddlylink-missing");
} else {
this.linkInfo.attributes["class"].push("tw-tiddlylink-resolves");
}
}
if(this.classes) {
$tw.utils.pushTop(this.linkInfo.attributes["class"],this.classes);
}
// Create the link
var child;
if(this.linkInfo.suppressLink) {
child = $tw.Tree.Element("span",{},this.content);
} else {
child = $tw.Tree.Element("a",this.linkInfo.attributes,this.content,{events: ["click"], eventHandler: this});
}
child.execute(this.parents,this.tiddlerTitle);
return child;
};
})();