Roo/bootstrap/NavSidebarItem.js
[roojs1] / Roo / bootstrap / NavSidebarItem.js
1 /*
2  * - LGPL
3  *
4  * sidebar item
5  *
6  *  li
7  *    <span> icon </span>
8  *    <span> text </span>
9  *    <span>badge </span>
10  */
11
12 /**
13  * @class Roo.bootstrap.NavSidebarItem
14  * @extends Roo.bootstrap.NavItem
15  * Bootstrap Navbar.NavSidebarItem class
16  * {String} badgeWeight (default|primary|success|info|warning|danger)the extra classes for the badge
17  * {Boolean} open is the menu open
18  * {Boolean} buttonView use button as the tigger el rather that a (default false)
19  * {String} buttonWeight (default|primary|success|info|warning|danger)the extra classes for the button
20  * {String} buttonSize (sm|md|lg)the extra classes for the button
21  * @constructor
22  * Create a new Navbar Button
23  * @param {Object} config The config object
24  */
25 Roo.bootstrap.NavSidebarItem = function(config){
26     Roo.bootstrap.NavSidebarItem.superclass.constructor.call(this, config);
27     this.addEvents({
28         // raw events
29         /**
30          * @event click
31          * The raw click event for the entire grid.
32          * @param {Roo.EventObject} e
33          */
34         "click" : true,
35          /**
36             * @event changed
37             * Fires when the active item active state changes
38             * @param {Roo.bootstrap.NavSidebarItem} this
39             * @param {boolean} state the new state
40              
41          */
42         'changed': true
43     });
44    
45 };
46
47 Roo.extend(Roo.bootstrap.NavSidebarItem, Roo.bootstrap.NavItem,  {
48     
49     badgeWeight : 'default',
50     
51     open: false,
52     
53     buttonView : false,
54     
55     buttonWeight : 'default',
56     
57     buttonSize : 'md',
58     
59     showArrow : true,
60     
61     getAutoCreate : function(){
62         
63         
64         var a = {
65                 tag: 'a',
66                 href : this.href || '#',
67                 cls: '',
68                 html : '',
69                 cn : []
70         };
71         
72         if(this.buttonView){
73             a = {
74                 tag: 'button',
75                 href : this.href || '#',
76                 cls: 'btn btn-' + this.buttonWeight + ' btn-' + this.buttonSize + 'roo-button-dropdown-toggle',
77                 html : this.html,
78                 cn : []
79             };
80         }
81         
82         var cfg = {
83             tag: 'li',
84             cls: '',
85             cn: [ a ]
86         };
87         
88         if (this.active) {
89             cfg.cls += ' active';
90         }
91         
92         if (this.disabled) {
93             cfg.cls += ' disabled';
94         }
95         if (this.open) {
96             cfg.cls += ' open x-open';
97         }
98         // left icon..
99         if (this.glyphicon || this.icon) {
100             var c = this.glyphicon  ? ('glyphicon glyphicon-'+this.glyphicon)  : this.icon;
101             a.cn.push({ tag : 'i', cls : c }) ;
102         }
103         
104         if(!this.buttonView){
105             var span = {
106                 tag: 'span',
107                 html : this.html || ''
108             };
109
110             a.cn.push(span);
111             
112         }
113         
114         if (this.badge !== '') {
115             a.cn.push({ tag: 'span',  cls : 'badge pull-right badge-' + this.badgeWeight, html: this.badge }); 
116         }
117             
118         // then badge..
119         
120         // fi
121         if (this.menu) {
122             a.cn.push({ tag : 'i', cls : 'glyphicon glyphicon-chevron-down pull-right'});
123             a.cls += ' dropdown-toggle treeview' ;
124         }
125         
126         return cfg;
127          
128            
129     },
130     
131     initEvents : function()
132     { 
133         if (typeof (this.menu) != 'undefined') {
134             this.menu.parentType = this.xtype;
135             this.menu.triggerEl = this.el;
136             this.menu = this.addxtype(Roo.apply({}, this.menu));
137         }
138         
139         this.el.on('click', this.onClick, this);
140         
141         if(this.badge !== ''){
142             this.badgeEl = this.el.select('.badge', true).first().setVisibilityMode(Roo.Element.DISPLAY);
143         }
144         
145     },
146     
147     onClick : function(e)
148     {
149         if(this.disabled){
150             e.preventDefault();
151             return;
152         }
153         
154         if(this.preventDefault){
155             e.preventDefault();
156         }
157         
158         this.fireEvent('click', this);
159     },
160     
161     disable : function()
162     {
163         this.setDisabled(true);
164     },
165     
166     enable : function()
167     {
168         this.setDisabled(false);
169     },
170     
171     setDisabled : function(state)
172     {
173         if(this.disabled == state){
174             return;
175         }
176         
177         this.disabled = state;
178         
179         if (state) {
180             this.el.addClass('disabled');
181             return;
182         }
183         
184         this.el.removeClass('disabled');
185         
186         return;
187     },
188     
189     setActive : function(state)
190     {
191         if(this.active == state){
192             return;
193         }
194         
195         this.active = state;
196         
197         if (state) {
198             this.el.addClass('active');
199             return;
200         }
201         
202         this.el.removeClass('active');
203         
204         return;
205     },
206     
207     isActive: function () 
208     {
209         return this.active;
210     },
211     
212     setBadge : function(str)
213     {
214         if(!this.badgeEl){
215             return;
216         }
217         
218         this.badgeEl.dom.innerHTML = str;
219     }
220     
221    
222      
223  
224 });
225  
226
227