Added destroy method

This commit is contained in:
Francisco Hodge
2019-06-02 02:56:24 -04:00
parent ba20077e7a
commit 8795232092
5 changed files with 63 additions and 49 deletions
+5
View File
@@ -220,6 +220,11 @@ declare module 'simple-keyboard' {
* @param {string} button The button layout name to select
*/
getButtonElement(button: string): HTMLElement | HTMLElement[];
/**
* Clears keyboard listeners and DOM elements.
*/
destroy(): void;
}
export default Keyboard;
+18
View File
@@ -64,6 +64,7 @@ class SimpleKeyboard {
* @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.
* @property {boolean} useMouseEvents Opt out of PointerEvents handling, falling back to the prior mouse event logic.
* @property {function} destroy Clears keyboard listeners and DOM elements.
*/
this.options = options;
this.options.layoutName = this.options.layoutName || "default";
@@ -646,6 +647,23 @@ class SimpleKeyboard {
});
}
/**
* Destroy keyboard listeners and DOM elements
*/
destroy() {
/**
* Remove listeners
*/
document.removeEventListener("keyup", this.caretEventHandler);
document.removeEventListener("mouseup", this.caretEventHandler);
document.removeEventListener("touchend", this.caretEventHandler);
/**
* Clear DOM
*/
this.clear();
}
/**
* Process buttonTheme option
*/
+10
View File
@@ -1222,4 +1222,14 @@ it('Keyboard inputName change will trigget caretPosition reset', () => {
keyboard.getButtonElement("b").onpointerdown();
expect(keyboard.caretPosition).toBe(null);
});
it('Keyboard destroy will work', () => {
testUtil.setDOM();
let keyboard = new Keyboard();
keyboard.destroy();
expect(keyboard.keyboardDOM.innerHTML).toBeFalsy();
});