From 4ee3f42e0ef39cd0bc37c13d850d9bce8dc21ba4 Mon Sep 17 00:00:00 2001 From: Antony Lesuisse Date: Mon, 30 Apr 2012 01:09:26 +0200 Subject: [PATCH] menu trigger leaf and cleanups bzr revid: al@openerp.com-20120429230926-n0y087us3xgleguq --- addons/web/static/src/js/chrome.js | 107 +++++++++++++++-------------- 1 file changed, 55 insertions(+), 52 deletions(-) diff --git a/addons/web/static/src/js/chrome.js b/addons/web/static/src/js/chrome.js index 542f3aa0985..fde173df9ae 100644 --- a/addons/web/static/src/js/chrome.js +++ b/addons/web/static/src/js/chrome.js @@ -544,34 +544,33 @@ instance.web.Menu = instance.web.Widget.extend({ this._super.apply(this, arguments); this.has_been_loaded = $.Deferred(); this.maximum_visible_links = 'auto'; // # of menu to show. 0 = do not crop, 'auto' = algo + this.data = {data:{children:[]}}; }, start: function() { this._super.apply(this, arguments); this.$secondary_menus = this.getParent().$element.find('.oe_secondary_menus_container'); - this.$secondary_menus.on('click', 'a[data-menu]', this.on_menu_click); - $('html').bind('click', this.do_hide_more); + return this.do_reload(); }, do_reload: function() { - var self = this; - return this.rpc("/web/menu/load", {}, this.on_loaded).then(function () { - if (self.current_menu) { - self.open_menu(self.current_menu); - } - }); + return this.rpc("/web/menu/load", {}).then(this.on_loaded); }, on_loaded: function(data) { var self = this; this.data = data; this.renderElement(); this.limit_entries(); - this.$element.on('click', 'a[data-menu]', this.on_menu_click); + this.$secondary_menus.html(QWeb.render("Menu.secondary", { widget : this })); this.$element.on('click', 'a.oe_menu_more_link', function() { self.$element.find('.oe_menu_more').toggle(); return false; }); - this.$secondary_menus.html(QWeb.render("Menu.secondary", { widget : this })); + this.$element.on('click', 'a[data-menu]', this.on_menu_click); + this.$secondary_menus.on('click', 'a[data-menu]', this.on_menu_click); // Hide second level submenus this.$secondary_menus.find('.oe_menu_toggler').siblings('.oe_secondary_submenu').hide(); + if (self.current_menu) { + self.open_menu(self.current_menu); + } this.has_been_loaded.resolve(); }, limit_entries: function() { @@ -585,6 +584,7 @@ instance.web.Menu = instance.web.Widget.extend({ $index.after($more); $more.find('.oe_menu_more').append($index.next().nextAll()); } + this.do_hide_more(); }, auto_limit_entries: function() { // TODO: auto detect overflow and bind window on resize @@ -603,6 +603,7 @@ instance.web.Menu = instance.web.Widget.extend({ open_menu: function (id) { var $clicked_menu, $sub_menu, $main_menu; $clicked_menu = this.$element.add(this.$secondary_menus).find('a[data-menu=' + id + ']'); + this.trigger('open_menu', id, $clicked_menu); if (this.$secondary_menus.has($clicked_menu).length) { $sub_menu = $clicked_menu.parents('.oe_secondary_menu'); @@ -631,56 +632,58 @@ instance.web.Menu = instance.web.Widget.extend({ } } }, + /** + * Call open_menu with the first menu_item matching an action_id + * + * @param {Number} id the action_id to match + */ open_action: function (id) { var menu_id, $menu = this.$element.add(this.$secondary_menus).find('a[data-action-id=' + id + ']'); if (menu_id = $menu.data('menu')) { this.open_menu(menu_id); } }, - on_menu_click: function(ev, id) { - // TODO If first level menu doesnt have action trigger first leaf - this.do_hide_more(); - id = id || 0; - var $clicked_menu, manual = false; - + /** + * Process a click on a menu item + * + * @param {Number} id the menu_id + */ + menu_click: function(id) { if (id) { - // We can manually activate a menu with it's id (for hash url mapping) - manual = true; - $clicked_menu = this.$element.find('a[data-menu=' + id + ']'); - if (!$clicked_menu.length) { - $clicked_menu = this.$secondary_menus.find('a[data-menu=' + id + ']'); + this.do_hide_more(); + // find back the menuitem in dom to get the action + var $item = this.$element.find('a[data-menu=' + id + ']'); + if (!$item.length) { + $item = this.$secondary_menus.find('a[data-menu=' + id + ']'); + } + var action_id = $item.data('action-id'); + // If first level menu doesnt have action trigger first leaf + if (!action_id) { + if(this.$element.has($item)) { + $sub_menu = this.$secondary_menus.find('.oe_secondary_menu[data-menu-parent=' + id + ']'); + $items = $sub_menu.find('a[data-action-id]').filter('[data-action-id!=""]'); + if($items) { + action_id = $items.data('action-id'); + id = $items.data('menu'); + } + } } - } else { - $clicked_menu = $(ev.currentTarget); - id = $clicked_menu.data('menu'); - } - - this.trigger('menuClicked', id, $clicked_menu); - - if (id) { this.open_menu(id); this.current_menu = id; this.session.active_id = id; - var action_id = $clicked_menu.data('action-id'); - if (action_id) { - this.on_action(action_id); - } - } - if (ev) { - ev.stopPropagation(); + this.trigger('menu_click', action_id, id, $item); } + }, + /** + * Jquery event handler for menu click + * + * @param {Event} ev the jquery event + */ + on_menu_click: function(ev) { + this.menu_click($(ev.currentTarget).data('menu')); + ev.stopPropagation(); return false; }, - do_show_secondary: function($sub_menu, $main_menu) { - var self = this; - this.$secondary_menus.show(); - if (!arguments.length) { - return; - } - $sub_menu.show(); - }, - on_action: function(action) { - } }); instance.web.UserMenu = instance.web.Widget.extend({ @@ -900,8 +903,6 @@ instance.web.WebClient = instance.web.Widget.extend({ this.session.on_session_valid.add(function() { self.show_application(); - self.user_menu.do_update(); - self.menu.do_reload(); if(self.action_manager) self.action_manager.destroy(); self.action_manager = new instance.web.ActionManager(self); @@ -932,11 +933,12 @@ instance.web.WebClient = instance.web.Widget.extend({ self.$element.append(self.$table); self.menu = new instance.web.Menu(self); self.menu.replace(this.$element.find('.oe_menu_placeholder')); - self.menu.on_action.add(this.proxy('on_menu_action')); + self.menu.on('menu_click', this, this.on_menu_action); self.user_menu = new instance.web.UserMenu(self); self.user_menu.replace(this.$element.find('.oe_user_menu_placeholder')); self.user_menu.on_menu_logout.add(this.proxy('on_logout')); self.user_menu.on_action.add(this.proxy('on_menu_action')); + self.user_menu.do_update(); }, show_common: function() { var self = this; @@ -997,8 +999,9 @@ instance.web.WebClient = instance.web.Widget.extend({ } else { self.menu.has_been_loaded.then(function() { var first_menu_id = self.menu.$element.find("a:first").data("menu"); - if(first_menu_id) - self.menu.on_menu_click(null,first_menu_id); + if(first_menu_id) { + self.menu.menu_click(first_menu_id); + } }); } }, @@ -1022,7 +1025,7 @@ instance.web.WebClient = instance.web.Widget.extend({ // TODO replace by client action menuclick if(action.menu_id) { this.do_reload().then(function () { - self.menu.on_menu_click(null, action.menu_id); + self.menu.menu_click(action.menu_id); }); } },