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-06 10:57:28 +00:00
|
|
|
var HTML = require("../HTML.js").HTML,
|
|
|
|
utils = require("../Utils.js");
|
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",
|
|
|
|
wrapperTag: "span",
|
|
|
|
types: ["text/html","text/plain"],
|
|
|
|
params: {
|
2012-02-07 19:03:59 +00:00
|
|
|
target: {byName: "default", type: "tiddler", rel: "link", optional: false}
|
2012-01-15 13:29:16 +00:00
|
|
|
},
|
2012-02-02 18:00:42 +00:00
|
|
|
events: {
|
|
|
|
click: function(event,node,tiddler,store,params) {
|
2012-02-09 13:36:46 +00:00
|
|
|
if(isLinkExternal(params.target)) {
|
|
|
|
event.target.setAttribute("target","_blank");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
var navEvent = document.createEvent("Event");
|
|
|
|
navEvent.initEvent("tw-navigate",true,true);
|
|
|
|
navEvent.navigateTo = params.target;
|
|
|
|
node.dispatchEvent(navEvent);
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-02 18:00:42 +00:00
|
|
|
}
|
|
|
|
},
|
2012-01-30 20:04:47 +00:00
|
|
|
render: function(type,tiddler,store,params,content) {
|
2012-02-09 13:36:46 +00:00
|
|
|
var classes = ["tw-tiddlylink"],
|
|
|
|
target = params.target;
|
|
|
|
if(isLinkExternal(params.target)) {
|
|
|
|
classes.push("tw-tiddlylink-external");
|
|
|
|
} else {
|
|
|
|
classes.push("tw-tiddlylink-internal");
|
|
|
|
if(store.tiddlerExists(params.target)) {
|
|
|
|
classes.push("tw-tiddlylink-resolves");
|
|
|
|
} else {
|
|
|
|
classes.push("tw-tiddlylink-missing");
|
|
|
|
}
|
|
|
|
target = encodeURIComponent(target);
|
|
|
|
}
|
2012-02-06 10:57:28 +00:00
|
|
|
return HTML(HTML.elem(
|
|
|
|
"a",{
|
2012-02-09 13:36:46 +00:00
|
|
|
href: target,
|
|
|
|
"class": classes
|
2012-02-06 10:57:28 +00:00
|
|
|
},[
|
|
|
|
HTML.raw(content)
|
|
|
|
]
|
|
|
|
),type);
|
2012-01-15 13:29:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|