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