2015-01-03 20:02:27 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/railroad/wrapper.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Wrapper for `railroad-diagrams.js` that provides a `<$railroad>` widget.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Parser = require("$:/plugins/tiddlywiki/railroad/parser.js").parser,
|
|
|
|
Widget = require("$:/core/modules/widgets/widget.js").widget;
|
|
|
|
|
|
|
|
var RailroadWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
2015-01-19 23:11:25 +00:00
|
|
|
var RAILROAD_OPTIONS = "$:/config/railroad";
|
|
|
|
|
2015-01-03 20:02:27 +00:00
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
RailroadWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
RailroadWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
// Housekeeping
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
// Get the source text
|
|
|
|
var source = this.getAttribute("text",this.parseTreeNode.text || "");
|
|
|
|
// Create a div to contain the SVG or error message
|
|
|
|
var div = this.document.createElement("div");
|
|
|
|
try {
|
2015-01-19 23:11:25 +00:00
|
|
|
// Initialise options from the config tiddler or widget attributes
|
|
|
|
var config = $tw.wiki.getTiddlerData(RAILROAD_OPTIONS,{});
|
2015-01-19 22:21:22 +00:00
|
|
|
var options = {
|
2015-01-19 23:11:25 +00:00
|
|
|
arrow: this.getAttribute("arrow", config.arrow || "yes") === "yes",
|
|
|
|
debug: this.getAttribute("debug", config.debug || "no") === "yes",
|
|
|
|
start: this.getAttribute("start", config.start || "single"),
|
|
|
|
end: this.getAttribute("end", config.end || "single")
|
2015-01-19 22:21:22 +00:00
|
|
|
};
|
2015-01-03 20:02:27 +00:00
|
|
|
// Parse the source
|
2015-01-19 22:21:22 +00:00
|
|
|
var parser = new Parser(this,source,options);
|
2015-01-06 01:39:24 +00:00
|
|
|
// Generate content into the div
|
2015-01-19 22:21:22 +00:00
|
|
|
if(parser.options.debug) {
|
2015-01-06 01:39:24 +00:00
|
|
|
this.renderDebug(parser,div);
|
2015-01-03 20:02:27 +00:00
|
|
|
} else {
|
2015-01-06 01:39:24 +00:00
|
|
|
this.renderSvg(parser,div);
|
2015-01-03 20:02:27 +00:00
|
|
|
}
|
|
|
|
} catch(ex) {
|
|
|
|
div.className = "tc-error";
|
|
|
|
div.textContent = ex;
|
|
|
|
}
|
2015-01-06 01:39:24 +00:00
|
|
|
// Insert the div into the DOM
|
2015-01-03 20:02:27 +00:00
|
|
|
parent.insertBefore(div,nextSibling);
|
|
|
|
this.domNodes.push(div);
|
|
|
|
};
|
|
|
|
|
2015-01-06 01:39:24 +00:00
|
|
|
RailroadWidget.prototype.renderDebug = function(parser,div) {
|
|
|
|
var output = ["<pre>"];
|
|
|
|
parser.root.debug(output, "");
|
|
|
|
output.push("</pre>");
|
|
|
|
div.innerHTML = output.join("");
|
|
|
|
};
|
|
|
|
|
|
|
|
RailroadWidget.prototype.renderSvg = function(parser,div) {
|
|
|
|
// Generate a model of the diagram
|
2015-01-19 22:21:22 +00:00
|
|
|
var fakeSvg = parser.root.toSvg(parser.options);
|
2015-01-06 01:39:24 +00:00
|
|
|
// Render the model into a tree of SVG DOM nodes
|
|
|
|
var svg = fakeSvg.toSVG();
|
|
|
|
// Fill in the remaining attributes of any link nodes
|
|
|
|
this.patchLinks(svg);
|
|
|
|
// Insert the SVG tree into the div
|
|
|
|
div.appendChild(svg);
|
|
|
|
};
|
|
|
|
|
|
|
|
RailroadWidget.prototype.patchLinks = function(node) {
|
|
|
|
var self = this;
|
2015-01-11 18:31:53 +00:00
|
|
|
if(!$tw.node && node.hasChildNodes()) {
|
2015-01-06 01:39:24 +00:00
|
|
|
var children = node.childNodes;
|
|
|
|
for(var i=0; i<children.length; i++) {
|
|
|
|
var child = children[i];
|
|
|
|
var attributes = child.attributes;
|
|
|
|
if(attributes) {
|
|
|
|
// Find each element that has a data-tw-target attribute
|
|
|
|
var target = child.attributes["data-tw-target"];
|
|
|
|
if(target !== undefined) {
|
|
|
|
target = target.value;
|
|
|
|
if(child.attributes["data-tw-external"]) {
|
|
|
|
// External links are straightforward
|
|
|
|
child.setAttribute("target","_blank");
|
2016-05-05 10:49:40 +00:00
|
|
|
child.setAttribute("rel","noopener noreferrer");
|
2015-01-06 01:39:24 +00:00
|
|
|
} else {
|
|
|
|
// Each internal link gets its own onclick handler, capturing its own copy of target
|
|
|
|
(function(myTarget) {
|
|
|
|
child.onclick = function(event) {
|
|
|
|
self.dispatchLink(myTarget,event);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})(target);
|
|
|
|
target = "#" + target;
|
|
|
|
}
|
|
|
|
child.setAttributeNS("http://www.w3.org/1999/xlink","href",target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.patchLinks(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-03 20:02:27 +00:00
|
|
|
RailroadWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
2015-01-20 08:38:00 +00:00
|
|
|
if(changedAttributes.text || changedTiddlers[RAILROAD_OPTIONS]) {
|
2015-01-03 20:02:27 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2015-01-06 01:39:24 +00:00
|
|
|
RailroadWidget.prototype.dispatchLink = function(to,event) {
|
|
|
|
// Send the click on its way as a navigate event
|
|
|
|
var bounds = this.domNodes[0].getBoundingClientRect();
|
|
|
|
this.dispatchEvent({
|
|
|
|
type: "tm-navigate",
|
|
|
|
navigateTo: to,
|
|
|
|
navigateFromTitle: this.getVariable("storyTiddler"),
|
|
|
|
navigateFromNode: this,
|
|
|
|
navigateFromClientRect: { top: bounds.top, left: bounds.left, width: bounds.width, right: bounds.right, bottom: bounds.bottom, height: bounds.height
|
|
|
|
},
|
|
|
|
navigateSuppressNavigation: event.metaKey || event.ctrlKey || (event.button === 1)
|
|
|
|
});
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2015-01-03 20:02:27 +00:00
|
|
|
exports.railroad = RailroadWidget;
|
|
|
|
|
|
|
|
})();
|