2019-02-15 21:49:27 -05:00
|
|
|
|
import "./Keyboard.css";
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
|
|
|
|
// Services
|
2020-02-06 00:30:41 -05:00
|
|
|
|
import { getDefaultLayout } from "../services/KeyboardLayout";
|
2019-02-15 21:49:27 -05:00
|
|
|
|
import PhysicalKeyboard from "../services/PhysicalKeyboard";
|
|
|
|
|
import Utilities from "../services/Utilities";
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Root class for simple-keyboard
|
|
|
|
|
* This class:
|
|
|
|
|
* - Parses the options
|
|
|
|
|
* - Renders the rows and buttons
|
|
|
|
|
* - Handles button functionality
|
|
|
|
|
*/
|
2018-04-20 16:34:02 -04:00
|
|
|
|
class SimpleKeyboard {
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Creates an instance of SimpleKeyboard
|
|
|
|
|
* @param {Array} params If first parameter is a string, it is considered the container class. The second parameter is then considered the options object. If first parameter is an object, it is considered the options object.
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
constructor(...params) {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const { keyboardDOMClass, keyboardDOM, options = {} } = this.handleParams(
|
|
|
|
|
params
|
|
|
|
|
);
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
2018-10-06 02:16:12 -04:00
|
|
|
|
/**
|
|
|
|
|
* Initializing Utilities
|
|
|
|
|
*/
|
2019-07-14 11:27:18 -04:00
|
|
|
|
this.utilities = new Utilities({
|
|
|
|
|
getOptions: this.getOptions,
|
|
|
|
|
getCaretPosition: this.getCaretPosition,
|
2020-08-07 17:52:12 -04:00
|
|
|
|
getCaretPositionEnd: this.getCaretPositionEnd,
|
2019-07-14 11:27:18 -04:00
|
|
|
|
dispatch: this.dispatch
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Caret position
|
|
|
|
|
*/
|
|
|
|
|
this.caretPosition = null;
|
2018-10-06 02:16:12 -04:00
|
|
|
|
|
2020-08-07 17:52:12 -04:00
|
|
|
|
/**
|
|
|
|
|
* Caret position end
|
|
|
|
|
*/
|
|
|
|
|
this.caretPositionEnd = null;
|
|
|
|
|
|
2018-04-20 16:34:02 -04:00
|
|
|
|
/**
|
|
|
|
|
* Processing options
|
|
|
|
|
*/
|
2020-02-06 00:30:41 -05:00
|
|
|
|
this.keyboardDOM = keyboardDOM;
|
2018-10-24 18:18:24 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {object}
|
|
|
|
|
* @property {object} layout Modify the keyboard layout.
|
|
|
|
|
* @property {string} layoutName Specifies which layout should be used.
|
|
|
|
|
* @property {object} display Replaces variable buttons (such as {bksp}) with a human-friendly name (e.g.: “backspace”).
|
|
|
|
|
* @property {boolean} mergeDisplay By default, when you set the display property, you replace the default one. This setting merges them instead.
|
|
|
|
|
* @property {string} theme A prop to add your own css classes to the keyboard wrapper. You can add multiple classes separated by a space.
|
2020-02-08 11:35:21 -05:00
|
|
|
|
* @property {array} buttonTheme A prop to add your own css classes to one or several buttons.
|
|
|
|
|
* @property {array} buttonAttributes A prop to add your own attributes to one or several buttons.
|
2018-10-24 18:18:24 -04:00
|
|
|
|
* @property {boolean} debug Runs a console.log every time a key is pressed. Displays the buttons pressed and the current input.
|
|
|
|
|
* @property {boolean} newLineOnEnter Specifies whether clicking the “ENTER” button will input a newline (\n) or not.
|
|
|
|
|
* @property {boolean} tabCharOnTab Specifies whether clicking the “TAB” button will input a tab character (\t) or not.
|
|
|
|
|
* @property {string} inputName Allows you to use a single simple-keyboard instance for several inputs.
|
|
|
|
|
* @property {number} maxLength Restrains all of simple-keyboard inputs to a certain length. This should be used in addition to the input element’s maxlengthattribute.
|
|
|
|
|
* @property {object} maxLength Restrains simple-keyboard’s individual inputs to a certain length. This should be used in addition to the input element’s maxlengthattribute.
|
|
|
|
|
* @property {boolean} syncInstanceInputs When set to true, this option synchronizes the internal input of every simple-keyboard instance.
|
|
|
|
|
* @property {boolean} physicalKeyboardHighlight Enable highlighting of keys pressed on physical keyboard.
|
2018-11-24 16:29:13 +01:00
|
|
|
|
* @property {boolean} preventMouseDownDefault Calling preventDefault for the mousedown events keeps the focus on the input.
|
2019-11-20 16:49:34 -05:00
|
|
|
|
* @property {boolean} stopMouseDownPropagation Stops pointer down events on simple-keyboard buttons from bubbling to parent elements.
|
2018-10-24 18:18:24 -04:00
|
|
|
|
* @property {string} physicalKeyboardHighlightTextColor Define the text color that the physical keyboard highlighted key should have.
|
2018-11-24 16:29:13 +01:00
|
|
|
|
* @property {string} physicalKeyboardHighlightBgColor Define the background color that the physical keyboard highlighted key should have.
|
2018-10-24 18:18:24 -04:00
|
|
|
|
* @property {function(button: string):string} onKeyPress Executes the callback function on key press. Returns button layout name (i.e.: “{shift}”).
|
|
|
|
|
* @property {function(input: string):string} onChange Executes the callback function on input change. Returns the current input’s string.
|
|
|
|
|
* @property {function} onRender Executes the callback function every time simple-keyboard is rendered (e.g: when you change layouts).
|
|
|
|
|
* @property {function} onInit Executes the callback function once simple-keyboard is rendered for the first time (on initialization).
|
|
|
|
|
* @property {function(inputs: object):object} onChangeAll Executes the callback function on input change. Returns the input object with all defined inputs.
|
2018-12-14 00:28:18 -05:00
|
|
|
|
* @property {boolean} useButtonTag Render buttons as a button element instead of a div element.
|
2019-01-16 19:38:42 -05:00
|
|
|
|
* @property {boolean} disableCaretPositioning A prop to ensure characters are always be added/removed at the end of the string.
|
2019-01-24 23:04:59 -05:00
|
|
|
|
* @property {object} inputPattern Restrains input(s) change to the defined regular expression pattern.
|
2019-03-06 19:28:12 -05:00
|
|
|
|
* @property {boolean} useTouchEvents Instructs simple-keyboard to use touch events instead of click events.
|
|
|
|
|
* @property {boolean} autoUseTouchEvents Enable useTouchEvents automatically when touch device is detected.
|
2019-03-07 20:51:29 -05:00
|
|
|
|
* @property {boolean} useMouseEvents Opt out of PointerEvents handling, falling back to the prior mouse event logic.
|
2019-06-02 02:56:24 -04:00
|
|
|
|
* @property {function} destroy Clears keyboard listeners and DOM elements.
|
2019-06-05 21:08:53 -04:00
|
|
|
|
* @property {boolean} disableButtonHold Disable button hold action.
|
2019-07-14 11:27:18 -04:00
|
|
|
|
* @property {function} onKeyReleased Executes the callback function on key release.
|
2020-02-08 11:35:21 -05:00
|
|
|
|
* @property {array} modules Module classes to be loaded by simple-keyboard.
|
2018-10-24 18:18:24 -04:00
|
|
|
|
*/
|
2018-04-20 16:34:02 -04:00
|
|
|
|
this.options = options;
|
|
|
|
|
this.options.layoutName = this.options.layoutName || "default";
|
|
|
|
|
this.options.theme = this.options.theme || "hg-theme-default";
|
2018-04-30 10:35:51 -04:00
|
|
|
|
this.options.inputName = this.options.inputName || "default";
|
2019-02-15 21:49:27 -05:00
|
|
|
|
this.options.preventMouseDownDefault =
|
|
|
|
|
this.options.preventMouseDownDefault || false;
|
2018-10-24 18:18:24 -04:00
|
|
|
|
|
2018-10-31 19:48:17 -04:00
|
|
|
|
/**
|
|
|
|
|
* @type {object} Classes identifying loaded plugins
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
this.keyboardPluginClasses = "";
|
2018-10-31 19:48:17 -04:00
|
|
|
|
|
2018-10-30 23:28:23 -04:00
|
|
|
|
/**
|
|
|
|
|
* Bindings
|
|
|
|
|
*/
|
2019-03-06 20:22:09 -05:00
|
|
|
|
Utilities.bindMethods(SimpleKeyboard, this);
|
2018-10-30 23:28:23 -04:00
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* simple-keyboard uses a non-persistent internal input to keep track of the entered string (the variable `keyboard.input`).
|
|
|
|
|
* This removes any dependency to input DOM elements. You can type and directly display the value in a div element, for example.
|
|
|
|
|
* @example
|
|
|
|
|
* // To get entered input
|
2020-02-06 00:30:41 -05:00
|
|
|
|
* const input = keyboard.getInput();
|
2019-02-15 21:49:27 -05:00
|
|
|
|
*
|
2018-10-24 18:18:24 -04:00
|
|
|
|
* // To clear entered input.
|
|
|
|
|
* keyboard.clearInput();
|
2019-02-15 21:49:27 -05:00
|
|
|
|
*
|
2018-10-24 18:18:24 -04:00
|
|
|
|
* @type {object}
|
|
|
|
|
* @property {object} default Default SimpleKeyboard internal input.
|
|
|
|
|
* @property {object} myInputName Example input that can be set through `options.inputName:"myInputName"`.
|
|
|
|
|
*/
|
2018-04-30 10:35:51 -04:00
|
|
|
|
this.input = {};
|
2019-02-15 21:49:27 -05:00
|
|
|
|
this.input[this.options.inputName] = "";
|
2018-10-24 18:18:24 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {string} DOM class of the keyboard wrapper, normally "simple-keyboard" by default.
|
|
|
|
|
*/
|
2020-02-06 00:30:41 -05:00
|
|
|
|
this.keyboardDOMClass = keyboardDOMClass;
|
2018-10-24 18:18:24 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {object} Contains the DOM elements of every rendered button, the key being the button's layout name (e.g.: "{enter}").
|
|
|
|
|
*/
|
2018-10-06 02:17:54 -04:00
|
|
|
|
this.buttonElements = {};
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-14 11:27:18 -04:00
|
|
|
|
* Simple-keyboard Instances
|
2018-09-23 23:36:01 -04:00
|
|
|
|
* This enables multiple simple-keyboard support with easier management
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (!window["SimpleKeyboardInstances"])
|
|
|
|
|
window["SimpleKeyboardInstances"] = {};
|
|
|
|
|
|
2019-11-07 11:25:18 -05:00
|
|
|
|
this.currentInstanceName = this.utilities.camelCase(this.keyboardDOMClass);
|
|
|
|
|
window["SimpleKeyboardInstances"][this.currentInstanceName] = this;
|
2018-09-23 23:36:01 -04:00
|
|
|
|
|
2019-07-14 11:27:18 -04:00
|
|
|
|
/**
|
|
|
|
|
* Instance vars
|
|
|
|
|
*/
|
|
|
|
|
this.allKeyboardInstances = window["SimpleKeyboardInstances"];
|
|
|
|
|
this.keyboardInstanceNames = Object.keys(window["SimpleKeyboardInstances"]);
|
|
|
|
|
this.isFirstKeyboardInstance =
|
|
|
|
|
this.keyboardInstanceNames[0] === this.currentInstanceName;
|
|
|
|
|
|
2018-09-23 23:37:46 -04:00
|
|
|
|
/**
|
|
|
|
|
* Physical Keyboard support
|
|
|
|
|
*/
|
2019-07-14 11:27:18 -04:00
|
|
|
|
this.physicalKeyboard = new PhysicalKeyboard({
|
|
|
|
|
dispatch: this.dispatch,
|
|
|
|
|
getOptions: this.getOptions
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Rendering keyboard
|
|
|
|
|
*/
|
|
|
|
|
if (this.keyboardDOM) this.render();
|
|
|
|
|
else {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
console.warn(`".${keyboardDOMClass}" was not found in the DOM.`);
|
2019-07-14 11:27:18 -04:00
|
|
|
|
throw new Error("KEYBOARD_DOM_ERROR");
|
|
|
|
|
}
|
2018-10-31 17:57:33 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Modules
|
|
|
|
|
*/
|
|
|
|
|
this.modules = {};
|
|
|
|
|
this.loadModules();
|
2018-04-20 16:34:02 -04:00
|
|
|
|
}
|
2018-10-31 17:57:33 -04:00
|
|
|
|
|
2020-02-06 00:30:41 -05:00
|
|
|
|
/**
|
|
|
|
|
* parseParams
|
|
|
|
|
*/
|
|
|
|
|
handleParams = params => {
|
|
|
|
|
let keyboardDOMClass;
|
|
|
|
|
let keyboardDOM;
|
|
|
|
|
let options;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If first parameter is a string:
|
|
|
|
|
* Consider it as an element's class
|
|
|
|
|
*/
|
|
|
|
|
if (typeof params[0] === "string") {
|
|
|
|
|
keyboardDOMClass = params[0].split(".").join("");
|
|
|
|
|
keyboardDOM = document.querySelector(`.${keyboardDOMClass}`);
|
|
|
|
|
options = params[1];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If first parameter is an HTMLDivElement
|
|
|
|
|
* Consider it as the keyboard DOM element
|
|
|
|
|
*/
|
|
|
|
|
} else if (params[0] instanceof HTMLDivElement) {
|
|
|
|
|
/**
|
|
|
|
|
* This element must have a class, otherwise throw
|
|
|
|
|
*/
|
|
|
|
|
if (!params[0].className) {
|
|
|
|
|
console.warn("Any DOM element passed as parameter must have a class.");
|
|
|
|
|
throw new Error("KEYBOARD_DOM_CLASS_ERROR");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keyboardDOMClass = params[0].className.split(" ")[0];
|
|
|
|
|
keyboardDOM = params[0];
|
|
|
|
|
options = params[1];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Otherwise, search for .simple-keyboard DOM element
|
|
|
|
|
*/
|
|
|
|
|
} else {
|
|
|
|
|
keyboardDOMClass = "simple-keyboard";
|
|
|
|
|
keyboardDOM = document.querySelector(`.${keyboardDOMClass}`);
|
|
|
|
|
options = params[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
keyboardDOMClass,
|
|
|
|
|
keyboardDOM,
|
|
|
|
|
options
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-14 11:27:18 -04:00
|
|
|
|
/**
|
|
|
|
|
* Getters
|
|
|
|
|
*/
|
|
|
|
|
getOptions = () => this.options;
|
|
|
|
|
getCaretPosition = () => this.caretPosition;
|
2020-08-07 17:52:12 -04:00
|
|
|
|
getCaretPositionEnd = () => this.caretPositionEnd;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Setters
|
|
|
|
|
*/
|
2020-08-26 00:54:56 -04:00
|
|
|
|
setCaretPosition(position, endPosition) {
|
2020-08-07 17:52:12 -04:00
|
|
|
|
this.caretPosition = position;
|
|
|
|
|
this.caretPositionEnd = endPosition || position;
|
2020-08-26 00:54:56 -04:00
|
|
|
|
}
|
2019-07-14 11:27:18 -04:00
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Handles clicks made to keyboard buttons
|
|
|
|
|
* @param {string} button The button's layout name.
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
handleButtonClicked(button) {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const debug = this.options.debug;
|
2018-10-06 02:26:22 -04:00
|
|
|
|
|
2018-04-20 16:34:02 -04:00
|
|
|
|
/**
|
|
|
|
|
* Ignoring placeholder buttons
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (button === "{//}") return false;
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calling onKeyPress
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (typeof this.options.onKeyPress === "function")
|
2018-04-20 16:34:02 -04:00
|
|
|
|
this.options.onKeyPress(button);
|
2019-02-15 21:49:27 -05:00
|
|
|
|
|
|
|
|
|
if (!this.input[this.options.inputName])
|
|
|
|
|
this.input[this.options.inputName] = "";
|
2018-04-30 10:35:51 -04:00
|
|
|
|
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const updatedInput = this.utilities.getUpdatedInput(
|
2019-02-15 21:49:27 -05:00
|
|
|
|
button,
|
|
|
|
|
this.input[this.options.inputName],
|
2020-08-07 17:52:12 -04:00
|
|
|
|
this.caretPosition,
|
|
|
|
|
this.caretPositionEnd
|
2018-11-05 17:34:16 -05:00
|
|
|
|
);
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (
|
2019-01-24 23:04:59 -05:00
|
|
|
|
// If input will change as a result of this button press
|
|
|
|
|
this.input[this.options.inputName] !== updatedInput &&
|
|
|
|
|
// This pertains to the "inputPattern" option:
|
2019-02-15 21:49:27 -05:00
|
|
|
|
// If inputPattern isn't set
|
|
|
|
|
(!this.options.inputPattern ||
|
2019-01-24 23:04:59 -05:00
|
|
|
|
// Or, if it is set and if the pattern is valid - we proceed.
|
2019-02-15 21:49:27 -05:00
|
|
|
|
(this.options.inputPattern && this.inputPatternIsValid(updatedInput)))
|
|
|
|
|
) {
|
2018-10-06 02:18:55 -04:00
|
|
|
|
/**
|
|
|
|
|
* If maxLength and handleMaxLength yield true, halting
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (
|
|
|
|
|
this.options.maxLength &&
|
2019-07-14 11:27:18 -04:00
|
|
|
|
this.utilities.handleMaxLength(this.input, updatedInput)
|
2019-02-15 21:49:27 -05:00
|
|
|
|
) {
|
2018-10-06 02:18:55 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-15 21:49:27 -05:00
|
|
|
|
this.input[this.options.inputName] = this.utilities.getUpdatedInput(
|
|
|
|
|
button,
|
|
|
|
|
this.input[this.options.inputName],
|
|
|
|
|
this.caretPosition,
|
2020-08-07 17:52:12 -04:00
|
|
|
|
this.caretPositionEnd,
|
2019-02-15 21:49:27 -05:00
|
|
|
|
true
|
2018-11-05 17:34:16 -05:00
|
|
|
|
);
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (debug) console.log("Input changed:", this.input);
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
2020-08-07 17:52:12 -04:00
|
|
|
|
if (this.options.debug) {
|
|
|
|
|
console.log(
|
|
|
|
|
"Caret at: ",
|
|
|
|
|
this.getCaretPosition(),
|
|
|
|
|
this.getCaretPositionEnd(),
|
|
|
|
|
`(${this.keyboardDOMClass})`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-23 23:36:01 -04:00
|
|
|
|
/**
|
2018-10-24 18:18:24 -04:00
|
|
|
|
* Enforce syncInstanceInputs, if set
|
2018-09-23 23:36:01 -04:00
|
|
|
|
*/
|
2019-10-08 18:10:34 -04:00
|
|
|
|
if (this.options.syncInstanceInputs) this.syncInstanceInputs();
|
2018-09-23 23:36:01 -04:00
|
|
|
|
|
2018-04-20 16:34:02 -04:00
|
|
|
|
/**
|
|
|
|
|
* Calling onChange
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (typeof this.options.onChange === "function")
|
2018-04-30 10:35:51 -04:00
|
|
|
|
this.options.onChange(this.input[this.options.inputName]);
|
2019-03-07 20:51:57 -05:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calling onChangeAll
|
|
|
|
|
*/
|
|
|
|
|
if (typeof this.options.onChangeAll === "function")
|
|
|
|
|
this.options.onChangeAll(this.input);
|
2018-04-20 16:34:02 -04:00
|
|
|
|
}
|
2019-02-15 21:49:27 -05:00
|
|
|
|
|
|
|
|
|
if (debug) {
|
2018-04-20 16:34:02 -04:00
|
|
|
|
console.log("Key pressed:", button);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 17:57:33 -04:00
|
|
|
|
/**
|
|
|
|
|
* Handles button mousedown
|
|
|
|
|
*/
|
|
|
|
|
/* istanbul ignore next */
|
2019-02-15 21:49:27 -05:00
|
|
|
|
handleButtonMouseDown(button, e) {
|
2019-03-11 18:25:51 -04:00
|
|
|
|
/**
|
|
|
|
|
* Handle event options
|
|
|
|
|
*/
|
|
|
|
|
if (this.options.preventMouseDownDefault) e.preventDefault();
|
|
|
|
|
if (this.options.stopMouseDownPropagation) e.stopPropagation();
|
|
|
|
|
|
2019-11-07 11:25:43 -05:00
|
|
|
|
/**
|
|
|
|
|
* Add active class
|
|
|
|
|
*/
|
|
|
|
|
if (e) e.target.classList.add(this.activeButtonClass);
|
|
|
|
|
|
2020-08-07 17:52:12 -04:00
|
|
|
|
if (this.holdInteractionTimeout) clearTimeout(this.holdInteractionTimeout);
|
|
|
|
|
if (this.holdTimeout) clearTimeout(this.holdTimeout);
|
|
|
|
|
|
2018-10-31 17:57:33 -04:00
|
|
|
|
/**
|
|
|
|
|
* @type {boolean} Whether the mouse is being held onKeyPress
|
|
|
|
|
*/
|
|
|
|
|
this.isMouseHold = true;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {object} Time to wait until a key hold is detected
|
|
|
|
|
*/
|
2019-06-05 21:08:53 -04:00
|
|
|
|
if (!this.options.disableButtonHold) {
|
|
|
|
|
this.holdTimeout = setTimeout(() => {
|
|
|
|
|
if (
|
2020-05-05 23:58:54 +00:00
|
|
|
|
(this.isMouseHold &&
|
|
|
|
|
// TODO: This needs to be configurable through options
|
|
|
|
|
((!button.includes("{") && !button.includes("}")) ||
|
|
|
|
|
button === "{delete}" ||
|
|
|
|
|
button === "{backspace}" ||
|
|
|
|
|
button === "{bksp}" ||
|
|
|
|
|
button === "{space}" ||
|
|
|
|
|
button === "{tab}")) ||
|
|
|
|
|
button === "{arrowright}" ||
|
|
|
|
|
button === "{arrowleft}" ||
|
|
|
|
|
button === "{arrowup}" ||
|
|
|
|
|
button === "{arrowdown}"
|
2019-06-05 21:08:53 -04:00
|
|
|
|
) {
|
|
|
|
|
if (this.options.debug) console.log("Button held:", button);
|
|
|
|
|
|
|
|
|
|
this.handleButtonHold(button, e);
|
|
|
|
|
}
|
|
|
|
|
clearTimeout(this.holdTimeout);
|
|
|
|
|
}, 500);
|
|
|
|
|
}
|
2018-10-31 17:57:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles button mouseup
|
|
|
|
|
*/
|
2019-07-14 11:55:03 -04:00
|
|
|
|
handleButtonMouseUp(button) {
|
2020-08-07 17:52:12 -04:00
|
|
|
|
this.dispatch(instance => {
|
|
|
|
|
/**
|
|
|
|
|
* Remove active class
|
|
|
|
|
*/
|
|
|
|
|
instance.recurseButtons(buttonElement => {
|
|
|
|
|
buttonElement.classList.remove(this.activeButtonClass);
|
|
|
|
|
});
|
2019-11-07 11:25:43 -05:00
|
|
|
|
|
2020-08-07 17:52:12 -04:00
|
|
|
|
instance.isMouseHold = false;
|
|
|
|
|
if (instance.holdInteractionTimeout)
|
|
|
|
|
clearTimeout(instance.holdInteractionTimeout);
|
|
|
|
|
});
|
2019-07-14 11:27:18 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calling onKeyReleased
|
|
|
|
|
*/
|
2019-07-14 11:55:03 -04:00
|
|
|
|
if (button && typeof this.options.onKeyReleased === "function")
|
|
|
|
|
this.options.onKeyReleased(button);
|
2018-10-31 17:57:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 11:13:43 -04:00
|
|
|
|
/**
|
|
|
|
|
* Handles container mousedown
|
|
|
|
|
*/
|
|
|
|
|
handleKeyboardContainerMouseDown(e) {
|
|
|
|
|
/**
|
|
|
|
|
* Handle event options
|
|
|
|
|
*/
|
|
|
|
|
if (this.options.preventMouseDownDefault) e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 17:57:33 -04:00
|
|
|
|
/**
|
|
|
|
|
* Handles button hold
|
|
|
|
|
*/
|
|
|
|
|
/* istanbul ignore next */
|
2019-02-15 21:49:27 -05:00
|
|
|
|
handleButtonHold(button) {
|
|
|
|
|
if (this.holdInteractionTimeout) clearTimeout(this.holdInteractionTimeout);
|
2018-11-03 01:39:41 -04:00
|
|
|
|
|
2018-10-31 17:57:33 -04:00
|
|
|
|
/**
|
|
|
|
|
* @type {object} Timeout dictating the speed of key hold iterations
|
|
|
|
|
*/
|
|
|
|
|
this.holdInteractionTimeout = setTimeout(() => {
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (this.isMouseHold) {
|
2018-11-03 01:39:41 -04:00
|
|
|
|
this.handleButtonClicked(button);
|
|
|
|
|
this.handleButtonHold(button);
|
|
|
|
|
} else {
|
|
|
|
|
clearTimeout(this.holdInteractionTimeout);
|
|
|
|
|
}
|
2018-10-31 17:57:33 -04:00
|
|
|
|
}, 100);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Send a command to all simple-keyboard instances (if you have several instances).
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
syncInstanceInputs() {
|
|
|
|
|
this.dispatch(instance => {
|
2018-10-24 18:18:24 -04:00
|
|
|
|
instance.replaceInput(this.input);
|
2020-08-07 17:52:12 -04:00
|
|
|
|
instance.setCaretPosition(this.caretPosition, this.caretPositionEnd);
|
2018-09-23 23:36:01 -04:00
|
|
|
|
});
|
|
|
|
|
}
|
2019-02-15 21:49:27 -05:00
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Clear the keyboard’s input.
|
2018-10-24 20:27:29 -04:00
|
|
|
|
* @param {string} [inputName] optional - the internal input to select
|
2018-10-24 18:18:24 -04:00
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
clearInput(inputName) {
|
2018-04-30 10:35:51 -04:00
|
|
|
|
inputName = inputName || this.options.inputName;
|
2019-02-15 21:49:27 -05:00
|
|
|
|
this.input[inputName] = "";
|
2018-09-23 23:36:01 -04:00
|
|
|
|
|
2019-04-15 10:40:26 -04:00
|
|
|
|
/**
|
|
|
|
|
* Reset caretPosition
|
|
|
|
|
*/
|
2020-08-07 17:52:12 -04:00
|
|
|
|
this.setCaretPosition(0);
|
2019-04-15 10:40:26 -04:00
|
|
|
|
|
2018-09-23 23:36:01 -04:00
|
|
|
|
/**
|
2018-10-24 18:18:24 -04:00
|
|
|
|
* Enforce syncInstanceInputs, if set
|
2018-09-23 23:36:01 -04:00
|
|
|
|
*/
|
2019-10-08 18:10:34 -04:00
|
|
|
|
if (this.options.syncInstanceInputs) this.syncInstanceInputs();
|
2018-04-20 16:34:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Get the keyboard’s input (You can also get it from the onChange prop).
|
|
|
|
|
* @param {string} [inputName] optional - the internal input to select
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
getInput(inputName) {
|
2018-04-30 10:35:51 -04:00
|
|
|
|
inputName = inputName || this.options.inputName;
|
2018-09-23 23:36:01 -04:00
|
|
|
|
|
|
|
|
|
/**
|
2018-10-24 18:18:24 -04:00
|
|
|
|
* Enforce syncInstanceInputs, if set
|
2018-09-23 23:36:01 -04:00
|
|
|
|
*/
|
2019-10-08 18:10:34 -04:00
|
|
|
|
if (this.options.syncInstanceInputs) this.syncInstanceInputs();
|
2018-09-23 23:36:01 -04:00
|
|
|
|
|
2019-03-07 20:52:40 -05:00
|
|
|
|
return this.input[inputName];
|
2018-04-20 16:34:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Set the keyboard’s input.
|
|
|
|
|
* @param {string} input the input value
|
|
|
|
|
* @param {string} inputName optional - the internal input to select
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
setInput(input, inputName) {
|
2018-04-30 10:35:51 -04:00
|
|
|
|
inputName = inputName || this.options.inputName;
|
|
|
|
|
this.input[inputName] = input;
|
2018-09-23 23:36:01 -04:00
|
|
|
|
|
|
|
|
|
/**
|
2018-10-24 18:18:24 -04:00
|
|
|
|
* Enforce syncInstanceInputs, if set
|
2018-09-23 23:36:01 -04:00
|
|
|
|
*/
|
2019-10-08 18:10:34 -04:00
|
|
|
|
if (this.options.syncInstanceInputs) this.syncInstanceInputs();
|
2018-09-23 23:36:01 -04:00
|
|
|
|
}
|
2019-02-15 21:49:27 -05:00
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Replace the input object (`keyboard.input`)
|
|
|
|
|
* @param {object} inputObj The input object
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
replaceInput(inputObj) {
|
2018-09-23 23:36:01 -04:00
|
|
|
|
this.input = inputObj;
|
2018-04-20 16:34:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
2019-02-15 21:49:27 -05:00
|
|
|
|
* Set new option or modify existing ones after initialization.
|
2019-04-18 10:13:02 -04:00
|
|
|
|
* @param {object} options The options to set
|
2018-10-24 18:18:24 -04:00
|
|
|
|
*/
|
2020-04-22 21:52:17 -04:00
|
|
|
|
setOptions(options = {}) {
|
|
|
|
|
const changedOptions = this.changedOptions(options);
|
2019-04-18 10:13:02 -04:00
|
|
|
|
this.options = Object.assign(this.options, options);
|
|
|
|
|
|
2020-04-22 21:52:17 -04:00
|
|
|
|
if (changedOptions.length) {
|
|
|
|
|
if (this.options.debug) {
|
|
|
|
|
console.log("changedOptions", changedOptions);
|
|
|
|
|
}
|
2019-04-18 10:13:02 -04:00
|
|
|
|
|
2020-04-22 21:52:17 -04:00
|
|
|
|
/**
|
|
|
|
|
* Some option changes require adjustments before re-render
|
|
|
|
|
*/
|
|
|
|
|
this.onSetOptions(options);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Rendering
|
|
|
|
|
*/
|
|
|
|
|
this.render();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Detecting changes to non-function options
|
|
|
|
|
* This allows us to ascertain whether a button re-render is needed
|
|
|
|
|
*/
|
|
|
|
|
changedOptions(newOptions) {
|
|
|
|
|
return Object.keys(newOptions).filter(
|
|
|
|
|
optionName =>
|
|
|
|
|
JSON.stringify(newOptions[optionName]) !==
|
|
|
|
|
JSON.stringify(this.options[optionName])
|
|
|
|
|
);
|
2019-04-18 10:15:51 -04:00
|
|
|
|
}
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
2019-04-18 10:13:02 -04:00
|
|
|
|
/**
|
|
|
|
|
* Executing actions depending on changed options
|
|
|
|
|
* @param {object} options The options to set
|
|
|
|
|
*/
|
2019-04-18 10:15:51 -04:00
|
|
|
|
onSetOptions(options) {
|
|
|
|
|
if (options.inputName) {
|
2019-04-18 10:13:02 -04:00
|
|
|
|
/**
|
|
|
|
|
* inputName changed. This requires a caretPosition reset
|
|
|
|
|
*/
|
2020-08-26 00:54:56 -04:00
|
|
|
|
// TODO: Review side-effects
|
2019-04-18 10:13:02 -04:00
|
|
|
|
if (this.options.debug) {
|
|
|
|
|
console.log("inputName changed. caretPosition reset.");
|
|
|
|
|
}
|
2020-08-07 17:52:12 -04:00
|
|
|
|
this.setCaretPosition(null);
|
2019-04-18 10:13:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Remove all keyboard rows and reset keyboard values.
|
2019-11-07 11:25:43 -05:00
|
|
|
|
* Used internally between re-renders.
|
2018-10-24 18:18:24 -04:00
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
clear() {
|
|
|
|
|
this.keyboardDOM.innerHTML = "";
|
2018-08-13 14:48:21 -04:00
|
|
|
|
this.keyboardDOM.className = this.keyboardDOMClass;
|
2018-10-06 02:22:02 -04:00
|
|
|
|
this.buttonElements = {};
|
2018-04-20 16:34:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Send a command to all simple-keyboard instances at once (if you have multiple instances).
|
|
|
|
|
* @param {function(instance: object, key: string)} callback Function to run on every instance
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
dispatch(callback) {
|
|
|
|
|
if (!window["SimpleKeyboardInstances"]) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`SimpleKeyboardInstances is not defined. Dispatch cannot be called.`
|
|
|
|
|
);
|
2018-10-16 17:31:13 -04:00
|
|
|
|
throw new Error("INSTANCES_VAR_ERROR");
|
2018-09-23 23:36:01 -04:00
|
|
|
|
}
|
2019-02-15 21:49:27 -05:00
|
|
|
|
|
|
|
|
|
return Object.keys(window["SimpleKeyboardInstances"]).forEach(key => {
|
|
|
|
|
callback(window["SimpleKeyboardInstances"][key], key);
|
|
|
|
|
});
|
2018-09-23 23:36:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Adds/Modifies an entry to the `buttonTheme`. Basically a way to add a class to a button.
|
|
|
|
|
* @param {string} buttons List of buttons to select (separated by a space).
|
|
|
|
|
* @param {string} className Classes to give to the selected buttons (separated by space).
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
addButtonTheme(buttons, className) {
|
|
|
|
|
if (!className || !buttons) return false;
|
2018-10-06 02:26:00 -04:00
|
|
|
|
|
|
|
|
|
buttons.split(" ").forEach(button => {
|
|
|
|
|
className.split(" ").forEach(classNameItem => {
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (!this.options.buttonTheme) this.options.buttonTheme = [];
|
2018-10-06 02:26:00 -04:00
|
|
|
|
|
|
|
|
|
let classNameFound = false;
|
2019-02-15 21:49:27 -05:00
|
|
|
|
|
2018-10-06 02:26:00 -04:00
|
|
|
|
/**
|
|
|
|
|
* If class is already defined, we add button to class definition
|
|
|
|
|
*/
|
|
|
|
|
this.options.buttonTheme.map(buttonTheme => {
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (buttonTheme.class.split(" ").includes(classNameItem)) {
|
2018-10-06 02:26:00 -04:00
|
|
|
|
classNameFound = true;
|
2019-02-15 21:49:27 -05:00
|
|
|
|
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const buttonThemeArray = buttonTheme.buttons.split(" ");
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (!buttonThemeArray.includes(button)) {
|
2018-10-06 02:26:00 -04:00
|
|
|
|
classNameFound = true;
|
|
|
|
|
buttonThemeArray.push(button);
|
|
|
|
|
buttonTheme.buttons = buttonThemeArray.join(" ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return buttonTheme;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If class is not defined, we create a new entry
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (!classNameFound) {
|
2018-10-06 02:26:00 -04:00
|
|
|
|
this.options.buttonTheme.push({
|
|
|
|
|
class: classNameItem,
|
|
|
|
|
buttons: buttons
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.render();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Removes/Amends an entry to the `buttonTheme`. Basically a way to remove a class previously added to a button through buttonTheme or addButtonTheme.
|
|
|
|
|
* @param {string} buttons List of buttons to select (separated by a space).
|
|
|
|
|
* @param {string} className Classes to give to the selected buttons (separated by space).
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
removeButtonTheme(buttons, className) {
|
2018-10-06 02:26:00 -04:00
|
|
|
|
/**
|
|
|
|
|
* When called with empty parameters, remove all button themes
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (!buttons && !className) {
|
2018-10-06 02:26:00 -04:00
|
|
|
|
this.options.buttonTheme = [];
|
|
|
|
|
this.render();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If buttons are passed and buttonTheme has items
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (
|
|
|
|
|
buttons &&
|
|
|
|
|
Array.isArray(this.options.buttonTheme) &&
|
|
|
|
|
this.options.buttonTheme.length
|
|
|
|
|
) {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const buttonArray = buttons.split(" ");
|
|
|
|
|
buttonArray.forEach(button => {
|
2018-10-06 02:26:00 -04:00
|
|
|
|
this.options.buttonTheme.map((buttonTheme, index) => {
|
|
|
|
|
/**
|
|
|
|
|
* If className is set, we affect the buttons only for that class
|
|
|
|
|
* Otherwise, we afect all classes
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (
|
2018-10-06 02:26:00 -04:00
|
|
|
|
(className && className.includes(buttonTheme.class)) ||
|
|
|
|
|
!className
|
2019-02-15 21:49:27 -05:00
|
|
|
|
) {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const filteredButtonArray = buttonTheme.buttons
|
2019-02-15 21:49:27 -05:00
|
|
|
|
.split(" ")
|
|
|
|
|
.filter(item => item !== button);
|
2018-10-06 02:26:00 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If buttons left, return them, otherwise, remove button Theme
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (filteredButtonArray.length) {
|
2018-10-06 02:26:00 -04:00
|
|
|
|
buttonTheme.buttons = filteredButtonArray.join(" ");
|
|
|
|
|
} else {
|
|
|
|
|
this.options.buttonTheme.splice(index, 1);
|
|
|
|
|
buttonTheme = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buttonTheme;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.render();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-06 02:17:54 -04:00
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Get the DOM Element of a button. If there are several buttons with the same name, an array of the DOM Elements is returned.
|
|
|
|
|
* @param {string} button The button layout name to select
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
getButtonElement(button) {
|
2018-10-06 02:17:54 -04:00
|
|
|
|
let output;
|
|
|
|
|
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const buttonArr = this.buttonElements[button];
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (buttonArr) {
|
|
|
|
|
if (buttonArr.length > 1) {
|
2018-10-06 02:17:54 -04:00
|
|
|
|
output = buttonArr;
|
|
|
|
|
} else {
|
|
|
|
|
output = buttonArr[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-24 23:04:59 -05:00
|
|
|
|
/**
|
|
|
|
|
* This handles the "inputPattern" option
|
|
|
|
|
* by checking if the provided inputPattern passes
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
inputPatternIsValid(inputVal) {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const inputPatternRaw = this.options.inputPattern;
|
2019-01-24 23:04:59 -05:00
|
|
|
|
let inputPattern;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if input pattern is global or targeted to individual inputs
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (inputPatternRaw instanceof RegExp) {
|
2019-01-24 23:04:59 -05:00
|
|
|
|
inputPattern = inputPatternRaw;
|
|
|
|
|
} else {
|
|
|
|
|
inputPattern = inputPatternRaw[this.options.inputName];
|
|
|
|
|
}
|
2019-02-15 21:49:27 -05:00
|
|
|
|
|
|
|
|
|
if (inputPattern && inputVal) {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const didInputMatch = inputPattern.test(inputVal);
|
2019-01-24 23:04:59 -05:00
|
|
|
|
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (this.options.debug) {
|
|
|
|
|
console.log(
|
|
|
|
|
`inputPattern ("${inputPattern}"): ${
|
|
|
|
|
didInputMatch ? "passed" : "did not pass!"
|
|
|
|
|
}`
|
|
|
|
|
);
|
2019-01-24 23:04:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return didInputMatch;
|
|
|
|
|
} else {
|
|
|
|
|
/**
|
|
|
|
|
* inputPattern doesn't seem to be set for the current input, or input is empty. Pass.
|
|
|
|
|
*/
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
2019-07-14 11:27:18 -04:00
|
|
|
|
* Handles simple-keyboard event listeners
|
2018-10-24 18:18:24 -04:00
|
|
|
|
*/
|
2019-07-14 11:27:18 -04:00
|
|
|
|
setEventListeners() {
|
2020-08-07 17:52:12 -04:00
|
|
|
|
const { useTouchEvents, useMouseEvents } = this.options;
|
|
|
|
|
|
2018-11-24 21:12:19 -05:00
|
|
|
|
/**
|
2019-07-14 11:27:18 -04:00
|
|
|
|
* Only first instance should set the event listeners
|
2018-11-24 21:12:19 -05:00
|
|
|
|
*/
|
2019-07-14 11:27:18 -04:00
|
|
|
|
if (this.isFirstKeyboardInstance || !this.allKeyboardInstances) {
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (this.options.debug) {
|
|
|
|
|
console.log(`Caret handling started (${this.keyboardDOMClass})`);
|
|
|
|
|
}
|
2018-10-06 02:22:02 -04:00
|
|
|
|
|
2019-07-14 11:27:18 -04:00
|
|
|
|
/**
|
|
|
|
|
* Event Listeners
|
|
|
|
|
*/
|
2020-08-07 17:52:12 -04:00
|
|
|
|
document.onkeyup = this.handleKeyUp;
|
|
|
|
|
document.onkeydown = this.handleKeyDown;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pointer events
|
|
|
|
|
*/
|
|
|
|
|
if (
|
|
|
|
|
this.utilities.pointerEventsSupported() &&
|
|
|
|
|
!useTouchEvents &&
|
|
|
|
|
!useMouseEvents
|
|
|
|
|
) {
|
|
|
|
|
document.onpointerdown = this.handlePointerDown;
|
|
|
|
|
document.onpointerup = this.handlePointerUp;
|
|
|
|
|
document.onpointercancel = this.handlePointerUp;
|
|
|
|
|
|
|
|
|
|
this.keyboardDOM.onpointerdown = this.handleKeyboardContainerMouseDown;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Touch events
|
|
|
|
|
*/
|
|
|
|
|
} else if (useTouchEvents) {
|
|
|
|
|
document.ontouchstart = this.handlePointerDown;
|
|
|
|
|
document.ontouchend = this.handlePointerUp;
|
|
|
|
|
document.ontouchcancel = this.handlePointerUp;
|
|
|
|
|
|
|
|
|
|
this.keyboardDOM.ontouchstart = this.handleKeyboardContainerMouseDown;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mouse events
|
|
|
|
|
*/
|
|
|
|
|
} else if (!useTouchEvents) {
|
|
|
|
|
document.onmousedown = this.handlePointerDown;
|
|
|
|
|
document.onmouseup = this.handlePointerUp;
|
|
|
|
|
|
|
|
|
|
this.keyboardDOM.onmousedown = this.handleKeyboardContainerMouseDown;
|
|
|
|
|
}
|
2019-02-15 21:49:27 -05:00
|
|
|
|
}
|
2018-11-24 21:12:19 -05:00
|
|
|
|
}
|
2018-10-06 02:22:02 -04:00
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
2019-07-14 11:27:18 -04:00
|
|
|
|
* Event Handler: KeyUp
|
|
|
|
|
*/
|
|
|
|
|
handleKeyUp(event) {
|
|
|
|
|
this.caretEventHandler(event);
|
|
|
|
|
|
|
|
|
|
if (this.options.physicalKeyboardHighlight) {
|
|
|
|
|
this.physicalKeyboard.handleHighlightKeyUp(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Event Handler: KeyDown
|
|
|
|
|
*/
|
|
|
|
|
handleKeyDown(event) {
|
|
|
|
|
if (this.options.physicalKeyboardHighlight) {
|
|
|
|
|
this.physicalKeyboard.handleHighlightKeyDown(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-08-07 17:52:12 -04:00
|
|
|
|
* Event Handler: PointerDown
|
2019-07-14 11:27:18 -04:00
|
|
|
|
*/
|
2020-08-07 17:52:12 -04:00
|
|
|
|
handlePointerDown(event) {
|
2019-07-14 11:27:18 -04:00
|
|
|
|
this.caretEventHandler(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-08-07 17:52:12 -04:00
|
|
|
|
* Event Handler: PointerUp
|
2019-07-14 11:27:18 -04:00
|
|
|
|
*/
|
2020-08-07 17:52:12 -04:00
|
|
|
|
handlePointerUp(event) {
|
|
|
|
|
this.handleButtonMouseUp();
|
2019-07-14 11:27:18 -04:00
|
|
|
|
this.caretEventHandler(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-14 11:55:03 -04:00
|
|
|
|
* Called by {@link setEventListeners} when an event that warrants a cursor position update is triggered
|
2018-10-24 18:18:24 -04:00
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
caretEventHandler(event) {
|
2020-08-07 17:52:12 -04:00
|
|
|
|
if (this.options.disableCaretPositioning) {
|
|
|
|
|
this.setCaretPosition(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 11:06:46 -05:00
|
|
|
|
let targetTagName;
|
2020-08-07 17:52:12 -04:00
|
|
|
|
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (event.target.tagName) {
|
2018-11-09 11:06:46 -05:00
|
|
|
|
targetTagName = event.target.tagName.toLowerCase();
|
|
|
|
|
}
|
2018-10-06 02:22:02 -04:00
|
|
|
|
|
2020-08-07 17:52:12 -04:00
|
|
|
|
/* istanbul ignore next */
|
2018-11-24 21:12:19 -05:00
|
|
|
|
this.dispatch(instance => {
|
2020-08-07 17:52:12 -04:00
|
|
|
|
const isKeyboard =
|
|
|
|
|
event.target === instance.keyboardDOM ||
|
|
|
|
|
(event.target && instance.keyboardDOM.contains(event.target));
|
2018-11-24 21:12:19 -05:00
|
|
|
|
|
2020-08-07 17:52:12 -04:00
|
|
|
|
// if (!this.isMouseHold) {
|
|
|
|
|
// instance.isMouseHold = false;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if (targetTagName === "textarea" || targetTagName === "input") {
|
2019-01-17 18:36:09 -05:00
|
|
|
|
/**
|
|
|
|
|
* Tracks current cursor position
|
|
|
|
|
* As keys are pressed, text will be added/removed at that position within the input.
|
|
|
|
|
*/
|
2020-08-07 17:52:12 -04:00
|
|
|
|
instance.setCaretPosition(
|
|
|
|
|
event.target.selectionStart,
|
|
|
|
|
event.target.selectionEnd
|
|
|
|
|
);
|
2019-02-15 21:49:27 -05:00
|
|
|
|
|
|
|
|
|
if (instance.options.debug) {
|
|
|
|
|
console.log(
|
|
|
|
|
"Caret at: ",
|
2020-08-07 17:52:12 -04:00
|
|
|
|
instance.getCaretPosition(),
|
|
|
|
|
instance.getCaretPositionEnd(),
|
|
|
|
|
event && event.target.tagName.toLowerCase(),
|
2019-02-15 21:49:27 -05:00
|
|
|
|
`(${instance.keyboardDOMClass})`
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-08-26 00:54:56 -04:00
|
|
|
|
|
|
|
|
|
// TODO: Review side-effects
|
2020-08-07 17:52:12 -04:00
|
|
|
|
} else if (!isKeyboard) {
|
|
|
|
|
instance.setCaretPosition(null);
|
2019-01-17 18:36:09 -05:00
|
|
|
|
}
|
2018-11-24 21:12:19 -05:00
|
|
|
|
});
|
2018-10-06 02:22:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 11:25:18 -05:00
|
|
|
|
/**
|
|
|
|
|
* Execute an operation on each button
|
|
|
|
|
*/
|
|
|
|
|
recurseButtons(fn) {
|
|
|
|
|
if (!fn) return false;
|
|
|
|
|
|
|
|
|
|
Object.keys(this.buttonElements).forEach(buttonName =>
|
|
|
|
|
this.buttonElements[buttonName].forEach(fn)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-02 02:56:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Destroy keyboard listeners and DOM elements
|
|
|
|
|
*/
|
|
|
|
|
destroy() {
|
2019-11-07 11:25:18 -05:00
|
|
|
|
if (this.options.debug)
|
|
|
|
|
console.log(
|
|
|
|
|
`Destroying simple-keyboard instance: ${this.currentInstanceName}`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove buttons
|
|
|
|
|
*/
|
|
|
|
|
let deleteButton = buttonElement => {
|
|
|
|
|
buttonElement.onpointerdown = null;
|
|
|
|
|
buttonElement.onpointerup = null;
|
|
|
|
|
buttonElement.onpointercancel = null;
|
|
|
|
|
buttonElement.ontouchstart = null;
|
|
|
|
|
buttonElement.ontouchend = null;
|
|
|
|
|
buttonElement.ontouchcancel = null;
|
|
|
|
|
buttonElement.onclick = null;
|
|
|
|
|
buttonElement.onmousedown = null;
|
|
|
|
|
buttonElement.onmouseup = null;
|
|
|
|
|
|
|
|
|
|
buttonElement.remove();
|
|
|
|
|
buttonElement = null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.recurseButtons(deleteButton);
|
|
|
|
|
deleteButton = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove wrapper events
|
|
|
|
|
*/
|
|
|
|
|
this.keyboardDOM.onpointerdown = null;
|
|
|
|
|
this.keyboardDOM.ontouchstart = null;
|
|
|
|
|
this.keyboardDOM.onmousedown = null;
|
|
|
|
|
|
2019-11-13 06:59:24 -05:00
|
|
|
|
/**
|
|
|
|
|
* Clearing keyboard wrapper
|
|
|
|
|
*/
|
|
|
|
|
this.clear();
|
2019-06-02 02:56:24 -04:00
|
|
|
|
|
2020-08-07 17:52:12 -04:00
|
|
|
|
/**
|
|
|
|
|
* Remove timouts
|
|
|
|
|
*/
|
|
|
|
|
/* istanbul ignore next */
|
|
|
|
|
if (this.holdInteractionTimeout) clearTimeout(this.holdInteractionTimeout);
|
|
|
|
|
/* istanbul ignore next */
|
|
|
|
|
if (this.holdTimeout) clearTimeout(this.holdTimeout);
|
|
|
|
|
|
2019-06-02 02:56:24 -04:00
|
|
|
|
/**
|
2019-11-07 11:25:18 -05:00
|
|
|
|
* Remove instance
|
2019-06-02 02:56:24 -04:00
|
|
|
|
*/
|
2019-11-07 11:25:18 -05:00
|
|
|
|
window["SimpleKeyboardInstances"][this.currentInstanceName] = null;
|
|
|
|
|
delete window["SimpleKeyboardInstances"][this.currentInstanceName];
|
2019-11-13 07:14:49 -05:00
|
|
|
|
|
2020-08-07 17:52:12 -04:00
|
|
|
|
/**
|
|
|
|
|
* Removing document listeners if there are no more instances
|
|
|
|
|
*/
|
|
|
|
|
if (!Object.keys(window["SimpleKeyboardInstances"]).length) {
|
|
|
|
|
/**
|
|
|
|
|
* Remove document listeners
|
|
|
|
|
*/
|
|
|
|
|
document.onkeydown = null;
|
|
|
|
|
document.onkeyup = null;
|
|
|
|
|
|
|
|
|
|
document.onpointerdown = null;
|
|
|
|
|
document.onpointerup = null;
|
|
|
|
|
|
|
|
|
|
document.onmousedown = null;
|
|
|
|
|
document.onmouseup = null;
|
|
|
|
|
|
|
|
|
|
document.ontouchstart = null;
|
|
|
|
|
document.ontouchend = null;
|
|
|
|
|
document.ontouchcancel = null;
|
|
|
|
|
|
|
|
|
|
if (this.options.debug) {
|
|
|
|
|
console.log(
|
|
|
|
|
"Destroy: No instances remaining. Document listeners removed",
|
|
|
|
|
window["SimpleKeyboardInstances"]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-08-07 18:09:06 -04:00
|
|
|
|
if (this.options.debug) {
|
|
|
|
|
console.log(
|
|
|
|
|
"Destroy: Instances remaining! Document listeners not removed",
|
|
|
|
|
window["SimpleKeyboardInstances"]
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-08-07 17:52:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 07:14:49 -05:00
|
|
|
|
/**
|
|
|
|
|
* Reset initialized flag
|
|
|
|
|
*/
|
|
|
|
|
this.initialized = false;
|
2019-06-02 02:56:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 19:29:29 -05:00
|
|
|
|
/**
|
|
|
|
|
* Process buttonTheme option
|
|
|
|
|
*/
|
2019-08-11 20:21:21 -04:00
|
|
|
|
getButtonThemeClasses(button) {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const buttonTheme = this.options.buttonTheme;
|
2019-08-11 20:21:21 -04:00
|
|
|
|
let buttonClasses = [];
|
2019-03-06 19:37:49 -05:00
|
|
|
|
|
2019-08-11 20:21:21 -04:00
|
|
|
|
if (Array.isArray(buttonTheme)) {
|
|
|
|
|
buttonTheme.forEach(themeObj => {
|
|
|
|
|
if (
|
|
|
|
|
themeObj.class &&
|
|
|
|
|
typeof themeObj.class === "string" &&
|
2019-11-12 10:24:58 +00:00
|
|
|
|
themeObj.buttons &&
|
|
|
|
|
typeof themeObj.buttons === "string"
|
2019-08-11 20:21:21 -04:00
|
|
|
|
) {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const themeObjClasses = themeObj.class.split(" ");
|
|
|
|
|
const themeObjButtons = themeObj.buttons.split(" ");
|
2019-03-06 19:29:29 -05:00
|
|
|
|
|
2019-08-11 20:21:21 -04:00
|
|
|
|
if (themeObjButtons.includes(button)) {
|
|
|
|
|
buttonClasses = [...buttonClasses, ...themeObjClasses];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Incorrect "buttonTheme". Please check the documentation.`,
|
|
|
|
|
themeObj
|
|
|
|
|
);
|
2019-03-06 19:29:29 -05:00
|
|
|
|
}
|
2019-08-11 20:21:21 -04:00
|
|
|
|
});
|
|
|
|
|
}
|
2019-03-06 19:29:29 -05:00
|
|
|
|
|
2019-08-11 20:21:21 -04:00
|
|
|
|
return buttonClasses;
|
|
|
|
|
}
|
2019-03-06 19:29:29 -05:00
|
|
|
|
|
2019-08-11 20:21:21 -04:00
|
|
|
|
/**
|
|
|
|
|
* Process buttonAttributes option
|
|
|
|
|
*/
|
|
|
|
|
setDOMButtonAttributes(button, callback) {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const buttonAttributes = this.options.buttonAttributes;
|
2019-08-11 20:21:21 -04:00
|
|
|
|
|
|
|
|
|
if (Array.isArray(buttonAttributes)) {
|
|
|
|
|
buttonAttributes.forEach(attrObj => {
|
|
|
|
|
if (
|
|
|
|
|
attrObj.attribute &&
|
|
|
|
|
typeof attrObj.attribute === "string" &&
|
2019-11-12 10:24:58 +00:00
|
|
|
|
attrObj.value &&
|
|
|
|
|
typeof attrObj.value === "string" &&
|
|
|
|
|
attrObj.buttons &&
|
|
|
|
|
typeof attrObj.buttons === "string"
|
2019-08-11 20:21:21 -04:00
|
|
|
|
) {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const attrObjButtons = attrObj.buttons.split(" ");
|
2019-08-11 20:21:21 -04:00
|
|
|
|
|
|
|
|
|
if (attrObjButtons.includes(button)) {
|
|
|
|
|
callback(attrObj.attribute, attrObj.value);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Incorrect "buttonAttributes". Please check the documentation.`,
|
|
|
|
|
attrObj
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-03-06 19:29:29 -05:00
|
|
|
|
}
|
2019-03-06 19:28:12 -05:00
|
|
|
|
|
2019-03-06 19:37:49 -05:00
|
|
|
|
onTouchDeviceDetected() {
|
2019-03-06 19:32:03 -05:00
|
|
|
|
/**
|
|
|
|
|
* Processing autoTouchEvents
|
|
|
|
|
*/
|
|
|
|
|
this.processAutoTouchEvents();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disabling contextual window on touch devices
|
|
|
|
|
*/
|
|
|
|
|
this.disableContextualWindow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disabling contextual window for hg-button
|
|
|
|
|
*/
|
|
|
|
|
/* istanbul ignore next */
|
2019-03-06 19:37:49 -05:00
|
|
|
|
disableContextualWindow() {
|
|
|
|
|
window.oncontextmenu = event => {
|
|
|
|
|
if (event.target.classList.contains("hg-button")) {
|
2019-03-06 19:32:03 -05:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 19:28:12 -05:00
|
|
|
|
/**
|
|
|
|
|
* Process autoTouchEvents option
|
|
|
|
|
*/
|
2019-03-06 19:37:49 -05:00
|
|
|
|
processAutoTouchEvents() {
|
|
|
|
|
if (this.options.autoUseTouchEvents) {
|
2019-03-06 19:28:12 -05:00
|
|
|
|
this.options.useTouchEvents = true;
|
|
|
|
|
|
|
|
|
|
if (this.options.debug) {
|
2019-03-06 19:37:49 -05:00
|
|
|
|
console.log(
|
|
|
|
|
`autoUseTouchEvents: Touch device detected, useTouchEvents enabled.`
|
|
|
|
|
);
|
2019-03-06 19:28:12 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Executes the callback function once simple-keyboard is rendered for the first time (on initialization).
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
onInit() {
|
|
|
|
|
if (this.options.debug) {
|
|
|
|
|
console.log(`${this.keyboardDOMClass} Initialized`);
|
2018-10-06 02:23:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-14 11:27:18 -04:00
|
|
|
|
* setEventListeners
|
2018-10-06 02:23:38 -04:00
|
|
|
|
*/
|
2019-07-14 11:27:18 -04:00
|
|
|
|
this.setEventListeners();
|
2018-10-06 02:23:38 -04:00
|
|
|
|
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (typeof this.options.onInit === "function") this.options.onInit();
|
2018-10-06 02:23:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 19:33:28 -05:00
|
|
|
|
/**
|
|
|
|
|
* Executes the callback function before a simple-keyboard render.
|
|
|
|
|
*/
|
2019-03-06 19:37:49 -05:00
|
|
|
|
beforeFirstRender() {
|
2019-03-06 19:33:28 -05:00
|
|
|
|
/**
|
|
|
|
|
* Performing actions when touch device detected
|
|
|
|
|
*/
|
2019-03-06 19:37:49 -05:00
|
|
|
|
if (this.utilities.isTouchDevice()) {
|
2019-03-06 19:33:28 -05:00
|
|
|
|
this.onTouchDeviceDetected();
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 19:37:49 -05:00
|
|
|
|
if (typeof this.options.beforeFirstRender === "function")
|
|
|
|
|
this.options.beforeFirstRender();
|
2019-03-07 07:32:07 -05:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notify about PointerEvents usage
|
|
|
|
|
*/
|
|
|
|
|
if (
|
2019-07-14 11:27:18 -04:00
|
|
|
|
this.isFirstKeyboardInstance &&
|
2019-03-07 07:32:07 -05:00
|
|
|
|
this.utilities.pointerEventsSupported() &&
|
2019-03-07 21:26:22 -05:00
|
|
|
|
!this.options.useTouchEvents &&
|
|
|
|
|
!this.options.useMouseEvents
|
2019-03-07 07:32:07 -05:00
|
|
|
|
) {
|
|
|
|
|
if (this.options.debug) {
|
|
|
|
|
console.log("Using PointerEvents as it is supported by this browser");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notify about touch events usage
|
|
|
|
|
*/
|
|
|
|
|
if (this.options.useTouchEvents) {
|
|
|
|
|
if (this.options.debug) {
|
|
|
|
|
console.log(
|
|
|
|
|
"useTouchEvents has been enabled. Only touch events will be used."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-06 19:33:28 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Executes the callback function before a simple-keyboard render.
|
|
|
|
|
*/
|
2019-03-06 19:37:49 -05:00
|
|
|
|
beforeRender() {
|
|
|
|
|
if (typeof this.options.beforeRender === "function")
|
|
|
|
|
this.options.beforeRender();
|
2019-03-06 19:33:28 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Executes the callback function every time simple-keyboard is rendered (e.g: when you change layouts).
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
onRender() {
|
|
|
|
|
if (typeof this.options.onRender === "function") this.options.onRender();
|
2018-10-06 02:24:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-15 21:49:27 -05:00
|
|
|
|
/**
|
|
|
|
|
* Executes the callback function once all modules have been loaded
|
|
|
|
|
*/
|
|
|
|
|
onModulesLoaded() {
|
|
|
|
|
if (typeof this.options.onModulesLoaded === "function")
|
2020-09-02 11:09:45 -04:00
|
|
|
|
this.options.onModulesLoaded(this);
|
2018-11-01 00:16:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 23:28:23 -04:00
|
|
|
|
/**
|
|
|
|
|
* Register module
|
|
|
|
|
*/
|
|
|
|
|
registerModule = (name, initCallback) => {
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (!this.modules[name]) this.modules[name] = {};
|
2018-10-30 23:28:23 -04:00
|
|
|
|
|
|
|
|
|
initCallback(this.modules[name]);
|
2019-02-15 21:49:27 -05:00
|
|
|
|
};
|
2018-10-30 23:28:23 -04:00
|
|
|
|
|
2018-10-31 17:57:33 -04:00
|
|
|
|
/**
|
|
|
|
|
* Load modules
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
loadModules() {
|
|
|
|
|
if (Array.isArray(this.options.modules)) {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
this.options.modules.forEach(KeyboardModule => {
|
|
|
|
|
const keyboardModule = new KeyboardModule();
|
2018-10-31 17:57:33 -04:00
|
|
|
|
|
|
|
|
|
/* istanbul ignore next */
|
2020-02-06 00:30:41 -05:00
|
|
|
|
if (
|
|
|
|
|
keyboardModule.constructor.name &&
|
|
|
|
|
keyboardModule.constructor.name !== "Function"
|
|
|
|
|
) {
|
|
|
|
|
const classStr = `module-${this.utilities.camelCase(
|
|
|
|
|
keyboardModule.constructor.name
|
2019-02-15 21:49:27 -05:00
|
|
|
|
)}`;
|
|
|
|
|
this.keyboardPluginClasses =
|
|
|
|
|
this.keyboardPluginClasses + ` ${classStr}`;
|
2018-10-31 17:57:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 00:30:41 -05:00
|
|
|
|
keyboardModule.init(this);
|
2018-10-31 17:57:33 -04:00
|
|
|
|
});
|
2018-11-01 00:16:11 -04:00
|
|
|
|
|
2019-02-15 21:49:27 -05:00
|
|
|
|
this.keyboardPluginClasses =
|
|
|
|
|
this.keyboardPluginClasses + " modules-loaded";
|
2018-11-01 00:16:11 -04:00
|
|
|
|
|
|
|
|
|
this.render();
|
|
|
|
|
this.onModulesLoaded();
|
2018-10-31 17:57:33 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 23:28:23 -04:00
|
|
|
|
/**
|
|
|
|
|
* Get module prop
|
|
|
|
|
*/
|
2019-03-09 21:47:31 -05:00
|
|
|
|
getModuleProp(name, prop) {
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (!this.modules[name]) return false;
|
|
|
|
|
|
2018-10-30 23:28:23 -04:00
|
|
|
|
return this.modules[name][prop];
|
2019-03-09 21:47:31 -05:00
|
|
|
|
}
|
2018-10-30 23:28:23 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* getModulesList
|
|
|
|
|
*/
|
2019-03-09 21:47:31 -05:00
|
|
|
|
getModulesList() {
|
2018-10-30 23:28:23 -04:00
|
|
|
|
return Object.keys(this.modules);
|
2019-03-09 21:47:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse Row DOM containers
|
|
|
|
|
*/
|
|
|
|
|
parseRowDOMContainers(
|
|
|
|
|
rowDOM,
|
|
|
|
|
rowIndex,
|
|
|
|
|
containerStartIndexes,
|
|
|
|
|
containerEndIndexes
|
|
|
|
|
) {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const rowDOMArray = Array.from(rowDOM.children);
|
2019-03-09 21:47:31 -05:00
|
|
|
|
let removedElements = 0;
|
|
|
|
|
|
|
|
|
|
if (rowDOMArray.length) {
|
|
|
|
|
containerStartIndexes.forEach((startIndex, arrIndex) => {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const endIndex = containerEndIndexes[arrIndex];
|
2019-03-09 21:47:31 -05:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If there exists a respective end index
|
|
|
|
|
* if end index comes after start index
|
|
|
|
|
*/
|
|
|
|
|
if (!endIndex || !(endIndex > startIndex)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updated startIndex, endIndex
|
|
|
|
|
* This is since the removal of buttons to place a single button container
|
|
|
|
|
* results in a modified array size
|
|
|
|
|
*/
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const updated_startIndex = startIndex - removedElements;
|
|
|
|
|
const updated_endIndex = endIndex - removedElements;
|
2019-03-09 21:47:31 -05:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create button container
|
|
|
|
|
*/
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const containerDOM = document.createElement("div");
|
2019-03-09 21:47:31 -05:00
|
|
|
|
containerDOM.className += "hg-button-container";
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const containerUID = `${this.options.layoutName}-r${rowIndex}c${arrIndex}`;
|
2019-03-09 21:47:31 -05:00
|
|
|
|
containerDOM.setAttribute("data-skUID", containerUID);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Taking elements due to be inserted into container
|
|
|
|
|
*/
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const containedElements = rowDOMArray.splice(
|
2019-03-09 21:47:31 -05:00
|
|
|
|
updated_startIndex,
|
|
|
|
|
updated_endIndex - updated_startIndex + 1
|
|
|
|
|
);
|
|
|
|
|
removedElements = updated_endIndex - updated_startIndex;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Inserting elements to container
|
|
|
|
|
*/
|
|
|
|
|
containedElements.forEach(element => containerDOM.appendChild(element));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adding container at correct position within rowDOMArray
|
|
|
|
|
*/
|
|
|
|
|
rowDOMArray.splice(updated_startIndex, 0, containerDOM);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clearing old rowDOM children structure
|
|
|
|
|
*/
|
|
|
|
|
rowDOM.innerHTML = "";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Appending rowDOM new children list
|
|
|
|
|
*/
|
|
|
|
|
rowDOMArray.forEach(element => rowDOM.appendChild(element));
|
|
|
|
|
|
|
|
|
|
if (this.options.debug) {
|
|
|
|
|
console.log(
|
|
|
|
|
"rowDOMContainer",
|
|
|
|
|
containedElements,
|
|
|
|
|
updated_startIndex,
|
|
|
|
|
updated_endIndex,
|
|
|
|
|
removedElements + 1
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rowDOM;
|
|
|
|
|
}
|
2018-10-30 23:28:23 -04:00
|
|
|
|
|
2020-02-06 00:30:41 -05:00
|
|
|
|
/**
|
|
|
|
|
* getKeyboardClassString
|
|
|
|
|
*/
|
|
|
|
|
getKeyboardClassString = (...baseDOMClasses) => {
|
|
|
|
|
const keyboardClasses = [this.keyboardDOMClass, ...baseDOMClasses].filter(
|
|
|
|
|
DOMClass => !!DOMClass
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return keyboardClasses.join(" ");
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* Renders rows and buttons as per options
|
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
render() {
|
2018-04-20 16:34:02 -04:00
|
|
|
|
/**
|
|
|
|
|
* Clear keyboard
|
|
|
|
|
*/
|
|
|
|
|
this.clear();
|
|
|
|
|
|
2019-03-06 19:33:28 -05:00
|
|
|
|
/**
|
|
|
|
|
* Calling beforeFirstRender
|
|
|
|
|
*/
|
2019-03-06 19:37:49 -05:00
|
|
|
|
if (!this.initialized) {
|
2019-03-06 19:33:28 -05:00
|
|
|
|
this.beforeFirstRender();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calling beforeRender
|
|
|
|
|
*/
|
|
|
|
|
this.beforeRender();
|
|
|
|
|
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const layoutClass = `hg-layout-${this.options.layoutName}`;
|
|
|
|
|
const layout = this.options.layout || getDefaultLayout();
|
|
|
|
|
const useTouchEvents = this.options.useTouchEvents || false;
|
|
|
|
|
const useTouchEventsClass = useTouchEvents ? "hg-touch-events" : "";
|
|
|
|
|
const useMouseEvents = this.options.useMouseEvents || false;
|
|
|
|
|
const disableRowButtonContainers = this.options.disableRowButtonContainers;
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adding themeClass, layoutClass to keyboardDOM
|
|
|
|
|
*/
|
2020-02-06 00:30:41 -05:00
|
|
|
|
this.keyboardDOM.className = this.getKeyboardClassString(
|
|
|
|
|
this.options.theme,
|
|
|
|
|
layoutClass,
|
|
|
|
|
this.keyboardPluginClasses,
|
|
|
|
|
useTouchEventsClass
|
|
|
|
|
);
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Iterating through each row
|
|
|
|
|
*/
|
2018-10-06 02:25:04 -04:00
|
|
|
|
layout[this.options.layoutName].forEach((row, rIndex) => {
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const rowArray = row.split(" ");
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creating empty row
|
|
|
|
|
*/
|
2018-12-14 00:28:18 -05:00
|
|
|
|
let rowDOM = document.createElement("div");
|
2018-04-20 16:34:02 -04:00
|
|
|
|
rowDOM.className += "hg-row";
|
|
|
|
|
|
2019-03-09 21:47:31 -05:00
|
|
|
|
/**
|
|
|
|
|
* Tracking container indicators in rows
|
|
|
|
|
*/
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const containerStartIndexes = [];
|
|
|
|
|
const containerEndIndexes = [];
|
2019-03-09 21:47:31 -05:00
|
|
|
|
|
2018-04-20 16:34:02 -04:00
|
|
|
|
/**
|
|
|
|
|
* Iterating through each button in row
|
|
|
|
|
*/
|
2018-10-06 02:25:04 -04:00
|
|
|
|
rowArray.forEach((button, bIndex) => {
|
2019-03-09 21:47:31 -05:00
|
|
|
|
/**
|
|
|
|
|
* Check if button has a container indicator
|
|
|
|
|
*/
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const buttonHasContainerStart =
|
2019-03-09 21:47:31 -05:00
|
|
|
|
!disableRowButtonContainers &&
|
2020-05-09 15:36:05 -04:00
|
|
|
|
typeof button === "string" &&
|
|
|
|
|
button.length > 1 &&
|
|
|
|
|
button.indexOf("[") === 0;
|
|
|
|
|
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const buttonHasContainerEnd =
|
2019-03-09 21:47:31 -05:00
|
|
|
|
!disableRowButtonContainers &&
|
2020-05-09 15:36:05 -04:00
|
|
|
|
typeof button === "string" &&
|
|
|
|
|
button.length > 1 &&
|
|
|
|
|
button.indexOf("]") === button.length - 1;
|
2019-03-09 21:47:31 -05:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save container start index, if applicable
|
|
|
|
|
*/
|
|
|
|
|
if (buttonHasContainerStart) {
|
|
|
|
|
containerStartIndexes.push(bIndex);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removing indicator
|
|
|
|
|
*/
|
|
|
|
|
button = button.replace(/\[/g, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (buttonHasContainerEnd) {
|
|
|
|
|
containerEndIndexes.push(bIndex);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removing indicator
|
|
|
|
|
*/
|
|
|
|
|
button = button.replace(/\]/g, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Processing button options
|
|
|
|
|
*/
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const fctBtnClass = this.utilities.getButtonClass(button);
|
|
|
|
|
const buttonDisplayName = this.utilities.getButtonDisplayName(
|
2019-02-15 21:49:27 -05:00
|
|
|
|
button,
|
|
|
|
|
this.options.display,
|
|
|
|
|
this.options.mergeDisplay
|
|
|
|
|
);
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creating button
|
|
|
|
|
*/
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const buttonType = this.options.useButtonTag ? "button" : "div";
|
|
|
|
|
const buttonDOM = document.createElement(buttonType);
|
2019-08-11 20:21:21 -04:00
|
|
|
|
buttonDOM.className += `hg-button ${fctBtnClass}`;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adding buttonTheme
|
|
|
|
|
*/
|
|
|
|
|
buttonDOM.classList.add(...this.getButtonThemeClasses(button));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adding buttonAttributes
|
|
|
|
|
*/
|
|
|
|
|
this.setDOMButtonAttributes(button, (attribute, value) => {
|
|
|
|
|
buttonDOM.setAttribute(attribute, value);
|
|
|
|
|
});
|
2019-02-15 21:49:27 -05:00
|
|
|
|
|
2019-11-07 11:25:43 -05:00
|
|
|
|
this.activeButtonClass = "hg-activeButton";
|
2019-11-02 09:00:19 +01:00
|
|
|
|
|
2019-03-07 07:32:07 -05:00
|
|
|
|
/**
|
|
|
|
|
* Handle button click event
|
|
|
|
|
*/
|
|
|
|
|
/* istanbul ignore next */
|
2019-03-07 20:51:29 -05:00
|
|
|
|
if (
|
|
|
|
|
this.utilities.pointerEventsSupported() &&
|
|
|
|
|
!useTouchEvents &&
|
|
|
|
|
!useMouseEvents
|
|
|
|
|
) {
|
2019-03-07 07:32:07 -05:00
|
|
|
|
/**
|
2019-03-11 18:25:51 -04:00
|
|
|
|
* Handle PointerEvents
|
2019-03-07 07:32:07 -05:00
|
|
|
|
*/
|
|
|
|
|
buttonDOM.onpointerdown = e => {
|
2018-11-15 16:49:22 +01:00
|
|
|
|
this.handleButtonClicked(button);
|
|
|
|
|
this.handleButtonMouseDown(button, e);
|
2019-02-15 21:49:27 -05:00
|
|
|
|
};
|
2019-11-02 09:00:19 +01:00
|
|
|
|
buttonDOM.onpointerup = () => {
|
|
|
|
|
this.handleButtonMouseUp(button);
|
|
|
|
|
};
|
|
|
|
|
buttonDOM.onpointercancel = () => {
|
|
|
|
|
this.handleButtonMouseUp(button);
|
|
|
|
|
};
|
2019-03-07 07:32:07 -05:00
|
|
|
|
} else {
|
|
|
|
|
/**
|
|
|
|
|
* Fallback for browsers not supporting PointerEvents
|
|
|
|
|
*/
|
|
|
|
|
if (useTouchEvents) {
|
2019-03-11 18:25:51 -04:00
|
|
|
|
/**
|
|
|
|
|
* Handle touch events
|
|
|
|
|
*/
|
2019-03-07 07:32:07 -05:00
|
|
|
|
buttonDOM.ontouchstart = e => {
|
|
|
|
|
this.handleButtonClicked(button);
|
|
|
|
|
this.handleButtonMouseDown(button, e);
|
|
|
|
|
};
|
2019-11-02 09:00:19 +01:00
|
|
|
|
buttonDOM.ontouchend = () => {
|
|
|
|
|
this.handleButtonMouseUp(button);
|
|
|
|
|
};
|
|
|
|
|
buttonDOM.ontouchcancel = () => {
|
|
|
|
|
this.handleButtonMouseUp(button);
|
|
|
|
|
};
|
2019-03-07 07:32:07 -05:00
|
|
|
|
} else {
|
2019-03-11 18:25:51 -04:00
|
|
|
|
/**
|
|
|
|
|
* Handle mouse events
|
|
|
|
|
*/
|
2019-03-07 07:32:07 -05:00
|
|
|
|
buttonDOM.onclick = () => {
|
|
|
|
|
this.handleButtonClicked(button);
|
|
|
|
|
};
|
2019-11-02 09:00:19 +01:00
|
|
|
|
buttonDOM.onmousedown = e => {
|
|
|
|
|
this.handleButtonMouseDown(button, e);
|
|
|
|
|
};
|
|
|
|
|
buttonDOM.onmouseup = () => {
|
|
|
|
|
this.handleButtonMouseUp(button);
|
|
|
|
|
};
|
2019-03-07 07:32:07 -05:00
|
|
|
|
}
|
2018-11-03 01:39:41 -04:00
|
|
|
|
}
|
2019-02-15 21:49:27 -05:00
|
|
|
|
|
2018-10-06 02:25:04 -04:00
|
|
|
|
/**
|
|
|
|
|
* Adding identifier
|
|
|
|
|
*/
|
|
|
|
|
buttonDOM.setAttribute("data-skBtn", button);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adding unique id
|
|
|
|
|
* Since there's no limit on spawning same buttons, the unique id ensures you can style every button
|
|
|
|
|
*/
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const buttonUID = `${this.options.layoutName}-r${rIndex}b${bIndex}`;
|
2018-10-06 02:25:04 -04:00
|
|
|
|
buttonDOM.setAttribute("data-skBtnUID", buttonUID);
|
|
|
|
|
|
2018-04-20 16:34:02 -04:00
|
|
|
|
/**
|
|
|
|
|
* Adding button label to button
|
|
|
|
|
*/
|
2020-02-06 00:30:41 -05:00
|
|
|
|
const buttonSpanDOM = document.createElement("span");
|
2018-04-20 16:34:02 -04:00
|
|
|
|
buttonSpanDOM.innerHTML = buttonDisplayName;
|
|
|
|
|
buttonDOM.appendChild(buttonSpanDOM);
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-06 02:17:54 -04:00
|
|
|
|
* Adding to buttonElements
|
2018-04-20 16:34:02 -04:00
|
|
|
|
*/
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (!this.buttonElements[button]) this.buttonElements[button] = [];
|
2018-10-06 02:17:54 -04:00
|
|
|
|
|
|
|
|
|
this.buttonElements[button].push(buttonDOM);
|
2018-04-20 16:34:02 -04:00
|
|
|
|
|
|
|
|
|
/**
|
2018-10-06 02:25:04 -04:00
|
|
|
|
* Appending button to row
|
2018-04-20 16:34:02 -04:00
|
|
|
|
*/
|
2018-10-06 02:25:04 -04:00
|
|
|
|
rowDOM.appendChild(buttonDOM);
|
2018-04-20 16:34:02 -04:00
|
|
|
|
});
|
|
|
|
|
|
2019-03-09 21:47:31 -05:00
|
|
|
|
/**
|
|
|
|
|
* Parse containers in row
|
|
|
|
|
*/
|
|
|
|
|
rowDOM = this.parseRowDOMContainers(
|
|
|
|
|
rowDOM,
|
|
|
|
|
rIndex,
|
|
|
|
|
containerStartIndexes,
|
|
|
|
|
containerEndIndexes
|
|
|
|
|
);
|
|
|
|
|
|
2018-04-20 16:34:02 -04:00
|
|
|
|
/**
|
|
|
|
|
* Appending row to keyboard
|
|
|
|
|
*/
|
|
|
|
|
this.keyboardDOM.appendChild(rowDOM);
|
|
|
|
|
});
|
2018-10-06 02:24:04 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calling onRender
|
|
|
|
|
*/
|
|
|
|
|
this.onRender();
|
|
|
|
|
|
2019-02-15 21:49:27 -05:00
|
|
|
|
if (!this.initialized) {
|
2018-10-24 18:18:24 -04:00
|
|
|
|
/**
|
2019-03-06 19:33:28 -05:00
|
|
|
|
* Ensures that onInit and beforeFirstRender are only called once per instantiation
|
2018-10-24 18:18:24 -04:00
|
|
|
|
*/
|
2018-10-06 02:24:04 -04:00
|
|
|
|
this.initialized = true;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calling onInit
|
|
|
|
|
*/
|
|
|
|
|
this.onInit();
|
|
|
|
|
}
|
2018-04-20 16:34:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default SimpleKeyboard;
|