1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-29 03:20:27 +00:00
TiddlyWiki5/js/macros/link.js
Jeremy Ruston 392e717497 Refactored handling of context tiddler
Now the title is passed around, rather than a reference to the tiddler.
This lets the context be a missing tiddler, enabling us to correctly
render missing tiddlers
2012-03-29 14:30:22 +01:00

64 lines
1.5 KiB
JavaScript

/*\
title: js/macros/link.js
\*/
(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: {
target: {byName: "default", type: "tiddler", skinny: true}
},
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() {
var classes = ["tw-tiddlylink"],
target = this.params.target;
if(isLinkExternal(target)) {
classes.push("tw-tiddlylink-external");
} else {
classes.push("tw-tiddlylink-internal");
if(this.store.tiddlerExists(target)) {
classes.push("tw-tiddlylink-resolves");
} else {
classes.push("tw-tiddlylink-missing");
}
target = encodeURIComponent(target);
}
var content = [Renderer.ElementNode(
"a",{
href: target,
"class": classes
},this.cloneChildren())];
for(var t=0; t<content.length; t++) {
content[t].execute(this.parents,this.tiddlerTitle);
}
return content;
}
};
})();