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:
parent
a0a0df9655
commit
f87b3bfcdb
@ -33,12 +33,44 @@ 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) {
|
||||||
|
// 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
|
// Render children
|
||||||
this.renderChildren(parent,null);
|
this.renderChildren(parent,null);
|
||||||
};
|
};
|
||||||
@ -47,48 +79,16 @@ MessageCatcherWidget.prototype.render = function(parent,nextSibling) {
|
|||||||
Compute the internal state of the widget
|
Compute the internal state of the widget
|
||||||
*/
|
*/
|
||||||
MessageCatcherWidget.prototype.execute = function() {
|
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
|
// Make child widgets
|
||||||
this.makeChildWidgets();
|
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
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
||||||
*/
|
*/
|
||||||
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…
Reference in New Issue
Block a user