1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-17 23:34:50 +00:00
TiddlyWiki5/core/modules/widgets/linkcatcher.js

108 lines
3.0 KiB
JavaScript
Raw Normal View History

2013-10-16 15:30:06 +00:00
/*\
title: $:/core/modules/widgets/linkcatcher.js
2013-10-16 15:30:06 +00:00
type: application/javascript
module-type: widget
2013-10-16 15:30:06 +00:00
Linkcatcher widget
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
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);
this.addEventListeners([
2014-08-28 20:43:44 +00:00
{type: "tm-navigate", handler: "handleNavigateEvent"}
2013-10-16 15:30:06 +00:00
]);
};
/*
Inherit from the base widget class
*/
LinkCatcherWidget.prototype = new Widget();
/*
Render this widget into the DOM
*/
LinkCatcherWidget.prototype.render = function(parent,nextSibling) {
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");
this.catchActions = this.getAttribute("actions");
2013-10-16 15:30:06 +00:00
// Construct the child widgets
this.makeChildWidgets();
// 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 {
return this.refreshChildren(changedTiddlers);
}
};
/*
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) {
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;
var modifierKey = $tw.keyboardManager.getEventModifierKeyDescriptor(event);
this.invokeActionString(this.catchActions,this,event,{navigateTo: event.navigateTo, modifier: modifierKey});
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
this.parentWidget.dispatchEvent({
type: "tm-navigate",
param: event.navigateTo,
navigateTo: event.navigateTo
2013-10-16 15:30:06 +00:00
});
}
return false;
};
exports.linkcatcher = LinkCatcherWidget;
})();