Extend messagecatcher widget to allow setting multiple handlers at once

new-json-store-area
jeremy@jermolene.com 2021-06-30 16:17:59 +01:00
rodzic a0a0df9655
commit f87b3bfcdb
2 zmienionych plików z 39 dodań i 38 usunięć

Wyświetl plik

@ -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;
}

Wyświetl plik

@ -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