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)) {
|
2016-04-29 17:53:23 +00:00
|
|
|
self.invokeActions(self,event);
|
|
|
|
if(self.actions) {
|
|
|
|
self.invokeActionString(self.actions,self,event);
|
|
|
|
}
|
2014-01-30 13:40:36 +00:00
|
|
|
self.dispatchMessage(event);
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
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() {
|
|
|
|
// Get attributes
|
2016-04-29 17:53:23 +00:00
|
|
|
this.actions = this.getAttribute("actions");
|
2014-01-30 13:40:36 +00:00
|
|
|
this.message = this.getAttribute("message");
|
|
|
|
this.param = this.getAttribute("param");
|
|
|
|
this.key = this.getAttribute("key");
|
2017-01-13 18:17:19 +00:00
|
|
|
this.tag = this.getAttribute("tag");
|
2016-04-22 07:36:29 +00:00
|
|
|
this.keyInfoArray = $tw.keyboardManager.parseKeyDescriptors(this.key);
|
2014-01-30 13:40:36 +00:00
|
|
|
this["class"] = this.getAttribute("class");
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.keyboard = KeyboardWidget;
|
|
|
|
|
|
|
|
})();
|