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         return cfg;
100          
101            
102     },
103     
104     initEvents : function()
105     { 
106         Roo.log('NavSidebarItem initEvents??');
107         
108         Roo.log(typeof(this.menu));
109         
110         if (typeof (this.menu) != 'undefined') {
111             this.menu.parentType = this.xtype;
112             this.menu.triggerEl = this.el;
113             this.menu = this.addxtype(Roo.apply({}, this.menu));
114         }
115         
116         this.el.on('click', this.onClick, this);
117        
118     
119         if(this.badge !== ''){
120  
121             this.badgeEl = this.el.select('.badge', true).first().setVisibilityMode(Roo.Element.DISPLAY);
122         }
123         
124     },
125     
126     onClick : function(e)
127     {
128         if(this.disabled){
129             e.preventDefault();
130             return;
131         }
132         
133         if(this.preventDefault){
134             e.preventDefault();
135         }
136         
137         this.fireEvent('click', this);
138     },
139     
140     disable : function()
141     {
142         this.setDisabled(true);
143     },
144     
145     enable : function()
146     {
147         this.setDisabled(false);
148     },
149     
150     setDisabled : function(state)
151     {
152         if(this.disabled == state){
153             return;
154         }
155         
156         this.disabled = state;
157         
158         if (state) {
159             this.el.addClass('disabled');
160             return;
161         }
162         
163         this.el.removeClass('disabled');
164         
165         return;
166     },
167     
168     setActive : function(state)
169     {
170         if(this.active == state){
171             return;
172         }
173         
174         this.active = state;
175         
176         if (state) {
177             this.el.addClass('active');
178             return;
179         }
180         
181         this.el.removeClass('active');
182         
183         return;
184     },
185     
186     isActive: function () 
187     {
188         return this.active;
189     },
190     
191     setBadge : function(str)
192     {
193         if(!this.badgeEl){
194             return;
195         }
196         
197         this.badgeEl.dom.innerHTML = str;
198     }
199     
200    
201      
202  
203 });
204  
205
206