1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-25 09:30:28 +00:00

Add ability for link macro to link through a given field

This is a bit of a hack, and a more harmonious way of doing this is
planned
This commit is contained in:
Jeremy Ruston 2012-09-14 17:29:17 +01:00
parent 238946b32b
commit 52f9e495ae

View File

@ -21,6 +21,7 @@ exports.info = {
name: "link", name: "link",
params: { params: {
to: {byName: "default", type: "tiddler", skinny: true}, to: {byName: "default", type: "tiddler", skinny: true},
throughField: {byname: true, type: "text"},
space: {byName: true, type: "text"} space: {byName: true, type: "text"}
} }
}; };
@ -33,7 +34,7 @@ exports.handleEvent = function (event) {
} else { } else {
var navEvent = document.createEvent("Event"); var navEvent = document.createEvent("Event");
navEvent.initEvent("tw-navigate",true,true); navEvent.initEvent("tw-navigate",true,true);
navEvent.navigateTo = this.params.to; navEvent.navigateTo = this.linkInfo.to;
navEvent.navigateFrom = this; navEvent.navigateFrom = this;
event.target.dispatchEvent(navEvent); event.target.dispatchEvent(navEvent);
event.preventDefault(); event.preventDefault();
@ -44,42 +45,49 @@ exports.handleEvent = function (event) {
exports.executeMacro = function() { exports.executeMacro = function() {
// Assemble the information about the link // Assemble the information about the link
var linkInfo = { this.linkInfo = {
to: this.params.to,
space: this.params.space space: this.params.space
}; };
// Generate the default link characteristics if(this.hasParameter("to")) {
linkInfo.isExternal = isLinkExternal(linkInfo.to); this.linkInfo.to = this.params.to;
if(!linkInfo.isExternal) { } else if(this.hasParameter("throughField") && this.tiddlerTitle) {
linkInfo.isMissing = !this.wiki.tiddlerExists(linkInfo.to); var currTiddler = this.wiki.getTiddler(this.tiddlerTitle);
if(currTiddler && this.params.throughField in currTiddler.fields) {
this.linkInfo.to = currTiddler.fields[this.params.throughField];
}
} }
linkInfo.attributes = { // Generate the default link characteristics
href: linkInfo.to this.linkInfo.isExternal = isLinkExternal(this.linkInfo.to);
if(!this.linkInfo.isExternal) {
this.linkInfo.isMissing = !this.wiki.tiddlerExists(this.linkInfo.to);
}
this.linkInfo.attributes = {
href: this.linkInfo.to
}; };
if(!linkInfo.isExternal) { if(!this.linkInfo.isExternal) {
linkInfo.attributes.href = encodeURIComponent(linkInfo.to); this.linkInfo.attributes.href = encodeURIComponent(this.linkInfo.to);
} }
// Generate the default classes for the link // Generate the default classes for the link
linkInfo.attributes["class"] = ["tw-tiddlylink"]; this.linkInfo.attributes["class"] = ["tw-tiddlylink"];
if(linkInfo.isExternal) { if(this.linkInfo.isExternal) {
linkInfo.attributes["class"].push("tw-tiddlylink-external"); this.linkInfo.attributes["class"].push("tw-tiddlylink-external");
} else { } else {
linkInfo.attributes["class"].push("tw-tiddlylink-internal"); this.linkInfo.attributes["class"].push("tw-tiddlylink-internal");
if(linkInfo.isMissing) { if(this.linkInfo.isMissing) {
linkInfo.attributes["class"].push("tw-tiddlylink-missing"); this.linkInfo.attributes["class"].push("tw-tiddlylink-missing");
} else { } else {
linkInfo.attributes["class"].push("tw-tiddlylink-resolves"); this.linkInfo.attributes["class"].push("tw-tiddlylink-resolves");
} }
} }
if(this.classes) { if(this.classes) {
$tw.utils.pushTop(linkInfo.attributes["class"],this.classes); $tw.utils.pushTop(this.linkInfo.attributes["class"],this.classes);
} }
// Create the link // Create the link
var child; var child;
if(linkInfo.suppressLink) { if(this.linkInfo.suppressLink) {
child = $tw.Tree.Element("span",{},this.content); child = $tw.Tree.Element("span",{},this.content);
} else { } else {
child = $tw.Tree.Element("a",linkInfo.attributes,this.content,{events: ["click"], eventHandler: this}); child = $tw.Tree.Element("a",this.linkInfo.attributes,this.content,{events: ["click"], eventHandler: this});
} }
child.execute(this.parents,this.tiddlerTitle); child.execute(this.parents,this.tiddlerTitle);
return child; return child;