2021-03-09 18:07:07 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widgets/messagecatcher.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Message catcher widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
|
|
|
|
|
|
|
var MessageCatcherWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
MessageCatcherWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
MessageCatcherWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
var self = this;
|
|
|
|
// Remember parent
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
// Compute attributes and execute state
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
2021-06-30 15:17:59 +00:00
|
|
|
// Helper to add an event handler
|
|
|
|
var addEventHandler = function(type,actions) {
|
|
|
|
if(type && actions) {
|
2021-09-20 15:46:26 +00:00
|
|
|
var isActionStringExecuting = false;
|
2021-06-30 15:17:59 +00:00
|
|
|
self.addEventListener(
|
|
|
|
type,
|
|
|
|
function(event) {
|
2021-09-20 15:46:26 +00:00
|
|
|
// Don't trap the event if it came from one of our action handlers
|
|
|
|
if(isActionStringExecuting) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-06-30 15:17:59 +00:00
|
|
|
// Collect all the event properties into variables
|
|
|
|
var collectProps = function(obj,prefix) {
|
|
|
|
prefix = prefix || "";
|
2021-08-21 11:50:38 +00:00
|
|
|
var props = {},
|
|
|
|
names = [];
|
2021-06-30 15:17:59 +00:00
|
|
|
$tw.utils.each(obj,function(value,name) {
|
|
|
|
if(["string","boolean","number"].indexOf(typeof value) !== -1) {
|
2021-08-21 11:50:38 +00:00
|
|
|
names.push(name);
|
|
|
|
props[prefix + "-" + name] = value.toString();
|
2021-06-30 15:17:59 +00:00
|
|
|
}
|
|
|
|
});
|
2021-08-21 11:50:38 +00:00
|
|
|
props["list-" + prefix] = $tw.utils.stringifyList(names);
|
2021-06-30 15:17:59 +00:00
|
|
|
return props;
|
|
|
|
};
|
|
|
|
var variables = $tw.utils.extend(
|
|
|
|
{},
|
2021-08-21 11:50:38 +00:00
|
|
|
collectProps(event.paramObject,"event-paramObject"),
|
|
|
|
collectProps(event,"event"),
|
2021-06-30 15:17:59 +00:00
|
|
|
{
|
|
|
|
modifier: $tw.keyboardManager.getEventModifierKeyDescriptor(event)
|
|
|
|
});
|
2021-09-20 15:46:26 +00:00
|
|
|
isActionStringExecuting = true;
|
2021-06-30 15:17:59 +00:00
|
|
|
self.invokeActionString(actions,self,event,variables);
|
2021-09-20 15:46:26 +00:00
|
|
|
isActionStringExecuting = false;
|
2021-06-30 15:17:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2021-03-09 18:07:07 +00:00
|
|
|
}
|
2021-06-30 15:17:59 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
});
|
2021-03-09 18:07:07 +00:00
|
|
|
// Render children
|
2023-01-16 19:34:02 +00:00
|
|
|
this.renderChildren(parent,nextSibling);
|
2021-03-09 18:07:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
MessageCatcherWidget.prototype.execute = function() {
|
|
|
|
// Make child widgets
|
|
|
|
this.makeChildWidgets();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
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();
|
2021-06-30 15:17:59 +00:00
|
|
|
if($tw.utils.count(changedAttributes) > 0) {
|
2021-03-09 18:07:07 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.messagecatcher = MessageCatcherWidget;
|
|
|
|
|
|
|
|
})();
|