From 933949085d274d766c9f23094bb41fe8ebf7cf2f Mon Sep 17 00:00:00 2001
From: qsm-odoo <qsm@odoo.com>
Date: Wed, 24 Aug 2016 12:26:38 +0200
Subject: [PATCH] [FIX] web_tour,web_editor: allow running tour in every
 context

Previous implementation allowed to run tour as a non-superuser but
this was only working in the backend where the static file tip.xml
is always loaded. In the frontend or in other non-backend context,
this file has to be loaded just before trying to launch the tour as
a non superuser (indeed, previous implementation loaded the file in
the frontend only if the user is a superuser).
---
 addons/web_editor/views/iframe.xml            |  7 +++
 addons/web_tour/static/src/js/tour_service.js | 57 ++++++++++++-------
 2 files changed, 45 insertions(+), 19 deletions(-)

diff --git a/addons/web_editor/views/iframe.xml b/addons/web_editor/views/iframe.xml
index 282d408a2dc0..4c29a1b12f04 100644
--- a/addons/web_editor/views/iframe.xml
+++ b/addons/web_editor/views/iframe.xml
@@ -14,6 +14,13 @@
             <script type="text/javascript">
                 var snippets_url = '<t t-esc="snippets or ''"/>';
                 var callback = window.location.href.match(/callback=([^&amp;=]+)/)[1];
+
+                var odoo = {
+                    session_info: {
+                        is_superuser: <t t-esc="json.dumps(request.env.user._is_superuser())"/>,
+                        is_frontend: true, // this is not really the frontend here but this key is used to detect if xml has to be manually loaded
+                    },
+                };
             </script>
             <t t-if="not dont_load_assets">
                 <t t-call-assets="web.assets_common" t-js="false"/>
diff --git a/addons/web_tour/static/src/js/tour_service.js b/addons/web_tour/static/src/js/tour_service.js
index 261b4c13fe9f..85c3a7b88118 100644
--- a/addons/web_tour/static/src/js/tour_service.js
+++ b/addons/web_tour/static/src/js/tour_service.js
@@ -27,7 +27,6 @@ return session.is_bound.then(function () {
     // in the page source.
     if (session.is_frontend && session.is_superuser) {
         defs.push(new Model('web_tour.tour').call('get_consumed_tours'));
-        defs.push(ajax.loadXML('/web_tour/static/src/xml/tip.xml', QWeb));
     }
     return $.when.apply($, defs).then(function (consumed_tours) {
         var tour = new TourManager(session.is_frontend ? consumed_tours : session.web_tours);
@@ -51,24 +50,37 @@ return session.is_bound.then(function () {
             }
         }, 500);
         var observer = new MutationObserver(check_tooltip);
-        var start_service = function (observe) {
-            $(function () {
-                /**
-                 * Once the DOM is ready, we still have to wait all the modules are loaded before completing the tours
-                 * registration and starting listening for DOM mutations.
-                 */
-                _.defer(function () {
-                    tour._register_all(observe);
-                    if (observe) {
-                        observer.observe(document.body, {
-                            attributes: true,
-                            childList: true,
-                            subtree: true,
+        var start_service = (function () {
+            var load_def;
+
+            return function (observe) {
+                if (load_def === undefined && observe && session.is_frontend) {
+                    load_def = ajax.loadXML('/web_tour/static/src/xml/tip.xml', QWeb);
+                }
+
+                var def = $.Deferred();
+                $(function () {
+                    /**
+                     * Once the DOM is ready, we still have to wait all the modules are loaded before completing the tours
+                     * registration and starting listening for DOM mutations.
+                     */
+                     $.when(load_def).then(function () {
+                         _.defer(function () {
+                            tour._register_all(observe);
+                            if (observe) {
+                                observer.observe(document.body, {
+                                    attributes: true,
+                                    childList: true,
+                                    subtree: true,
+                                });
+                            }
+                            def.resolve();
                         });
-                    }
+                    });
                 });
-            });
-        };
+                return def;
+            };
+        })();
 
         // Enable the MutationObserver for the admin or if a tour is running, when the DOM is ready
         start_service(session.is_superuser || tour.running_tour);
@@ -77,8 +89,15 @@ return session.is_bound.then(function () {
         if (!session.is_superuser) {
             var run = tour.run;
             tour.run = function () {
-                run.apply(this, arguments);
-                if (this.running_tour) { start_service(true); }
+                var self = this;
+                var args = arguments;
+
+                start_service(true).then(function () {
+                    run.apply(self, args);
+                    if (!self.running_tour) {
+                        observer.disconnect();
+                    }
+                });
             };
             var _consume_tour = tour._consume_tour;
             tour._consume_tour = function () {
-- 
GitLab