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 * @cfg {bool} hideTrigger (true|false) default false - hide the carret for trigger.
17 * @cfg {String} align default tl-bl? == below - how the menu should be aligned.
21 * @param {Object} config The config object
25 Roo.bootstrap.Menu = function(config){
26 Roo.bootstrap.Menu.superclass.constructor.call(this, config);
27 if (this.registerMenu && this.type != 'treeview') {
28 Roo.bootstrap.MenuMgr.register(this);
35 * Fires before this menu is displayed (return false to block)
36 * @param {Roo.menu.Menu} this
41 * Fires before this menu is hidden (return false to block)
42 * @param {Roo.menu.Menu} this
47 * Fires after this menu is displayed
48 * @param {Roo.menu.Menu} this
53 * Fires after this menu is hidden
54 * @param {Roo.menu.Menu} this
59 * Fires when this menu is clicked (or when the enter key is pressed while it is active)
60 * @param {Roo.menu.Menu} this
61 * @param {Roo.menu.Item} menuItem The menu item that was clicked
62 * @param {Roo.EventObject} e
67 * Fires when the mouse is hovering over this menu
68 * @param {Roo.menu.Menu} this
69 * @param {Roo.EventObject} e
70 * @param {Roo.menu.Item} menuItem The menu item that was clicked
75 * Fires when the mouse exits this menu
76 * @param {Roo.menu.Menu} this
77 * @param {Roo.EventObject} e
78 * @param {Roo.menu.Item} menuItem The menu item that was clicked
83 * Fires when a menu item contained in this menu is clicked
84 * @param {Roo.menu.BaseItem} baseItem The BaseItem that was clicked
85 * @param {Roo.EventObject} e
89 this.menuitems = new Roo.util.MixedCollection(false, function(o) { return o.el.id; });
92 Roo.extend(Roo.bootstrap.Menu, Roo.bootstrap.Component, {
96 triggerEl : false, // is this set by component builder? -- it should really be fetched from parent()???
99 * @cfg {Boolean} registerMenu True (default) - means that clicking on screen etc. hides it.
103 menuItems :false, // stores the menu items..
113 container_method : 'getDocumentBody', // so the menu is rendered on the body and zIndex works.
120 getChildContainer : function() {
124 getAutoCreate : function(){
126 //if (['right'].indexOf(this.align)!==-1) {
127 // cfg.cn[1].cls += ' pull-right'
132 cls : 'dropdown-menu shadow' ,
133 style : 'z-index:1000'
137 if (this.type === 'submenu') {
138 cfg.cls = 'submenu active';
140 if (this.type === 'treeview') {
141 cfg.cls = 'treeview-menu';
146 initEvents : function() {
148 // Roo.log("ADD event");
149 // Roo.log(this.triggerEl.dom);
151 this.triggerEl.on('click', this.onTriggerClick, this);
153 this.triggerEl.on(Roo.isTouch ? 'touchstart' : 'mouseup', this.onTriggerPress, this);
155 if (!this.hideTrigger) {
156 if (this.triggerEl.hasClass('nav-item') && this.triggerEl.select('.nav-link',true).length) {
157 // dropdown toggle on the 'a' in BS4?
158 this.triggerEl.select('.nav-link',true).first().addClass('dropdown-toggle');
160 this.triggerEl.addClass('dropdown-toggle');
164 this.el.on('touchstart' , this.onTouch, this);
166 this.el.on('click' , this.onClick, this);
168 this.el.on("mouseover", this.onMouseOver, this);
169 this.el.on("mouseout", this.onMouseOut, this);
173 findTargetItem : function(e)
175 var t = e.getTarget(".dropdown-menu-item", this.el, true);
179 //Roo.log(t); Roo.log(t.id);
181 //Roo.log(this.menuitems);
182 return this.menuitems.get(t.id);
184 //return this.items.get(t.menuItemId);
190 onTouch : function(e)
192 Roo.log("menu.onTouch");
193 //e.stopEvent(); this make the user popdown broken
197 onClick : function(e)
199 Roo.log("menu.onClick");
201 var t = this.findTargetItem(e);
202 if(!t || t.isContainer){
207 if (Roo.isTouch && e.type == 'touchstart' && t.menu && !t.disabled) {
208 if(t == this.activeItem && t.shouldDeactivate(e)){
209 this.activeItem.deactivate();
210 delete this.activeItem;
214 this.setActiveItem(t, true);
222 Roo.log('pass click event');
226 this.fireEvent("click", this, t, e);
230 if(!t.href.length || t.href == '#'){
231 (function() { _this.hide(); }).defer(100);
236 onMouseOver : function(e){
237 var t = this.findTargetItem(e);
240 // if(t.canActivate && !t.disabled){
241 // this.setActiveItem(t, true);
245 this.fireEvent("mouseover", this, e, t);
247 isVisible : function(){
250 onMouseOut : function(e){
251 var t = this.findTargetItem(e);
254 // if(t == this.activeItem && t.shouldDeactivate(e)){
255 // this.activeItem.deactivate();
256 // delete this.activeItem;
259 this.fireEvent("mouseout", this, e, t);
264 * Displays this menu relative to another element
265 * @param {String/HTMLElement/Roo.Element} element The element to align to
266 * @param {String} position (optional) The {@link Roo.Element#alignTo} anchor position to use in aligning to
267 * the element (defaults to this.defaultAlign)
268 * @param {Roo.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
270 show : function(el, pos, parentMenu)
272 if (false === this.fireEvent("beforeshow", this)) {
273 Roo.log("show canceled");
276 this.parentMenu = parentMenu;
280 this.el.addClass('show'); // show otherwise we do not know how big we are..
282 var xy = this.el.getAlignToXY(el, pos);
284 // bl-tl << left align below
285 // tl-bl << left align
287 if(this.el.getWidth() + xy[0] >= Roo.lib.Dom.getViewWidth()){
288 // if it goes to far to the right.. -> align left.
289 xy = this.el.getAlignToXY(el, this.align.replace('/l/g', 'r'))
292 // was left align - go right?
293 xy = this.el.getAlignToXY(el, this.align.replace('/r/g', 'l'))
296 // goes down the bottom
297 if(this.el.getHeight() + xy[1] >= Roo.lib.Dom.getViewHeight() ||
299 var a = this.align.replace('?', '').split('-');
300 xy = this.el.getAlignToXY(el, a[1] + '-' + a[0] + '?')
304 this.showAt( xy , parentMenu, false);
307 * Displays this menu at a specific xy position
308 * @param {Array} xyPosition Contains X & Y [x, y] values for the position at which to show the menu (coordinates are page-based)
309 * @param {Roo.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
311 showAt : function(xy, parentMenu, /* private: */_e){
312 this.parentMenu = parentMenu;
317 this.fireEvent("beforeshow", this);
318 //xy = this.el.adjustForConstraints(xy);
322 this.hideMenuItems();
324 this.triggerEl.addClass('open');
325 this.el.addClass('show');
329 // reassign x when hitting right
331 // reassign y when hitting bottom
333 // but the list may align on trigger left or trigger top... should it be a properity?
335 if(this.el.getStyle('top') != 'auto' && this.el.getStyle('top').slice(-1) != "%"){
340 this.fireEvent("show", this);
346 this.doFocus.defer(50, this);
350 doFocus : function(){
352 this.focusEl.focus();
357 * Hides this menu and optionally all parent menus
358 * @param {Boolean} deep (optional) True to hide all parent menus recursively, if any (defaults to false)
360 hide : function(deep)
362 if (false === this.fireEvent("beforehide", this)) {
363 Roo.log("hide canceled");
366 this.hideMenuItems();
367 if(this.el && this.isVisible()){
370 this.activeItem.deactivate();
371 this.activeItem = null;
373 this.triggerEl.removeClass('open');;
374 this.el.removeClass('show');
376 this.fireEvent("hide", this);
378 if(deep === true && this.parentMenu){
379 this.parentMenu.hide(true);
383 onTriggerClick : function(e)
385 Roo.log('trigger click');
387 var target = e.getTarget();
389 Roo.log(target.nodeName.toLowerCase());
391 if(target.nodeName.toLowerCase() === 'i'){
397 onTriggerPress : function(e)
399 Roo.log('trigger press');
400 //Roo.log(e.getTarget());
401 // Roo.log(this.triggerEl.dom);
403 // trigger only occurs on normal menu's -- if it's a treeview or dropdown... do not hide/show..
404 var pel = Roo.get(e.getTarget());
405 if (pel.findParent('.dropdown-menu') || pel.findParent('.treeview-menu') ) {
406 Roo.log('is treeview or dropdown?');
410 if(e.getTarget().nodeName.toLowerCase() !== 'i' && this.isLink){
414 if (this.isVisible()) {
420 this.show(this.triggerEl, this.align, false);
423 if(this.stopEvent || e.getTarget().nodeName.toLowerCase() === 'i'){
430 hideMenuItems : function()
432 Roo.log("hide Menu Items");
437 this.el.select('.open',true).each(function(aa) {
439 aa.removeClass('open');
443 addxtypeChild : function (tree, cntr) {
444 var comp= Roo.bootstrap.Menu.superclass.addxtypeChild.call(this, tree, cntr);
446 this.menuitems.add(comp);
458 this.getEl().dom.innerHTML = '';
459 this.menuitems.clear();