[FIX] Dashboard: merge domain & context of board action to loaded action
[odoo/odoo.git] / addons / web_dashboard / static / src / js / dashboard.js
index edee3e9..ce7e877 100644 (file)
@@ -1,6 +1,10 @@
 openerp.web_dashboard = function(openerp) {
 var QWeb = openerp.web.qweb;
-QWeb.add_template('/web_dashboard/static/src/xml/web_dashboard.xml');
+
+if (!openerp.web_dashboard) {
+    /** @namespace */
+    openerp.web_dashboard = {};
+}
 
 openerp.web.form.DashBoard = openerp.web.form.Widget.extend({
     init: function(view, node) {
@@ -20,9 +24,7 @@ openerp.web.form.DashBoard = openerp.web.form.Widget.extend({
         }).disableSelection().bind('sortstop', self.do_save_dashboard);
 
         // Events
-        this.$element.find('.oe-dashboard-link-undo').click(this.on_undo);
         this.$element.find('.oe-dashboard-link-reset').click(this.on_reset);
-        this.$element.find('.oe-dashboard-link-add_widget').click(this.on_add_widget);
         this.$element.find('.oe-dashboard-link-change_layout').click(this.on_change_layout);
 
         this.$element.delegate('.oe-dashboard-column .oe-dashboard-fold', 'click', this.on_fold_action);
@@ -30,24 +32,19 @@ openerp.web.form.DashBoard = openerp.web.form.Widget.extend({
 
         this.actions_attrs = {};
         // Init actions
-        _.each(this.node.children, function(column) {
-            _.each(column.children, function(action) {
+        _.each(this.node.children, function(column, column_index) {
+            _.each(column.children, function(action, action_index) {
                 delete(action.attrs.width);
                 delete(action.attrs.height);
                 delete(action.attrs.colspan);
                 self.actions_attrs[action.attrs.name] = action.attrs;
                 self.rpc('/web/action/load', {
                     action_id: parseInt(action.attrs.name, 10)
-                }, self.on_load_action);
+                }, function(result) {
+                    self.on_load_action(result, column_index + '_' + action_index);
+                });
             });
         });
-
-        //this.$element.find('a.oe-dashboard-action-rename').live('click', this.on_rename);
-    },
-    on_undo: function() {
-        this.rpc('/web/view/undo_custom', {
-            view_id: this.view.fields_view.view_id
-        }, this.do_reload);
     },
     on_reset: function() {
         this.rpc('/web/view/undo_custom', {
@@ -55,85 +52,6 @@ openerp.web.form.DashBoard = openerp.web.form.Widget.extend({
             reset: true
         }, this.do_reload);
     },
-    on_add_widget: function() {
-        var self = this;
-        var action_manager = new openerp.web.ActionManager(this);
-        var dialog = new openerp.web.Dialog(this, {
-            title : 'Actions',
-            width: 800,
-            height: 600,
-            buttons : {
-                Cancel : function() {
-                    $(this).dialog('destroy');
-                },
-                Add : function() {
-                    self.do_add_widget(action_manager.inner_viewmanager.views.list.controller);
-                    $(this).dialog('destroy');
-                }
-            }
-        }).start().open();
-        action_manager.appendTo(dialog.$element);
-        action_manager.do_action({
-            res_model : 'ir.actions.actions',
-            views : [[false, 'list']],
-            type : 'ir.actions.act_window',
-            limit : 80,
-            auto_search : true,
-            flags : {
-                sidebar : false,
-                views_switcher : false,
-                action_buttons : false
-            }
-        });
-        // TODO: should bind ListView#select_record in order to catch record clicking
-    },
-    do_add_widget : function(listview) {
-        var self = this,
-            actions = listview.groups.get_selection().ids,
-            results = [],
-            qdict = { view : this.view };
-        // TODO: should load multiple actions at once
-        _.each(actions, function(aid) {
-            self.rpc('/web/action/load', {
-                action_id: aid
-            }, function(result) {
-                self.actions_attrs[aid] = {
-                    name: aid,
-                    string: _.trim(result.result.name)
-                };
-                qdict.action = {
-                    attrs : self.actions_attrs[aid]
-                };
-                self.$element.find('.oe-dashboard-column:first').prepend(QWeb.render('DashBoard.action', qdict));
-                self.do_save_dashboard();
-                self.on_load_action(result)
-            });
-        });
-    },
-    on_rename : function(e) {
-        var self = this,
-            id = parseInt($(e.currentTarget).parents('.oe-dashboard-action:first').attr('data-id'), 10),
-            $header = $(e.currentTarget).parents('.oe-dashboard-action-header:first'),
-            $rename = $header.find('a.oe-dashboard-action-rename').hide(),
-            $title = $header.find('span.oe-dashboard-action-title').hide(),
-            $input = $header.find('input[name=title]');
-        $input.val($title.text()).show().focus().bind('keydown', function(e) {
-            if (e.which == 13 || e.which == 27) {
-                if (e.which == 13) { //enter
-                    var val = $input.val();
-                    if (!val) {
-                        return false;
-                    }
-                    $title.text(val);
-                    self.actions_attrs[id].string = val;
-                    self.do_save_dashboard();
-                }
-                $input.unbind('keydown').hide();
-                $rename.show();
-                $title.show();
-            }
-        });
-    },
     on_change_layout: function() {
         var self = this;
         var qdict = {
@@ -187,8 +105,10 @@ openerp.web.form.DashBoard = openerp.web.form.Widget.extend({
         this.do_save_dashboard();
     },
     on_close_action: function(e) {
-        $(e.currentTarget).parents('.oe-dashboard-action:first').remove();
-        this.do_save_dashboard();
+        if (confirm("Are you sure you want to remove this item ?")) {
+            $(e.currentTarget).parents('.oe-dashboard-action:first').remove();
+            this.do_save_dashboard();
+        }
     },
     do_save_dashboard: function() {
         var self = this;
@@ -200,8 +120,17 @@ openerp.web.form.DashBoard = openerp.web.form.Widget.extend({
         this.$element.find('.oe-dashboard-column').each(function() {
             var actions = [];
             $(this).find('.oe-dashboard-action').each(function() {
-                var action_id = $(this).attr('data-id');
-                actions.push(self.actions_attrs[action_id]);
+                var action_id = $(this).attr('data-id'),
+                    new_attrs = _.clone(self.actions_attrs[action_id]);
+                if (new_attrs.domain) {
+                    new_attrs.domain = new_attrs.domain_string;
+                    delete(new_attrs.domain_string);
+                }
+                if (new_attrs.context) {
+                    new_attrs.context = new_attrs.context_string;
+                    delete(new_attrs.context_string);
+                }
+                actions.push(new_attrs);
             });
             board.columns.push(actions);
         });
@@ -210,11 +139,38 @@ openerp.web.form.DashBoard = openerp.web.form.Widget.extend({
             view_id: this.view.fields_view.view_id,
             arch: arch
         }, function() {
-            self.$element.find('.oe-dashboard-link-undo, .oe-dashboard-link-reset').show();
+            self.$element.find('.oe-dashboard-link-reset').show();
         });
     },
-    on_load_action: function(result) {
-        var action = result.result;
+    on_load_action: function(result, index) {
+        var self = this,
+            action = result.result,
+            action_attrs = this.actions_attrs[action.id],
+            view_mode = action_attrs.view_mode;
+
+        if (action_attrs.context) {
+            action.context = _.extend((action.context || {}), action_attrs.context);
+        }
+        if (action_attrs.domain) {
+            action.domain = action.domain || [];
+            action.domain.unshift.apply(action.domain, action_attrs.domain);
+        }
+        var action_orig = _.extend({ flags : {} }, action);
+
+        if (view_mode && view_mode != action.view_mode) {
+            var action_view_mode = action.view_mode.split(',');
+            action.views = _.map(view_mode.split(','), function(mode) {
+                if (_.indexOf(action_view_mode, mode) < 0) {
+                    return [false, mode == 'tree' ? 'list': mode];
+                } else {
+                    mode = mode === 'tree' ? 'list' : mode;
+                    return _.find(action.views, function(view) {
+                        return view[1] == mode;
+                    });
+                }
+            });
+        }
+
         action.flags = {
             search_view : false,
             sidebar : false,
@@ -222,12 +178,51 @@ openerp.web.form.DashBoard = openerp.web.form.Widget.extend({
             action_buttons : false,
             pager: false,
             low_profile: true,
-            display_title: false
+            display_title: false,
+            list: {
+                selectable: false
+            }
         };
-        var am = new openerp.web.ActionManager(this);
+        var am = new openerp.web.ActionManager(this),
+            // FIXME: ideally the dashboard view shall be refactored like kanban.
+            $action = $('#' + this.view.element_id + '_action_' + index);
         this.action_managers.push(am);
-        am.appendTo($("#"+this.view.element_id + '_action_' + action.id));
+        am.appendTo($action);
         am.do_action(action);
+        am.do_action = function(action) {
+            self.do_action(action);
+        }
+        if (action_attrs.creatable && action_attrs.creatable !== 'false') {
+            var action_id = parseInt(action_attrs.creatable, 10);
+            $action.parent().find('button.oe_dashboard_button_create').click(function() {
+                if (isNaN(action_id)) {
+                    action_orig.flags.default_view = 'form';
+                    self.do_action(action_orig);
+                } else {
+                    self.rpc('/web/action/load', {
+                        action_id: action_id
+                    }, function(result) {
+                        result.result.flags = result.result.flags || {};
+                        result.result.flags.default_view = 'form';
+                        self.do_action(result.result);
+                    });
+                }
+            });
+        }
+        if (am.inner_viewmanager) {
+            am.inner_viewmanager.on_mode_switch.add(function(mode) {
+                var new_views = [];
+                _.each(action_orig.views, function(view) {
+                    new_views[view[1] === mode ? 'unshift' : 'push'](view);
+                });
+                if (!new_views.length || new_views[0][1] !== mode) {
+                    new_views.unshift([false, mode]);
+                }
+                action_orig.views = new_views;
+                action_orig.res_id = am.inner_viewmanager.dataset.ids[am.inner_viewmanager.dataset.index];
+                self.do_action(action_orig);
+            });
+        }
     },
     render: function() {
         // We should start with three columns available
@@ -241,12 +236,10 @@ openerp.web.form.DashBoard = openerp.web.form.Widget.extend({
         return QWeb.render(this.template, this);
     },
     do_reload: function() {
-        _.each(this.action_managers, function(am) {
-            am.stop();
-        });
-        this.action_managers = [];
+        var view_manager = this.view.widget_parent,
+            action_manager = view_manager.widget_parent;
         this.view.stop();
-        this.view.start();
+        action_manager.do_action(view_manager.action);
     }
 });
 openerp.web.form.DashBoardLegacy = openerp.web.form.DashBoard.extend({
@@ -277,31 +270,39 @@ openerp.web.form.widgets.add('hpaned', 'openerp.web.form.DashBoardLegacy');
 openerp.web.form.widgets.add('vpaned', 'openerp.web.form.DashBoardLegacy');
 openerp.web.form.widgets.add('board', 'openerp.web.form.DashBoard');
 
-openerp.web.client_actions.add(
-    'board.config.overview', 'openerp.web_dashboard.ConfigOverview'
-);
-if (!openerp.web_dashboard) {
-    /** @namespace */
-    openerp.web_dashboard = {};
-}
+/*
+ * ConfigOverview
+ * This client action designed to be used as a dashboard widget display
+ * ir.actions.todo in a fancy way
+ */
+openerp.web.client_actions.add( 'board.config.overview', 'openerp.web_dashboard.ConfigOverview');
 openerp.web_dashboard.ConfigOverview = openerp.web.View.extend({
     template: 'ConfigOverview',
     init: function (parent) {
         this._super(parent);
-        this.dataset = new openerp.web.DataSetSearch(
-                this, 'ir.actions.todo');
-        this.dataset.domain = [['type', '=', 'manual']];
+        this.user = _.extend(new openerp.web.DataSet(this, 'res.users'), {
+            index: 0,
+            ids: [this.session.uid]
+        });
+        this.dataset = new openerp.web.DataSetSearch(this, 'ir.actions.todo');
     },
     start: function () {
         this._super();
-        $.when(this.dataset.read_slice(['state', 'action_id', 'category_id']),
-               this.dataset.call('progress'))
-            .then(this.on_records_loaded);
-    },
-    on_records_loaded: function (read_response, progress_response) {
-        var records = read_response,
-           progress = progress_response[0];
+        var self = this;
+        return this.user.read_index(['groups_id']).pipe(function (record) {
+            var todos_filter = [
+                ['type', '!=', 'automatic'],
+                '|', ['groups_id', '=', false],
+                     ['groups_id', 'in', record['groups_id']]];
+            return $.when(
+                self.dataset.read_slice(['state', 'action_id', 'category_id'],{
+                        domain: todos_filter }),
+                self.dataset.call('progress').pipe(
+                        function (arg) { return arg; }, null))
+        }, null).then(this.on_records_loaded);
 
+    },
+    on_records_loaded: function (records, progress) {
         var grouped_todos = _(records).chain()
             .map(function (record) {
                 return {
@@ -346,35 +347,12 @@ openerp.web_dashboard.ConfigOverview = openerp.web.View.extend({
     }
 });
 
-openerp.web.client_actions.add(
-    'board.home.applications', 'openerp.web_dashboard.ApplicationTiles');
-openerp.web_dashboard.ApplicationTiles = openerp.web.View.extend({
-    template: 'ApplicationTiles',
-    start: function () {
-        this._super();
-        var self = this;
-        return new openerp.web.DataSetSearch(
-                this, 'ir.ui.menu', null, [['parent_id', '=', false]])
-            .read_slice( ['name', 'web_icon_data', 'web_icon_hover_data'], {}, function (applications) {
-                // Create a matrix of 3*x applications
-                var rows = [];
-                while (applications.length) {
-                    rows.push(applications.splice(0, 3));
-                }
-                self.$element
-                    .append(QWeb.render(
-                        'ApplicationTiles.content', {rows: rows}))
-                    .find('.oe-dashboard-home-tile')
-                        .click(function () {
-                            var $this = $(this);
-                            $this.closest('.openerp')
-                                 .find('.menu a[data-menu=' + $this.data('menuid') + ']')
-                                 .click();});
-            });
-    }
-});
-openerp.web.client_actions.add(
-    'board.home.widgets', 'openerp.web_dashboard.Widget');
+/*
+ * Widgets
+ * This client action designed to be used as a dashboard widget display
+ * the html content of a res_widget given as argument
+ */
+openerp.web.client_actions.add( 'board.home.widgets', 'openerp.web_dashboard.Widget');
 openerp.web_dashboard.Widget = openerp.web.View.extend(/** @lends openerp.web_dashboard.Widgets# */{
     template: 'HomeWidget',
     /**
@@ -399,7 +377,7 @@ openerp.web_dashboard.Widget = openerp.web.View.extend(/** @lends openerp.web_da
     },
     on_widget_loaded: function (widgets) {
         var widget = widgets[0];
-        var url = _.sprintf(
+        var url = _.str.sprintf(
             '/web_dashboard/widgets/content?session_id=%s&widget_id=%d',
             this.session.session_id, widget.id);
         this.$element.html(QWeb.render('HomeWidget.content', {
@@ -408,4 +386,85 @@ openerp.web_dashboard.Widget = openerp.web.View.extend(/** @lends openerp.web_da
         }));
     }
 });
+
+/*
+ * HomeTiles this client action display either the list of application to
+ * install (if none is installed yet) or a list of root menu items
+ */
+openerp.web.client_actions.add('default_home', 'session.web_dashboard.ApplicationTiles');
+openerp.web_dashboard.ApplicationTiles = openerp.web.Widget.extend({
+    template: 'web_dashboard.ApplicationTiles',
+    init: function(parent) {
+        this._super(parent);
+    },
+    start: function() {
+        var self = this;
+        openerp.webclient.menu.do_hide_secondary();
+        var domain = [['application','=',true], ['state','=','installed'], ['name', '!=', 'base']];
+        var ds = new openerp.web.DataSetSearch(this, 'ir.module.module',{},domain);
+        ds.read_slice(['id'], {}, function(result) {
+            if(result.length) {
+                self.on_installed_database();
+            } else {
+                self.on_uninstalled_database();
+            }
+        });
+    },
+    on_uninstalled_database: function() {
+        installer = new openerp.web_dashboard.ApplicationInstaller(this);
+        installer.appendTo(this.$element);
+    },
+    on_installed_database: function() {
+        var self = this;
+        var ds = new openerp.web.DataSetSearch(this, 'ir.ui.menu', null, [['parent_id', '=', false]]);
+        var r = ds.read_slice( ['name', 'web_icon_data', 'web_icon_hover_data', 'module'], {}, function (applications) {
+            //// Create a matrix of 3*x applications
+            //var rows = [];
+            //while (applications.length) {
+            //    rows.push(applications.splice(0, 3));
+            //}
+            //var tiles = QWeb.render('ApplicationTiles.content', {rows: rows});
+            var tiles = QWeb.render('ApplicationTiles.content', {applications: applications});
+            $(tiles).appendTo(self.$element).find('.oe_install-module-link').click(function () {
+                openerp.webclient.menu.on_menu_click(null, $(this).data('menu'))
+            });
+        });
+    }
+});
+
+/**
+ * ApplicationInstaller
+ * This client action  display a list of applications to install.
+ */
+openerp.web.client_actions.add( 'board.application.installer', 'openerp.web_dashboard.ApplicationInstaller');
+openerp.web_dashboard.ApplicationInstaller = openerp.web.Widget.extend({
+    template: 'web_dashboard.ApplicationInstaller',
+    start: function () {
+        // TODO menu hide
+        var r = this._super();
+        this.action_manager = new openerp.web.ActionManager(this);
+        this.action_manager.appendTo(this.$element.find('.oe_installer'));
+        this.action_manager.do_action({
+            type: 'ir.actions.act_window',
+            res_model: 'ir.module.module',
+            domain: [['application','=',true]],
+            views: [[false, 'kanban']],
+            flags: {
+                display_title:false,
+                search_view: false,
+                views_switcher: false,
+                action_buttons: false,
+                sidebar: false,
+                pager: false
+            }
+        });
+        return r;
+    },
+    stop: function() {
+        this.action_manager.stop();
+        return this._super();
+    }
+});
+
+
 };