Upgrade to ExtJS 3.3.0 - Released 10/06/2010
[extjs.git] / src / widgets / menu / MenuMgr.js
1 /*!
2  * Ext JS Library 3.3.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
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.
10  * @singleton
11  */
12 Ext.menu.MenuMgr = function(){
13    var menus, active, groups = {}, attached = false, lastShow = new Date();
14
15    // private - called when first menu is created
16    function init(){
17        menus = {};
18        active = new Ext.util.MixedCollection();
19        Ext.getDoc().addKeyListener(27, function(){
20            if(active.length > 0){
21                hideAll();
22            }
23        });
24    }
25
26    // private
27    function hideAll(){
28        if(active && active.length > 0){
29            var c = active.clone();
30            c.each(function(m){
31                m.hide();
32            });
33            return true;
34        }
35        return false;
36    }
37
38    // private
39    function onHide(m){
40        active.remove(m);
41        if(active.length < 1){
42            Ext.getDoc().un("mousedown", onMouseDown);
43            attached = false;
44        }
45    }
46
47    // private
48    function onShow(m){
49        var last = active.last();
50        lastShow = new Date();
51        active.add(m);
52        if(!attached){
53            Ext.getDoc().on("mousedown", onMouseDown);
54            attached = true;
55        }
56        if(m.parentMenu){
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);
61        }
62    }
63
64    // private
65    function onBeforeHide(m){
66        if(m.activeChild){
67            m.activeChild.hide();
68        }
69        if(m.autoHideTimer){
70            clearTimeout(m.autoHideTimer);
71            delete m.autoHideTimer;
72        }
73    }
74
75    // private
76    function onBeforeShow(m){
77        var pm = m.parentMenu;
78        if(!pm && !m.allowOtherMenus){
79            hideAll();
80        }else if(pm && pm.activeChild){
81            pm.activeChild.hide();
82        }
83    }
84
85    // private
86    function onMouseDown(e){
87        if(lastShow.getElapsed() > 50 && active.length > 0 && !e.getTarget(".x-menu")){
88            hideAll();
89        }
90    }
91
92    return {
93
94        /**
95         * Hides all menus that are currently visible
96         * @return {Boolean} success True if any active menus were hidden.
97         */
98        hideAll : function(){
99             return hideAll();
100        },
101
102        // private
103        register : function(menu){
104            if(!menus){
105                init();
106            }
107            menus[menu.id] = menu;
108            menu.on({
109                beforehide: onBeforeHide,
110                hide: onHide,
111                beforeshow: onBeforeShow,
112                show: onShow
113            });
114        },
115
116         /**
117          * Returns a {@link Ext.menu.Menu} object
118          * @param {String/Object} menu The string menu id, an existing menu object reference, or a Menu config that will
119          * be used to generate and return a new Menu instance.
120          * @return {Ext.menu.Menu} The specified menu, or null if none are found
121          */
122        get : function(menu){
123            if(typeof menu == "string"){ // menu id
124                if(!menus){  // not initialized, no menus to return
125                    return null;
126                }
127                return menus[menu];
128            }else if(menu.events){  // menu instance
129                return menu;
130            }else if(typeof menu.length == 'number'){ // array of menu items?
131                return new Ext.menu.Menu({items:menu});
132            }else{ // otherwise, must be a config
133                return Ext.create(menu, 'menu');
134            }
135        },
136
137        // private
138        unregister : function(menu){
139            delete menus[menu.id];
140            menu.un("beforehide", onBeforeHide);
141            menu.un("hide", onHide);
142            menu.un("beforeshow", onBeforeShow);
143            menu.un("show", onShow);
144        },
145
146        // private
147        registerCheckable : function(menuItem){
148            var g = menuItem.group;
149            if(g){
150                if(!groups[g]){
151                    groups[g] = [];
152                }
153                groups[g].push(menuItem);
154            }
155        },
156
157        // private
158        unregisterCheckable : function(menuItem){
159            var g = menuItem.group;
160            if(g){
161                groups[g].remove(menuItem);
162            }
163        },
164        
165        // private
166        onCheckChange: function(item, state){
167            if(item.group && state){
168                var group = groups[item.group],
169                    i = 0,
170                    len = group.length,
171                    current;
172                    
173                for(; i < len; i++){
174                    current = group[i];
175                    if(current != item){
176                        current.setChecked(false);
177                    }
178                }
179            }
180        },
181
182        getCheckedItem : function(groupId){
183            var g = groups[groupId];
184            if(g){
185                for(var i = 0, l = g.length; i < l; i++){
186                    if(g[i].checked){
187                        return g[i];
188                    }
189                }
190            }
191            return null;
192        },
193
194        setCheckedItem : function(groupId, itemId){
195            var g = groups[groupId];
196            if(g){
197                for(var i = 0, l = g.length; i < l; i++){
198                    if(g[i].id == itemId){
199                        g[i].setChecked(true);
200                    }
201                }
202            }
203            return null;
204        }
205    };
206 }();