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);
31 * Fires before this menu is displayed
32 * @param {Roo.menu.Menu} this
37 * Fires before this menu is hidden
38 * @param {Roo.menu.Menu} this
43 * Fires after this menu is displayed
44 * @param {Roo.menu.Menu} this
49 * Fires after this menu is hidden
50 * @param {Roo.menu.Menu} this
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
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
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
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
85 this.menuitems = new Roo.util.MixedCollection(false, function(o) { return o.el.id; });
88 Roo.extend(Roo.bootstrap.Menu, Roo.bootstrap.Component, {
92 triggerEl : false, // is this set by component builder? -- it should really be fetched from parent()???
95 * @cfg {Boolean} registerMenu True (default) - means that clicking on screen etc. hides it.
99 menuItems :false, // stores the menu items..
109 getChildContainer : function() {
113 getAutoCreate : function(){
115 //if (['right'].indexOf(this.align)!==-1) {
116 // cfg.cn[1].cls += ' pull-right'
122 cls : 'dropdown-menu' ,
123 style : 'z-index:1000'
127 if (this.type === 'submenu') {
128 cfg.cls = 'submenu active';
130 if (this.type === 'treeview') {
131 cfg.cls = 'treeview-menu';
136 initEvents : function() {
138 // Roo.log("ADD event");
139 // Roo.log(this.triggerEl.dom);
141 this.triggerEl.on('click', this.onTriggerClick, this);
143 this.triggerEl.on(Roo.isTouch ? 'touchstart' : 'mouseup', this.onTriggerPress, this);
146 if (this.triggerEl.hasClass('nav-item')) {
147 // dropdown toggle on the 'a' in BS4?
148 this.triggerEl.addClass('dropdown-toggle').select('.nav-link',true).first().addClass('nav-item');
150 this.triggerEl.addClass('dropdown-toggle');
153 this.el.on('touchstart' , this.onTouch, this);
155 this.el.on('click' , this.onClick, this);
157 this.el.on("mouseover", this.onMouseOver, this);
158 this.el.on("mouseout", this.onMouseOut, this);
162 findTargetItem : function(e)
164 var t = e.getTarget(".dropdown-menu-item", this.el, true);
168 //Roo.log(t); Roo.log(t.id);
170 //Roo.log(this.menuitems);
171 return this.menuitems.get(t.id);
173 //return this.items.get(t.menuItemId);
179 onTouch : function(e)
181 Roo.log("menu.onTouch");
182 //e.stopEvent(); this make the user popdown broken
186 onClick : function(e)
188 Roo.log("menu.onClick");
190 var t = this.findTargetItem(e);
191 if(!t || t.isContainer){
196 if (Roo.isTouch && e.type == 'touchstart' && t.menu && !t.disabled) {
197 if(t == this.activeItem && t.shouldDeactivate(e)){
198 this.activeItem.deactivate();
199 delete this.activeItem;
203 this.setActiveItem(t, true);
211 Roo.log('pass click event');
215 this.fireEvent("click", this, t, e);
219 if(!t.href.length || t.href == '#'){
220 (function() { _this.hide(); }).defer(100);
225 onMouseOver : function(e){
226 var t = this.findTargetItem(e);
229 // if(t.canActivate && !t.disabled){
230 // this.setActiveItem(t, true);
234 this.fireEvent("mouseover", this, e, t);
236 isVisible : function(){
239 onMouseOut : function(e){
240 var t = this.findTargetItem(e);
243 // if(t == this.activeItem && t.shouldDeactivate(e)){
244 // this.activeItem.deactivate();
245 // delete this.activeItem;
248 this.fireEvent("mouseout", this, e, t);
253 * Displays this menu relative to another element
254 * @param {String/HTMLElement/Roo.Element} element The element to align to
255 * @param {String} position (optional) The {@link Roo.Element#alignTo} anchor position to use in aligning to
256 * the element (defaults to this.defaultAlign)
257 * @param {Roo.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
259 show : function(el, pos, parentMenu){
260 this.parentMenu = parentMenu;
264 this.fireEvent("beforeshow", this);
265 this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign), parentMenu, false);
268 * Displays this menu at a specific xy position
269 * @param {Array} xyPosition Contains X & Y [x, y] values for the position at which to show the menu (coordinates are page-based)
270 * @param {Roo.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
272 showAt : function(xy, parentMenu, /* private: */_e){
273 this.parentMenu = parentMenu;
278 this.fireEvent("beforeshow", this);
279 //xy = this.el.adjustForConstraints(xy);
283 this.hideMenuItems();
285 this.triggerEl.addClass('open');
287 // reassign x when hitting right
288 if(this.el.getWidth() + xy[0] >= Roo.lib.Dom.getViewWidth()){
289 xy[0] = xy[0] - this.el.getWidth() + this.triggerEl.getWidth();
292 // reassign y when hitting bottom
293 if(this.el.getHeight() + xy[1] >= Roo.lib.Dom.getViewHeight()){
294 xy[1] = xy[1] - this.el.getHeight() - this.triggerEl.getHeight();
297 // but the list may align on trigger left or trigger top... should it be a properity?
299 if(this.el.getStyle('top') != 'auto' && this.el.getStyle('top').slice(-1) != "%"){
304 this.fireEvent("show", this);
310 this.doFocus.defer(50, this);
314 doFocus : function(){
316 this.focusEl.focus();
321 * Hides this menu and optionally all parent menus
322 * @param {Boolean} deep (optional) True to hide all parent menus recursively, if any (defaults to false)
324 hide : function(deep)
327 this.hideMenuItems();
328 if(this.el && this.isVisible()){
329 this.fireEvent("beforehide", this);
331 this.activeItem.deactivate();
332 this.activeItem = null;
334 this.triggerEl.removeClass('open');;
336 this.fireEvent("hide", this);
338 if(deep === true && this.parentMenu){
339 this.parentMenu.hide(true);
343 onTriggerClick : function(e)
345 Roo.log('trigger click');
347 var target = e.getTarget();
349 Roo.log(target.nodeName.toLowerCase());
351 if(target.nodeName.toLowerCase() === 'i'){
357 onTriggerPress : function(e)
359 Roo.log('trigger press');
360 //Roo.log(e.getTarget());
361 // Roo.log(this.triggerEl.dom);
363 // trigger only occurs on normal menu's -- if it's a treeview or dropdown... do not hide/show..
364 var pel = Roo.get(e.getTarget());
365 if (pel.findParent('.dropdown-menu') || pel.findParent('.treeview-menu') ) {
366 Roo.log('is treeview or dropdown?');
370 if(e.getTarget().nodeName.toLowerCase() !== 'i' && this.isLink){
374 if (this.isVisible()) {
379 this.show(this.triggerEl, false, false);
382 if(this.stopEvent || e.getTarget().nodeName.toLowerCase() === 'i'){
389 hideMenuItems : function()
391 Roo.log("hide Menu Items");
395 //$(backdrop).remove()
396 this.el.select('.open',true).each(function(aa) {
398 aa.removeClass('open');
399 //var parent = getParent($(this))
400 //var relatedTarget = { relatedTarget: this }
402 //$parent.trigger(e = $.Event('hide.bs.dropdown', relatedTarget))
403 //if (e.isDefaultPrevented()) return
404 //$parent.removeClass('open').trigger('hidden.bs.dropdown', relatedTarget)
407 addxtypeChild : function (tree, cntr) {
408 var comp= Roo.bootstrap.Menu.superclass.addxtypeChild.call(this, tree, cntr);
410 this.menuitems.add(comp);
422 this.getEl().dom.innerHTML = '';
423 this.menuitems.clear();