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",
|
|
|
|
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-21 21:57:30 +00:00
|
|
|
click: function(event) {
|
|
|
|
if(isLinkExternal(this.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-21 21:57:30 +00:00
|
|
|
navEvent.navigateTo = this.params.target;
|
2012-02-16 20:38:10 +00:00
|
|
|
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-21 21:57:30 +00:00
|
|
|
execute: function() {
|
2012-02-09 13:36:46 +00:00
|
|
|
var classes = ["tw-tiddlylink"],
|
2012-02-21 21:57:30 +00:00
|
|
|
target = this.params.target;
|
2012-02-16 20:38:10 +00:00
|
|
|
if(isLinkExternal(target)) {
|
2012-02-09 13:36:46 +00:00
|
|
|
classes.push("tw-tiddlylink-external");
|
|
|
|
} else {
|
|
|
|
classes.push("tw-tiddlylink-internal");
|
2012-02-21 21:57:30 +00:00
|
|
|
if(this.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-21 21:57:30 +00:00
|
|
|
},this.cloneChildren())];
|
2012-02-16 20:38:10 +00:00
|
|
|
for(var t=0; t<content.length; t++) {
|
2012-03-29 13:30:22 +00:00
|
|
|
content[t].execute(this.parents,this.tiddlerTitle);
|
2012-02-16 20:38:10 +00:00
|
|
|
}
|
|
|
|
return content;
|
2012-01-15 13:29:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|