2014-01-30 13:40:36 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widgets/keyboard.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Keyboard shortcut widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
|
|
|
|
|
|
|
var KeyboardWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
KeyboardWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
KeyboardWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
var self = this;
|
|
|
|
// Remember parent
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
// Compute attributes and execute state
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
2017-01-13 18:17:19 +00:00
|
|
|
var tag = this.parseTreeNode.isBlock ? "div" : "span";
|
|
|
|
if(this.tag && $tw.config.htmlUnsafeElements.indexOf(this.tag) === -1) {
|
|
|
|
tag = this.tag;
|
|
|
|
}
|
2014-01-30 13:40:36 +00:00
|
|
|
// Create element
|
2017-01-13 18:17:19 +00:00
|
|
|
var domNode = this.document.createElement(tag);
|
2014-01-30 13:40:36 +00:00
|
|
|
// Assign classes
|
|
|
|
var classes = (this["class"] || "").split(" ");
|
2014-08-28 17:59:35 +00:00
|
|
|
classes.push("tc-keyboard");
|
2014-01-30 13:40:36 +00:00
|
|
|
domNode.className = classes.join(" ");
|
|
|
|
// Add a keyboard event handler
|
|
|
|
domNode.addEventListener("keydown",function (event) {
|
2016-04-22 07:36:29 +00:00
|
|
|
if($tw.keyboardManager.checkKeyDescriptors(event,self.keyInfoArray)) {
|
2020-11-06 17:32:26 +00:00
|
|
|
var handled = self.invokeActions(self,event);
|
2016-04-29 17:53:23 +00:00
|
|
|
if(self.actions) {
|
|
|
|
self.invokeActionString(self.actions,self,event);
|
|
|
|
}
|
2014-01-30 13:40:36 +00:00
|
|
|
self.dispatchMessage(event);
|
2020-11-06 17:32:26 +00:00
|
|
|
if(handled || self.actions || self.message) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2014-01-30 13:40:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},false);
|
|
|
|
// Insert element
|
|
|
|
parent.insertBefore(domNode,nextSibling);
|
|
|
|
this.renderChildren(domNode,null);
|
|
|
|
this.domNodes.push(domNode);
|
|
|
|
};
|
|
|
|
|
|
|
|
KeyboardWidget.prototype.dispatchMessage = function(event) {
|
|
|
|
this.dispatchEvent({type: this.message, param: this.param, tiddlerTitle: this.getVariable("currentTiddler")});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
KeyboardWidget.prototype.execute = function() {
|
2018-11-15 21:40:27 +00:00
|
|
|
var self = this;
|
2014-01-30 13:40:36 +00:00
|
|
|
// Get attributes
|
2019-07-30 12:47:56 +00:00
|
|
|
this.actions = this.getAttribute("actions","");
|
|
|
|
this.message = this.getAttribute("message","");
|
|
|
|
this.param = this.getAttribute("param","");
|
|
|
|
this.key = this.getAttribute("key","");
|
|
|
|
this.tag = this.getAttribute("tag","");
|
2016-04-22 07:36:29 +00:00
|
|
|
this.keyInfoArray = $tw.keyboardManager.parseKeyDescriptors(this.key);
|
2019-07-30 12:47:56 +00:00
|
|
|
this["class"] = this.getAttribute("class","");
|
2018-11-15 16:50:23 +00:00
|
|
|
if(this.key.substr(0,2) === "((" && this.key.substr(-2,2) === "))") {
|
|
|
|
this.shortcutTiddlers = [];
|
|
|
|
var name = this.key.substring(2,this.key.length -2);
|
|
|
|
$tw.utils.each($tw.keyboardManager.lookupNames,function(platformDescriptor) {
|
|
|
|
self.shortcutTiddlers.push("$:/config/" + platformDescriptor + "/" + name);
|
|
|
|
});
|
|
|
|
}
|
2014-01-30 13:40:36 +00:00
|
|
|
// Make child widgets
|
|
|
|
this.makeChildWidgets();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
KeyboardWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
2017-01-13 18:17:19 +00:00
|
|
|
if(changedAttributes.message || changedAttributes.param || changedAttributes.key || changedAttributes["class"] || changedAttributes.tag) {
|
2014-01-30 13:40:36 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-15 16:50:23 +00:00
|
|
|
// Update the keyInfoArray if one of its shortcut-config-tiddlers has changed
|
|
|
|
if(this.shortcutTiddlers && $tw.utils.hopArray(changedTiddlers,this.shortcutTiddlers)) {
|
|
|
|
this.keyInfoArray = $tw.keyboardManager.parseKeyDescriptors(this.key);
|
|
|
|
}
|
2014-01-30 13:40:36 +00:00
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.keyboard = KeyboardWidget;
|
|
|
|
|
|
|
|
})();
|