1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
5 openerp.base.views = function(openerp) {
7 openerp.base.ActionManager = openerp.base.Controller.extend({
8 // process all kind of actions
9 init: function(session, element_id) {
10 this._super(session, element_id);
11 this.viewmanager = null;
12 this.dialog_stack = []
13 // Temporary linking view_manager to session.
14 // Will use controller_parent to find it when implementation will be done.
15 session.action_manager = this;
19 * Supported actions: act_window
21 do_action: function(action) {
23 action.flags = _.extend({
27 views_switcher : true,
28 action_buttons : true,
30 }, action.flags || {});
31 // instantiate the right controllers by understanding the action
32 switch (action.type) {
33 case 'ir.actions.act_window':
34 if (action.target == 'new') {
35 var element_id = _.uniqueId("act_window_dialog");
36 var dialog = $('<div id="' + element_id + '"></div>');
42 }).bind('dialogclose', function(event) {
43 // When dialog is closed with ESC key or close manually, branch to act_window_close logic
44 self.do_action({ type: 'ir.actions.act_window_close' });
46 var viewmanager = new openerp.base.ViewManagerAction(this.session, element_id, action);
48 this.dialog_stack.push(viewmanager);
49 } else if (action.flags.new_window) {
50 action.flags.new_window = false;
51 this.rpc("/base/session/save_session_action", { the_action : action}, function(key) {
52 var url = window.location.protocol + "//" + window.location.host +
53 window.location.pathname + "?" + jQuery.param({ s_action : "" + key });
57 if (this.viewmanager) {
58 this.viewmanager.stop();
60 this.viewmanager = new openerp.base.ViewManagerAction(this.session, this.element_id, action);
61 this.viewmanager.start();
64 case 'ir.actions.act_window_close':
65 var dialog = this.dialog_stack.pop();
66 dialog.$element.dialog('destroy');
70 console.log(_.sprintf("Action manager can't handle action of type %s", action.type), action);
75 openerp.base.ViewManager = openerp.base.Controller.extend({
76 init: function(session, element_id, dataset, views) {
77 this._super(session, element_id);
78 this.model = dataset.model;
79 this.dataset = dataset;
80 this.searchview = null;
81 this.active_view = null;
82 this.views_src = views;
84 this.flags = this.flags || {};
87 * @returns {jQuery.Deferred} initial view loading promise
92 this.$element.html(QWeb.render("ViewManager", {"prefix": this.element_id, views: this.views_src}));
93 this.$element.find('.oe_vm_switch button').click(function() {
94 self.on_mode_switch($(this).data('view-type'));
96 _.each(this.views_src, function(view) {
97 self.views[view[1]] = { view_id: view[0], controller: null };
99 if (this.flags.views_switcher === false) {
100 this.$element.find('.oe_vm_switch').hide();
102 // switch to the first one in sequence
103 return this.on_mode_switch(this.views_src[0][1]);
108 * Asks the view manager to switch visualization mode.
110 * @param {String} view_type type of view to display
111 * @returns {jQuery.Deferred} new view loading promise
113 on_mode_switch: function(view_type) {
115 this.active_view = view_type;
116 var view = this.views[view_type];
117 if (!view.controller) {
118 // Lazy loading of views
119 var controllerclass = openerp.base.views.get_object(view_type);
120 var controller = new controllerclass( this, this.session, this.element_id + "_view_" + view_type, this.dataset, view.view_id);
121 view_promise = controller.start();
122 this.views[view_type].controller = controller;
125 if (this.searchview) {
126 if (view.controller.searchable === false) {
127 this.searchview.hide();
129 this.searchview.show();
134 .find('.views-switchers button').removeAttr('disabled')
135 .filter('[data-view-type="' + view_type + '"]')
136 .attr('disabled', true);
138 for (var i in this.views) {
139 if (this.views[i].controller) {
140 if (i === view_type) {
141 $.when(view_promise).then(this.views[i].controller.do_show);
143 this.views[i].controller.do_hide();
150 * Sets up the current viewmanager's search view.
152 * @param view_id the view to use or false for a default one
153 * @returns {jQuery.Deferred} search view startup deferred
155 setup_search_view: function(view_id, search_defaults) {
157 if (this.searchview) {
158 this.searchview.stop();
160 this.searchview = new openerp.base.SearchView(this, this.session, this.element_id + "_search", this.dataset, view_id, search_defaults);
161 if (this.flags.search_view === false) {
162 this.searchview.hide();
164 this.searchview.on_search.add(function(domains, contexts, groupbys) {
165 self.views[self.active_view].controller.do_search.call(
166 self, domains.concat(self.domains()),
167 contexts.concat(self.contexts()), groupbys);
169 return this.searchview.start();
172 * Called when one of the view want to execute an action
174 on_action: function(action) {
176 on_create: function() {
178 on_remove: function() {
180 on_edit: function() {
183 * Domains added on searches by the view manager, to override in subsequent
184 * view manager in order to add new pieces of domains to searches
186 * @returns an empty list
188 domains: function () {
192 * Contexts added on searches by the view manager.
194 * @returns an empty list
196 contexts: function () {
201 openerp.base.ViewManagerAction = openerp.base.ViewManager.extend({
202 init: function(session, element_id, action) {
205 dataset = new openerp.base.DataSetSearch(session, action.res_model);
207 dataset = new openerp.base.DataSetStatic(session, action.res_model);
208 dataset.ids = [action.res_id];
211 this._super(session, element_id, dataset, action.views);
212 this.action = action;
213 this.flags = this.action.flags || {};
214 if (action.res_model == 'board.board' && action.views.length == 1 && action.views) {
215 // Not elegant but allows to avoid flickering of SearchView#do_hide
216 this.flags.search_view = this.flags.pager = this.flags.action_buttons = false;
218 if (this.flags.sidebar) {
219 this.sidebar = new openerp.base.Sidebar(null, this);
223 var inital_view_loaded = this._super();
227 this.$element.find('.view-manager-main-sidebar').html(this.sidebar.render());
228 this.sidebar.start();
231 var search_defaults = {};
232 _.each(this.action.context, function (value, key) {
233 var match = /^search_default_(.*)$/.exec(key);
235 search_defaults[match[1]] = value;
240 var searchview_id = this.action.search_view_id && this.action.search_view_id[0];
242 var searchview_loaded = this.setup_search_view(
243 searchview_id || false, search_defaults);
245 // schedule auto_search
246 if (searchview_loaded != null && this.action['auto_search']) {
247 $.when(searchview_loaded, inital_view_loaded)
248 .then(this.searchview.do_search);
252 // should be replaced by automatic destruction implemented in BaseWidget
259 * adds action domain to the search domains
261 * @returns the action's domain
263 domains: function () {
264 if (!this.action.domain) {
267 return [this.action.domain];
270 * adds action context to the search contexts
272 * @returns the action's context
274 contexts: function () {
275 if (!this.action.context) {
278 return [this.action.context];
282 openerp.base.Sidebar = openerp.base.BaseWidget.extend({
283 template: "ViewManager.sidebar",
284 init: function(parent, view_manager) {
285 this._super(parent, view_manager.session);
286 this.view_manager = view_manager;
289 set_toolbar: function(toolbar) {
292 _.each([["print", "Reports"], ["action", "Actions"], ["relate", "Links"]], function(type) {
293 if (toolbar[type[0]].length == 0)
295 var section = {elements:toolbar[type[0]], label:type[1]};
296 self.sections.push(section);
300 refresh: function(new_view) {
301 var view = this.view_manager.active_view;
302 the_condition = this.sections.length > 0 && _.detect(this.sections,
303 function(x) {return x.elements.length > 0;}) != undefined
304 && (!new_view || view != 'list');
305 if (!the_condition) {
306 this.$element.addClass('closed-sidebar');
307 this.$element.removeClass('open-sidebar');
309 this.$element.addClass('open-sidebar');
310 this.$element.removeClass('closed-sidebar');
313 this.$element.html(QWeb.render("ViewManager.sidebar.internal",this));
316 this.$element.find(".toggle-sidebar").click(function(e) {
317 self.$element.toggleClass('open-sidebar closed-sidebar');
322 this.$element.find("a.oe_sidebar_action_a").click(function(e) {
323 var $this = jQuery(this);
324 var i = $this.attr("data-i");
325 var j = $this.attr("data-j");
326 var action = self.sections[i].elements[j];
330 self.session.action_manager.do_action(action);
341 openerp.base.View = openerp.base.Controller.extend({
343 * Fetches and executes the action identified by ``action_data``.
345 * @param {Object} action_data the action descriptor data
346 * @param {String} action_data.name the action name, used to uniquely identify the action to find and execute it
347 * @param {String} [action_data.special=null] special action handlers (currently: only ``'cancel'``)
348 * @param {String} [action_data.type='workflow'] the action type, if present, one of ``'object'``, ``'action'`` or ``'workflow'``
349 * @param {Object} [action_data.context=null] additional action context, to add to the current context
350 * @param {openerp.base.DataSet} dataset a dataset object used to communicate with the server
351 * @param {openerp.base.ActionManager} action_manager object able to actually execute the action, if any is fetched
352 * @param {Number} [record_id] the identifier of the object on which the action is to be applied
353 * @param {Function} on_no_action callback to execute if the action does not generate any result (no new action)
355 execute_action: function (action_data, dataset, action_manager, record_id, on_no_action) {
356 var handler = function (r) {
357 if (r.result && r.result.constructor == Object) {
358 action_manager.do_action(r.result);
360 on_no_action(r.result);
364 if (action_data.special) {
366 result : { type: 'ir.actions.act_window_close' }
369 var context = _.extend({}, dataset.context, action_data.context || {});
370 switch(action_data.type) {
372 return dataset.call(action_data.name, [record_id], [context], handler);
374 return this.rpc('/base/action/load', { action_id: parseInt(action_data.name, 10) }, handler);
376 return dataset.exec_workflow(record_id, action_data.name, handler);
383 * Registry for all the main views
385 openerp.base.views = new openerp.base.Registry();
387 openerp.base.ProcessView = openerp.base.Controller.extend({
390 openerp.base.HelpView = openerp.base.Controller.extend({
395 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: