From 1a53fae5d606debbd9d7134f9230ef2f9ea7cceb Mon Sep 17 00:00:00 2001 From: Francisco Hodge Date: Wed, 6 Mar 2019 19:32:03 -0500 Subject: [PATCH] Added isTouchDevice and touch device helper methods --- src/lib/components/Keyboard.js | 31 ++++++++++++++++++++++++++++--- src/lib/services/Utilities.js | 6 ++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/lib/components/Keyboard.js b/src/lib/components/Keyboard.js index 1a077b65..84421f2e 100644 --- a/src/lib/components/Keyboard.js +++ b/src/lib/components/Keyboard.js @@ -646,6 +646,32 @@ class SimpleKeyboard { return buttonThemesParsed; } + onTouchDeviceDetected(){ + /** + * Processing autoTouchEvents + */ + this.processAutoTouchEvents(); + + /** + * Disabling contextual window on touch devices + */ + this.disableContextualWindow(); + } + + /** + * Disabling contextual window for hg-button + */ + /* istanbul ignore next */ + disableContextualWindow(){ + window.oncontextmenu = (event) => { + if(event.target.classList.contains("hg-button")){ + event.preventDefault(); + event.stopPropagation(); + return false; + } + }; + } + /** * Process autoTouchEvents option */ @@ -755,6 +781,7 @@ class SimpleKeyboard { let layoutClass = `hg-layout-${this.options.layoutName}`; let layout = this.options.layout || KeyboardLayout.getDefaultLayout(); let useTouchEvents = this.options.useTouchEvents || false; + let useTouchEventsClass = useTouchEvents ? "hg-touch-events" : ""; /** * Account for buttonTheme, if set @@ -764,9 +791,7 @@ class SimpleKeyboard { /** * Adding themeClass, layoutClass to keyboardDOM */ - this.keyboardDOM.className += ` ${this.options.theme} ${layoutClass} ${ - this.keyboardPluginClasses - }`; + this.keyboardDOM.className += ` ${this.options.theme} ${layoutClass} ${this.keyboardPluginClasses} ${useTouchEventsClass}`; /** * Iterating through each row diff --git a/src/lib/services/Utilities.js b/src/lib/services/Utilities.js index f1da3d29..52917023 100644 --- a/src/lib/services/Utilities.js +++ b/src/lib/services/Utilities.js @@ -365,6 +365,12 @@ class Utilities { return Boolean(this.maxLengthReached); } + /** + * Determines whether a touch device is being used + */ + isTouchDevice(){ + return 'ontouchstart' in window || navigator.maxTouchPoints; + } /** * Bind all methods in a given class