[ADD] Web: ControlPanelMixin

Creation of the ControlPanelMixin that should be used by widgets
needing a ControlPanel. It facilitates the way those widgets communicate
with the ControlPanel to update it according to their status.

From now on, client action widgets can easily use the main ControlPanel.
This commit is contained in:
Aaron Bohy
2015-03-20 16:34:42 +01:00
parent 4b0a8d4096
commit c6b1d0158e
4 changed files with 52 additions and 20 deletions
+14 -9
View File
@@ -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,
+29
View File
@@ -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";
+7 -10
View File
@@ -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) {
@@ -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,