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         this.el.on('click' , this.onClick, this);
151
152         this.el.on("mouseover", this.onMouseOver, this);
153         this.el.on("mouseout", this.onMouseOut, this);
154         
155     },
156     
157     findTargetItem : function(e)
158     {
159         var t = e.getTarget(".dropdown-menu-item", this.el,  true);
160         if(!t){
161             return false;
162         }
163         //Roo.log(t);         Roo.log(t.id);
164         if(t && t.id){
165             //Roo.log(this.menuitems);
166             return this.menuitems.get(t.id);
167             
168             //return this.items.get(t.menuItemId);
169         }
170         
171         return false;
172     },
173     
174     onTouch : function(e) 
175     {
176         Roo.log("menu.onTouch");
177         //e.stopEvent(); this make the user popdown broken
178         this.onClick(e);
179     },
180     
181     onClick : function(e)
182     {
183         Roo.log("menu.onClick");
184         
185         var t = this.findTargetItem(e);
186         if(!t || t.isContainer){
187             return;
188         }
189         Roo.log(e);
190         /*
191         if (Roo.isTouch && e.type == 'touchstart' && t.menu  && !t.disabled) {
192             if(t == this.activeItem && t.shouldDeactivate(e)){
193                 this.activeItem.deactivate();
194                 delete this.activeItem;
195                 return;
196             }
197             if(t.canActivate){
198                 this.setActiveItem(t, true);
199             }
200             return;
201             
202             
203         }
204         */
205        
206         Roo.log('pass click event');
207         
208         t.onClick(e);
209         
210         this.fireEvent("click", this, t, e);
211         
212         var _this = this;
213         
214         if(!t.href.length || t.href == '#'){
215             (function() { _this.hide(); }).defer(100);
216         }
217         
218     },
219     
220     onMouseOver : function(e){
221         var t  = this.findTargetItem(e);
222         //Roo.log(t);
223         //if(t){
224         //    if(t.canActivate && !t.disabled){
225         //        this.setActiveItem(t, true);
226         //    }
227         //}
228         
229         this.fireEvent("mouseover", this, e, t);
230     },
231     isVisible : function(){
232         return !this.hidden;
233     },
234      onMouseOut : function(e){
235         var t  = this.findTargetItem(e);
236         
237         //if(t ){
238         //    if(t == this.activeItem && t.shouldDeactivate(e)){
239         //        this.activeItem.deactivate();
240         //        delete this.activeItem;
241         //    }
242         //}
243         this.fireEvent("mouseout", this, e, t);
244     },
245     
246     
247     /**
248      * Displays this menu relative to another element
249      * @param {String/HTMLElement/Roo.Element} element The element to align to
250      * @param {String} position (optional) The {@link Roo.Element#alignTo} anchor position to use in aligning to
251      * the element (defaults to this.defaultAlign)
252      * @param {Roo.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
253      */
254     show : function(el, pos, parentMenu){
255         this.parentMenu = parentMenu;
256         if(!this.el){
257             this.render();
258         }
259         
260         this.fireEvent("beforeshow", this);
261         
262         this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign), parentMenu, false);
263         
264     },
265      /**
266      * Displays this menu at a specific xy position
267      * @param {Array} xyPosition Contains X & Y [x, y] values for the position at which to show the menu (coordinates are page-based)
268      * @param {Roo.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
269      */
270     showAt : function(xy, parentMenu, /* private: */_e){
271         this.parentMenu = parentMenu;
272         if(!this.el){
273             this.render();
274         }
275         if(_e !== false){
276             this.fireEvent("beforeshow", this);
277             //xy = this.el.adjustForConstraints(xy);
278         }
279         
280         //this.el.show();
281         this.hideMenuItems();
282         this.hidden = false;
283         this.triggerEl.addClass('open');
284         
285         if(this.el.getWidth() + xy[0] > Roo.lib.Dom.getViewWidth()){
286             xy[0] = xy[0] - this.el.getWidth() + this.triggerEl.getWidth();
287         }
288         
289         if(this.el.getStyle('top') != 'auto' && this.el.getStyle('top').slice(-1) != "%"){
290             this.el.setXY(xy);
291         }
292         
293         this.focus();
294         this.fireEvent("show", this);
295     },
296     
297     focus : function(){
298         return;
299         if(!this.hidden){
300             this.doFocus.defer(50, this);
301         }
302     },
303
304     doFocus : function(){
305         if(!this.hidden){
306             this.focusEl.focus();
307         }
308     },
309
310     /**
311      * Hides this menu and optionally all parent menus
312      * @param {Boolean} deep (optional) True to hide all parent menus recursively, if any (defaults to false)
313      */
314     hide : function(deep)
315     {
316         
317         this.hideMenuItems();
318         if(this.el && this.isVisible()){
319             this.fireEvent("beforehide", this);
320             if(this.activeItem){
321                 this.activeItem.deactivate();
322                 this.activeItem = null;
323             }
324             this.triggerEl.removeClass('open');;
325             this.hidden = true;
326             this.fireEvent("hide", this);
327         }
328         if(deep === true && this.parentMenu){
329             this.parentMenu.hide(true);
330         }
331     },
332     
333     onTriggerClick : function(e)
334     {
335         Roo.log('trigger click');
336         
337         var target = e.getTarget();
338         
339         Roo.log(target.nodeName.toLowerCase());
340         
341         if(target.nodeName.toLowerCase() === 'i'){
342             e.preventDefault();
343         }
344         
345     },
346     
347     onTriggerPress  : function(e)
348     {
349         Roo.log('trigger press');
350         //Roo.log(e.getTarget());
351        // Roo.log(this.triggerEl.dom);
352        
353         // trigger only occurs on normal menu's -- if it's a treeview or dropdown... do not hide/show..
354         var pel = Roo.get(e.getTarget());
355         if (pel.findParent('.dropdown-menu') || pel.findParent('.treeview-menu') ) {
356             Roo.log('is treeview or dropdown?');
357             return;
358         }
359         
360         if(e.getTarget().nodeName.toLowerCase() !== 'i' && this.isLink){
361             return;
362         }
363         
364         if (this.isVisible()) {
365             Roo.log('hide');
366             this.hide();
367         } else {
368             Roo.log('show');
369             this.show(this.triggerEl, false, false);
370         }
371         
372         if(this.stopEvent || e.getTarget().nodeName.toLowerCase() === 'i'){
373             e.stopEvent();
374         }
375         
376     },
377        
378     
379     hideMenuItems : function()
380     {
381         Roo.log("hide Menu Items");
382         if (!this.el) { 
383             return;
384         }
385         //$(backdrop).remove()
386         this.el.select('.open',true).each(function(aa) {
387             
388             aa.removeClass('open');
389           //var parent = getParent($(this))
390           //var relatedTarget = { relatedTarget: this }
391           
392            //$parent.trigger(e = $.Event('hide.bs.dropdown', relatedTarget))
393           //if (e.isDefaultPrevented()) return
394            //$parent.removeClass('open').trigger('hidden.bs.dropdown', relatedTarget)
395         });
396     },
397     addxtypeChild : function (tree, cntr) {
398         var comp= Roo.bootstrap.Menu.superclass.addxtypeChild.call(this, tree, cntr);
399           
400         this.menuitems.add(comp);
401         return comp;
402
403     },
404     getEl : function()
405     {
406         Roo.log(this.el);
407         return this.el;
408     }
409 });
410
411  
412