2013-05-01 11:02:33 +00:00
|
|
|
/*\
|
2013-05-31 12:23:25 +00:00
|
|
|
title: $:/core/modules/widgets/linkcatcher.js
|
2013-05-01 11:02:33 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
2013-05-01 15:11:52 +00:00
|
|
|
Implements the linkcatcher widget. It intercepts navigation events from its children, preventing normal navigation, and instead stores the name of the target tiddler in the text reference specified in the `to` attribute.
|
|
|
|
|
|
|
|
Using the linkcatcher widget allows the linking mechanism to be used for tasks like selecting the current theme tiddler from a list.
|
2013-05-01 11:02:33 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var LinkCatcherWidget = function(renderer) {
|
|
|
|
// Save state
|
|
|
|
this.renderer = renderer;
|
|
|
|
// Generate child nodes
|
|
|
|
this.generate();
|
|
|
|
};
|
|
|
|
|
|
|
|
LinkCatcherWidget.prototype.generate = function() {
|
|
|
|
// Get our attributes
|
|
|
|
this.to = this.renderer.getAttribute("to");
|
2013-06-09 18:26:42 +00:00
|
|
|
this.message = this.renderer.getAttribute("message");
|
|
|
|
this.set = this.renderer.getAttribute("set");
|
|
|
|
this.setTo = this.renderer.getAttribute("setTo");
|
2013-05-01 11:02:33 +00:00
|
|
|
// Set the element
|
|
|
|
this.tag = "div";
|
|
|
|
this.attributes = {
|
|
|
|
"class": "tw-linkcatcher"
|
|
|
|
};
|
2013-05-15 16:32:17 +00:00
|
|
|
this.children = this.renderer.renderTree.createRenderers(this.renderer,this.renderer.parseTreeNode.children);
|
2013-05-01 11:02:33 +00:00
|
|
|
this.events = [
|
|
|
|
{name: "tw-navigate", handlerObject: this, handlerMethod: "handleNavigateEvent"}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
LinkCatcherWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
|
|
|
|
// We don't need to refresh ourselves, so just refresh any child nodes
|
|
|
|
$tw.utils.each(this.children,function(node) {
|
|
|
|
if(node.refreshInDom) {
|
|
|
|
node.refreshInDom(changedTiddlers);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Navigate to a specified tiddler
|
|
|
|
LinkCatcherWidget.prototype.handleNavigateEvent = function(event) {
|
|
|
|
if(this.to) {
|
2013-05-15 16:32:17 +00:00
|
|
|
this.renderer.renderTree.wiki.setTextReference(this.to,event.navigateTo,this.renderer.tiddlerTitle);
|
2013-05-01 11:02:33 +00:00
|
|
|
}
|
2013-06-09 18:26:42 +00:00
|
|
|
if(this.message) {
|
|
|
|
$tw.utils.dispatchCustomEvent(this.renderer.domNode,this.message,{
|
|
|
|
param: event.navigateTo,
|
|
|
|
tiddlerTitle: this.renderer.tiddlerTitle
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if(this.set) {
|
|
|
|
var tiddler = this.renderer.renderTree.wiki.getTiddler(this.set);
|
|
|
|
this.renderer.renderTree.wiki.addTiddler(new $tw.Tiddler(tiddler,{title: this.set, text: this.setTo}));
|
|
|
|
}
|
2013-05-01 11:02:33 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.linkcatcher = LinkCatcherWidget;
|
|
|
|
|
2013-06-09 18:26:42 +00:00
|
|
|
})();
|