[619] | 1 | /* |
---|
| 2 | * Ext JS Library 2.2.1 |
---|
| 3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
| 4 | * licensing@extjs.com |
---|
| 5 | * |
---|
| 6 | * http://extjs.com/license |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /** |
---|
| 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 | } |
---|
| 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.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 | // private |
---|
| 93 | function onBeforeCheck(mi, state){ |
---|
| 94 | if(state){ |
---|
| 95 | var g = groups[mi.group]; |
---|
| 96 | for(var i = 0, l = g.length; i < l; i++){ |
---|
| 97 | if(g[i] != mi){ |
---|
| 98 | g[i].setChecked(false); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | return { |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * Hides all menus that are currently visible |
---|
| 108 | */ |
---|
| 109 | hideAll : function(){ |
---|
| 110 | hideAll(); |
---|
| 111 | }, |
---|
| 112 | |
---|
| 113 | // private |
---|
| 114 | register : function(menu){ |
---|
| 115 | if(!menus){ |
---|
| 116 | init(); |
---|
| 117 | } |
---|
| 118 | menus[menu.id] = menu; |
---|
| 119 | menu.on("beforehide", onBeforeHide); |
---|
| 120 | menu.on("hide", onHide); |
---|
| 121 | menu.on("beforeshow", onBeforeShow); |
---|
| 122 | menu.on("show", onShow); |
---|
| 123 | var g = menu.group; |
---|
| 124 | if(g && menu.events["checkchange"]){ |
---|
| 125 | if(!groups[g]){ |
---|
| 126 | groups[g] = []; |
---|
| 127 | } |
---|
| 128 | groups[g].push(menu); |
---|
| 129 | menu.on("checkchange", onCheck); |
---|
| 130 | } |
---|
| 131 | }, |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * Returns a {@link Ext.menu.Menu} object |
---|
| 135 | * @param {String/Object} menu The string menu id, an existing menu object reference, or a Menu config that will |
---|
| 136 | * be used to generate and return a new Menu instance. |
---|
| 137 | * @return {Ext.menu.Menu} The specified menu, or null if none are found |
---|
| 138 | */ |
---|
| 139 | get : function(menu){ |
---|
| 140 | if(typeof menu == "string"){ // menu id |
---|
| 141 | if(!menus){ // not initialized, no menus to return |
---|
| 142 | return null; |
---|
| 143 | } |
---|
| 144 | return menus[menu]; |
---|
| 145 | }else if(menu.events){ // menu instance |
---|
| 146 | return menu; |
---|
| 147 | }else if(typeof menu.length == 'number'){ // array of menu items? |
---|
| 148 | return new Ext.menu.Menu({items:menu}); |
---|
| 149 | }else{ // otherwise, must be a config |
---|
| 150 | return new Ext.menu.Menu(menu); |
---|
| 151 | } |
---|
| 152 | }, |
---|
| 153 | |
---|
| 154 | // private |
---|
| 155 | unregister : function(menu){ |
---|
| 156 | delete menus[menu.id]; |
---|
| 157 | menu.un("beforehide", onBeforeHide); |
---|
| 158 | menu.un("hide", onHide); |
---|
| 159 | menu.un("beforeshow", onBeforeShow); |
---|
| 160 | menu.un("show", onShow); |
---|
| 161 | var g = menu.group; |
---|
| 162 | if(g && menu.events["checkchange"]){ |
---|
| 163 | groups[g].remove(menu); |
---|
| 164 | menu.un("checkchange", onCheck); |
---|
| 165 | } |
---|
| 166 | }, |
---|
| 167 | |
---|
| 168 | // private |
---|
| 169 | registerCheckable : function(menuItem){ |
---|
| 170 | var g = menuItem.group; |
---|
| 171 | if(g){ |
---|
| 172 | if(!groups[g]){ |
---|
| 173 | groups[g] = []; |
---|
| 174 | } |
---|
| 175 | groups[g].push(menuItem); |
---|
| 176 | menuItem.on("beforecheckchange", onBeforeCheck); |
---|
| 177 | } |
---|
| 178 | }, |
---|
| 179 | |
---|
| 180 | // private |
---|
| 181 | unregisterCheckable : function(menuItem){ |
---|
| 182 | var g = menuItem.group; |
---|
| 183 | if(g){ |
---|
| 184 | groups[g].remove(menuItem); |
---|
| 185 | menuItem.un("beforecheckchange", onBeforeCheck); |
---|
| 186 | } |
---|
| 187 | }, |
---|
| 188 | |
---|
| 189 | getCheckedItem : function(groupId){ |
---|
| 190 | var g = groups[groupId]; |
---|
| 191 | if(g){ |
---|
| 192 | for(var i = 0, l = g.length; i < l; i++){ |
---|
| 193 | if(g[i].checked){ |
---|
| 194 | return g[i]; |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | return null; |
---|
| 199 | }, |
---|
| 200 | |
---|
| 201 | setCheckedItem : function(groupId, itemId){ |
---|
| 202 | var g = groups[groupId]; |
---|
| 203 | if(g){ |
---|
| 204 | for(var i = 0, l = g.length; i < l; i++){ |
---|
| 205 | if(g[i].id == itemId){ |
---|
| 206 | g[i].setChecked(true); |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | return null; |
---|
| 211 | } |
---|
| 212 | }; |
---|
| 213 | }(); |
---|