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