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