[IMP] change cursor when loading
[odoo/odoo.git] / addons / web / static / src / js / chrome.js
index cfe4b02..bdb8e23 100644 (file)
@@ -1,20 +1,20 @@
 /*---------------------------------------------------------
  * OpenERP Web chrome
  *---------------------------------------------------------*/
-
 openerp.web.chrome = function(openerp) {
 var QWeb = openerp.web.qweb;
 
 openerp.web.Notification =  openerp.web.Widget.extend(/** @lends openerp.web.Notification# */{
-    /**
-     * @constructs openerp.web.Notification
-     * @extends openerp.web.Widget
-     *
-     * @param parent
-     * @param element_id
-     */
-    init: function(parent, element_id) {
-        this._super(parent, element_id);
+    template: 'Notification',
+    identifier_prefix: 'notification-',
+
+    init: function() {
+        this._super.apply(this, arguments);
+        openerp.notification = this;
+    },
+
+    start: function() {
+        this._super.apply(this, arguments);
         this.$element.notify({
             speed: 500,
             expires: 1500
@@ -29,9 +29,12 @@ openerp.web.Notification =  openerp.web.Widget.extend(/** @lends openerp.web.Not
     warn: function(title, text) {
         this.$element.notify('create', 'oe_notification_alert', {
             title: title,
-            text: text
+            text: text,
+        }, {
+            expires: false,
         });
-    }
+    },
+
 });
 
 openerp.web.Dialog = openerp.web.OldWidget.extend(/** @lends openerp.web.Dialog# */{
@@ -129,24 +132,15 @@ openerp.web.Dialog = openerp.web.OldWidget.extend(/** @lends openerp.web.Dialog#
         // Destroy widget
         this.close();
         this.$dialog.dialog('destroy');
+        this._super();
     }
 });
 
-openerp.web.CrashManager = openerp.web.Dialog.extend(/** @lends openerp.web.CrashManager# */{
-    identifier_prefix: 'dialog_crash',
-    /**
-     * @constructs opener.web.CrashManager
-     * @extends openerp.web.Dialog
-     *
-     * @param parent
-     */
+openerp.web.CrashManager = openerp.web.SessionAware.extend({
     init: function(parent) {
-        this._super(parent);
+        this._super((parent || {}).session);
         this.session.on_rpc_error.add(this.on_rpc_error);
     },
-    on_button_Ok: function() {
-        this.close();
-    },
     on_rpc_error: function(error) {
         this.error = error;
         if (error.data.fault_code) {
@@ -157,20 +151,36 @@ openerp.web.CrashManager = openerp.web.Dialog.extend(/** @lends openerp.web.Cras
             }
         }
         if (error.code === 200 && error.type) {
-            this.dialog_title = "OpenERP " + _.capitalize(error.type);
-            this.template = 'DialogWarning';
-            this.open({
-                width: 'auto',
-                height: 'auto'
-            });
+            this.on_managed_error(error);
         } else {
-            this.dialog_title = "OpenERP Error";
-            this.template = 'DialogTraceback';
-            this.open({
-                width: 'auto',
-                height: 'auto'
-            });
+            this.on_traceback(error);
         }
+    },
+    on_managed_error: function(error) {
+        $('<div>' + QWeb.render('DialogWarning', {error: error}) + '</div>').dialog({
+            title: "OpenERP " + _.capitalize(error.type),
+            buttons: {
+                Ok: function() {
+                    $(this).dialog("close");
+                }
+            }
+        });
+    },
+    on_traceback: function(error) {
+        var dialog = new openerp.web.Dialog(this, {
+            title: "OpenERP " + _.capitalize(error.type),
+            autoOpen: true,
+            width: '90%',
+            height: '90%',
+            min_width: '800px',
+            min_height: '600px',
+            buttons: {
+                Ok: function() {
+                    $(this).dialog("close");
+                }
+            }
+        }).start();
+        dialog.$element.html(QWeb.render('DialogTraceback', {error: error}));
     }
 });
 
@@ -194,8 +204,10 @@ openerp.web.Loading =  openerp.web.Widget.extend(/** @lends openerp.web.Loading#
             //this.$element.html(QWeb.render("Loading", {}));
             this.$element.html("Loading ("+this.count+")");
             this.$element.show();
+            this.widget_parent.$element.addClass('loading');
         } else {
             this.$element.fadeOut();
+            this.widget_parent.$element.removeClass('loading');
         }
     }
 });
@@ -218,9 +230,8 @@ openerp.web.Database = openerp.web.Widget.extend(/** @lends openerp.web.Database
         this.$element.closest(".openerp")
                 .removeClass("login-mode")
                 .addClass("database_block");
-        
+
         var self = this;
-        
         var fetch_db = this.rpc("/web/database/get_list", {}, function(result) {
             self.db_list = result.db_list;
         });
@@ -232,7 +243,7 @@ openerp.web.Database = openerp.web.Widget.extend(/** @lends openerp.web.Database
             self.lang_list = result.lang_list;
         });
         $.when(fetch_db, fetch_langs).then(function () {self.do_create();});
-        
+
         this.$element.find('#db-create').click(this.do_create);
         this.$element.find('#db-drop').click(this.do_drop);
         this.$element.find('#db-backup').click(this.do_backup);
@@ -254,7 +265,7 @@ openerp.web.Database = openerp.web.Widget.extend(/** @lends openerp.web.Database
                 .removeClass("database_block")
             .end()
             .empty();
-
+        this._super();
     },
     /**
      * Converts a .serializeArray() result into a dict. Does not bother folding
@@ -300,9 +311,9 @@ openerp.web.Database = openerp.web.Widget.extend(/** @lends openerp.web.Database
 
             var admin = result[1][0];
             setTimeout(function () {
-                self.stop();
                 self.widget_parent.do_login(
                         info.db, admin.login, admin.password);
+                self.stop();
                 $.unblockUI();
             });
         });
@@ -370,7 +381,7 @@ openerp.web.Database = openerp.web.Widget.extend(/** @lends openerp.web.Database
                     }
                     $db_list.find(':selected').remove();
                     self.db_list.splice(_.indexOf(self.db_list, db, true), 1);
-                    self.notification.notify("Dropping database", "The database '" + db + "' has been dropped");
+                    self.do_notify("Dropping database", "The database '" + db + "' has been dropped");
                 });
             }
         });
@@ -399,7 +410,7 @@ openerp.web.Database = openerp.web.Widget.extend(/** @lends openerp.web.Database
     do_restore: function() {
         var self = this;
                self.$option_id.html(QWeb.render("RestoreDB", self));
-               
+
                self.$option_id.find("form[name=restore_db_form]").validate({
             submitHandler: function (form) {
                 $.blockUI({message:'<img src="/web/static/src/img/throbber2.gif">'});
@@ -455,7 +466,7 @@ openerp.web.Database = openerp.web.Widget.extend(/** @lends openerp.web.Database
                         self.display_error(result);
                         return;
                     }
-                    self.notification.notify("Changed Password", "Password has been changed successfully");
+                    self.do_notify("Changed Password", "Password has been changed successfully");
                 });
             }
         });
@@ -463,41 +474,43 @@ openerp.web.Database = openerp.web.Widget.extend(/** @lends openerp.web.Database
 });
 
 openerp.web.Login =  openerp.web.Widget.extend(/** @lends openerp.web.Login# */{
-    remember_creditentials: true,
+    remember_credentials: true,
+    
+    template: "Login",
+    identifier_prefix: 'oe-app-login-',
     /**
      * @constructs openerp.web.Login
      * @extends openerp.web.Widget
-     * 
+     *
      * @param parent
      * @param element_id
      */
-    init: function(parent, element_id) {
-        this._super(parent, element_id);
+
+    init: function(parent) {
+        this._super(parent);
         this.has_local_storage = typeof(localStorage) != 'undefined';
         this.selected_db = null;
         this.selected_login = null;
 
-        if (this.has_local_storage && this.remember_creditentials) {
+        if (this.has_local_storage && this.remember_credentials) {
             this.selected_db = localStorage.getItem('last_db_login_success');
             this.selected_login = localStorage.getItem('last_login_login_success');
             if (jQuery.deparam(jQuery.param.querystring()).debug != undefined) {
                 this.selected_password = localStorage.getItem('last_password_login_success');
             }
         }
+        
+        var qs = jQuery.deparam(jQuery.param.querystring());
+        if (qs.db) {
+            this.selected_db = qs.db;
+        }
+        if (qs.login) {
+            this.selected_login = qs.login;
+        }
+
     },
     start: function() {
         var self = this;
-        this.rpc("/web/database/get_list", {}, function(result) {
-            self.db_list = result.db_list;
-            self.display();
-        }, function() {
-            self.display();
-        });
-    },
-    display: function() {
-        var self = this;
-        
-        this.$element.html(QWeb.render("Login", this));
         this.database = new openerp.web.Database(
                 this, "oe_database", "oe_db_options");
 
@@ -506,6 +519,17 @@ openerp.web.Login =  openerp.web.Widget.extend(/** @lends openerp.web.Login# */{
         });
 
         this.$element.find("form").submit(this.on_submit);
+
+        this.rpc("/web/database/get_list", {}, function(result) {
+            var tpl = openerp.web.qweb.render('Login_dblist', {db_list: result.db_list, selected_db: self.selected_db});
+            self.$element.find("input[name=db]").replaceWith(tpl)
+        }, 
+        function(error, event) {
+            if (error.data.fault_code === 'AccessDenied') {
+                event.preventDefault();
+            }
+        });
+
     },
     on_login_invalid: function() {
         this.$element.closest(".openerp").addClass("login-mode");
@@ -534,7 +558,7 @@ openerp.web.Login =  openerp.web.Widget.extend(/** @lends openerp.web.Login# */{
         this.session.session_login(db, login, password, function() {
             if(self.session.session_is_valid()) {
                 if (self.has_local_storage) {
-                    if(self.remember_creditentials) {
+                    if(self.remember_credentials) {
                         localStorage.setItem('last_db_login_success', db);
                         localStorage.setItem('last_login_login_success', login);
                         if (jQuery.deparam(jQuery.param.querystring()).debug != undefined) {
@@ -574,14 +598,13 @@ openerp.web.Header =  openerp.web.Widget.extend(/** @lends openerp.web.Header# *
     /**
      * @constructs openerp.web.Header
      * @extends openerp.web.Widget
-     * 
+     *
      * @param parent
      */
     init: function(parent) {
         this._super(parent);
         this.qs = "?" + jQuery.param.querystring();
         this.$content = $();
-        console.debug("initializing header with id", this.element_id);
         this.update_promise = $.Deferred().resolve();
     },
     start: function() {
@@ -666,7 +689,7 @@ openerp.web.Header =  openerp.web.Widget.extend(/** @lends openerp.web.Header# *
                 });
         });
     },
-    
+
     on_action: function(action) {
     },
     on_preferences: function(){
@@ -702,10 +725,11 @@ openerp.web.Header =  openerp.web.Widget.extend(/** @lends openerp.web.Header# *
             },
                 Save: function(){
                     var inner_viewmanager = action_manager.inner_viewmanager;
-                    inner_viewmanager.views[inner_viewmanager.active_view].controller.do_save(function(){
-                        inner_viewmanager.start();
+                    inner_viewmanager.views[inner_viewmanager.active_view].controller.do_save()
+                    .then(function() {
+                        self.dialog.stop();
+                        window.location.reload();
                     });
-                    $(this).dialog('destroy')
                 }
             }
         });
@@ -713,7 +737,7 @@ openerp.web.Header =  openerp.web.Widget.extend(/** @lends openerp.web.Header# *
        action_manager.appendTo(this.dialog);
        action_manager.render(this.dialog);
     },
-    
+
     change_password :function() {
         var self = this;
         this.dialog = new openerp.web.Dialog(this,{
@@ -728,21 +752,13 @@ openerp.web.Header =  openerp.web.Widget.extend(/** @lends openerp.web.Header# *
             submitHandler: function (form) {
                 self.rpc("/web/session/change_password",{
                     'fields': $(form).serializeArray()
-                        }, function(result) {
-                         if (result.error) {
-                            self.display_error(result);
+                }, function(result) {
+                    if (result.error) {
+                        self.display_error(result);
                         return;
-                        }
-                        else {
-                            if (result.new_password) {
-                                self.session.password = result.new_password;
-                                var session = new openerp.web.Session(self.session.server, self.session.port);
-                                session.start();
-                                session.session_login(self.session.db, self.session.login, self.session.password)
-                            }
-                        }
-                    self.notification.notify("Changed Password", "Password has been changed successfully");
-                    self.dialog.close();
+                    } else {
+                        self.session.logout();
+                    }
                 });
             }
         });
@@ -766,7 +782,7 @@ openerp.web.Menu =  openerp.web.Widget.extend(/** @lends openerp.web.Menu# */{
     /**
      * @constructs openerp.web.Menu
      * @extends openerp.web.Widget
-     * 
+     *
      * @param parent
      * @param element_id
      * @param secondary_menu_id
@@ -777,8 +793,15 @@ openerp.web.Menu =  openerp.web.Widget.extend(/** @lends openerp.web.Menu# */{
         this.$secondary_menu = $("#" + secondary_menu_id).hide();
         this.menu = false;
         this.folded = false;
+        if (window.localStorage) {
+            this.folded = localStorage.getItem('oe_menu_folded') === 'true';
+        }
+        this.float_timeout = 700;
     },
     start: function() {
+        this.$secondary_menu.addClass(this.folded ? 'oe_folded' : 'oe_unfolded');
+    },
+    do_reload: function() {
         this.rpc("/web/menu/load", {}, this.on_loaded);
     },
     on_loaded: function(data) {
@@ -789,60 +812,114 @@ openerp.web.Menu =  openerp.web.Widget.extend(/** @lends openerp.web.Menu# */{
         this.$secondary_menu.find('.oe_toggle_secondary_menu').click(this.on_toggle_fold);
     },
     on_toggle_fold: function() {
+        this.$secondary_menu.toggleClass('oe_folded').toggleClass('oe_unfolded');
         if (this.folded) {
             this.$secondary_menu.find('.oe_secondary_menu.active').show();
         } else {
             this.$secondary_menu.find('.oe_secondary_menu').hide();
         }
         this.folded = !this.folded;
+        if (window.localStorage) {
+            localStorage.setItem('oe_menu_folded', this.folded.toString());
+        }
     },
     on_menu_click: function(ev, id) {
         id = id || 0;
-        var $menu, $parent, $secondary;
+        var $clicked_menu, manual = false;
 
         if (id) {
             // We can manually activate a menu with it's id (for hash url mapping)
-            $menu = this.$element.find('a[data-menu=' + id + ']');
-            if (!$menu.length) {
-                $menu = this.$secondary_menu.find('a[data-menu=' + id + ']');
+            manual = true;
+            $clicked_menu = this.$element.find('a[data-menu=' + id + ']');
+            if (!$clicked_menu.length) {
+                $clicked_menu = this.$secondary_menu.find('a[data-menu=' + id + ']');
             }
         } else {
-            $menu = $(ev.currentTarget);
-            id = $menu.data('menu');
+            $clicked_menu = $(ev.currentTarget);
+            id = $clicked_menu.data('menu');
         }
-        if (this.$secondary_menu.has($menu).length) {
-            $secondary = $menu.parents('.oe_secondary_menu');
-            $parent = this.$element.find('a[data-menu=' + $secondary.data('menu-parent') + ']');
+
+        if (this.do_menu_click($clicked_menu, manual) && id) {
+            this.session.active_id = id;
+            this.rpc('/web/menu/action', {'menu_id': id}, this.on_menu_action_loaded);
+        }
+        if (ev) {
+            ev.stopPropagation();
+        }
+        return false;
+    },
+    do_menu_click: function($clicked_menu, manual) {
+        var $sub_menu, $main_menu,
+            active = $clicked_menu.is('.active'),
+            sub_menu_visible = false;
+
+        if (this.$secondary_menu.has($clicked_menu).length) {
+            $sub_menu = $clicked_menu.parents('.oe_secondary_menu');
+            $main_menu = this.$element.find('a[data-menu=' + $sub_menu.data('menu-parent') + ']');
         } else {
-            $parent = $menu;
-            $secondary = this.$secondary_menu.find('.oe_secondary_menu[data-menu-parent=' + $menu.attr('data-menu') + ']');
+            $sub_menu = this.$secondary_menu.find('.oe_secondary_menu[data-menu-parent=' + $clicked_menu.attr('data-menu') + ']');
+            $main_menu = $clicked_menu;
         }
 
-        this.$secondary_menu.find('.oe_secondary_menu').hide().removeClass('active');
-        $secondary.show();
+        sub_menu_visible = $sub_menu.is(':visible');
+        this.$secondary_menu.find('.oe_secondary_menu').hide();
 
-        if (id) {
-            this.session.active_id = id;
-            this.rpc('/web/menu/action', {'menu_id': id},
-                    this.on_menu_action_loaded);
+        $('.active', this.$element.add(this.$secondary_menu.show())).removeClass('active');
+        $main_menu.add($clicked_menu).add($sub_menu).addClass('active');
+
+        if (!(this.folded && manual)) {
+            this.do_show_secondary($sub_menu, $main_menu);
         }
 
-        $('.active', this.$element.add(this.$secondary_menu.show())).removeClass('active');
-        $parent.addClass('active');
-        $menu.addClass('active');
-        $secondary.addClass('active');
-
-        if (this.$secondary_menu.has($menu).length) {
-            if ($menu.is('.submenu')) {
-                //this.$secondary_menu.find('.submenu').removeClass('opened').next().hide();
-                $menu.toggleClass('opened').next().toggle();
-                return false;
+        if ($main_menu != $clicked_menu) {
+            if ($clicked_menu.is('.submenu')) {
+                $sub_menu.find('.submenu.opened').each(function() {
+                    if (!$(this).next().has($clicked_menu).length && !$(this).is($clicked_menu)) {
+                        $(this).removeClass('opened').next().hide();
+                    }
+                });
+                $clicked_menu.toggleClass('opened').next().toggle();
+            } else if ($clicked_menu.is('.leaf')) {
+                $sub_menu.toggle(!this.folded);
+                return true;
+            }
+        } else if (this.folded) {
+            if (active && sub_menu_visible) {
+                $sub_menu.hide();
+                return true;
             }
-            return !$menu.is(".leaf");
+            return manual;
         } else {
-            $secondary.find('.oe_secondary_submenu').hide().eq(0).show();
-            return false;
+            return true;
         }
+        return false;
+    },
+    do_show_secondary: function($sub_menu, $main_menu) {
+        var self = this;
+        if (this.folded) {
+            var css = $main_menu.position(),
+                fold_width = this.$secondary_menu.width() + 2,
+                window_width = $(window).width();
+            css.top += 33;
+            css.left -= Math.round(($sub_menu.width() - $main_menu.width()) / 2);
+            css.left = css.left < fold_width ? fold_width : css.left;
+            if ((css.left + $sub_menu.width()) > window_width) {
+                delete(css.left);
+                css.right = 1;
+            }
+            $sub_menu.css(css);
+            $sub_menu.mouseenter(function() {
+                clearTimeout($sub_menu.data('timeoutId'));
+            }).mouseleave(function(evt) {
+                var timeoutId = setTimeout(function() {
+                    if (self.folded) {
+                        $sub_menu.hide();
+                    }
+                }, self.float_timeout);
+                $sub_menu.data('timeoutId', timeoutId);
+            });
+        }
+        $sub_menu.show();
     },
     on_menu_action_loaded: function(data) {
         var self = this;
@@ -859,7 +936,7 @@ openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClie
     /**
      * @constructs openerp.web.WebClient
      * @extends openerp.web.Widget
-     * 
+     *
      * @param element_id
      */
     init: function(element_id) {
@@ -873,16 +950,13 @@ openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClie
         }
         this.$element.html(QWeb.render("Interface", params));
 
+        this.notification = new openerp.web.Notification();
         this.session = new openerp.web.Session();
         this.loading = new openerp.web.Loading(this,"oe_loading");
         this.crashmanager =  new openerp.web.CrashManager(this);
-        this.crashmanager.start();
-
-        // Do you autorize this ? will be replaced by notify() in controller
-        openerp.web.Widget.prototype.notification = new openerp.web.Notification(this, "oe_notification");
 
         this.header = new openerp.web.Header(this);
-        this.login = new openerp.web.Login(this, "oe_login");
+        this.login = new openerp.web.Login(this);
         this.header.on_logout.add(this.login.on_logout);
         this.header.on_action.add(this.on_menu_action);
 
@@ -900,13 +974,27 @@ openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClie
 
     },
     start: function() {
+        this._super.apply(this, arguments);
+        this.notification.prependTo(this.$element);
         this.header.appendTo($("#oe_header"));
         this.session.start();
-        this.login.start();
+        this.login.appendTo($('#oe_login'));
         this.menu.start();
-        console.debug("The openerp client has been initialized.");
+    },
+    do_reload: function() {
+        this.session.session_restore();
+        this.menu.do_reload();
+    },
+    do_notify: function() {
+        var n = this.notification;
+        n.notify.apply(n, arguments);
+    },
+    do_warn: function() {
+        var n = this.notification;
+        n.warn.apply(n, arguments);
     },
     on_logged: function() {
+        this.menu.do_reload();
         if(this.action_manager)
             this.action_manager.stop();
         this.action_manager = new openerp.web.ActionManager(this);
@@ -950,7 +1038,7 @@ openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClie
             self.execute_home_action(home_action[0], ds);
         })
     },
-    default_home: function () { 
+    default_home: function () {
     },
     /**
      * Bundles the execution of the home action
@@ -975,7 +1063,6 @@ openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClie
     },
     do_url_set_hash: function(url) {
         if(!this.url_external_hashchange) {
-            console.log("url set #hash to",url);
             this.url_internal_hashchange = true;
             jQuery.bbq.pushState(url);
         }
@@ -983,10 +1070,8 @@ openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClie
     on_url_hashchange: function() {
         if(this.url_internal_hashchange) {
             this.url_internal_hashchange = false;
-            console.log("url jump to FLAG OFF");
         } else {
             var url = jQuery.deparam.fragment();
-            console.log("url jump to",url);
             this.url_external_hashchange = true;
             this.action_manager.on_url_hashchange(url);
             this.url_external_hashchange = false;
@@ -996,7 +1081,8 @@ openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClie
         this.action_manager.do_action(action);
     },
     do_about: function() {
-    }
+    },
+
 });
 
 };