From 968be6be5530bc79d6f93b1ef7a02c6f46122ebe Mon Sep 17 00:00:00 2001 From: Pierre Paridans Date: Wed, 13 Oct 2021 14:44:23 +0000 Subject: [PATCH] [IMP] web: feature detection impl. normalization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit normalizes the different feature/browser detection modules' implementations to avoid difference between "newer" implementation and legacy ones. Its main goal is to ease future maintainability by having only one single source of truth for feature detection and, also ensure fixes applied to one version are applied everywhere. closes odoo/odoo#79499 X-original-commit: 3b90e15983edcb79e6bd6e45685926cd4dfaae6b Signed-off-by: Adrien Dieudonné (adr) Signed-off-by: Pierre Paridans (app) --- .../src/core/browser/feature_detection.js | 29 ++++++++++++------- .../src/legacy/js/core/browser_detection.js | 11 ++----- .../static/src/legacy/js/services/config.js | 14 ++++----- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/addons/web/static/src/core/browser/feature_detection.js b/addons/web/static/src/core/browser/feature_detection.js index b493ceeb204..1c32494640f 100644 --- a/addons/web/static/src/core/browser/feature_detection.js +++ b/addons/web/static/src/core/browser/feature_detection.js @@ -15,26 +15,35 @@ export function isBrowserChrome() { return browser.navigator.userAgent.includes("Chrome"); } +export function isAndroid() { + return /Android/i.test(browser.navigator.userAgent); +} + +export function isIOS() { + return /(iPad|iPhone|iPod)/i.test(browser.navigator.userAgent) || + (browser.navigator.platform === 'MacIntel' && maxTouchPoints() > 1); +} + +export function isOtherMobileOS() { + return /(webOS|BlackBerry|Windows Phone)/i.test(browser.navigator.userAgent); +} + export function isMacOS() { return Boolean(browser.navigator.userAgent.match(/Mac/i)); } export function isMobileOS() { - return Boolean( - browser.navigator.userAgent.match(/Android/i) || - browser.navigator.userAgent.match(/webOS/i) || - browser.navigator.userAgent.match(/iPhone/i) || - browser.navigator.userAgent.match(/iPad/i) || - browser.navigator.userAgent.match(/iPod/i) || - browser.navigator.userAgent.match(/BlackBerry/i) || - browser.navigator.userAgent.match(/Windows Phone/i) - ); + return isAndroid() || isIOS() || isOtherMobileOS(); } export function isIosApp() { - return navigator.userAgent.match(/OdooMobile \(iOS\)/i); + return /OdooMobile \(iOS\)/i.test(browser.navigator.userAgent); } export function hasTouch() { return "ontouchstart" in window || "onmsgesturechange" in window; } + +export function maxTouchPoints() { + return browser.navigator.maxTouchPoints || 1; +} diff --git a/addons/web/static/src/legacy/js/core/browser_detection.js b/addons/web/static/src/legacy/js/core/browser_detection.js index 37c6a35eea2..4b6605bfeda 100644 --- a/addons/web/static/src/legacy/js/core/browser_detection.js +++ b/addons/web/static/src/legacy/js/core/browser_detection.js @@ -1,19 +1,14 @@ odoo.define('web.BrowserDetection', function (require) { "use strict"; var Class = require('web.Class'); + const { isMacOS, isBrowserChrome } = require('@web/core/browser/feature_detection'); var BrowserDetection = Class.extend({ init: function () { }, - isOsMac: function () { - return navigator.platform.toLowerCase().indexOf('mac') !== -1; - }, - isBrowserChrome: function () { - return $.browser.chrome && // depends on jquery 1.x, removed in jquery 2 and above - navigator.userAgent.toLocaleLowerCase().indexOf('edge') === -1; // as far as jquery is concerned, Edge is chrome - } - + isOsMac: isMacOS, + isBrowserChrome, }); return BrowserDetection; }); diff --git a/addons/web/static/src/legacy/js/services/config.js b/addons/web/static/src/legacy/js/services/config.js index 4a428103de7..45f5b00aabc 100644 --- a/addons/web/static/src/legacy/js/services/config.js +++ b/addons/web/static/src/legacy/js/services/config.js @@ -2,6 +2,7 @@ odoo.define('web.config', function (require) { "use strict"; const Bus = require('web.Bus'); +const { hasTouch, isAndroid, isIOS, isMobileOS } = require('@web/core/browser/feature_detection'); const bus = new Bus(); @@ -14,11 +15,6 @@ const bus = new Bus(); * this file someday. */ -const maxTouchPoints = navigator.maxTouchPoints || 1; -const isAndroid = /Android/i.test(navigator.userAgent); -const isIOS = /(iPad|iPhone|iPod)/i.test(navigator.userAgent) || (navigator.platform === 'MacIntel' && maxTouchPoints > 1); -const isOtherMobileDevice = /(webOS|BlackBerry|Windows Phone)/i.test(navigator.userAgent); - var config = { device: { /** @@ -33,7 +29,7 @@ var config = { * * @type Boolean */ - touch: 'ontouchstart' in window || 'onmsgesturechange' in window, + touch: hasTouch(), /** * size_class is an integer: 0, 1, 2, 3 or 4, depending on the (current) * size of the device. This is a dynamic property, updated whenever the @@ -48,14 +44,14 @@ var config = { * * @return Boolean */ - isAndroid: isAndroid, + isAndroid: isAndroid(), /** * Mobile OS (iOS) device detection using userAgent. * This flag doesn't depend on the size/resolution of the screen. * * @return Boolean */ - isIOS: isIOS, + isIOS: isIOS(), /** * A frequent use case is to have a different render in 'mobile' mode, * meaning when the screen is small. This flag (boolean) is true when @@ -71,7 +67,7 @@ var config = { * * @return {boolean} */ - isMobileDevice: isAndroid || isIOS || isOtherMobileDevice, + isMobileDevice: isMobileOS(), /** * Mapping between the numbers 0,1,2,3,4,5,6 and some descriptions */