[imp] improved sidebar, now hide it by default in list views.
[odoo/odoo.git] / addons / base / static / src / js / views.js
1 /*---------------------------------------------------------
2  * OpenERP base library
3  *---------------------------------------------------------*/
4
5 openerp.base.views = function(openerp) {
6
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;
16     },
17     /**
18      * Process an action
19      * Supported actions: act_window
20      */
21     do_action: function(action) {
22         var self = this;
23         action.flags = _.extend({
24             sidebar : true,
25             search_view : true,
26             new_window : false,
27             views_switcher : true,
28             action_buttons : true,
29             pager : 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>');
37                     dialog.dialog({
38                         title: action.name,
39                         modal: true,
40                         width: '50%',
41                         height: 'auto'
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' });
45                     });
46                     var viewmanager = new openerp.base.ViewManagerAction(this.session, element_id, action);
47                     viewmanager.start();
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 });
54                         window.open(url);
55                     });
56                 } else {
57                     if (this.viewmanager) {
58                         this.viewmanager.stop();
59                     }
60                     this.viewmanager = new openerp.base.ViewManagerAction(this.session, this.element_id, action);
61                     this.viewmanager.start();
62                 }
63                 break;
64             case 'ir.actions.act_window_close':
65                 var dialog = this.dialog_stack.pop();
66                 dialog.$element.dialog('destroy');
67                 dialog.stop();
68                 break;
69             default:
70                 console.log(_.sprintf("Action manager can't handle action of type %s", action.type), action);
71         }
72     }
73 });
74
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;
83         this.views = {};
84         this.flags = this.flags || {};
85     },
86     /**
87      * @returns {jQuery.Deferred} initial view loading promise
88      */
89     start: function() {
90         var self = this;
91         this.dataset.start();
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'));
95         });
96         _.each(this.views_src, function(view) {
97             self.views[view[1]] = { view_id: view[0], controller: null };
98         });
99         if (this.flags.views_switcher === false) {
100             this.$element.find('.oe_vm_switch').hide();
101         }
102         // switch to the first one in sequence
103         return this.on_mode_switch(this.views_src[0][1]);
104     },
105     stop: function() {
106     },
107     /**
108      * Asks the view manager to switch visualization mode.
109      *
110      * @param {String} view_type type of view to display
111      * @returns {jQuery.Deferred} new view loading promise
112      */
113     on_mode_switch: function(view_type) {
114         var view_promise;
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;
123         }
124
125         if (this.searchview) {
126             if (view.controller.searchable === false) {
127                 this.searchview.hide();
128             } else {
129                 this.searchview.show();
130             }
131         }
132
133         this.$element
134             .find('.views-switchers button').removeAttr('disabled')
135             .filter('[data-view-type="' + view_type + '"]')
136             .attr('disabled', true);
137
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);
142                 } else {
143                     this.views[i].controller.do_hide();
144                 }
145             }
146         }
147         return view_promise;
148     },
149     /**
150      * Sets up the current viewmanager's search view.
151      *
152      * @param view_id the view to use or false for a default one
153      * @returns {jQuery.Deferred} search view startup deferred
154      */
155     setup_search_view: function(view_id, search_defaults) {
156         var self = this;
157         if (this.searchview) {
158             this.searchview.stop();
159         }
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();
163         }
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);
168         });
169         return this.searchview.start();
170     },
171     /**
172      * Called when one of the view want to execute an action
173      */
174     on_action: function(action) {
175     },
176     on_create: function() {
177     },
178     on_remove: function() {
179     },
180     on_edit: function() {
181     },
182     /**
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
185      *
186      * @returns an empty list
187      */
188     domains: function () {
189         return [];
190     },
191     /**
192      * Contexts added on searches by the view manager.
193      *
194      * @returns an empty list
195      */
196     contexts: function () {
197         return [];
198     }
199 });
200
201 openerp.base.ViewManagerAction = openerp.base.ViewManager.extend({
202     init: function(session, element_id, action) {
203         var dataset;
204         if(!action.res_id) {
205             dataset = new openerp.base.DataSetSearch(session, action.res_model);
206         } else {
207             dataset = new openerp.base.DataSetStatic(session, action.res_model);
208             dataset.ids = [action.res_id];
209             dataset.count = 1;
210         }
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;
217         }
218         if (this.flags.sidebar) {
219             this.sidebar = new openerp.base.Sidebar(null, this);
220         }
221     },
222     start: function() {
223         var inital_view_loaded = this._super();
224
225         // init sidebar
226         if (this.sidebar) {
227             this.$element.find('.view-manager-main-sidebar').html(this.sidebar.render());
228             this.sidebar.start();
229         }
230
231         var search_defaults = {};
232         _.each(this.action.context, function (value, key) {
233             var match = /^search_default_(.*)$/.exec(key);
234             if (match) {
235                 search_defaults[match[1]] = value;
236             }
237         });
238
239         // init search view
240         var searchview_id = this.action.search_view_id && this.action.search_view_id[0];
241
242         var searchview_loaded = this.setup_search_view(
243                 searchview_id || false, search_defaults);
244
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);
249         }
250     },
251     stop: function() {
252         // should be replaced by automatic destruction implemented in BaseWidget
253         if (this.sidebar) {
254             this.sidebar.stop();
255         }
256         this._super();
257     },
258     /**
259      * adds action domain to the search domains
260      *
261      * @returns the action's domain
262      */
263     domains: function () {
264         if (!this.action.domain) {
265             return [];
266         }
267         return [this.action.domain];
268     },
269     /**
270      * adds action context to the search contexts
271      *
272      * @returns the action's context
273      */
274     contexts: function () {
275         if (!this.action.context) {
276             return [];
277         }
278         return [this.action.context];
279     }
280 });
281
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;
287         this.sections = [];
288     },
289     set_toolbar: function(toolbar) {
290         this.sections = [];
291         var self = this;
292         _.each([["print", "Reports"], ["action", "Actions"], ["relate", "Links"]], function(type) {
293             if (toolbar[type[0]].length == 0)
294                 return;
295             var section = {elements:toolbar[type[0]], label:type[1]};
296             self.sections.push(section);
297         });
298         this.refresh(true);
299     },
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');
308         } else {
309             this.$element.addClass('open-sidebar');
310             this.$element.removeClass('closed-sidebar');
311         }
312         
313         this.$element.html(QWeb.render("ViewManager.sidebar.internal",this));
314         
315         var self = this;
316         this.$element.find(".toggle-sidebar").click(function(e) {
317             self.$element.toggleClass('open-sidebar closed-sidebar');
318             e.stopPropagation();
319             e.preventDefault();
320         });
321         
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];
327             action.flags = {
328                 new_window : true
329             }
330             self.session.action_manager.do_action(action);
331             e.stopPropagation();
332             e.preventDefault();
333         });
334     },
335     start: function() {
336         this._super();
337         this.refresh(false);
338     }
339 });
340
341 openerp.base.View = openerp.base.Controller.extend({
342     /**
343      * Fetches and executes the action identified by ``action_data``.
344      *
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)
354      */
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);
359             } else {
360                 on_no_action(r.result);
361             }
362         };
363
364         if (action_data.special) {
365             handler({
366                 result : { type: 'ir.actions.act_window_close' }
367             });
368         } else {
369             var context = _.extend({}, dataset.context, action_data.context || {});
370             switch(action_data.type) {
371                 case 'object':
372                     return dataset.call(action_data.name, [record_id], [context], handler);
373                 case 'action':
374                     return this.rpc('/base/action/load', { action_id: parseInt(action_data.name, 10) }, handler);
375                 default:
376                     return dataset.exec_workflow(record_id, action_data.name, handler);
377             }
378         }
379     }
380 });
381
382 /**
383  * Registry for all the main views
384  */
385 openerp.base.views = new openerp.base.Registry();
386
387 openerp.base.ProcessView = openerp.base.Controller.extend({
388 });
389
390 openerp.base.HelpView = openerp.base.Controller.extend({
391 });
392
393 };
394
395 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: