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){
27 if (config.type == 'treeview') {
28 // normally menu's are drawn attached to the document to handle layering etc..
29 // however treeview (used by the docs menu is drawn into the parent element)
30 this.container_method = 'getChildContainer';
33 Roo.bootstrap.Menu.superclass.constructor.call(this, config);
34 if (this.registerMenu && this.type != 'treeview') {
35 Roo.bootstrap.MenuMgr.register(this);
42 * Fires before this menu is displayed (return false to block)
43 * @param {Roo.menu.Menu} this
48 * Fires before this menu is hidden (return false to block)
49 * @param {Roo.menu.Menu} this
54 * Fires after this menu is displayed
55 * @param {Roo.menu.Menu} this
60 * Fires after this menu is hidden
61 * @param {Roo.menu.Menu} this
66 * Fires when this menu is clicked (or when the enter key is pressed while it is active)
67 * @param {Roo.menu.Menu} this
68 * @param {Roo.menu.Item} menuItem The menu item that was clicked
69 * @param {Roo.EventObject} e
74 * Fires when the mouse is hovering over this menu
75 * @param {Roo.menu.Menu} this
76 * @param {Roo.EventObject} e
77 * @param {Roo.menu.Item} menuItem The menu item that was clicked
82 * Fires when the mouse exits this menu
83 * @param {Roo.menu.Menu} this
84 * @param {Roo.EventObject} e
85 * @param {Roo.menu.Item} menuItem The menu item that was clicked
90 * Fires when a menu item contained in this menu is clicked
91 * @param {Roo.menu.BaseItem} baseItem The BaseItem that was clicked
92 * @param {Roo.EventObject} e
96 this.menuitems = new Roo.util.MixedCollection(false, function(o) { return o.el.id; });
99 Roo.extend(Roo.bootstrap.Menu, Roo.bootstrap.Component, {
103 triggerEl : false, // is this set by component builder? -- it should really be fetched from parent()???
106 * @cfg {Boolean} registerMenu True (default) - means that clicking on screen etc. hides it.
110 menuItems :false, // stores the menu items..
120 container_method : 'getDocumentBody', // so the menu is rendered on the body and zIndex works.
127 getChildContainer : function() {
131 getAutoCreate : function(){
133 //if (['right'].indexOf(this.align)!==-1) {
134 // cfg.cn[1].cls += ' pull-right'
139 cls : 'dropdown-menu shadow' ,
140 style : 'z-index:1000'
144 if (this.type === 'submenu') {
145 cfg.cls = 'submenu active';
147 if (this.type === 'treeview') {
148 cfg.cls = 'treeview-menu';
153 initEvents : function() {
155 // Roo.log("ADD event");
156 // Roo.log(this.triggerEl.dom);
158 this.triggerEl.on('click', this.onTriggerClick, this);
160 this.triggerEl.on(Roo.isTouch ? 'touchstart' : 'mouseup', this.onTriggerPress, this);
162 if (!this.hideTrigger) {
163 if (this.triggerEl.hasClass('nav-item') && this.triggerEl.select('.nav-link',true).length) {
164 // dropdown toggle on the 'a' in BS4?
165 this.triggerEl.select('.nav-link',true).first().addClass('dropdown-toggle');
167 this.triggerEl.addClass('dropdown-toggle');
171 this.el.on('touchstart' , this.onTouch, this);
173 this.el.on('click' , this.onClick, this);
175 this.el.on("mouseover", this.onMouseOver, this);
176 this.el.on("mouseout", this.onMouseOut, this);
180 findTargetItem : function(e)
182 var t = e.getTarget(".dropdown-menu-item", this.el, true);
186 //Roo.log(t); Roo.log(t.id);
188 //Roo.log(this.menuitems);
189 return this.menuitems.get(t.id);
191 //return this.items.get(t.menuItemId);
197 onTouch : function(e)
199 Roo.log("menu.onTouch");
200 //e.stopEvent(); this make the user popdown broken
204 onClick : function(e)
206 Roo.log("menu.onClick");
208 var t = this.findTargetItem(e);
209 if(!t || t.isContainer){
214 if (Roo.isTouch && e.type == 'touchstart' && t.menu && !t.disabled) {
215 if(t == this.activeItem && t.shouldDeactivate(e)){
216 this.activeItem.deactivate();
217 delete this.activeItem;
221 this.setActiveItem(t, true);
229 Roo.log('pass click event');
233 this.fireEvent("click", this, t, e);
237 if(!t.href.length || t.href == '#'){
238 (function() { _this.hide(); }).defer(100);
243 onMouseOver : function(e){
244 var t = this.findTargetItem(e);
247 // if(t.canActivate && !t.disabled){
248 // this.setActiveItem(t, true);
252 this.fireEvent("mouseover", this, e, t);
254 isVisible : function(){
257 onMouseOut : function(e){
258 var t = this.findTargetItem(e);
261 // if(t == this.activeItem && t.shouldDeactivate(e)){
262 // this.activeItem.deactivate();
263 // delete this.activeItem;
266 this.fireEvent("mouseout", this, e, t);
271 * Displays this menu relative to another element
272 * @param {String/HTMLElement/Roo.Element} element The element to align to
273 * @param {String} position (optional) The {@link Roo.Element#alignTo} anchor position to use in aligning to
274 * the element (defaults to this.defaultAlign)
275 * @param {Roo.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
277 show : function(el, pos, parentMenu)
279 if (false === this.fireEvent("beforeshow", this)) {
280 Roo.log("show canceled");
283 this.parentMenu = parentMenu;
287 this.el.addClass('show'); // show otherwise we do not know how big we are..
289 var xy = this.el.getAlignToXY(el, pos);
291 // bl-tl << left align below
292 // tl-bl << left align
294 if(this.el.getWidth() + xy[0] >= Roo.lib.Dom.getViewWidth()){
295 // if it goes to far to the right.. -> align left.
296 xy = this.el.getAlignToXY(el, this.align.replace('/l/g', 'r'))
299 // was left align - go right?
300 xy = this.el.getAlignToXY(el, this.align.replace('/r/g', 'l'))
303 // goes down the bottom
304 if(this.el.getHeight() + xy[1] >= Roo.lib.Dom.getViewHeight() ||
306 var a = this.align.replace('?', '').split('-');
307 xy = this.el.getAlignToXY(el, a[1] + '-' + a[0] + '?')
311 this.showAt( xy , parentMenu, false);
314 * Displays this menu at a specific xy position
315 * @param {Array} xyPosition Contains X & Y [x, y] values for the position at which to show the menu (coordinates are page-based)
316 * @param {Roo.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
318 showAt : function(xy, parentMenu, /* private: */_e){
319 this.parentMenu = parentMenu;
324 this.fireEvent("beforeshow", this);
325 //xy = this.el.adjustForConstraints(xy);
329 this.hideMenuItems();
331 this.triggerEl.addClass('open');
332 this.el.addClass('show');
336 // reassign x when hitting right
338 // reassign y when hitting bottom
340 // but the list may align on trigger left or trigger top... should it be a properity?
342 if(this.el.getStyle('top') != 'auto' && this.el.getStyle('top').slice(-1) != "%"){
347 this.fireEvent("show", this);
353 this.doFocus.defer(50, this);
357 doFocus : function(){
359 this.focusEl.focus();
364 * Hides this menu and optionally all parent menus
365 * @param {Boolean} deep (optional) True to hide all parent menus recursively, if any (defaults to false)
367 hide : function(deep)
369 if (false === this.fireEvent("beforehide", this)) {
370 Roo.log("hide canceled");
373 this.hideMenuItems();
374 if(this.el && this.isVisible()){
377 this.activeItem.deactivate();
378 this.activeItem = null;
380 this.triggerEl.removeClass('open');;
381 this.el.removeClass('show');
383 this.fireEvent("hide", this);
385 if(deep === true && this.parentMenu){
386 this.parentMenu.hide(true);
390 onTriggerClick : function(e)
392 Roo.log('trigger click');
394 var target = e.getTarget();
396 Roo.log(target.nodeName.toLowerCase());
398 if(target.nodeName.toLowerCase() === 'i'){
404 onTriggerPress : function(e)
406 Roo.log('trigger press');
407 //Roo.log(e.getTarget());
408 // Roo.log(this.triggerEl.dom);
410 // trigger only occurs on normal menu's -- if it's a treeview or dropdown... do not hide/show..
411 var pel = Roo.get(e.getTarget());
412 if (pel.findParent('.dropdown-menu') || pel.findParent('.treeview-menu') ) {
413 Roo.log('is treeview or dropdown?');
417 if(e.getTarget().nodeName.toLowerCase() !== 'i' && this.isLink){
421 if (this.isVisible()) {
427 this.show(this.triggerEl, this.align, false);
430 if(this.stopEvent || e.getTarget().nodeName.toLowerCase() === 'i'){
437 hideMenuItems : function()
439 Roo.log("hide Menu Items");
444 this.el.select('.open',true).each(function(aa) {
446 aa.removeClass('open');
450 addxtypeChild : function (tree, cntr) {
451 var comp= Roo.bootstrap.Menu.superclass.addxtypeChild.call(this, tree, cntr);
453 this.menuitems.add(comp);
465 this.getEl().dom.innerHTML = '';
466 this.menuitems.clear();