From 5bdf2a4e452c12861fc494e38823257949d156c5 Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Fri, 10 Jun 2022 14:11:06 +0000 Subject: [PATCH] [IMP] mail, web: dont patch window in patchUiSIze MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Patching the window object is not robust and can lead to errors. In order to make patchUiSize more robust, patching of window have been replaced by a patch of the uiService. closes odoo/odoo#93355 X-original-commit: 46be08a551ead8d2e0304a753af3d3b13744e6ed Signed-off-by: Sébastien Theys (seb) --- .../mail/static/tests/helpers/patch_ui_size.js | 9 ++++----- addons/web/static/src/core/ui/ui_service.js | 18 +++++++++--------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/addons/mail/static/tests/helpers/patch_ui_size.js b/addons/mail/static/tests/helpers/patch_ui_size.js index bad1ba3741a..745f4da3c77 100644 --- a/addons/mail/static/tests/helpers/patch_ui_size.js +++ b/addons/mail/static/tests/helpers/patch_ui_size.js @@ -1,7 +1,7 @@ /** @odoo-module **/ import { browser } from '@web/core/browser/browser'; -import { getMediaQueryLists, MEDIAS_BREAKPOINTS, SIZES } from '@web/core/ui/ui_service'; +import { MEDIAS_BREAKPOINTS, SIZES, uiService } from '@web/core/ui/ui_service'; import { patchWithCleanup } from "@web/../tests/helpers/utils"; import config from 'web.config'; @@ -87,15 +87,14 @@ function patchUiSize({ height, size, width }) { } size = size === undefined ? getSizeFromWidth(width) : size; width = width || getWidthFromSize(size); - const MEDIAS = getMediaQueryLists(); patchWithCleanup(browser, { innerWidth: width, innerHeight: height || browser.innerHeight, }); - patchWithCleanup(window.MediaQueryList.prototype, { - get matches() { - return this.media === MEDIAS[size].media; + patchWithCleanup(uiService, { + getSize() { + return size; }, }); legacyPatchUiSize(height, size, width); diff --git a/addons/web/static/src/core/ui/ui_service.js b/addons/web/static/src/core/ui/ui_service.js index a50964e7be6..70e5245e707 100644 --- a/addons/web/static/src/core/ui/ui_service.js +++ b/addons/web/static/src/core/ui/ui_service.js @@ -67,7 +67,13 @@ export const MEDIAS_BREAKPOINTS = [ }); } +// window size handling. +const MEDIAS = getMediaQueryLists(); + export const uiService = { + getSize() { + return MEDIAS.findIndex(media => media.matches); + }, start(env) { let ui = {}; @@ -137,20 +143,14 @@ export const uiService = { }, }); - // window size handling - const MEDIAS = getMediaQueryLists(); - function getSize() { - return MEDIAS.findIndex((media) => media.matches); - } - // listen to media query status changes function updateSize() { - ui.size = getSize(); + ui.size = this.getSize(); } - browser.addEventListener("resize", debounce(updateSize, 100)); + browser.addEventListener("resize", debounce(updateSize.bind(this), 100)); Object.assign(ui, { - size: getSize(), + size: this.getSize(), }); Object.defineProperty(ui, "isSmall", { get() {