diff --git a/core/modules/widgets/messagecatcher.js b/core/modules/widgets/messagecatcher.js index ae5435f6a..51da35772 100644 --- a/core/modules/widgets/messagecatcher.js +++ b/core/modules/widgets/messagecatcher.js @@ -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; } diff --git a/editions/tw5.com/tiddlers/widgets/MessageCatcherWidget.tid b/editions/tw5.com/tiddlers/widgets/MessageCatcherWidget.tid index 60f2f23c3..bead0f75d 100644 --- a/editions/tw5.com/tiddlers/widgets/MessageCatcherWidget.tid +++ b/editions/tw5.com/tiddlers/widgets/MessageCatcherWidget.tid @@ -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