From 06e93c2489cb89e73cf84735689609d1e63bb1f5 Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Sat, 20 Aug 2022 12:23:57 +0000 Subject: [PATCH] [FIX] web: prevent wrong date format if picker used too soon on frontend If datetimepickers were used too soon after page loading, they would use the wrong format and UI options as the code in charge of initializing those options was not fully lazy loaded yet. With this patch, we prevent the tempusdominus lib to consider pickers that are in a body that is marked by the lazyloader during the lazy loading of JS files. We could potentially not add the lib attribute (data-toggle="datetimepicker") on those elements and let the business code initializing the pickers add it but that would not have been a stable fix and it may be better this way as this works generically. opw-2944720 closes odoo/odoo#98917 X-original-commit: 7e012340d3adcd64e829a5dca34173da24d649bc Signed-off-by: Romain Derie (rde) Signed-off-by: Quentin Smetz (qsm) --- addons/web/static/lib/tempusdominus/tempusdominus.js | 9 ++++++++- addons/web/static/src/legacy/js/public/lazyloader.js | 6 +++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/addons/web/static/lib/tempusdominus/tempusdominus.js b/addons/web/static/lib/tempusdominus/tempusdominus.js index 6a1d9544fec..bcda3437db2 100644 --- a/addons/web/static/lib/tempusdominus/tempusdominus.js +++ b/addons/web/static/lib/tempusdominus/tempusdominus.js @@ -45,7 +45,14 @@ var DateTimePicker = function ($, moment) { EVENT_KEY = '.' + DATA_KEY, DATA_API_KEY = '.data-api', Selector = { - DATA_TOGGLE: '[data-toggle="' + DATA_KEY + '"]' + // /!\ ODOO PATCH: ensure the datetimepickers can be toggled only after + // the Odoo lazy loader finished loading all lazy scripts. Another + // solution could have been to temporarily removing the data-toggle + // attributes during lazyloading but that would not have been stable as + // custom code could search for data-toggle elements while the lazy + // loading is being performed. Without this, clicking too soon on a + // datetimepicker would not use the right format and UI options. + DATA_TOGGLE: 'body:not(.o_lazy_js_waiting) [data-toggle="' + DATA_KEY + '"]' }, ClassName = { INPUT: NAME + '-input' diff --git a/addons/web/static/src/legacy/js/public/lazyloader.js b/addons/web/static/src/legacy/js/public/lazyloader.js index 8ca0948cde8..37b5cab17ad 100644 --- a/addons/web/static/src/legacy/js/public/lazyloader.js +++ b/addons/web/static/src/legacy/js/public/lazyloader.js @@ -10,7 +10,7 @@ var blockFunction = function (ev) { var waitingLazy = false; /** - * Blocks the DOM sections which explicitely require the lazy loaded JS to be + * Blocks the DOM sections which explicitly require the lazy loaded JS to be * working (those sections should be marked with the 'o_wait_lazy_js' class). * * @see stopWaitingLazy @@ -28,6 +28,8 @@ function waitLazy() { element.addEventListener(evType, blockFunction); }); } + + document.body.classList.add('o_lazy_js_waiting'); } /** * Unblocks the DOM sections blocked by @see waitLazy and removes the related @@ -47,6 +49,8 @@ function stopWaitingLazy() { }); element.classList.remove('o_wait_lazy_js'); } + + document.body.classList.remove('o_lazy_js_waiting'); } // Start waiting for lazy loading as soon as the DOM is available