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