Roo/bootstrap/Menu.js
[roojs1] / Roo / bootstrap / Menu.js
1 /*
2  * - LGPL
3  *
4  * menu
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.Menu
10  * @extends Roo.bootstrap.Component
11  * Bootstrap Menu class - container for MenuItems
12  * @cfg {String} type (dropdown|treeview|submenu) type of menu
13  * @cfg {bool} hidden  if the menu should be hidden when rendered.
14  * @cfg {bool} stopEvent (true|false)  Stop event after trigger press (default true)
15  * @cfg {bool} isLink (true|false)  the menu has link disable auto expand and collaspe (default false)
16  * 
17  * @constructor
18  * Create a new Menu
19  * @param {Object} config The config object
20  */
21
22
23 Roo.bootstrap.Menu = function(config){
24     Roo.bootstrap.Menu.superclass.constructor.call(this, config);
25     if (this.registerMenu && this.type != 'treeview')  {
26         Roo.bootstrap.MenuMgr.register(this);
27     }
28     this.addEvents({
29         /**
30          * @event beforeshow
31          * Fires before this menu is displayed
32          * @param {Roo.menu.Menu} this
33          */
34         beforeshow : true,
35         /**
36          * @event beforehide
37          * Fires before this menu is hidden
38          * @param {Roo.menu.Menu} this
39          */
40         beforehide : true,
41         /**
42          * @event show
43          * Fires after this menu is displayed
44          * @param {Roo.menu.Menu} this
45          */
46         show : true,
47         /**
48          * @event hide
49          * Fires after this menu is hidden
50          * @param {Roo.menu.Menu} this
51          */
52         hide : true,
53         /**
54          * @event click
55          * Fires when this menu is clicked (or when the enter key is pressed while it is active)
56          * @param {Roo.menu.Menu} this
57          * @param {Roo.menu.Item} menuItem The menu item that was clicked
58          * @param {Roo.EventObject} e
59          */
60         click : true,
61         /**
62          * @event mouseover
63          * Fires when the mouse is hovering over this menu
64          * @param {Roo.menu.Menu} this
65          * @param {Roo.EventObject} e
66          * @param {Roo.menu.Item} menuItem The menu item that was clicked
67          */
68         mouseover : true,
69         /**
70          * @event mouseout
71          * Fires when the mouse exits this menu
72          * @param {Roo.menu.Menu} this
73          * @param {Roo.EventObject} e
74          * @param {Roo.menu.Item} menuItem The menu item that was clicked
75          */
76         mouseout : true,
77         /**
78          * @event itemclick
79          * Fires when a menu item contained in this menu is clicked
80          * @param {Roo.menu.BaseItem} baseItem The BaseItem that was clicked
81          * @param {Roo.EventObject} e
82          */
83         itemclick: true
84     });
85     this.menuitems = new Roo.util.MixedCollection(false, function(o) { return o.el.id; });
86 };
87
88 Roo.extend(Roo.bootstrap.Menu, Roo.bootstrap.Component,  {
89     
90    /// html : false,
91     //align : '',
92     triggerEl : false,  // is this set by component builder? -- it should really be fetched from parent()???
93     type: false,
94     /**
95      * @cfg {Boolean} registerMenu True (default) - means that clicking on screen etc. hides it.
96      */
97     registerMenu : true,
98     
99     menuItems :false, // stores the menu items..
100     
101     hidden:true,
102         
103     parentMenu : false,
104     
105     stopEvent : true,
106     
107     isLink : false,
108     
109     getChildContainer : function() {
110         return this.el;  
111     },
112     
113     getAutoCreate : function(){
114          
115         //if (['right'].indexOf(this.align)!==-1) {
116         //    cfg.cn[1].cls += ' pull-right'
117         //}
118         
119         
120         var cfg = {
121             tag : 'ul',
122             cls : 'dropdown-menu' ,
123             style : 'z-index:1000'
124             
125         };
126         
127         if (this.type === 'submenu') {
128             cfg.cls = 'submenu active';
129         }
130         if (this.type === 'treeview') {
131             cfg.cls = 'treeview-menu';
132         }
133         
134         return cfg;
135     },
136     initEvents : function() {
137         
138        // Roo.log("ADD event");
139        // Roo.log(this.triggerEl.dom);
140         
141         this.triggerEl.on('click', this.onTriggerClick, this);
142         
143         this.triggerEl.on(Roo.isTouch ? 'touchstart' : 'mouseup', this.onTriggerPress, this);
144         
145         this.triggerEl.addClass('dropdown-toggle');
146         
147 //        if (Roo.isTouch) {
148 //            this.el.on('touchstart'  , this.onTouch, this);
149 //        }
150
151         this.el.on('click' , this.onClick, this);
152
153         this.el.on("mouseover", this.onMouseOver, this);
154         this.el.on("mouseout", this.onMouseOut, this);
155         
156     },
157     
158     findTargetItem : function(e)
159     {
160         var t = e.getTarget(".dropdown-menu-item", this.el,  true);
161         if(!t){
162             return false;
163         }
164         //Roo.log(t);         Roo.log(t.id);
165         if(t && t.id){
166             //Roo.log(this.menuitems);
167             return this.menuitems.get(t.id);
168             
169             //return this.items.get(t.menuItemId);
170         }
171         
172         return false;
173     },
174     
175     onTouch : function(e) 
176     {
177         Roo.log("menu.onTouch");
178         //e.stopEvent(); this make the user popdown broken
179         this.onClick(e);
180     },
181     
182     onClick : function(e)
183     {
184         Roo.log("menu.onClick");
185         
186         var t = this.findTargetItem(e);
187         if(!t || t.isContainer){
188             return;
189         }
190         Roo.log(e);
191         /*
192         if (Roo.isTouch && e.type == 'touchstart' && t.menu  && !t.disabled) {
193             if(t == this.activeItem && t.shouldDeactivate(e)){
194                 this.activeItem.deactivate();
195                 delete this.activeItem;
196                 return;
197             }
198             if(t.canActivate){
199                 this.setActiveItem(t, true);
200             }
201             return;
202             
203             
204         }
205         */
206        
207         Roo.log('pass click event');
208         
209         t.onClick(e);
210         
211         this.fireEvent("click", this, t, e);
212         
213         var _this = this;
214         
215         (function() { _this.hide(); }).defer(100);
216     },
217     
218     onMouseOver : function(e){
219         var t  = this.findTargetItem(e);
220         //Roo.log(t);
221         //if(t){
222         //    if(t.canActivate && !t.disabled){
223         //        this.setActiveItem(t, true);
224         //    }
225         //}
226         
227         this.fireEvent("mouseover", this, e, t);
228     },
229     isVisible : function(){
230         return !this.hidden;
231     },
232      onMouseOut : function(e){
233         var t  = this.findTargetItem(e);
234         
235         //if(t ){
236         //    if(t == this.activeItem && t.shouldDeactivate(e)){
237         //        this.activeItem.deactivate();
238         //        delete this.activeItem;
239         //    }
240         //}
241         this.fireEvent("mouseout", this, e, t);
242     },
243     
244     
245     /**
246      * Displays this menu relative to another element
247      * @param {String/HTMLElement/Roo.Element} element The element to align to
248      * @param {String} position (optional) The {@link Roo.Element#alignTo} anchor position to use in aligning to
249      * the element (defaults to this.defaultAlign)
250      * @param {Roo.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
251      */
252     show : function(el, pos, parentMenu){
253         this.parentMenu = parentMenu;
254         if(!this.el){
255             this.render();
256         }
257         this.fireEvent("beforeshow", this);
258         this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign), parentMenu, false);
259     },
260      /**
261      * Displays this menu at a specific xy position
262      * @param {Array} xyPosition Contains X & Y [x, y] values for the position at which to show the menu (coordinates are page-based)
263      * @param {Roo.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
264      */
265     showAt : function(xy, parentMenu, /* private: */_e){
266         this.parentMenu = parentMenu;
267         if(!this.el){
268             this.render();
269         }
270         if(_e !== false){
271             this.fireEvent("beforeshow", this);
272             //xy = this.el.adjustForConstraints(xy);
273         }
274         
275         //this.el.show();
276         this.hideMenuItems();
277         this.hidden = false;
278         this.triggerEl.addClass('open');
279         
280         if(this.el.getWidth() + xy[0] > Roo.lib.Dom.getViewWidth()){
281             xy[0] = xy[0] - this.el.getWidth() + this.triggerEl.getWidth();
282         }
283         
284         if(this.el.getStyle('top') != 'auto' && this.el.getStyle('top').slice(-1) != "%"){
285             this.el.setXY(xy);
286         }
287         
288         this.focus();
289         this.fireEvent("show", this);
290     },
291     
292     focus : function(){
293         return;
294         if(!this.hidden){
295             this.doFocus.defer(50, this);
296         }
297     },
298
299     doFocus : function(){
300         if(!this.hidden){
301             this.focusEl.focus();
302         }
303     },
304
305     /**
306      * Hides this menu and optionally all parent menus
307      * @param {Boolean} deep (optional) True to hide all parent menus recursively, if any (defaults to false)
308      */
309     hide : function(deep)
310     {
311         
312         this.hideMenuItems();
313         if(this.el && this.isVisible()){
314             this.fireEvent("beforehide", this);
315             if(this.activeItem){
316                 this.activeItem.deactivate();
317                 this.activeItem = null;
318             }
319             this.triggerEl.removeClass('open');;
320             this.hidden = true;
321             this.fireEvent("hide", this);
322         }
323         if(deep === true && this.parentMenu){
324             this.parentMenu.hide(true);
325         }
326     },
327     
328     onTriggerClick : function(e)
329     {
330         Roo.log('trigger click');
331         
332         var target = e.getTarget();
333         
334         Roo.log(target.nodeName.toLowerCase());
335         
336         if(target.nodeName.toLowerCase() === 'i'){
337             e.preventDefault();
338         }
339         
340     },
341     
342     onTriggerPress  : function(e)
343     {
344         Roo.log('trigger press');
345         //Roo.log(e.getTarget());
346        // Roo.log(this.triggerEl.dom);
347        
348         // trigger only occurs on normal menu's -- if it's a treeview or dropdown... do not hide/show..
349         var pel = Roo.get(e.getTarget());
350         if (pel.findParent('.dropdown-menu') || pel.findParent('.treeview-menu') ) {
351             Roo.log('is treeview or dropdown?');
352             return;
353         }
354         
355         if(e.getTarget().nodeName.toLowerCase() !== 'i' && this.isLink){
356             return;
357         }
358         
359         if (this.isVisible()) {
360             Roo.log('hide');
361             this.hide();
362         } else {
363             Roo.log('show');
364             this.show(this.triggerEl, false, false);
365         }
366         
367         if(this.stopEvent || e.getTarget().nodeName.toLowerCase() === 'i'){
368             e.stopEvent();
369         }
370         
371     },
372        
373     
374     hideMenuItems : function()
375     {
376         Roo.log("hide Menu Items");
377         if (!this.el) { 
378             return;
379         }
380         //$(backdrop).remove()
381         this.el.select('.open',true).each(function(aa) {
382             
383             aa.removeClass('open');
384           //var parent = getParent($(this))
385           //var relatedTarget = { relatedTarget: this }
386           
387            //$parent.trigger(e = $.Event('hide.bs.dropdown', relatedTarget))
388           //if (e.isDefaultPrevented()) return
389            //$parent.removeClass('open').trigger('hidden.bs.dropdown', relatedTarget)
390         });
391     },
392     addxtypeChild : function (tree, cntr) {
393         var comp= Roo.bootstrap.Menu.superclass.addxtypeChild.call(this, tree, cntr);
394           
395         this.menuitems.add(comp);
396         return comp;
397
398     },
399     getEl : function()
400     {
401         Roo.log(this.el);
402         return this.el;
403     }
404 });
405
406  
407