mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-02-06 06:00:03 +00:00
Extend messagecatcher widget to allow setting multiple handlers at once
This commit is contained in:
parent
a0a0df9655
commit
f87b3bfcdb
@ -33,33 +33,12 @@ MessageCatcherWidget.prototype.render = function(parent,nextSibling) {
|
|||||||
// Compute attributes and execute state
|
// Compute attributes and execute state
|
||||||
this.computeAttributes();
|
this.computeAttributes();
|
||||||
this.execute();
|
this.execute();
|
||||||
// Add our message handler
|
// Helper to add an event handler
|
||||||
if(this.messageType) {
|
var addEventHandler = function(type,actions) {
|
||||||
this.addEventListeners([
|
if(type && actions) {
|
||||||
{type: this.messageType, handler: "handleEvent"}
|
self.addEventListener(
|
||||||
]);
|
type,
|
||||||
}
|
function(event) {
|
||||||
// Render children
|
|
||||||
this.renderChildren(parent,null);
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
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
|
// Collect all the event properties into variables
|
||||||
var collectProps = function(obj,prefix) {
|
var collectProps = function(obj,prefix) {
|
||||||
prefix = prefix || "";
|
prefix = prefix || "";
|
||||||
@ -78,9 +57,30 @@ MessageCatcherWidget.prototype.handleEvent = function(event) {
|
|||||||
{
|
{
|
||||||
modifier: $tw.keyboardManager.getEventModifierKeyDescriptor(event)
|
modifier: $tw.keyboardManager.getEventModifierKeyDescriptor(event)
|
||||||
});
|
});
|
||||||
this.invokeActionString(this.messageActions,this,event,variables);
|
self.invokeActionString(actions,self,event,variables);
|
||||||
}
|
|
||||||
return false;
|
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);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Compute the internal state of the widget
|
||||||
|
*/
|
||||||
|
MessageCatcherWidget.prototype.execute = function() {
|
||||||
|
// Make child widgets
|
||||||
|
this.makeChildWidgets();
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -88,7 +88,7 @@ Selectively refreshes the widget if needed. Returns true if the widget or any of
|
|||||||
*/
|
*/
|
||||||
MessageCatcherWidget.prototype.refresh = function(changedTiddlers) {
|
MessageCatcherWidget.prototype.refresh = function(changedTiddlers) {
|
||||||
var changedAttributes = this.computeAttributes();
|
var changedAttributes = this.computeAttributes();
|
||||||
if(changedAttributes["type"]) {
|
if($tw.utils.count(changedAttributes) > 0) {
|
||||||
this.refreshSelf();
|
this.refreshSelf();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ The content of the `<$messagecatcher>` widget is displayed normally.
|
|||||||
|!Attribute |!Description |
|
|!Attribute |!Description |
|
||||||
|type |Name of the message be trapped, for example "tm-scroll" or "tm-navigate" |
|
|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 |
|
|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
|
! Variables
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user