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             Roo.log('sub??');
120             return true;
121         }
122         
123         return false;
124         
125     },
126     
127     initEvents : function() 
128     {
129         
130         if(this.isSubMenu){
131             Roo.log('is submenu');
132             return;
133         }
134         
135         Roo.log('not submenu');
136         
137         this.hidden = true;
138         
139         this.triggerEl = this.el.select('button.dropdown-toggle', true).first();
140         this.triggerEl.on('click', this.onTriggerPress, this);
141         
142         this.buttonEl = this.el.select('button.dropdown-button', true).first();
143         this.buttonEl.on('click', this.onClick, this);
144         
145     },
146     
147     onClick : function(e)
148     {
149         this.fireEvent("click", this, e);
150     },
151     
152     onTriggerPress  : function(e)
153     {   
154         if (this.isVisible()) {
155             this.hide();
156         } else {
157             this.show();
158         }
159     },
160     
161     isVisible : function(){
162         return !this.hidden;
163     },
164     
165     show : function()
166     {
167         this.fireEvent("beforeshow", this);
168         
169         this.hidden = false;
170         this.el.addClass('open');
171         
172         Roo.get(document).on("mouseup", this.onMouseUp, this);
173         
174         this.fireEvent("show", this);
175         
176     },
177     
178     hide : function()
179     {
180         this.fireEvent("beforehide", this);
181         
182         this.hidden = true;
183         this.el.removeClass('open');
184         
185         Roo.get(document).un("mouseup", this.onMouseUp);
186         
187         this.fireEvent("hide", this);
188     },
189     
190     onMouseUp : function()
191     {
192         this.hide();
193     }
194     
195 });
196
197  
198