2012-01-15 13:29:16 +00:00
|
|
|
/*\
|
|
|
|
title: js/macros/link.js
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
2012-02-02 18:00:42 +00:00
|
|
|
/*jslint node: true, browser: true */
|
2012-01-15 13:29:16 +00:00
|
|
|
"use strict";
|
|
|
|
|
2012-02-16 20:38:10 +00:00
|
|
|
var Renderer = require("../Renderer.js").Renderer;
|
2012-01-15 13:29:16 +00:00
|
|
|
|
2012-02-09 13:36:46 +00:00
|
|
|
var isLinkExternal = function(target) {
|
|
|
|
var externalRegExp = /(?:file|http|https|mailto|ftp|irc|news|data):[^\s'"]+(?:\/|\b)/i;
|
|
|
|
return externalRegExp.test(target);
|
|
|
|
};
|
|
|
|
|
2012-01-15 13:29:16 +00:00
|
|
|
exports.macro = {
|
|
|
|
name: "link",
|
|
|
|
types: ["text/html","text/plain"],
|
|
|
|
params: {
|
2012-02-17 17:32:32 +00:00
|
|
|
target: {byName: "default", type: "tiddler", skinny: true}
|
2012-01-15 13:29:16 +00:00
|
|
|
},
|
2012-02-02 18:00:42 +00:00
|
|
|
events: {
|
2012-02-16 20:38:10 +00:00
|
|
|
click: function(event,macroNode) {
|
|
|
|
if(isLinkExternal(macroNode.params.target)) {
|
2012-02-09 13:36:46 +00:00
|
|
|
event.target.setAttribute("target","_blank");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
var navEvent = document.createEvent("Event");
|
|
|
|
navEvent.initEvent("tw-navigate",true,true);
|
2012-02-16 20:38:10 +00:00
|
|
|
navEvent.navigateTo = macroNode.params.target;
|
|
|
|
event.target.dispatchEvent(navEvent);
|
2012-02-09 13:36:46 +00:00
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-02 18:00:42 +00:00
|
|
|
}
|
|
|
|
},
|
2012-02-16 20:38:10 +00:00
|
|
|
execute: function(macroNode,tiddler,store) {
|
2012-02-09 13:36:46 +00:00
|
|
|
var classes = ["tw-tiddlylink"],
|
2012-02-16 20:38:10 +00:00
|
|
|
target = macroNode.params.target;
|
|
|
|
if(isLinkExternal(target)) {
|
2012-02-09 13:36:46 +00:00
|
|
|
classes.push("tw-tiddlylink-external");
|
|
|
|
} else {
|
|
|
|
classes.push("tw-tiddlylink-internal");
|
2012-02-16 20:38:10 +00:00
|
|
|
if(store.tiddlerExists(target)) {
|
2012-02-09 13:36:46 +00:00
|
|
|
classes.push("tw-tiddlylink-resolves");
|
|
|
|
} else {
|
|
|
|
classes.push("tw-tiddlylink-missing");
|
|
|
|
}
|
|
|
|
target = encodeURIComponent(target);
|
|
|
|
}
|
2012-02-16 20:38:10 +00:00
|
|
|
var content = [Renderer.ElementNode(
|
2012-02-06 10:57:28 +00:00
|
|
|
"a",{
|
2012-02-09 13:36:46 +00:00
|
|
|
href: target,
|
|
|
|
"class": classes
|
2012-02-16 20:38:10 +00:00
|
|
|
},macroNode.cloneChildren())];
|
|
|
|
for(var t=0; t<content.length; t++) {
|
|
|
|
content[t].execute(tiddler);
|
|
|
|
}
|
|
|
|
return content;
|
2012-01-15 13:29:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|