1 openerp.web_dashboard = function(openerp) {
2 var QWeb = openerp.web.qweb;
3 QWeb.add_template('/web_dashboard/static/src/xml/web_dashboard.xml');
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 = [];
14 this._super.apply(this, arguments);
16 this.$element.find('.oe-dashboard-column').sortable({
17 connectWith: '.oe-dashboard-column',
18 handle: '.oe-dashboard-action-header',
20 }).disableSelection().bind('sortstop', self.do_save_dashboard);
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);
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);
31 this.actions_attrs = {};
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);
45 //this.$element.find('a.oe-dashboard-action-rename').live('click', this.on_rename);
48 this.rpc('/web/view/undo_custom', {
49 view_id: this.view.fields_view.view_id
52 on_reset: function() {
53 this.rpc('/web/view/undo_custom', {
54 view_id: this.view.fields_view.view_id,
58 on_add_widget: function() {
60 var action_manager = new openerp.web.ActionManager(this);
61 var dialog = new openerp.web.Dialog(this, {
67 $(this).dialog('destroy');
70 self.do_add_widget(action_manager.inner_viewmanager.views.list.controller);
71 $(this).dialog('destroy');
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',
84 views_switcher : false,
85 action_buttons : false
88 // TODO: should bind ListView#select_record in order to catch record clicking
90 do_add_widget : function(listview) {
92 actions = listview.groups.get_selection().ids,
94 qdict = { view : this.view };
95 // TODO: should load multiple actions at once
96 _.each(actions, function(aid) {
97 self.rpc('/web/action/load', {
100 self.actions_attrs[aid] = {
102 string: _.trim(result.result.name)
105 attrs : self.actions_attrs[aid]
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)
113 on_rename : function(e) {
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();
128 self.actions_attrs[id].string = val;
129 self.do_save_dashboard();
131 $input.unbind('keydown').hide();
137 on_change_layout: function() {
140 current_layout : this.$element.find('.oe-dashboard').attr('data-layout')
142 var $dialog = $('<div>').dialog({
144 title: 'Edit Layout',
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);
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) {
165 $(v).find('.oe-dashboard-action').appendTo($last_column);
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();
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';
183 delete(this.actions_attrs[id].fold);
185 $e.toggleClass('ui-icon-minusthick ui-icon-plusthick');
186 $action.find('.oe-dashboard-action-content').toggle();
187 this.do_save_dashboard();
189 on_close_action: function(e) {
190 $(e.currentTarget).parents('.oe-dashboard-action:first').remove();
191 this.do_save_dashboard();
193 do_save_dashboard: function() {
196 form_title : this.view.fields_view.arch.attrs.string,
197 style : this.$element.find('.oe-dashboard').attr('data-layout'),
200 this.$element.find('.oe-dashboard-column').each(function() {
202 $(this).find('.oe-dashboard-action').each(function() {
203 var action_id = $(this).attr('data-id');
204 actions.push(self.actions_attrs[action_id]);
206 board.columns.push(actions);
208 var arch = QWeb.render('DashBoard.xml', board);
209 this.rpc('/web/view/add_custom', {
210 view_id: this.view.fields_view.view_id,
213 self.$element.find('.oe-dashboard-link-undo, .oe-dashboard-link-reset').show();
216 on_load_action: function(result) {
217 var action = result.result;
221 views_switcher : false,
222 action_buttons : false,
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);
233 // We should start with three columns available
234 for (var i = this.node.children.length; i < 3; i++) {
235 this.node.children.push({
241 return QWeb.render(this.template, this);
243 do_reload: function() {
244 _.each(this.action_managers, function(am) {
247 this.action_managers = [];
252 openerp.web.form.DashBoardLegacy = openerp.web.form.DashBoard.extend({
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';
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);
268 child.children = actions;
272 return this._super(this, arguments);
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');
280 openerp.web.client_actions.add(
281 'board.config.overview', 'openerp.web_dashboard.ConfigOverview'
283 if (!openerp.web_dashboard) {
285 openerp.web_dashboard = {};
287 openerp.web_dashboard.ConfigOverview = openerp.web.View.extend({
288 template: 'ConfigOverview',
289 init: function (parent) {
291 this.dataset = new openerp.web.DataSetSearch(
292 this, 'ir.actions.todo');
293 this.dataset.domain = [['type', '!=', 'automatic']];
297 $.when(this.dataset.read_slice(['state', 'action_id', 'category_id']),
298 this.dataset.call('progress'))
299 .then(this.on_records_loaded);
301 on_records_loaded: function (read_response, progress_response) {
302 var records = read_response,
303 progress = progress_response[0];
305 var grouped_todos = _(records).chain()
306 .map(function (record) {
309 name: record.action_id[1],
310 done: record.state !== 'open',
311 to_do: record.state === 'open',
312 category: record['category_id'][1] || "Uncategorized"
315 .groupBy(function (record) {return record.category})
317 this.$element.html(QWeb.render('ConfigOverview.content', {
318 completion: 100 * progress.done / progress.total,
319 groups: grouped_todos
321 var $progress = this.$element.find('div.oe-config-progress-bar');
322 $progress.progressbar({value: $progress.data('completion')});
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 () {
335 .delegate('li:not(.oe-done)', 'click', function () {
336 self.widget_parent.widget_parent.widget_parent.do_execute_action({
338 name: 'action_launch'
340 $(this).data('id'), function () {
341 // after action popup closed, refresh configuration
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',
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
361 while (applications.length) {
362 rows.push(applications.splice(0, 3));
366 'ApplicationTiles.content', {rows: rows}))
367 .find('.oe-dashboard-home-tile')
370 $this.closest('.openerp')
371 .find('.menu a[data-menu=' + $this.data('menuid') + ']')
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',
381 * Initializes a "HomeWidget" client widget: handles the display of a given
382 * res.widget objects in an OpenERP view (mainly a dashboard).
384 * @constructs openerp.web_dashboard.Widget
385 * @extends openerp.web.View
387 * @param {Object} parent
388 * @param {Object} options
389 * @param {Number} options.widget_id
391 init: function (parent, options) {
393 this.widget_id = options.widget_id;
397 return new openerp.web.DataSet(this, 'res.widget').read_ids(
398 [this.widget_id], ['title'], this.on_widget_loaded);
400 on_widget_loaded: function (widgets) {
401 var widget = widgets[0];
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', {