Roo/bootstrap/menu/Menu.js
[roojs1] / Roo / bootstrap / menu / Menu.js
1 /*
2  * - LGPL
3  *
4  * menu
5  * 
6  */
7 Roo.bootstrap.menu = Roo.bootstrap.menu || {};
8
9 /**
10  * @class Roo.bootstrap.menu.Menu
11  * @extends Roo.bootstrap.Component
12  * Bootstrap Menu class - container for Menu
13  * @cfg {String} html Text of the menu
14  * @cfg {String} weight (default | primary | success | info | warning | danger | inverse)
15  * @cfg {Boolean} preventDefault (true | false) default true
16  * 
17  * @constructor
18  * Create a new Menu
19  * @param {Object} config The config object
20  */
21
22
23 Roo.bootstrap.menu.Menu = function(config){
24     Roo.bootstrap.menu.Menu.superclass.constructor.call(this, config);
25     
26     this.addEvents({
27         /**
28          * @event beforeshow
29          * Fires before this menu is displayed
30          * @param {Roo.bootstrap.menu.Menu} this
31          */
32         beforeshow : true,
33         /**
34          * @event beforehide
35          * Fires before this menu is hidden
36          * @param {Roo.bootstrap.menu.Menu} this
37          */
38         beforehide : true,
39         /**
40          * @event show
41          * Fires after this menu is displayed
42          * @param {Roo.bootstrap.menu.Menu} this
43          */
44         show : true,
45         /**
46          * @event hide
47          * Fires after this menu is hidden
48          * @param {Roo.bootstrap.menu.Menu} this
49          */
50         hide : true,
51         /**
52          * @event click
53          * Fires when this menu is clicked (or when the enter key is pressed while it is active)
54          * @param {Roo.bootstrap.menu.Menu} this
55          * @param {Roo.EventObject} e
56          */
57         click : true
58     });
59     
60 };
61
62 Roo.extend(Roo.bootstrap.menu.Menu, Roo.bootstrap.Component,  {
63     
64     submenu : false,
65     html : '',
66     weight : 'default',
67     preventDefault: true,
68     
69     
70     getChildContainer : function() {
71         if(this.isSubMenu()){
72             return this.el;
73         }
74         
75         return this.el.select('ul.dropdown-menu', true).first();  
76     },
77     
78     getAutoCreate : function()
79     {
80         var cfg = {
81             tag : 'div',
82             cls : 'btn-group',
83             cn : [
84                 {
85                     tag : 'button',
86                     cls : 'dropdown-button btn btn-' + this.weight,
87                     html : this.html
88                 },
89                 {
90                     tag : 'button',
91                     cls : 'dropdown-toggle btn btn-' + this.weight,
92                     cn : [
93                         {
94                             tag : 'span',
95                             cls : 'caret'
96                         }
97                     ]
98                 },
99                 {
100                     tag : 'ul',
101                     cls : 'dropdown-menu'
102                 }
103             ]
104             
105         };
106         
107         if(this.isSubMenu()){
108             cfg = {
109                 tag : 'ul',
110                 cls : 'dropdown-menu'
111             }
112         }
113         
114         return cfg;
115     },
116     
117     isSubMenu : function()
118     {
119         if(this.parent() instanceof Roo.bootstrap.menu.Item){
120             return true;
121         }
122         
123         return false;
124         
125     },
126     
127     initEvents : function() 
128     {
129         
130         if(this.isSubMenu()){
131             return;
132         }
133         
134         this.hidden = true;
135         
136         this.triggerEl = this.el.select('button.dropdown-toggle', true).first();
137         this.triggerEl.on('click', this.onTriggerPress, this);
138         
139         this.buttonEl = this.el.select('button.dropdown-button', true).first();
140         this.buttonEl.on('click', this.onClick, this);
141         
142     },
143     
144     onClick : function(e)
145     {
146         if(this.preventDefault){
147             e.preventDefault();
148         }
149         
150         this.fireEvent("click", this, e);
151     },
152     
153     onTriggerPress  : function(e)
154     {   
155         if (this.isVisible()) {
156             this.hide();
157         } else {
158             this.show();
159         }
160     },
161     
162     isVisible : function(){
163         return !this.hidden;
164     },
165     
166     show : function()
167     {
168         this.fireEvent("beforeshow", this);
169         
170         this.hidden = false;
171         this.el.addClass('open');
172         
173         Roo.get(document).on("mouseup", this.onMouseUp, this);
174         
175         this.fireEvent("show", this);
176         
177     },
178     
179     hide : function()
180     {
181         this.fireEvent("beforehide", this);
182         
183         this.hidden = true;
184         this.el.removeClass('open');
185         
186         Roo.get(document).un("mouseup", this.onMouseUp);
187         
188         this.fireEvent("hide", this);
189     },
190     
191     onMouseUp : function()
192     {
193         this.hide();
194     }
195     
196 });
197
198  
199