Skip to content

Commit

Permalink
Merge pull request #7599 from surveyjs/bug/7584-has-mouse-support
Browse files Browse the repository at this point in the history
Check for mouse support #7584
  • Loading branch information
andrewtelnov authored Dec 29, 2023
2 parents 36ccab8 + 3f5bf0d commit 1f597d5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
25 changes: 14 additions & 11 deletions src/utils/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ let _IPad = false;

export const IsMobile = _isMobile || _IPad;

export function getIsTouch() {
let _isTouch = false;

if (typeof window !== "undefined") {
_isTouch = "ontouchstart" in (<any>window) || navigator.maxTouchPoints > 0;
}

return _isTouch;
}

export let IsTouch = IsMobile && getIsTouch();
export var mouseInfo = {
get isTouch(): boolean {
return !this.hasMouse && this.hasTouchEvent;
},
get hasTouchEvent(): boolean {
return typeof window !== "undefined" && ("ontouchstart" in (<any>window) || navigator.maxTouchPoints > 0);
},
hasMouse: true
};

const pointerMatches = (!!matchMedia && matchMedia("(pointer:fine)")) || undefined;
mouseInfo.hasMouse = !!pointerMatches && !!pointerMatches.matches;

export let IsTouch = mouseInfo.isTouch;

//for tests
export function _setIsTouch(val: boolean): void {
Expand Down
26 changes: 19 additions & 7 deletions tests/utilstests.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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;
}
}
);

0 comments on commit 1f597d5

Please sign in to comment.