[FIX] display all non-automatic actions
[odoo/odoo.git] / addons / web_dashboard / static / src / js / dashboard.js
1 openerp.web_dashboard = function(openerp) {
2 var QWeb = openerp.web.qweb;
3 QWeb.add_template('/web_dashboard/static/src/xml/web_dashboard.xml');
4
5 openerp.web.form.DashBoard = openerp.web.form.Widget.extend({
6     init: function(view, node) {
7         this._super(view, node);
8         this.template = 'DashBoard';
9         this.actions_attrs = {};
10         this.action_managers = [];
11     },
12     start: function() {
13         var self = this;
14         this._super.apply(this, arguments);
15
16         this.$element.find('.oe-dashboard-column').sortable({
17             connectWith: '.oe-dashboard-column',
18             handle: '.oe-dashboard-action-header',
19             scroll: false
20         }).disableSelection().bind('sortstop', self.do_save_dashboard);
21
22         // Events
23         this.$element.find('.oe-dashboard-link-undo').click(this.on_undo);
24         this.$element.find('.oe-dashboard-link-reset').click(this.on_reset);
25         this.$element.find('.oe-dashboard-link-add_widget').click(this.on_add_widget);
26         this.$element.find('.oe-dashboard-link-change_layout').click(this.on_change_layout);
27
28         this.$element.delegate('.oe-dashboard-column .oe-dashboard-fold', 'click', this.on_fold_action);
29         this.$element.delegate('.oe-dashboard-column .ui-icon-closethick', 'click', this.on_close_action);
30
31         this.actions_attrs = {};
32         // Init actions
33         _.each(this.node.children, function(column) {
34             _.each(column.children, function(action) {
35                 delete(action.attrs.width);
36                 delete(action.attrs.height);
37                 delete(action.attrs.colspan);
38                 self.actions_attrs[action.attrs.name] = action.attrs;
39                 self.rpc('/web/action/load', {
40                     action_id: parseInt(action.attrs.name, 10)
41                 }, self.on_load_action);
42             });
43         });
44
45         //this.$element.find('a.oe-dashboard-action-rename').live('click', this.on_rename);
46     },
47     on_undo: function() {
48         this.rpc('/web/view/undo_custom', {
49             view_id: this.view.fields_view.view_id
50         }, this.do_reload);
51     },
52     on_reset: function() {
53         this.rpc('/web/view/undo_custom', {
54             view_id: this.view.fields_view.view_id,
55             reset: true
56         }, this.do_reload);
57     },
58     on_add_widget: function() {
59         var self = this;
60         var action_manager = new openerp.web.ActionManager(this);
61         var dialog = new openerp.web.Dialog(this, {
62             title : 'Actions',
63             width: 800,
64             height: 600,
65             buttons : {
66                 Cancel : function() {
67                     $(this).dialog('destroy');
68                 },
69                 Add : function() {
70                     self.do_add_widget(action_manager.inner_viewmanager.views.list.controller);
71                     $(this).dialog('destroy');
72                 }
73             }
74         }).start().open();
75         action_manager.appendTo(dialog.$element);
76         action_manager.do_action({
77             res_model : 'ir.actions.actions',
78             views : [[false, 'list']],
79             type : 'ir.actions.act_window',
80             limit : 80,
81             auto_search : true,
82             flags : {
83                 sidebar : false,
84                 views_switcher : false,
85                 action_buttons : false
86             }
87         });
88         // TODO: should bind ListView#select_record in order to catch record clicking
89     },
90     do_add_widget : function(listview) {
91         var self = this,
92             actions = listview.groups.get_selection().ids,
93             results = [],
94             qdict = { view : this.view };
95         // TODO: should load multiple actions at once
96         _.each(actions, function(aid) {
97             self.rpc('/web/action/load', {
98                 action_id: aid
99             }, function(result) {
100                 self.actions_attrs[aid] = {
101                     name: aid,
102                     string: _.trim(result.result.name)
103                 };
104                 qdict.action = {
105                     attrs : self.actions_attrs[aid]
106                 };
107                 self.$element.find('.oe-dashboard-column:first').prepend(QWeb.render('DashBoard.action', qdict));
108                 self.do_save_dashboard();
109                 self.on_load_action(result)
110             });
111         });
112     },
113     on_rename : function(e) {
114         var self = this,
115             id = parseInt($(e.currentTarget).parents('.oe-dashboard-action:first').attr('data-id'), 10),
116             $header = $(e.currentTarget).parents('.oe-dashboard-action-header:first'),
117             $rename = $header.find('a.oe-dashboard-action-rename').hide(),
118             $title = $header.find('span.oe-dashboard-action-title').hide(),
119             $input = $header.find('input[name=title]');
120         $input.val($title.text()).show().focus().bind('keydown', function(e) {
121             if (e.which == 13 || e.which == 27) {
122                 if (e.which == 13) { //enter
123                     var val = $input.val();
124                     if (!val) {
125                         return false;
126                     }
127                     $title.text(val);
128                     self.actions_attrs[id].string = val;
129                     self.do_save_dashboard();
130                 }
131                 $input.unbind('keydown').hide();
132                 $rename.show();
133                 $title.show();
134             }
135         });
136     },
137     on_change_layout: function() {
138         var self = this;
139         var qdict = {
140             current_layout : this.$element.find('.oe-dashboard').attr('data-layout')
141         };
142         var $dialog = $('<div>').dialog({
143                             modal: true,
144                             title: 'Edit Layout',
145                             width: 'auto',
146                             height: 'auto'
147                         }).html(QWeb.render('DashBoard.layouts', qdict));
148         $dialog.find('li').click(function() {
149             var layout = $(this).attr('data-layout');
150             $dialog.dialog('destroy');
151             self.do_change_layout(layout);
152         });
153     },
154     do_change_layout: function(new_layout) {
155         var $dashboard = this.$element.find('.oe-dashboard');
156         var current_layout = $dashboard.attr('data-layout');
157         if (current_layout != new_layout) {
158             var clayout = current_layout.split('-').length,
159                 nlayout = new_layout.split('-').length,
160                 column_diff = clayout - nlayout;
161             if (column_diff > 0) {
162                 var $last_column = $();
163                 $dashboard.find('.oe-dashboard-column').each(function(k, v) {
164                     if (k >= nlayout) {
165                         $(v).find('.oe-dashboard-action').appendTo($last_column);
166                     } else {
167                         $last_column = $(v);
168                     }
169                 });
170             }
171             $dashboard.toggleClass('oe-dashboard-layout_' + current_layout + ' oe-dashboard-layout_' + new_layout);
172             $dashboard.attr('data-layout', new_layout);
173             this.do_save_dashboard();
174         }
175     },
176     on_fold_action: function(e) {
177         var $e = $(e.currentTarget),
178             $action = $e.parents('.oe-dashboard-action:first'),
179             id = parseInt($action.attr('data-id'), 10);
180         if ($e.is('.ui-icon-minusthick')) {
181             this.actions_attrs[id].fold = '1';
182         } else {
183             delete(this.actions_attrs[id].fold);
184         }
185         $e.toggleClass('ui-icon-minusthick ui-icon-plusthick');
186         $action.find('.oe-dashboard-action-content').toggle();
187         this.do_save_dashboard();
188     },
189     on_close_action: function(e) {
190         $(e.currentTarget).parents('.oe-dashboard-action:first').remove();
191         this.do_save_dashboard();
192     },
193     do_save_dashboard: function() {
194         var self = this;
195         var board = {
196                 form_title : this.view.fields_view.arch.attrs.string,
197                 style : this.$element.find('.oe-dashboard').attr('data-layout'),
198                 columns : []
199             };
200         this.$element.find('.oe-dashboard-column').each(function() {
201             var actions = [];
202             $(this).find('.oe-dashboard-action').each(function() {
203                 var action_id = $(this).attr('data-id');
204                 actions.push(self.actions_attrs[action_id]);
205             });
206             board.columns.push(actions);
207         });
208         var arch = QWeb.render('DashBoard.xml', board);
209         this.rpc('/web/view/add_custom', {
210             view_id: this.view.fields_view.view_id,
211             arch: arch
212         }, function() {
213             self.$element.find('.oe-dashboard-link-undo, .oe-dashboard-link-reset').show();
214         });
215     },
216     on_load_action: function(result) {
217         var action = result.result;
218         action.flags = {
219             search_view : false,
220             sidebar : false,
221             views_switcher : false,
222             action_buttons : false,
223             pager: false,
224             low_profile: true,
225             display_title: false
226         };
227         var am = new openerp.web.ActionManager(this);
228         this.action_managers.push(am);
229         am.appendTo($("#"+this.view.element_id + '_action_' + action.id));
230         am.do_action(action);
231     },
232     render: function() {
233         // We should start with three columns available
234         for (var i = this.node.children.length; i < 3; i++) {
235             this.node.children.push({
236                 tag: 'column',
237                 attrs: {},
238                 children: []
239             });
240         }
241         return QWeb.render(this.template, this);
242     },
243     do_reload: function() {
244         _.each(this.action_managers, function(am) {
245             am.stop();
246         });
247         this.action_managers = [];
248         this.view.stop();
249         this.view.start();
250     }
251 });
252 openerp.web.form.DashBoardLegacy = openerp.web.form.DashBoard.extend({
253     render: function() {
254         if (this.node.tag == 'hpaned') {
255             this.node.attrs.style = '2-1';
256         } else if (this.node.tag == 'vpaned') {
257             this.node.attrs.style = '1';
258         }
259         this.node.tag = 'board';
260         _.each(this.node.children, function(child) {
261             if (child.tag.indexOf('child') == 0) {
262                 child.tag = 'column';
263                 var actions = [], first_child = child.children[0];
264                 if (first_child && first_child.tag == 'vpaned') {
265                     _.each(first_child.children, function(subchild) {
266                         actions.push.apply(actions, subchild.children);
267                     });
268                     child.children = actions;
269                 }
270             }
271         });
272         return this._super(this, arguments);
273     }
274 });
275
276 openerp.web.form.widgets.add('hpaned', 'openerp.web.form.DashBoardLegacy');
277 openerp.web.form.widgets.add('vpaned', 'openerp.web.form.DashBoardLegacy');
278 openerp.web.form.widgets.add('board', 'openerp.web.form.DashBoard');
279
280 openerp.web.client_actions.add(
281     'board.config.overview', 'openerp.web_dashboard.ConfigOverview'
282 );
283 if (!openerp.web_dashboard) {
284     /** @namespace */
285     openerp.web_dashboard = {};
286 }
287 openerp.web_dashboard.ConfigOverview = openerp.web.View.extend({
288     template: 'ConfigOverview',
289     init: function (parent) {
290         this._super(parent);
291         this.dataset = new openerp.web.DataSetSearch(
292                 this, 'ir.actions.todo');
293         this.dataset.domain = [['type', '!=', 'automatic']];
294     },
295     start: function () {
296         this._super();
297         $.when(this.dataset.read_slice(['state', 'action_id', 'category_id']),
298                this.dataset.call('progress'))
299             .then(this.on_records_loaded);
300     },
301     on_records_loaded: function (read_response, progress_response) {
302         var records = read_response,
303            progress = progress_response[0];
304
305         var grouped_todos = _(records).chain()
306             .map(function (record) {
307                 return {
308                     id: record.id,
309                     name: record.action_id[1],
310                     done: record.state !== 'open',
311                     to_do: record.state === 'open',
312                     category: record['category_id'][1] || "Uncategorized"
313                 }
314             })
315             .groupBy(function (record) {return record.category})
316             .value();
317         this.$element.html(QWeb.render('ConfigOverview.content', {
318             completion: 100 * progress.done / progress.total,
319             groups: grouped_todos
320         }));
321         var $progress = this.$element.find('div.oe-config-progress-bar');
322         $progress.progressbar({value: $progress.data('completion')});
323
324         var self = this;
325         this.$element.find('dl')
326             .delegate('input', 'click', function (e) {
327                 // switch todo status
328                 e.stopImmediatePropagation();
329                 var new_state = this.checked ? 'done' : 'open',
330                       todo_id = parseInt($(this).val(), 10);
331                 self.dataset.write(todo_id, {state: new_state}, {}, function () {
332                     self.start();
333                 });
334             })
335             .delegate('li:not(.oe-done)', 'click', function () {
336                 self.widget_parent.widget_parent.widget_parent.do_execute_action({
337                         type: 'object',
338                         name: 'action_launch'
339                     }, self.dataset,
340                     $(this).data('id'), function () {
341                         // after action popup closed, refresh configuration
342                         // thingie
343                         self.start();
344                     });
345             });
346     }
347 });
348
349 openerp.web.client_actions.add(
350     'board.home.applications', 'openerp.web_dashboard.ApplicationTiles');
351 openerp.web_dashboard.ApplicationTiles = openerp.web.View.extend({
352     template: 'ApplicationTiles',
353     start: function () {
354         this._super();
355         var self = this;
356         return new openerp.web.DataSetSearch(
357                 this, 'ir.ui.menu', null, [['parent_id', '=', false]])
358             .read_slice( ['name', 'web_icon_data', 'web_icon_hover_data'], {}, function (applications) {
359                 // Create a matrix of 3*x applications
360                 var rows = [];
361                 while (applications.length) {
362                     rows.push(applications.splice(0, 3));
363                 }
364                 self.$element
365                     .append(QWeb.render(
366                         'ApplicationTiles.content', {rows: rows}))
367                     .find('.oe-dashboard-home-tile')
368                         .click(function () {
369                             var $this = $(this);
370                             $this.closest('.openerp')
371                                  .find('.menu a[data-menu=' + $this.data('menuid') + ']')
372                                  .click();});
373             });
374     }
375 });
376 openerp.web.client_actions.add(
377     'board.home.widgets', 'openerp.web_dashboard.Widget');
378 openerp.web_dashboard.Widget = openerp.web.View.extend(/** @lends openerp.web_dashboard.Widgets# */{
379     template: 'HomeWidget',
380     /**
381      * Initializes a "HomeWidget" client widget: handles the display of a given
382      * res.widget objects in an OpenERP view (mainly a dashboard).
383      *
384      * @constructs openerp.web_dashboard.Widget
385      * @extends openerp.web.View
386      *
387      * @param {Object} parent
388      * @param {Object} options
389      * @param {Number} options.widget_id
390      */
391     init: function (parent, options) {
392         this._super(parent);
393         this.widget_id = options.widget_id;
394     },
395     start: function () {
396         this._super();
397         return new openerp.web.DataSet(this, 'res.widget').read_ids(
398                 [this.widget_id], ['title'], this.on_widget_loaded);
399     },
400     on_widget_loaded: function (widgets) {
401         var widget = widgets[0];
402         var url = _.sprintf(
403             '/web_dashboard/widgets/content?session_id=%s&widget_id=%d',
404             this.session.session_id, widget.id);
405         this.$element.html(QWeb.render('HomeWidget.content', {
406             widget: widget,
407             url: url
408         }));
409     }
410 });
411 };