From 52f9e495ae036f78829406cb9b7d6542ac2b0a2c Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Fri, 14 Sep 2012 17:29:17 +0100 Subject: [PATCH] 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 --- core/modules/macros/link.js | 50 +++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/core/modules/macros/link.js b/core/modules/macros/link.js index 1dcde7bcd..83e515da7 100644 --- a/core/modules/macros/link.js +++ b/core/modules/macros/link.js @@ -21,6 +21,7 @@ exports.info = { name: "link", params: { to: {byName: "default", type: "tiddler", skinny: true}, + throughField: {byname: true, type: "text"}, space: {byName: true, type: "text"} } }; @@ -33,7 +34,7 @@ exports.handleEvent = function (event) { } else { var navEvent = document.createEvent("Event"); navEvent.initEvent("tw-navigate",true,true); - navEvent.navigateTo = this.params.to; + navEvent.navigateTo = this.linkInfo.to; navEvent.navigateFrom = this; event.target.dispatchEvent(navEvent); event.preventDefault(); @@ -44,42 +45,49 @@ exports.handleEvent = function (event) { exports.executeMacro = function() { // Assemble the information about the link - var linkInfo = { - to: this.params.to, + this.linkInfo = { space: this.params.space }; - // Generate the default link characteristics - linkInfo.isExternal = isLinkExternal(linkInfo.to); - if(!linkInfo.isExternal) { - linkInfo.isMissing = !this.wiki.tiddlerExists(linkInfo.to); + if(this.hasParameter("to")) { + this.linkInfo.to = this.params.to; + } else if(this.hasParameter("throughField") && this.tiddlerTitle) { + 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 = { - href: linkInfo.to + // Generate the default link characteristics + 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) { - linkInfo.attributes.href = encodeURIComponent(linkInfo.to); + if(!this.linkInfo.isExternal) { + this.linkInfo.attributes.href = encodeURIComponent(this.linkInfo.to); } // Generate the default classes for the link - linkInfo.attributes["class"] = ["tw-tiddlylink"]; - if(linkInfo.isExternal) { - linkInfo.attributes["class"].push("tw-tiddlylink-external"); + this.linkInfo.attributes["class"] = ["tw-tiddlylink"]; + if(this.linkInfo.isExternal) { + this.linkInfo.attributes["class"].push("tw-tiddlylink-external"); } else { - linkInfo.attributes["class"].push("tw-tiddlylink-internal"); - if(linkInfo.isMissing) { - linkInfo.attributes["class"].push("tw-tiddlylink-missing"); + this.linkInfo.attributes["class"].push("tw-tiddlylink-internal"); + if(this.linkInfo.isMissing) { + this.linkInfo.attributes["class"].push("tw-tiddlylink-missing"); } else { - linkInfo.attributes["class"].push("tw-tiddlylink-resolves"); + this.linkInfo.attributes["class"].push("tw-tiddlylink-resolves"); } } if(this.classes) { - $tw.utils.pushTop(linkInfo.attributes["class"],this.classes); + $tw.utils.pushTop(this.linkInfo.attributes["class"],this.classes); } // Create the link var child; - if(linkInfo.suppressLink) { + if(this.linkInfo.suppressLink) { child = $tw.Tree.Element("span",{},this.content); } 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); return child;