Roo/bootstrap/NavItem.js
[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  * @cfg {Boolean} animateRef (true|false) link to element default false
25   
26  * @constructor
27  * Create a new Navbar Item
28  * @param {Object} config The config object
29  */
30 Roo.bootstrap.NavItem = function(config){
31     Roo.bootstrap.NavItem.superclass.constructor.call(this, config);
32     this.addEvents({
33         // raw events
34         /**
35          * @event click
36          * The raw click event for the entire grid.
37          * @param {Roo.EventObject} e
38          */
39         "click" : true,
40          /**
41             * @event changed
42             * Fires when the active item active state changes
43             * @param {Roo.bootstrap.NavItem} this
44             * @param {boolean} state the new state
45              
46          */
47         'changed': true
48     });
49    
50 };
51
52 Roo.extend(Roo.bootstrap.NavItem, Roo.bootstrap.Component,  {
53     
54     href: false,
55     html: '',
56     badge: '',
57     icon: false,
58     glyphicon: false,
59     active: false,
60     preventDefault : false,
61     tabId : false,
62     tagtype : 'a',
63     disabled : false,
64     animateRef : false,
65     was_active : false,
66     
67     getAutoCreate : function(){
68          
69         var cfg = {
70             tag: 'li',
71             cls: 'nav-item'
72             
73         }
74         if (this.active) {
75             cfg.cls = typeof(cfg.cls) == 'undefined' ? 'active' : cfg.cls + ' active';
76         }
77         if (this.disabled) {
78             cfg.cls += ' disabled';
79         }
80         
81         if (this.href || this.html || this.glyphicon || this.icon) {
82             cfg.cn = [
83                 {
84                     tag: this.tagtype,
85                     href : this.href || "#",
86                     html: this.html || ''
87                 }
88             ];
89             
90             if (this.icon) {
91                 cfg.cn[0].html = '<i class="'+this.icon+'"></i> <span>' + cfg.cn[0].html + '</span>'
92             }
93
94             if(this.glyphicon) {
95                 cfg.cn[0].html = '<span class="glyphicon glyphicon-' + this.glyphicon + '"></span> '  + cfg.cn[0].html;
96             }
97             
98             if (this.menu) {
99                 
100                 cfg.cn[0].html += " <span class='caret'></span>";
101              
102             }
103             
104             if (this.badge !== '') {
105                  
106                 cfg.cn[0].html += ' <span class="badge">' + this.badge + '</span>';
107             }
108         }
109         
110         
111         
112         return cfg;
113     },
114     initEvents: function() 
115     {
116         if (typeof (this.menu) != 'undefined') {
117             this.menu.parentType = this.xtype;
118             this.menu.triggerEl = this.el;
119             this.menu = this.addxtype(Roo.apply({}, this.menu));
120         }
121         
122         this.el.select('a',true).on('click', this.onClick, this);
123         
124         if(this.tagtype == 'span'){
125             this.el.select('span',true).on('click', this.onClick, this);
126         }
127        
128         // at this point parent should be available..
129         this.parent().register(this);
130     },
131     
132     onClick : function(e)
133     {
134         if(
135                 this.preventDefault || 
136                 this.href == '#' ||
137                 (this.animateRef && this.href.charAt(0) == '#')
138         ){
139             e.preventDefault();
140         }
141         
142         if (this.disabled) {
143             return;
144         }
145         
146         var tg = Roo.bootstrap.TabGroup.get(this.navId);
147         if (tg && tg.transition) {
148             Roo.log("waiting for the transitionend");
149             return;
150         }
151         
152         Roo.log("fire event clicked");
153         if(this.fireEvent('click', this, e) === false){
154             return;
155         };
156         
157         if(this.tagtype == 'span'){
158             return;
159         }
160         
161         if(this.animateRef && this.href.charAt(0) == '#'){
162             this.scrollToElement();
163             return;
164         }
165         
166         var p = this.parent();
167         if (['tabs','pills'].indexOf(p.type)!==-1) {
168             if (typeof(p.setActiveItem) !== 'undefined') {
169                 p.setActiveItem(this);
170             }
171         }
172         // if parent is a navbarheader....- and link is probably a '#' page ref.. then remove the expanded menu.
173         if (p.parentType == 'NavHeaderbar' && !this.menu) {
174             // remove the collapsed menu expand...
175             p.parent().el.select('.navbar-collapse',true).removeClass('in');  
176         }
177         
178     },
179     
180     isActive: function () {
181         return this.active
182     },
183     setActive : function(state, fire, is_was_active)
184     {
185         if (this.active && !state & this.navId) {
186             this.was_active = true;
187             var nv = Roo.bootstrap.NavGroup.get(this.navId);
188             if (nv) {
189                 nv.clearWasActive(this);
190             }
191             
192         }
193         this.active = state;
194         
195         if (!state ) {
196             this.el.removeClass('active');
197         } else if (!this.el.hasClass('active')) {
198             this.el.addClass('active');
199         }
200         if (fire) {
201             this.fireEvent('changed', this, state);
202         }
203         
204         // show a panel if it's registered and related..
205         
206         if (!this.navId || !this.tabId || !state || is_was_active) {
207             return;
208         }
209         
210         var tg = Roo.bootstrap.TabGroup.get(this.navId);
211         if (!tg) {
212             return;
213         }
214         var pan = tg.getPanelByName(this.tabId);
215         if (!pan) {
216             return;
217         }
218         // if we can not flip to new panel - go back to old nav highlight..
219         if (false == tg.showPanel(pan)) {
220             var nv = Roo.bootstrap.NavGroup.get(this.navId);
221             if (nv) {
222                 var onav = nv.getWasActive();
223                 if (onav) {
224                     onav.setActive(true, false, true);
225                 }
226             }
227             
228         }
229         
230         
231         
232     },
233      // this should not be here...
234     setDisabled : function(state)
235     {
236         this.disabled = state;
237         if (!state ) {
238             this.el.removeClass('disabled');
239         } else if (!this.el.hasClass('disabled')) {
240             this.el.addClass('disabled');
241         }
242         
243     },
244     
245     /**
246      * Fetch the element to display the tooltip on.
247      * @return {Roo.Element} defaults to this.el
248      */
249     tooltipEl : function()
250     {
251         return this.el.select('' + this.tagtype + '', true).first();
252     },
253     
254     scrollToElement : function()
255     {
256         var c = document.body;
257         
258         var target = Roo.get(c).select('a[name=' + this.href.replace('#', '') +']', true).first();
259         
260         if(!target){
261             return;
262         }
263         
264         var o = target.calcOffsetsTo(c);
265         
266         var value = o[1];
267
268         Roo.log(value);
269         Roo.get(c).scrollTo('top', value, true);
270         
271         return;
272     }
273 });
274  
275
276