From b2aafa23b822e99b4c252cd6a3af0e4e1c246ba5 Mon Sep 17 00:00:00 2001
From: Martin Geubelle <mge@odoo.com>
Date: Wed, 24 Aug 2016 15:16:45 +0200
Subject: [PATCH] [IMP] web: trigger event when changing action
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This comportement was added in the DebugManager to trigger an event.
As this event could be useful in other situations (e.g. studio), we trigger
the event directly in the ActionManager. By default, the WebClient does not
use this event but an handler is added to be overwritten.

This handler is overwritten in some widgets (DebugManager, Studio, etc.) that
requires the Systray items instances so we add them in ´widgets´ in the Systray Menu.

As the module dashboard uses multiple ActionManager, the event bubbling should be
avoided in this case.
---
 addons/board/static/src/js/dashboard.js       |  1 +
 .../web/static/src/js/abstract_web_client.js  |  6 +++++
 addons/web/static/src/js/action_manager.js    |  2 ++
 .../static/src/js/widgets/debug_manager.js    | 27 +++++--------------
 .../web/static/src/js/widgets/systray_menu.js |  2 ++
 5 files changed, 17 insertions(+), 21 deletions(-)

diff --git a/addons/board/static/src/js/dashboard.js b/addons/board/static/src/js/dashboard.js
index 6606ffe55690..87ff74f826ae 100644
--- a/addons/board/static/src/js/dashboard.js
+++ b/addons/board/static/src/js/dashboard.js
@@ -264,6 +264,7 @@ var DashBoard = form_common.FormWidget.extend({
                 }
             });
             am.do_action = self.do_action.bind(self);
+            am.current_action_updated = function() {};
         });
     },
     renderElement: function() {
diff --git a/addons/web/static/src/js/abstract_web_client.js b/addons/web/static/src/js/abstract_web_client.js
index c3985f4cf5ce..e6dab38f61c3 100644
--- a/addons/web/static/src/js/abstract_web_client.js
+++ b/addons/web/static/src/js/abstract_web_client.js
@@ -30,6 +30,9 @@ var WebClient = Widget.extend({
         toggle_fullscreen: function (event) {
             this.toggle_fullscreen(event.data.fullscreen);
         },
+        current_action_updated: function (e) {
+            this.current_action_updated(e.data.action);
+        },
     },
     init: function(parent) {
         this.client_options = {};
@@ -220,6 +223,9 @@ var WebClient = Widget.extend({
             this.connection_notification = false;
         }
     },
+    // Handler to be overwritten
+    current_action_updated: function () {
+    },
     // --------------------------------------------------------------
     // Scrolltop handling
     // --------------------------------------------------------------
diff --git a/addons/web/static/src/js/action_manager.js b/addons/web/static/src/js/action_manager.js
index 4a03fadc5431..0206e4cd25ef 100644
--- a/addons/web/static/src/js/action_manager.js
+++ b/addons/web/static/src/js/action_manager.js
@@ -326,6 +326,7 @@ var ActionManager = Widget.extend({
                 self.clear_action_stack(actions_to_destroy);
             }
             self.toggle_fullscreen();
+            self.trigger_up('current_action_updated', {action: new_action});
         }).fail(function () {
             // Destroy failed action and restore internal state
             new_action.destroy();
@@ -408,6 +409,7 @@ var ActionManager = Widget.extend({
                         callbacks: [{widget: action.widget}],
                     });
                 }
+                self.trigger_up('current_action_updated', {action: action});
             });
         }).fail(function() {
             return $.Deferred().reject();
diff --git a/addons/web/static/src/js/widgets/debug_manager.js b/addons/web/static/src/js/widgets/debug_manager.js
index f191eb739ea6..f7ddd008e1d9 100644
--- a/addons/web/static/src/js/widgets/debug_manager.js
+++ b/addons/web/static/src/js/widgets/debug_manager.js
@@ -48,7 +48,6 @@ var DebugManager = Widget.extend({
         }
     },
     start: function () {
-        core.bus.on('current_action_updated', this, this.update.bind(this, 'action'));
         core.bus.on('rpc:result', this, function (req, resp) {
             this._debug_events(resp.debug);
         });
@@ -647,26 +646,12 @@ if (core.debug) {
     SystrayMenu.Items.push(DebugManager);
 
     WebClient.include({
-        start: function() {
-            var self = this;
-            return this._super().then(function () {
-                // Override push_action and select_action so that it triggers
-                // an event each time a new action is pushed.
-                // The DebugManager listens to this event to keep itself up-to-date
-                var push_action = self.action_manager.push_action;
-                self.action_manager.push_action = function(widget, descr) {
-                    return push_action.apply(self.action_manager, arguments).then(function () {
-                        core.bus.trigger('current_action_updated', descr, widget);
-                    });
-                };
-                var select_action = self.action_manager.select_action;
-                self.action_manager.select_action = function() {
-                    return select_action.apply(self.action_manager, arguments).then(function () {
-                        var action = self.action_manager.get_inner_action();
-                        core.bus.trigger('current_action_updated', action.action_descr, action.widget);
-                    });
-                };
-            });
+        current_action_updated: function(action) {
+            this._super.apply(this, arguments);
+            var action_descr = action && action.action_descr;
+            var action_widget = action && action.widget;
+            var debug_manager = _.find(this.systray_menu.widgets, function(item) {return item instanceof DebugManager; });
+            debug_manager.update('action', action_descr, action_widget);
         },
     });
 
diff --git a/addons/web/static/src/js/widgets/systray_menu.js b/addons/web/static/src/js/widgets/systray_menu.js
index 025d8c83970c..0d0e18476b03 100644
--- a/addons/web/static/src/js/widgets/systray_menu.js
+++ b/addons/web/static/src/js/widgets/systray_menu.js
@@ -11,6 +11,7 @@ var SystrayMenu = Widget.extend({
     init: function(parent) {
         this._super(parent);
         this.items = [];
+        this.widgets = [];
         this.load = $.Deferred();
     },
     start: function() {
@@ -29,6 +30,7 @@ var SystrayMenu = Widget.extend({
         });
         _.each(SystrayMenu.Items, function(widgetCls) {
             var cur_systray_item = new widgetCls(self);
+            self.widgets.push(cur_systray_item);
             self.items.push(cur_systray_item.prependTo(self.$el));
         });
     },
-- 
GitLab