1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-06 01:56:20 +00:00
TiddlyWiki5/js/macros/link.js

99 lines
2.9 KiB
JavaScript
Raw Normal View History

/*\
title: js/macros/link.js
2012-04-03 16:07:38 +00:00
Implements the link macro.
A special callback function is used to massage links according to the needs of the host platform.
The linkMassager is stored in the `linkMassager` property of the store object. It is a function
that takes a `linkInfo` structure as the only parameter. It contains a hashmap of information
as follows:
{
target: the target of the link
space: an optional space associated with the link
isExternal: true if the link has been determined to be an external link by the default heuristics
isMissing: true if a non-external link references a missing tiddler
classes: an array of strings representing the CSS classes to be applied to the link. The default classes are already applied
href: the href to be used in the link
}
The linkMassager can modify the `classes` and `href` fields as required.
\*/
(function(){
/*jslint node: true, browser: true */
"use strict";
var Renderer = require("../Renderer.js").Renderer;
var isLinkExternal = function(target) {
var externalRegExp = /(?:file|http|https|mailto|ftp|irc|news|data):[^\s'"]+(?:\/|\b)/i;
return externalRegExp.test(target);
};
exports.macro = {
name: "link",
params: {
2012-04-03 16:07:38 +00:00
target: {byName: "default", type: "tiddler", skinny: true},
space: {byName: true, type: "text"}
},
events: {
click: function(event) {
if(isLinkExternal(this.params.target)) {
event.target.setAttribute("target","_blank");
return true;
} else {
var navEvent = document.createEvent("Event");
navEvent.initEvent("tw-navigate",true,true);
navEvent.navigateTo = this.params.target;
event.target.dispatchEvent(navEvent);
event.preventDefault();
return false;
}
}
},
execute: function() {
2012-04-03 16:07:38 +00:00
// Assemble the information about the link
var linkInfo = {
target: this.params.target,
space: this.params.space
};
// Generate the default link characteristics
linkInfo.isExternal = isLinkExternal(linkInfo.target);
if(!linkInfo.isExternal) {
linkInfo.isMissing = !this.store.tiddlerExists(linkInfo.target);
}
linkInfo.href = encodeURIComponent(linkInfo.target);
// Generate the default classes for the link
linkInfo.classes = ["tw-tiddlylink"];
if(linkInfo.isExternal) {
linkInfo.classes.push("tw-tiddlylink-external");
} else {
2012-04-03 16:07:38 +00:00
linkInfo.classes.push("tw-tiddlylink-internal");
if(linkInfo.isMissing) {
linkInfo.classes.push("tw-tiddlylink-missing");
} else {
2012-04-03 16:07:38 +00:00
linkInfo.classes.push("tw-tiddlylink-resolves");
}
}
2012-04-03 16:07:38 +00:00
// Invoke the link massager if defined
if(this.store.linkMassager) {
this.store.linkMassager(linkInfo);
}
// Figure out the classes to assign to the link
var content = [Renderer.ElementNode(
"a",{
2012-04-03 16:07:38 +00:00
href: linkInfo.target,
"class": linkInfo.classes
},this.cloneChildren())];
for(var t=0; t<content.length; t++) {
content[t].execute(this.parents,this.tiddlerTitle);
}
return content;
}
};
})();