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)
19 * @param {Object} config The config object
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);
33 * Fires before this menu is displayed (return false to block)
34 * @param {Roo.menu.Menu} this
39 * Fires before this menu is hidden (return false to block)
40 * @param {Roo.menu.Menu} this
45 * Fires after this menu is displayed
46 * @param {Roo.menu.Menu} this
51 * Fires after this menu is hidden
52 * @param {Roo.menu.Menu} this
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
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
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
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
87 this.menuitems = new Roo.util.MixedCollection(false, function(o) { return o.el.id; });
90 Roo.extend(Roo.bootstrap.Menu, Roo.bootstrap.Component, {
94 triggerEl : false, // is this set by component builder? -- it should really be fetched from parent()???
97 * @cfg {Boolean} registerMenu True (default) - means that clicking on screen etc. hides it.
101 menuItems :false, // stores the menu items..
111 container_method : 'getDocumentBody',
114 getChildContainer : function() {
118 getAutoCreate : function(){
120 //if (['right'].indexOf(this.align)!==-1) {
121 // cfg.cn[1].cls += ' pull-right'
127 cls : 'dropdown-menu' ,
128 style : 'z-index:1000'
132 if (this.type === 'submenu') {
133 cfg.cls = 'submenu active';
135 if (this.type === 'treeview') {
136 cfg.cls = 'treeview-menu';
141 initEvents : function() {
143 // Roo.log("ADD event");
144 // Roo.log(this.triggerEl.dom);
146 this.triggerEl.on('click', this.onTriggerClick, this);
148 this.triggerEl.on(Roo.isTouch ? 'touchstart' : 'mouseup', this.onTriggerPress, this);
151 if (this.triggerEl.hasClass('nav-item') && this.triggerEl.select('.nav-link',true).length) {
152 // dropdown toggle on the 'a' in BS4?
153 this.triggerEl.select('.nav-link',true).first().addClass('dropdown-toggle');
154 } else if (!this.triggerEl.hasClass('no-dropdown-toggle')) {
155 this.triggerEl.addClass('dropdown-toggle');
158 this.el.on('touchstart' , this.onTouch, this);
160 this.el.on('click' , this.onClick, this);
162 this.el.on("mouseover", this.onMouseOver, this);
163 this.el.on("mouseout", this.onMouseOut, this);
167 findTargetItem : function(e)
169 var t = e.getTarget(".dropdown-menu-item", this.el, true);
173 //Roo.log(t); Roo.log(t.id);
175 //Roo.log(this.menuitems);
176 return this.menuitems.get(t.id);
178 //return this.items.get(t.menuItemId);
184 onTouch : function(e)
186 Roo.log("menu.onTouch");
187 //e.stopEvent(); this make the user popdown broken
191 onClick : function(e)
193 Roo.log("menu.onClick");
195 var t = this.findTargetItem(e);
196 if(!t || t.isContainer){
201 if (Roo.isTouch && e.type == 'touchstart' && t.menu && !t.disabled) {
202 if(t == this.activeItem && t.shouldDeactivate(e)){
203 this.activeItem.deactivate();
204 delete this.activeItem;
208 this.setActiveItem(t, true);
216 Roo.log('pass click event');
220 this.fireEvent("click", this, t, e);
224 if(!t.href.length || t.href == '#'){
225 (function() { _this.hide(); }).defer(100);
230 onMouseOver : function(e){
231 var t = this.findTargetItem(e);
234 // if(t.canActivate && !t.disabled){
235 // this.setActiveItem(t, true);
239 this.fireEvent("mouseover", this, e, t);
241 isVisible : function(){
244 onMouseOut : function(e){
245 var t = this.findTargetItem(e);
248 // if(t == this.activeItem && t.shouldDeactivate(e)){
249 // this.activeItem.deactivate();
250 // delete this.activeItem;
253 this.fireEvent("mouseout", this, e, t);
258 * Displays this menu relative to another element
259 * @param {String/HTMLElement/Roo.Element} element The element to align to
260 * @param {String} position (optional) The {@link Roo.Element#alignTo} anchor position to use in aligning to
261 * the element (defaults to this.defaultAlign)
262 * @param {Roo.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
264 show : function(el, pos, parentMenu)
266 if (false === this.fireEvent("beforeshow", this)) {
267 Roo.log("show canceled");
270 this.parentMenu = parentMenu;
275 this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign), parentMenu, false);
278 * Displays this menu at a specific xy position
279 * @param {Array} xyPosition Contains X & Y [x, y] values for the position at which to show the menu (coordinates are page-based)
280 * @param {Roo.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
282 showAt : function(xy, parentMenu, /* private: */_e){
283 this.parentMenu = parentMenu;
288 this.fireEvent("beforeshow", this);
289 //xy = this.el.adjustForConstraints(xy);
293 this.hideMenuItems();
295 this.triggerEl.addClass('open');
296 this.el.addClass('show');
298 // reassign x when hitting right
299 if(this.el.getWidth() + xy[0] >= Roo.lib.Dom.getViewWidth()){
300 xy[0] = xy[0] - this.el.getWidth() + this.triggerEl.getWidth();
303 // reassign y when hitting bottom
304 if(this.el.getHeight() + xy[1] >= Roo.lib.Dom.getViewHeight()){
305 xy[1] = xy[1] - this.el.getHeight() - this.triggerEl.getHeight();
308 // but the list may align on trigger left or trigger top... should it be a properity?
310 if(this.el.getStyle('top') != 'auto' && this.el.getStyle('top').slice(-1) != "%"){
315 this.fireEvent("show", this);
321 this.doFocus.defer(50, this);
325 doFocus : function(){
327 this.focusEl.focus();
332 * Hides this menu and optionally all parent menus
333 * @param {Boolean} deep (optional) True to hide all parent menus recursively, if any (defaults to false)
335 hide : function(deep)
337 if (false === this.fireEvent("beforehide", this)) {
338 Roo.log("hide canceled");
341 this.hideMenuItems();
342 if(this.el && this.isVisible()){
345 this.activeItem.deactivate();
346 this.activeItem = null;
348 this.triggerEl.removeClass('open');;
349 this.el.removeClass('show');
351 this.fireEvent("hide", this);
353 if(deep === true && this.parentMenu){
354 this.parentMenu.hide(true);
358 onTriggerClick : function(e)
360 Roo.log('trigger click');
362 var target = e.getTarget();
364 Roo.log(target.nodeName.toLowerCase());
366 if(target.nodeName.toLowerCase() === 'i'){
372 onTriggerPress : function(e)
374 Roo.log('trigger press');
375 //Roo.log(e.getTarget());
376 // Roo.log(this.triggerEl.dom);
378 // trigger only occurs on normal menu's -- if it's a treeview or dropdown... do not hide/show..
379 var pel = Roo.get(e.getTarget());
380 if (pel.findParent('.dropdown-menu') || pel.findParent('.treeview-menu') ) {
381 Roo.log('is treeview or dropdown?');
385 if(e.getTarget().nodeName.toLowerCase() !== 'i' && this.isLink){
389 if (this.isVisible()) {
394 this.show(this.triggerEl, '?', false);
397 if(this.stopEvent || e.getTarget().nodeName.toLowerCase() === 'i'){
404 hideMenuItems : function()
406 Roo.log("hide Menu Items");
411 this.el.select('.open',true).each(function(aa) {
413 aa.removeClass('open');
417 addxtypeChild : function (tree, cntr) {
418 var comp= Roo.bootstrap.Menu.superclass.addxtypeChild.call(this, tree, cntr);
420 this.menuitems.add(comp);
432 this.getEl().dom.innerHTML = '';
433 this.menuitems.clear();