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