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