2013-10-16 15:30:06 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/linkcatcher.js
|
2013-10-16 15:30:06 +00:00
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-10-16 15:30:06 +00:00
|
|
|
|
|
|
|
Linkcatcher widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
2013-10-16 15:30:06 +00:00
|
|
|
|
|
|
|
var LinkCatcherWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
LinkCatcherWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
LinkCatcherWidget.prototype.render = function(parent,nextSibling) {
|
2021-01-09 13:25:48 +00:00
|
|
|
this.addEventListeners([
|
|
|
|
{type: "tm-navigate", handler: "handleNavigateEvent"}
|
|
|
|
]);
|
2013-10-16 15:30:06 +00:00
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
this.renderChildren(parent,nextSibling);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
LinkCatcherWidget.prototype.execute = function() {
|
|
|
|
// Get our parameters
|
|
|
|
this.catchTo = this.getAttribute("to");
|
|
|
|
this.catchMessage = this.getAttribute("message");
|
|
|
|
this.catchSet = this.getAttribute("set");
|
|
|
|
this.catchSetTo = this.getAttribute("setTo");
|
2016-04-29 17:54:18 +00:00
|
|
|
this.catchActions = this.getAttribute("actions");
|
2013-10-16 15:30:06 +00:00
|
|
|
// Construct the child widgets
|
|
|
|
this.makeChildWidgets();
|
2017-07-10 13:43:43 +00:00
|
|
|
// When executing actions we avoid trapping navigate events, so that we don't trigger ourselves recursively
|
|
|
|
this.executingActions = false;
|
2013-10-16 15:30:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
LinkCatcherWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
|
|
|
if(changedAttributes.to || changedAttributes.message || changedAttributes.set || changedAttributes.setTo) {
|
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
} else {
|
2021-05-30 18:20:17 +00:00
|
|
|
return this.refreshChildren(changedTiddlers);
|
2013-10-16 15:30:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2014-08-28 20:43:44 +00:00
|
|
|
Handle a tm-navigate event
|
2013-10-16 15:30:06 +00:00
|
|
|
*/
|
|
|
|
LinkCatcherWidget.prototype.handleNavigateEvent = function(event) {
|
2017-07-10 13:43:43 +00:00
|
|
|
if(!this.executingActions) {
|
|
|
|
// Execute the actions
|
|
|
|
if(this.catchTo) {
|
|
|
|
this.wiki.setTextReference(this.catchTo,event.navigateTo,this.getVariable("currentTiddler"));
|
|
|
|
}
|
|
|
|
if(this.catchMessage && this.parentWidget) {
|
|
|
|
this.parentWidget.dispatchEvent({
|
|
|
|
type: this.catchMessage,
|
|
|
|
param: event.navigateTo,
|
|
|
|
navigateTo: event.navigateTo
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if(this.catchSet) {
|
|
|
|
var tiddler = this.wiki.getTiddler(this.catchSet);
|
|
|
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,{title: this.catchSet, text: this.catchSetTo}));
|
|
|
|
}
|
|
|
|
if(this.catchActions) {
|
|
|
|
this.executingActions = true;
|
2020-07-15 11:27:14 +00:00
|
|
|
var modifierKey = $tw.keyboardManager.getEventModifierKeyDescriptor(event);
|
|
|
|
this.invokeActionString(this.catchActions,this,event,{navigateTo: event.navigateTo, modifier: modifierKey});
|
2017-07-10 13:43:43 +00:00
|
|
|
this.executingActions = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is a navigate event generated by the actions of this linkcatcher, so we don't trap it again, but just pass it to the parent
|
2014-04-09 15:09:12 +00:00
|
|
|
this.parentWidget.dispatchEvent({
|
2017-07-10 13:43:43 +00:00
|
|
|
type: "tm-navigate",
|
2014-04-09 15:09:12 +00:00
|
|
|
param: event.navigateTo,
|
|
|
|
navigateTo: event.navigateTo
|
2013-10-16 15:30:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.linkcatcher = LinkCatcherWidget;
|
|
|
|
|
|
|
|
})();
|