3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 * @class Ext.menu.MenuMgr
9 * Provides a common registry of all menu items on a page so that they can be easily accessed by id.
12 Ext.menu.MenuMgr = function(){
13 var menus, active, groups = {}, attached = false, lastShow = new Date();
15 // private - called when first menu is created
18 active = new Ext.util.MixedCollection();
19 Ext.getDoc().addKeyListener(27, function(){
20 if(active.length > 0){
28 if(active && active.length > 0){
29 var c = active.clone();
41 if(active.length < 1){
42 Ext.getDoc().un("mousedown", onMouseDown);
49 var last = active.last();
50 lastShow = new Date();
53 Ext.getDoc().on("mousedown", onMouseDown);
57 m.getEl().setZIndex(parseInt(m.parentMenu.getEl().getStyle("z-index"), 10) + 3);
58 m.parentMenu.activeChild = m;
59 }else if(last && !last.isDestroyed && last.isVisible()){
60 m.getEl().setZIndex(parseInt(last.getEl().getStyle("z-index"), 10) + 3);
65 function onBeforeHide(m){
70 clearTimeout(m.autoHideTimer);
71 delete m.autoHideTimer;
76 function onBeforeShow(m){
77 var pm = m.parentMenu;
78 if(!pm && !m.allowOtherMenus){
80 }else if(pm && pm.activeChild){
81 pm.activeChild.hide();
86 function onMouseDown(e){
87 if(lastShow.getElapsed() > 50 && active.length > 0 && !e.getTarget(".x-menu")){
93 function onBeforeCheck(mi, state){
95 var g = groups[mi.group];
96 for(var i = 0, l = g.length; i < l; i++){
98 g[i].setChecked(false);
107 * Hides all menus that are currently visible
108 * @return {Boolean} success True if any active menus were hidden.
110 hideAll : function(){
115 register : function(menu){
119 menus[menu.id] = menu;
121 beforehide: onBeforeHide,
123 beforeshow: onBeforeShow,
129 * Returns a {@link Ext.menu.Menu} object
130 * @param {String/Object} menu The string menu id, an existing menu object reference, or a Menu config that will
131 * be used to generate and return a new Menu instance.
132 * @return {Ext.menu.Menu} The specified menu, or null if none are found
134 get : function(menu){
135 if(typeof menu == "string"){ // menu id
136 if(!menus){ // not initialized, no menus to return
140 }else if(menu.events){ // menu instance
142 }else if(typeof menu.length == 'number'){ // array of menu items?
143 return new Ext.menu.Menu({items:menu});
144 }else{ // otherwise, must be a config
145 return Ext.create(menu, 'menu');
150 unregister : function(menu){
151 delete menus[menu.id];
152 menu.un("beforehide", onBeforeHide);
153 menu.un("hide", onHide);
154 menu.un("beforeshow", onBeforeShow);
155 menu.un("show", onShow);
159 registerCheckable : function(menuItem){
160 var g = menuItem.group;
165 groups[g].push(menuItem);
166 menuItem.on("beforecheckchange", onBeforeCheck);
171 unregisterCheckable : function(menuItem){
172 var g = menuItem.group;
174 groups[g].remove(menuItem);
175 menuItem.un("beforecheckchange", onBeforeCheck);
179 getCheckedItem : function(groupId){
180 var g = groups[groupId];
182 for(var i = 0, l = g.length; i < l; i++){
191 setCheckedItem : function(groupId, itemId){
192 var g = groups[groupId];
194 for(var i = 0, l = g.length; i < l; i++){
195 if(g[i].id == itemId){
196 g[i].setChecked(true);