1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Extend messagecatcher widget to allow setting multiple handlers at once

This commit is contained in:
jeremy@jermolene.com 2021-06-30 16:17:59 +01:00
parent a0a0df9655
commit f87b3bfcdb
2 changed files with 39 additions and 38 deletions

View File

@ -33,12 +33,44 @@ MessageCatcherWidget.prototype.render = function(parent,nextSibling) {
// Compute attributes and execute state
this.computeAttributes();
this.execute();
// Add our message handler
if(this.messageType) {
this.addEventListeners([
{type: this.messageType, handler: "handleEvent"}
]);
// Helper to add an event handler
var addEventHandler = function(type,actions) {
if(type && actions) {
self.addEventListener(
type,
function(event) {
// Collect all the event properties into variables
var collectProps = function(obj,prefix) {
prefix = prefix || "";
var props = {};
$tw.utils.each(obj,function(value,name) {
if(["string","boolean","number"].indexOf(typeof value) !== -1) {
props[prefix + name] = value.toString();
}
});
return props;
};
var variables = $tw.utils.extend(
{},
collectProps(event.paramObject,"event-paramObject-"),
collectProps(event,"event-"),
{
modifier: $tw.keyboardManager.getEventModifierKeyDescriptor(event)
});
self.invokeActionString(actions,self,event,variables);
return false;
}
);
}
}
// Add the main event handler
addEventHandler(this.getAttribute("type"),this.getAttribute("actions"));
// Add any other event handlers
$tw.utils.each(this.attributes,function(value,key) {
if(key.charAt(0) === "$") {
addEventHandler(key.slice(1),value);
}
});
// Render children
this.renderChildren(parent,null);
};
@ -47,48 +79,16 @@ MessageCatcherWidget.prototype.render = function(parent,nextSibling) {
Compute the internal state of the widget
*/
MessageCatcherWidget.prototype.execute = function() {
var self = this;
// Get attributes that require a refresh on change
this.messageType = this.getAttribute("type");
this.messageActions = this.getAttribute("actions");
// Make child widgets
this.makeChildWidgets();
};
/*
Handle an event
*/
MessageCatcherWidget.prototype.handleEvent = function(event) {
if(this.messageActions) {
// Collect all the event properties into variables
var collectProps = function(obj,prefix) {
prefix = prefix || "";
var props = {};
$tw.utils.each(obj,function(value,name) {
if(["string","boolean","number"].indexOf(typeof value) !== -1) {
props[prefix + name] = value.toString();
}
});
return props;
};
var variables = $tw.utils.extend(
{},
collectProps(event.paramObject,"event-paramObject-"),
collectProps(event,"event-"),
{
modifier: $tw.keyboardManager.getEventModifierKeyDescriptor(event)
});
this.invokeActionString(this.messageActions,this,event,variables);
}
return false;
};
/*
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/
MessageCatcherWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes();
if(changedAttributes["type"]) {
if($tw.utils.count(changedAttributes) > 0) {
this.refreshSelf();
return true;
}

View File

@ -17,6 +17,7 @@ The content of the `<$messagecatcher>` widget is displayed normally.
|!Attribute |!Description |
|type |Name of the message be trapped, for example "tm-scroll" or "tm-navigate" |
|actions |Action string to be invoked when a matching message is trapped |
|//{any attributes starting with $}// |Each attribute name (excluding the $) specifies the name of a message, and the value specifies the action string to be invoked |
! Variables