diff --git a/addons/web/static/src/js/action_manager.js b/addons/web/static/src/js/action_manager.js index de0f17691f3..1f9de9c2a7e 100644 --- a/addons/web/static/src/js/action_manager.js +++ b/addons/web/static/src/js/action_manager.js @@ -203,6 +203,14 @@ var ActionManager = Widget.extend({ this.inner_action = new_action; this.inner_widget = widget; + if (widget.need_control_panel) { + // Set the ControlPanel bus on the widget to allow it to communicate its status + widget.set_cp_bus(this.main_control_panel.get_bus()); + } else { + // Hide the main ControlPanel for widgets that do not use it + this.main_control_panel.do_hide(); + } + // render the inner_widget in a fragment, and append it to the // document only when it's ready var new_widget_fragment = document.createDocumentFragment(); @@ -497,12 +505,6 @@ var ActionManager = Widget.extend({ search_disable_custom_filters: action.context && action.context.search_disable_custom_filters }); } - // Do not permit popups to communicate with the main control panel - if (!popup) { - options.cp_bus = this.main_control_panel.get_bus(); - } else { - options.cp_bus = core.bus; - } return this[type](action, options); }, @@ -563,6 +565,11 @@ var ActionManager = Widget.extend({ widget.flags.headless = true; } } + if (widget.need_control_panel) { + // Set a fake bus to Dialogs needing a ControlPanel as they should not + // communicate with the main ControlPanel + widget.set_cp_bus(new core.Bus()); + } this.dialog_widget = widget; this.dialog_widget.setParent(this.dialog); var initialized = this.dialog_widget.appendTo(this.dialog.$el); @@ -580,7 +587,7 @@ var ActionManager = Widget.extend({ var self = this; return this.ir_actions_common({ widget: function () { - return new ViewManager(self, null, null, null, action, options.cp_bus); + return new ViewManager(self, null, null, null, action); }, action: action, klass: 'oe_act_window', @@ -602,8 +609,6 @@ var ActionManager = Widget.extend({ return this.ir_actions_common({ widget: function () { - // Hide main control panel as client actions do not use it - self.main_control_panel.do_hide(); return new ClientWidget(self, action); }, action: action, diff --git a/addons/web/static/src/js/control_panel.js b/addons/web/static/src/js/control_panel.js index 2064ada44b9..43f4e8bd8e9 100644 --- a/addons/web/static/src/js/control_panel.js +++ b/addons/web/static/src/js/control_panel.js @@ -1,3 +1,32 @@ +odoo.define('web.ControlPanelMixin', function (require) { +"use strict"; + +/** + * Mixin allowing widgets to communicate with the ControlPanel. Widgets needing a + * ControlPanel should use this mixin and call update_control_panel(cp_status) where + * cp_status contains information for the ControlPanel to update itself. + */ +var ControlPanelMixin = { + need_control_panel: true, + /** + * @param {web.Bus} [cp_bus] Bus to communicate with the ControlPanel + */ + set_cp_bus: function(cp_bus) { + this.cp_bus = cp_bus; + }, + /** + * Triggers 'update' on the cp_bus to update the ControlPanel according to cp_status + * @param {Object} [cp_status] see web.ControlPanel.update() for a description + */ + update_control_panel: function(cp_status) { + this.cp_bus.trigger("update", cp_status || {}); + }, +}; + +return ControlPanelMixin; + +}); + odoo.define('web.ControlPanel', function (require) { "use strict"; diff --git a/addons/web/static/src/js/view_manager.js b/addons/web/static/src/js/view_manager.js index 432b734f6c5..befbdf18fd2 100644 --- a/addons/web/static/src/js/view_manager.js +++ b/addons/web/static/src/js/view_manager.js @@ -1,6 +1,7 @@ odoo.define('web.ViewManager', function (require) { "use strict"; +var ControlPanelMixin = require('web.ControlPanelMixin'); var core = require('web.core'); var data = require('web.data'); var Model = require('web.Model'); @@ -11,15 +12,14 @@ var Widget = require('web.Widget'); var QWeb = core.qweb; var _t = core._t; -var ViewManager = Widget.extend({ +var ViewManager = Widget.extend(ControlPanelMixin, { template: "ViewManager", /** * @param {Object} [dataset] null object (... historical reasons) * @param {Array} [views] List of [view_id, view_type] * @param {Object} [flags] various boolean describing UI state - * @param {Object} [cp_bus] Bus to allow communication with ControlPanel */ - init: function(parent, dataset, views, flags, action, cp_bus) { + init: function(parent, dataset, views, flags, action) { if (action) { flags = action.flags || {}; if (!('auto_search' in flags)) { @@ -45,7 +45,6 @@ var ViewManager = Widget.extend({ this.active_view = null; this.registry = core.view_registry; this.title = this.action && this.action.name; - this.cp_bus = cp_bus; _.each(views, function (view) { var view_type = view[1] || view.view_type; @@ -157,18 +156,16 @@ var ViewManager = Widget.extend({ // Show the view this.active_view.$container.show(); $.when(view_controller.do_show(view_options)).done(function () { - // Render the control elements of the active view (buttons, sidebar, pager) - var view_elements = self.render_view_control_elements(); - var status = { + // Prepare the ControlPanel content and update it + var cp_status = { active_view_selector: '.oe-cp-switch-' + self.active_view.type, breadcrumbs: self.action_manager && self.action_manager.get_breadcrumbs(), - cp_content: _.extend({}, self.control_elements, view_elements), + cp_content: _.extend({}, self.control_elements, self.render_view_control_elements()), hidden: self.flags.headless, searchview: self.searchview, search_view_hidden: view_controller.searchable === false, }; - // Tell the ControlPanel to update its elements - self.cp_bus.trigger('update', status); + self.update_control_panel(cp_status); }); }, create_view: function(view, view_options) { diff --git a/addons/web/static/src/js/views/form_relational_widgets.js b/addons/web/static/src/js/views/form_relational_widgets.js index aa164a927e3..f7e31fc7517 100644 --- a/addons/web/static/src/js/views/form_relational_widgets.js +++ b/addons/web/static/src/js/views/form_relational_widgets.js @@ -1089,7 +1089,8 @@ var One2ManyViewManager = ViewManager.extend({ sidebar: false, }); this.control_panel = new ControlPanel(parent, "One2ManyControlPanel"); - this._super(parent, dataset, views, flags, null, this.control_panel.bus); + this.set_cp_bus(this.control_panel.get_bus()); + this._super(parent, dataset, views, flags); this.registry = core.view_registry.extend({ list: One2ManyListView, form: One2ManyFormView,