Roo/bootstrap/MenuMgr.js
[roojs1] / Roo / bootstrap / MenuMgr.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12 /**
13  * @class Roo.bootstrap.MenuMgr
14  * Provides a common registry of all menu items on a page so that they can be easily accessed by id.
15  * @singleton
16  */
17 Roo.bootstrap.MenuMgr = function(){
18    var menus, active, groups = {}, attached = false, lastShow = new Date();
19
20    // private - called when first menu is created
21    function init(){
22        menus = {};
23        active = new Roo.util.MixedCollection();
24        Roo.get(document).addKeyListener(27, function(){
25            if(active.length > 0){
26                hideAll();
27            }
28        });
29    }
30
31    // private
32    function hideAll(){
33        if(active && active.length > 0){
34            var c = active.clone();
35            c.each(function(m){
36                m.hide();
37            });
38        }
39    }
40
41    // private
42    function onHide(m){
43        active.remove(m);
44        if(active.length < 1){
45            Roo.get(document).un("mousedown", onMouseDown);
46            Roo.select('iframe').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            Roo.get(document).on("mousedown", onMouseDown);
58            Roo.select('iframe').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.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 && active != m){
86            pm.activeChild.hide();
87        }
88    }
89
90    // private
91    function onMouseDown(e){
92         //Roo.log("on MouseDown");
93        if(lastShow.getElapsed() > 50 && active.length > 0 && !e.getTarget(".x-menu")){
94            hideAll();
95        }
96    }
97
98    // private
99    function onBeforeCheck(mi, state){
100        if(state){
101            var g = groups[mi.group];
102            for(var i = 0, l = g.length; i < l; i++){
103                if(g[i] != mi){
104                    g[i].setChecked(false);
105                }
106            }
107        }
108    }
109
110    return {
111
112        /**
113         * Hides all menus that are currently visible
114         */
115        hideAll : function(){
116             hideAll();  
117        },
118
119        // private
120        register : function(menu){
121            if(!menus){
122                init();
123            }
124            menus[menu.id] = menu;
125            menu.on("beforehide", onBeforeHide);
126            menu.on("hide", onHide);
127            menu.on("beforeshow", onBeforeShow);
128            menu.on("show", onShow);
129            var g = menu.group;
130            if(g && menu.events["checkchange"]){
131                if(!groups[g]){
132                    groups[g] = [];
133                }
134                groups[g].push(menu);
135                menu.on("checkchange", onCheck);
136            }
137        },
138
139         /**
140          * Returns a {@link Roo.menu.Menu} object
141          * @param {String/Object} menu The string menu id, an existing menu object reference, or a Menu config that will
142          * be used to generate and return a new Menu instance.
143          */
144        get : function(menu){
145            if(typeof menu == "string"){ // menu id
146                return menus[menu];
147            }else if(menu.events){  // menu instance
148                return menu;
149            }
150            /*else if(typeof menu.length == 'number'){ // array of menu items?
151                return new Roo.bootstrap.Menu({items:menu});
152            }else{ // otherwise, must be a config
153                return new Roo.bootstrap.Menu(menu);
154            }
155            */
156            return false;
157        },
158
159        // private
160        unregister : function(menu){
161            delete menus[menu.id];
162            menu.un("beforehide", onBeforeHide);
163            menu.un("hide", onHide);
164            menu.un("beforeshow", onBeforeShow);
165            menu.un("show", onShow);
166            var g = menu.group;
167            if(g && menu.events["checkchange"]){
168                groups[g].remove(menu);
169                menu.un("checkchange", onCheck);
170            }
171        },
172
173        // private
174        registerCheckable : function(menuItem){
175            var g = menuItem.group;
176            if(g){
177                if(!groups[g]){
178                    groups[g] = [];
179                }
180                groups[g].push(menuItem);
181                menuItem.on("beforecheckchange", onBeforeCheck);
182            }
183        },
184
185        // private
186        unregisterCheckable : function(menuItem){
187            var g = menuItem.group;
188            if(g){
189                groups[g].remove(menuItem);
190                menuItem.un("beforecheckchange", onBeforeCheck);
191            }
192        }
193    };
194 }();