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:
parent
238946b32b
commit
52f9e495ae
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user