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