a556d1f6d77cc2c5c20d0dc7dd8b855e4fe808b7
[roojs1] / Roo / bootstrap / NavItem.js
1 /*
2  * - LGPL
3  *
4  * row
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.NavItem
10  * @extends Roo.bootstrap.Component
11  * Bootstrap Navbar.NavItem class
12  * @cfg {String} href  link to
13  * @cfg {String} html content of button
14  * @cfg {String} badge text inside badge
15  * @cfg {String} badgecls (bg-green|bg-red|bg-yellow)the extra classes for the badge
16  * @cfg {String} glyphicon name of glyphicon
17  * @cfg {String} icon name of font awesome icon
18  * @cfg {Boolean} active Is item active
19  * @cfg {Boolean} disabled Is item disabled
20  
21  * @cfg {Boolean} preventDefault (true | false) default false
22  * @cfg {String} tabId the tab that this item activates.
23  * @cfg {String} tagtype (a|span) render as a href or span?
24   
25  * @constructor
26  * Create a new Navbar Item
27  * @param {Object} config The config object
28  */
29 Roo.bootstrap.NavItem = function(config){
30     Roo.bootstrap.NavItem.superclass.constructor.call(this, config);
31     this.addEvents({
32         // raw events
33         /**
34          * @event click
35          * The raw click event for the entire grid.
36          * @param {Roo.EventObject} e
37          */
38         "click" : true,
39          /**
40             * @event changed
41             * Fires when the active item active state changes
42             * @param {Roo.bootstrap.NavItem} this
43             * @param {boolean} state the new state
44              
45          */
46         'changed': true
47     });
48    
49 };
50
51 Roo.extend(Roo.bootstrap.NavItem, Roo.bootstrap.Component,  {
52     
53     href: false,
54     html: '',
55     badge: '',
56     icon: false,
57     glyphicon: false,
58     active: false,
59     preventDefault : false,
60     tabId : false,
61     tagtype : 'a',
62     disabled : false,
63     
64     was_active : false,
65     
66     getAutoCreate : function(){
67          
68         var cfg = {
69             tag: 'li',
70             cls: 'nav-item'
71             
72         }
73         if (this.active) {
74             cfg.cls = typeof(cfg.cls) == 'undefined' ? 'active' : cfg.cls + ' active';
75         }
76         if (this.disabled) {
77             cfg.cls += ' disabled';
78         }
79         
80         if (this.href || this.html || this.glyphicon || this.icon) {
81             cfg.cn = [
82                 {
83                     tag: this.tagtype,
84                     href : this.href || "#",
85                     html: this.html || ''
86                 }
87             ];
88             
89             if (this.icon) {
90                 cfg.cn[0].html = '<i class="'+this.icon+'"></i> <span>' + cfg.cn[0].html + '</span>'
91             }
92
93             if(this.glyphicon) {
94                 cfg.cn[0].html = '<span class="glyphicon glyphicon-' + this.glyphicon + '"></span> '  + cfg.cn[0].html;
95             }
96             
97             if (this.menu) {
98                 
99                 cfg.cn[0].html += " <span class='caret'></span>";
100              
101             }
102             
103             if (this.badge !== '') {
104                  
105                 cfg.cn[0].html += ' <span class="badge">' + this.badge + '</span>';
106             }
107         }
108         
109         
110         
111         return cfg;
112     },
113     initEvents: function() {
114        // Roo.log('init events?');
115        // Roo.log(this.el.dom);
116         if (typeof (this.menu) != 'undefined') {
117             this.menu.parentType = this.xtype;
118             this.menu.triggerEl = this.el;
119             this.addxtype(Roo.apply({}, this.menu));
120         }
121
122        
123         this.el.select('a',true).on('click', this.onClick, this);
124         // at this point parent should be available..
125         this.parent().register(this);
126     },
127     
128     onClick : function(e)
129     {
130          
131         if(this.preventDefault){
132             e.preventDefault();
133         }
134         if (this.disabled) {
135             return;
136         }
137         
138         var tg = Roo.bootstrap.TabGroup.get(this.navId);
139         if (tg && tg.transition) {
140             Roo.log("waiting for the transitionend");
141             return;
142         }
143         
144         Roo.log("fire event clicked");
145         if(this.fireEvent('click', this, e) === false){
146             return;
147         };
148         var p = this.parent();
149         if (['tabs','pills'].indexOf(p.type)!==-1) {
150             if (typeof(p.setActiveItem) !== 'undefined') {
151                 p.setActiveItem(this);
152             }
153         }
154         // if parent is a navbarheader....
155         if (p.parentType == 'NavHeaderbar' && !this.menu) {
156             // remove the collapsed menu expand...
157             p.parent().el.select('.navbar-collapse',true).toggleClass('in');  
158         }
159         
160     },
161     
162     isActive: function () {
163         return this.active
164     },
165     setActive : function(state, fire, is_was_active)
166     {
167         if (this.active && !state & this.navId) {
168             this.was_active = true;
169             var nv = Roo.bootstrap.NavGroup.get(this.navId);
170             if (nv) {
171                 nv.clearWasActive(this);
172             }
173             
174         }
175         this.active = state;
176         
177         if (!state ) {
178             this.el.removeClass('active');
179         } else if (!this.el.hasClass('active')) {
180             this.el.addClass('active');
181         }
182         if (fire) {
183             this.fireEvent('changed', this, state);
184         }
185         
186         // show a panel if it's registered and related..
187         
188         if (!this.navId || !this.tabId || !state || is_was_active) {
189             return;
190         }
191         
192         var tg = Roo.bootstrap.TabGroup.get(this.navId);
193         if (!tg) {
194             return;
195         }
196         var pan = tg.getPanelByName(this.tabId);
197         if (!pan) {
198             return;
199         }
200         // if we can not flip to new panel - go back to old nav highlight..
201         if (false == tg.showPanel(pan)) {
202             var nv = Roo.bootstrap.NavGroup.get(this.navId);
203             if (nv) {
204                 var onav = nv.getWasActive();
205                 if (onav) {
206                     onav.setActive(true, false, true);
207                 }
208             }
209             
210         }
211         
212         
213         
214     },
215      // this should not be here...
216     setDisabled : function(state)
217     {
218         this.disabled = state;
219         if (!state ) {
220             this.el.removeClass('disabled');
221         } else if (!this.el.hasClass('disabled')) {
222             this.el.addClass('disabled');
223         }
224         
225     }
226 });
227  
228
229