bd1c06e613c53324ea508774947297913ecb11c6
[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             * @event scrollto
50             * Fires when scroll to element
51             * @param {Roo.bootstrap.NavItem} this
52             * @param {Object} options
53             * @param {Roo.EventObject} e
54              
55          */
56         'scrollto': true
57     });
58    
59 };
60
61 Roo.extend(Roo.bootstrap.NavItem, Roo.bootstrap.Component,  {
62     
63     href: false,
64     html: '',
65     badge: '',
66     icon: false,
67     glyphicon: false,
68     active: false,
69     preventDefault : false,
70     tabId : false,
71     tagtype : 'a',
72     disabled : false,
73     animateRef : false,
74     was_active : false,
75     
76     getAutoCreate : function(){
77          
78         var cfg = {
79             tag: 'li',
80             cls: 'nav-item'
81             
82         };
83         
84         if (this.active) {
85             cfg.cls = typeof(cfg.cls) == 'undefined' ? 'active' : cfg.cls + ' active';
86         }
87         if (this.disabled) {
88             cfg.cls += ' disabled';
89         }
90         
91         if (this.href || this.html || this.glyphicon || this.icon) {
92             cfg.cn = [
93                 {
94                     tag: this.tagtype,
95                     href : this.href || "#",
96                     html: this.html || ''
97                 }
98             ];
99             
100             if (this.icon) {
101                 cfg.cn[0].html = '<i class="'+this.icon+'"></i> <span>' + cfg.cn[0].html + '</span>'
102             }
103
104             if(this.glyphicon) {
105                 cfg.cn[0].html = '<span class="glyphicon glyphicon-' + this.glyphicon + '"></span> '  + cfg.cn[0].html;
106             }
107             
108             if (this.menu) {
109                 
110                 cfg.cn[0].html += " <span class='caret'></span>";
111              
112             }
113             
114             if (this.badge !== '') {
115                  
116                 cfg.cn[0].html += ' <span class="badge">' + this.badge + '</span>';
117             }
118         }
119         
120         
121         
122         return cfg;
123     },
124     initEvents: function() 
125     {
126         if (typeof (this.menu) != 'undefined') {
127             this.menu.parentType = this.xtype;
128             this.menu.triggerEl = this.el;
129             this.menu = this.addxtype(Roo.apply({}, this.menu));
130         }
131         
132         this.el.select('a',true).on('click', this.onClick, this);
133         
134         if(this.tagtype == 'span'){
135             this.el.select('span',true).on('click', this.onClick, this);
136         }
137        
138         // at this point parent should be available..
139         this.parent().register(this);
140     },
141     
142     onClick : function(e)
143     {
144         if(
145                 this.preventDefault || 
146                 this.href == '#' 
147         ){
148             
149             e.preventDefault();
150         }
151         
152         if (this.disabled) {
153             return;
154         }
155         
156         var tg = Roo.bootstrap.TabGroup.get(this.navId);
157         if (tg && tg.transition) {
158             Roo.log("waiting for the transitionend");
159             return;
160         }
161         
162         
163         
164         //Roo.log("fire event clicked");
165         if(this.fireEvent('click', this, e) === false){
166             return;
167         };
168         
169         if(this.tagtype == 'span'){
170             return;
171         }
172         
173         //Roo.log(this.href);
174         var ael = this.el.select('a',true).first();
175         //Roo.log(ael);
176         
177         if(ael && this.animateRef && this.href.indexOf('#') > -1){
178             //Roo.log(["test:",ael.dom.href.split("#")[0], document.location.toString().split("#")[0]]);
179             if (ael.dom.href.split("#")[0] != document.location.toString().split("#")[0]) {
180                 return; // ignore... - it's a 'hash' to another page.
181             }
182             
183             e.preventDefault();
184             this.scrollToElement(e);
185         }
186         
187         
188         var p =  this.parent();
189    
190         if (['tabs','pills'].indexOf(p.type)!==-1) {
191             if (typeof(p.setActiveItem) !== 'undefined') {
192                 p.setActiveItem(this);
193             }
194         }
195         
196         // if parent is a navbarheader....- and link is probably a '#' page ref.. then remove the expanded menu.
197         if (p.parentType == 'NavHeaderbar' && !this.menu) {
198             // remove the collapsed menu expand...
199             p.parent().el.select('.navbar-collapse',true).removeClass('in');  
200         }
201     },
202     
203     isActive: function () {
204         return this.active
205     },
206     setActive : function(state, fire, is_was_active)
207     {
208         if (this.active && !state && this.navId) {
209             this.was_active = true;
210             var nv = Roo.bootstrap.NavGroup.get(this.navId);
211             if (nv) {
212                 nv.clearWasActive(this);
213             }
214             
215         }
216         this.active = state;
217         
218         if (!state ) {
219             this.el.removeClass('active');
220         } else if (!this.el.hasClass('active')) {
221             this.el.addClass('active');
222         }
223         if (fire) {
224             this.fireEvent('changed', this, state);
225         }
226         
227         // show a panel if it's registered and related..
228         
229         if (!this.navId || !this.tabId || !state || is_was_active) {
230             return;
231         }
232         
233         var tg = Roo.bootstrap.TabGroup.get(this.navId);
234         if (!tg) {
235             return;
236         }
237         var pan = tg.getPanelByName(this.tabId);
238         if (!pan) {
239             return;
240         }
241         // if we can not flip to new panel - go back to old nav highlight..
242         if (false == tg.showPanel(pan)) {
243             var nv = Roo.bootstrap.NavGroup.get(this.navId);
244             if (nv) {
245                 var onav = nv.getWasActive();
246                 if (onav) {
247                     onav.setActive(true, false, true);
248                 }
249             }
250             
251         }
252         
253         
254         
255     },
256      // this should not be here...
257     setDisabled : function(state)
258     {
259         this.disabled = state;
260         if (!state ) {
261             this.el.removeClass('disabled');
262         } else if (!this.el.hasClass('disabled')) {
263             this.el.addClass('disabled');
264         }
265         
266     },
267     
268     /**
269      * Fetch the element to display the tooltip on.
270      * @return {Roo.Element} defaults to this.el
271      */
272     tooltipEl : function()
273     {
274         return this.el.select('' + this.tagtype + '', true).first();
275     },
276     
277     scrollToElement : function(e)
278     {
279         var c = document.body;
280         
281         /*
282          * Firefox / IE places the overflow at the html level, unless specifically styled to behave differently.
283          */
284         if(Roo.isFirefox || Roo.isIE || Roo.isIE11){
285             c = document.documentElement;
286         }
287         
288         var target = Roo.get(c).select('a[name=' + this.href.split('#')[1] +']', true).first();
289         
290         if(!target){
291             return;
292         }
293
294         var o = target.calcOffsetsTo(c);
295         
296         var options = {
297             target : target,
298             value : o[1]
299         };
300         
301         this.fireEvent('scrollto', this, options, e);
302         
303         Roo.get(c).scrollTo('top', options.value, true);
304         
305         return;
306     }
307 });
308  
309
310