From 22591206d4c585e51075f5d34a4833d499bb9a52 Mon Sep 17 00:00:00 2001 From: Pierre Paridans Date: Tue, 12 Jul 2022 15:40:03 +0000 Subject: [PATCH] [REF] web: touch detection - use 'browser' as abstraction, allowing to mock touch detection in QUnit tests - remove deprecated IE 10 touch detection (using non-standard MSGestureChange event) - some linting Note: touch detection using "ontouchstart" is a quite common practice. It's useful to note that, when in touch mode, browser's have different values for it ("null" in Chrome, "Function" in Firefox...) but all agrees to have "window.ontouchstart" being "undefined" when in non-touch mode. Part-of: odoo/odoo#95924 --- addons/web/static/src/core/browser/browser.js | 9 +++------ addons/web/static/src/core/browser/feature_detection.js | 8 +++++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/addons/web/static/src/core/browser/browser.js b/addons/web/static/src/core/browser/browser.js index a43a27da704..f7baaaea5b0 100644 --- a/addons/web/static/src/core/browser/browser.js +++ b/addons/web/static/src/core/browser/browser.js @@ -40,6 +40,7 @@ export const browser = { fetch: window.fetch.bind(window), innerHeight: window.innerHeight, innerWidth: window.innerWidth, + ontouchstart: window.ontouchstart, }; Object.defineProperty(browser, "location", { @@ -73,9 +74,7 @@ export function makeRAMLocalStorage() { return { setItem(key, value) { store[key] = value; - window.dispatchEvent( - new StorageEvent('storage', { key, newValue: value }) - ); + window.dispatchEvent(new StorageEvent("storage", { key, newValue: value })); }, getItem(key) { return store[key]; @@ -85,9 +84,7 @@ export function makeRAMLocalStorage() { }, removeItem(key) { delete store[key]; - window.dispatchEvent( - new StorageEvent('storage', { key, newValue: null }) - ); + window.dispatchEvent(new StorageEvent("storage", { key, newValue: null })); }, get length() { return Object.keys(store).length; diff --git a/addons/web/static/src/core/browser/feature_detection.js b/addons/web/static/src/core/browser/feature_detection.js index 1c32494640f..3b243f098cd 100644 --- a/addons/web/static/src/core/browser/feature_detection.js +++ b/addons/web/static/src/core/browser/feature_detection.js @@ -20,8 +20,10 @@ export function isAndroid() { } export function isIOS() { - return /(iPad|iPhone|iPod)/i.test(browser.navigator.userAgent) || - (browser.navigator.platform === 'MacIntel' && maxTouchPoints() > 1); + return ( + /(iPad|iPhone|iPod)/i.test(browser.navigator.userAgent) || + (browser.navigator.platform === "MacIntel" && maxTouchPoints() > 1) + ); } export function isOtherMobileOS() { @@ -41,7 +43,7 @@ export function isIosApp() { } export function hasTouch() { - return "ontouchstart" in window || "onmsgesturechange" in window; + return browser.ontouchstart !== undefined; } export function maxTouchPoints() {