diff --git a/src/utils/devices.ts b/src/utils/devices.ts index 54e0434d86..864fa9ef19 100644 --- a/src/utils/devices.ts +++ b/src/utils/devices.ts @@ -30,20 +30,21 @@ let _IPad = false; export const IsMobile = _isMobile || _IPad; -export function getIsTouch() { - let _isTouch = false; - - if (typeof window !== "undefined") { - _isTouch = "ontouchstart" in (window) || navigator.maxTouchPoints > 0; - } - - return _isTouch; -} +export var mouseInfo = { + get isTouch(): boolean { + if(this.hasMouse) return false; + return this.hasTouchEvent; + }, + get hasTouchEvent(): boolean { + return typeof window !== "undefined" && "ontouchstart" in (window) || navigator.maxTouchPoints > 0; + }, + hasMouse: true +}; const pointerMatches = (!!matchMedia && matchMedia("(pointer:fine)")) || undefined; -const hasMouse = !!pointerMatches && !!pointerMatches.matches; +mouseInfo.hasMouse = !!pointerMatches && !!pointerMatches.matches; -export let IsTouch = !hasMouse && getIsTouch(); +export let IsTouch = mouseInfo.isTouch; //for tests export function _setIsTouch(val: boolean): void { diff --git a/tests/utilstests.ts b/tests/utilstests.ts index 8b90cf1ce5..b14e70859c 100644 --- a/tests/utilstests.ts +++ b/tests/utilstests.ts @@ -1,8 +1,7 @@ import { IAction } from "../src/actions/action"; import { defaultListCss } from "../src/list"; -import { Question } from "../src/question"; import { createSvg, doKey2ClickDown, doKey2ClickUp, sanitizeEditableContent } from "../src/utils/utils"; -import { getIsTouch } from "../src/utils/devices"; +import { mouseInfo } from "../src/utils/devices"; export default QUnit.module("utils"); function checkSanitizer(element, text, selectionNodeIndex, selectionStart) { @@ -157,11 +156,24 @@ QUnit.test( } ); -QUnit.skip( - "utils: devices: getIsTouch", +QUnit.test( + "utils: devices: isTouch", function (assert) { - assert.equal(getIsTouch(), false, "getIsTouch() return false for 'mouse' screens"); - window["ontouchstart"] = ()=>{}; - assert.equal(getIsTouch(), true, "getIsTouch() return true for 'touch' screens"); + mouseInfo.hasMouse = true; + assert.equal(mouseInfo.isTouch, false, "isTouch, #1"); + mouseInfo.hasMouse = false; + const hasTouchEvent = mouseInfo.hasTouchEvent; + assert.equal(mouseInfo.isTouch, hasTouchEvent, "isTouch, #2. hasTouch in window: " + hasTouchEvent); + if(!hasTouchEvent) { + window["ontouchstart"] = ()=>{}; + } + mouseInfo.hasMouse = true; + assert.equal(mouseInfo.isTouch, false, "isTouch, #3"); + mouseInfo.hasMouse = false; + assert.equal(mouseInfo.isTouch, true, "isTouch, #4"); + mouseInfo.hasMouse = true; + if(!hasTouchEvent) { + window["ontouchstart"] = undefined; + } } ); \ No newline at end of file