From e0efbb08a4f51e2210f0412504e1fbd139540a42 Mon Sep 17 00:00:00 2001 From: Julien Mougenot Date: Mon, 30 Nov 2020 15:30:51 +0100 Subject: [PATCH] [IMP] web_tour: add tour 'disabled' option This commit changes the way tours are automatically disabled when in demo data. Previously, an override of the assets_common template was applied by a file loaded in the web_tour demo data, ensuring that the tours are disabled when demo data are loaded in the database. Now, a "tour_disable" boolean will be given to the session_info object to determine whether the tour manager must be disabled, meaning that it will automatically consume all registered tours. This has been done to improve consistency in asset bundles declarations by reducing them to the simplest possible templates. Part of task: 2352566 Co-authored-by: Julien Mougenot Co-authored-by: Simon Genin --- addons/web_tour/data/web_tour_demo.xml | 11 ------ addons/web_tour/models/ir_http.py | 4 +- addons/web_tour/static/src/js/tour_disable.js | 25 ------------ addons/web_tour/static/src/js/tour_manager.js | 11 ++++-- addons/web_tour/static/src/js/tour_service.js | 4 +- .../static/tests/tour_manager_tests.js | 38 ++++++++++++------- 6 files changed, 39 insertions(+), 54 deletions(-) delete mode 100644 addons/web_tour/data/web_tour_demo.xml delete mode 100644 addons/web_tour/static/src/js/tour_disable.js diff --git a/addons/web_tour/data/web_tour_demo.xml b/addons/web_tour/data/web_tour_demo.xml deleted file mode 100644 index d3a4e5349ae..00000000000 --- a/addons/web_tour/data/web_tour_demo.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - diff --git a/addons/web_tour/models/ir_http.py b/addons/web_tour/models/ir_http.py index abcfceb484e..1e86babb6ad 100644 --- a/addons/web_tour/models/ir_http.py +++ b/addons/web_tour/models/ir_http.py @@ -9,7 +9,9 @@ class Http(models.AbstractModel): _inherit = 'ir.http' def session_info(self): - result = super(Http, self).session_info() + result = super().session_info() if result['is_admin']: + demo_modules_count = self.env['ir.module.module'].search_count([('demo', '=', True)]) result['web_tours'] = request.env['web_tour.tour'].get_consumed_tours() + result['tour_disable'] = demo_modules_count > 0 return result diff --git a/addons/web_tour/static/src/js/tour_disable.js b/addons/web_tour/static/src/js/tour_disable.js deleted file mode 100644 index fad7a14dcf6..00000000000 --- a/addons/web_tour/static/src/js/tour_disable.js +++ /dev/null @@ -1,25 +0,0 @@ -odoo.define('web_tour.DisableTour', function (require) { -"use strict"; - -var local_storage = require('web.local_storage'); -var TourManager = require('web_tour.TourManager'); -var utils = require('web_tour.utils'); - -var get_debugging_key = utils.get_debugging_key; - -TourManager.include({ - /** - * Disables tours if Odoo installed with demo data. - * - * @override - */ - _register: function (do_update, tour, name) { - // Consuming tours which are not run by test case nor currently being debugged - if (!this.running_tour && !local_storage.getItem(get_debugging_key(name))) { - this.consumed_tours.push(name); - } - return this._super.apply(this, arguments); - }, -}); - -}); diff --git a/addons/web_tour/static/src/js/tour_manager.js b/addons/web_tour/static/src/js/tour_manager.js index 1f1207c6289..97fa343b583 100644 --- a/addons/web_tour/static/src/js/tour_manager.js +++ b/addons/web_tour/static/src/js/tour_manager.js @@ -26,7 +26,7 @@ var do_before_unload = utils.do_before_unload; var get_jquery_element_from_selector = utils.get_jquery_element_from_selector; return core.Class.extend(mixins.EventDispatcherMixin, ServicesMixin, { - init: function(parent, consumed_tours) { + init: function(parent, consumed_tours, disabled = false) { mixins.EventDispatcherMixin.init.call(this); this.setParent(parent); @@ -37,6 +37,7 @@ return core.Class.extend(mixins.EventDispatcherMixin, ServicesMixin, { this.consumed_tours = (consumed_tours || []).filter(tourName => { return !local_storage.getItem(get_debugging_key(tourName)); }); + this.disabled = disabled; this.running_tour = local_storage.getItem(get_running_key()); this.running_step_delay = parseInt(local_storage.getItem(get_running_delay_key()), 10) || 0; this.edition = (_.last(session.server_version_info) === 'e') ? 'enterprise' : 'community'; @@ -128,6 +129,11 @@ return core.Class.extend(mixins.EventDispatcherMixin, ServicesMixin, { }); }, _register: function (do_update, tour, name) { + const debuggingTour = local_storage.getItem(get_debugging_key(name)); + if (this.disabled && !this.running_tour && !debuggingTour) { + this.consumed_tours.push(name); + } + if (tour.ready) return Promise.resolve(); const tour_is_consumed = this._isTourConsumed(name); @@ -146,7 +152,6 @@ return core.Class.extend(mixins.EventDispatcherMixin, ServicesMixin, { tour.ready = true; - const debuggingTour = local_storage.getItem(get_debugging_key(name)); if (debuggingTour || (do_update && (this.running_tour === name || (!this.running_tour && !tour.test && !tour_is_consumed)))) { @@ -156,7 +161,7 @@ return core.Class.extend(mixins.EventDispatcherMixin, ServicesMixin, { }, /** * Resets the given tour to its initial step, and prevent it from being - * marked as consumed at reload, by the include in tour_disable.js + * marked as consumed at reload. * * @param {string} tourName */ diff --git a/addons/web_tour/static/src/js/tour_service.js b/addons/web_tour/static/src/js/tour_service.js index a055c987396..51f895a13e7 100644 --- a/addons/web_tour/static/src/js/tour_service.js +++ b/addons/web_tour/static/src/js/tour_service.js @@ -5,6 +5,7 @@ var rootWidget = require('root.widget'); var rpc = require('web.rpc'); var session = require('web.session'); var TourManager = require('web_tour.TourManager'); +const { device } = require('web.config'); const untrackedClassnames = ["o_tooltip", "o_tooltip_content", "o_tooltip_overlay"]; @@ -32,7 +33,8 @@ return session.is_bound.then(function () { } return Promise.all(defs).then(function (results) { var consumed_tours = session.is_frontend ? results[0] : session.web_tours; - var tour_manager = new TourManager(rootWidget, consumed_tours); + const disabled = session.tour_disable || device.isMobile; + var tour_manager = new TourManager(rootWidget, consumed_tours, disabled); function _isTrackedNode(node) { if (node.classList) { diff --git a/addons/web_tour/static/tests/tour_manager_tests.js b/addons/web_tour/static/tests/tour_manager_tests.js index aa2dff5fcf8..26ebaeb398c 100644 --- a/addons/web_tour/static/tests/tour_manager_tests.js +++ b/addons/web_tour/static/tests/tour_manager_tests.js @@ -18,12 +18,13 @@ odoo.define('web_tour.tour_manager_tests', async function (require) { * @param {Object} params * @param {string[]} [params.consumed_tours] * @param {boolean} [params.debug] + * @param {boolean} [params.disabled] * @param {string} params.template inner HTML content of the widget * @param {Object[]} params.tours { {string} name, {Object} option, {Object[]} steps } */ - async function createTourManager({ consumed_tours, debug, template, tours }) { + async function createTourManager({ consumed_tours, debug, disabled, template, tours }) { const parent = await testUtils.createParent({ debug }); - const tourManager = new TourManager(parent, consumed_tours); + const tourManager = new TourManager(parent, consumed_tours, disabled); tourManager.running_step_delay = 0; for (const { name, options, steps } of tours) { tourManager.register(name, options, steps); @@ -35,23 +36,13 @@ odoo.define('web_tour.tour_manager_tests', async function (require) { }; await parent.prependTo(testUtils.prepareTarget(debug)); parent.el.innerHTML = template; - testUtils.mock.patch(TourManager, { - // Since the `tour_disable.js` script automatically sets tours as consumed - // as soon as they are registered, we override the "is consumed" to - // assert that the tour is in the `consumed_tours` param key. - _isTourConsumed: name => (consumed_tours || []).includes(name), - }); await tourManager._register_all(true); // Wait for possible tooltips to be loaded and appended. await testUtils.nextTick(); return tourManager; } - QUnit.module("Tours", { - afterEach() { - testUtils.mock.unpatch(TourManager); - }, - }, function () { + QUnit.module("Tours", function () { QUnit.module("Tour manager"); @@ -201,5 +192,26 @@ odoo.define('web_tour.tour_manager_tests', async function (require) { kanban.destroy(); tourManager.destroy(); }); + + QUnit.test("Automatic tour disabling", async function (assert) { + assert.expect(2); + + const options = { + template: ``, + tours: [{ name: "Tour", options: {}, steps: [{ trigger: '.anchor' }] }], + }; + + const enabledTM = await createTourManager({ disabled: false, ...options }); + + assert.containsOnce(document.body, '.o_tooltip:visible'); + + enabledTM.destroy(); + + const disabledTM = await createTourManager({ disabled: true, ...options }); + + assert.containsNone(document.body, '.o_tooltip:visible'); + + disabledTM.destroy(); + }); }); });