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} button_weight (default|primary|secondary|success|info|warning|danger|link|light|dark) default none
14  * @cfg {String} html content of button
15  * @cfg {String} badge text inside badge
16  * @cfg {String} badgecls (bg-green|bg-red|bg-yellow)the extra classes for the badge
17  * @cfg {String} glyphicon DEPRICATED - use fa
18  * @cfg {String} icon DEPRICATED - use fa
19  * @cfg {String} fa - Fontawsome icon name (can add stuff to it like fa-2x)
20  * @cfg {Boolean} active Is item active
21  * @cfg {Boolean} disabled Is item disabled
22  
23  * @cfg {Boolean} preventDefault (true | false) default false
24  * @cfg {String} tabId the tab that this item activates.
25  * @cfg {String} tagtype (a|span) render as a href or span?
26  * @cfg {Boolean} animateRef (true|false) link to element default false  
27   
28  * @constructor
29  * Create a new Navbar Item
30  * @param {Object} config The config object
31  */
32 Roo.bootstrap.NavItem = function(config){
33     Roo.bootstrap.NavItem.superclass.constructor.call(this, config);
34     this.addEvents({
35         // raw events
36         /**
37          * @event click
38          * The raw click event for the entire grid.
39          * @param {Roo.EventObject} e
40          */
41         "click" : true,
42          /**
43             * @event changed
44             * Fires when the active item active state changes
45             * @param {Roo.bootstrap.NavItem} this
46             * @param {boolean} state the new state
47              
48          */
49         'changed': true,
50         /**
51             * @event scrollto
52             * Fires when scroll to element
53             * @param {Roo.bootstrap.NavItem} this
54             * @param {Object} options
55             * @param {Roo.EventObject} e
56              
57          */
58         'scrollto': true
59     });
60    
61 };
62
63 Roo.extend(Roo.bootstrap.NavItem, Roo.bootstrap.Component,  {
64     
65     href: false,
66     html: '',
67     badge: '',
68     icon: false,
69     fa : false,
70     glyphicon: false,
71     active: false,
72     preventDefault : false,
73     tabId : false,
74     tagtype : 'a',
75     tag: 'li',
76     disabled : false,
77     animateRef : false,
78     was_active : false,
79     button_weight : '',
80     button_outline : false,
81     
82     navLink: false,
83     
84     getAutoCreate : function(){
85          
86         var cfg = {
87             tag: this.tag,
88             cls: 'nav-item'
89         };
90         
91         cfg.cls =  typeof(cfg.cls) == 'undefined'  ? '' : cfg.cls;
92         
93         if (this.active) {
94             cfg.cls +=  ' active' ;
95         }
96         if (this.disabled) {
97             cfg.cls += ' disabled';
98         }
99         
100         // BS4 only?
101         if (this.button_weight.length) {
102             cfg.tag = this.href ? 'a' : 'button';
103             cfg.html = this.html || '';
104             cfg.cls += ' btn btn' + (this.button_outline ? '-outline' : '') + '-' + this.button_weight;
105             if (this.href) {
106                 cfg.href = this.href;
107             }
108             if (this.fa) {
109                 cfg.html = '<i class="fa fas fa-'+this.fa+'"></i> <span>' + this.html + '</span>';
110             }
111             
112             // menu .. should add dropdown-menu class - so no need for carat..
113             
114             if (this.badge !== '') {
115                  
116                 cfg.html += ' <span class="badge badge-secondary">' + this.badge + '</span>';
117             }
118             return cfg;
119         }
120         
121         if (this.href || this.html || this.glyphicon || this.icon || this.fa) {
122             cfg.cn = [
123                 {
124                     tag: this.tagtype,
125                     href : this.href || "#",
126                     html: this.html || ''
127                 }
128             ];
129             if (this.tagtype == 'a') {
130                 cfg.cn[0].cls = 'nav-link' +  (this.active ?  ' active'  : '');
131         
132             }
133             if (this.icon) {
134                 cfg.cn[0].html = '<i class="'+this.icon+'"></i> <span>' + cfg.cn[0].html + '</span>';
135             }
136             if (this.fa) {
137                 cfg.cn[0].html = '<i class="fa fas fa-'+this.fa+'"></i> <span>' + cfg.cn[0].html + '</span>';
138             }
139             if(this.glyphicon) {
140                 cfg.cn[0].html = '<span class="glyphicon glyphicon-' + this.glyphicon + '"></span> '  + cfg.cn[0].html;
141             }
142             
143             if (this.menu) {
144                 
145                 cfg.cn[0].html += " <span class='caret'></span>";
146              
147             }
148             
149             if (this.badge !== '') {
150                  
151                 cfg.cn[0].html += ' <span class="badge badge-secondary">' + this.badge + '</span>';
152             }
153         }
154         
155         
156         
157         return cfg;
158     },
159     onRender : function(ct, position)
160     {
161        // Roo.log("Call onRender: " + this.xtype);
162         if (Roo.bootstrap.version == 4 && ct.dom.type != 'ul') {
163             this.tag = 'div';
164         }
165         
166         var ret = Roo.bootstrap.NavItem.superclass.onRender.call(this, ct, position);
167         this.navLink = this.el.select('.nav-link',true).first();
168         return ret;
169     },
170       
171     
172     initEvents: function() 
173     {
174         if (typeof (this.menu) != 'undefined') {
175             this.menu.parentType = this.xtype;
176             this.menu.triggerEl = this.el;
177             this.menu = this.addxtype(Roo.apply({}, this.menu));
178         }
179         
180         this.el.select('a',true).on('click', this.onClick, this);
181         
182         if(this.tagtype == 'span'){
183             this.el.select('span',true).on('click', this.onClick, this);
184         }
185        
186         // at this point parent should be available..
187         this.parent().register(this);
188     },
189     
190     onClick : function(e)
191     {
192         if (e.getTarget('.dropdown-menu-item')) {
193             // did you click on a menu itemm.... - then don't trigger onclick..
194             return;
195         }
196         
197         if(
198                 this.preventDefault || 
199                 this.href == '#' 
200         ){
201             Roo.log("NavItem - prevent Default?");
202             e.preventDefault();
203         }
204         
205         if (this.disabled) {
206             return;
207         }
208         
209         var tg = Roo.bootstrap.TabGroup.get(this.navId);
210         if (tg && tg.transition) {
211             Roo.log("waiting for the transitionend");
212             return;
213         }
214         
215         
216         
217         //Roo.log("fire event clicked");
218         if(this.fireEvent('click', this, e) === false){
219             return;
220         };
221         
222         if(this.tagtype == 'span'){
223             return;
224         }
225         
226         //Roo.log(this.href);
227         var ael = this.el.select('a',true).first();
228         //Roo.log(ael);
229         
230         if(ael && this.animateRef && this.href.indexOf('#') > -1){
231             //Roo.log(["test:",ael.dom.href.split("#")[0], document.location.toString().split("#")[0]]);
232             if (ael.dom.href.split("#")[0] != document.location.toString().split("#")[0]) {
233                 return; // ignore... - it's a 'hash' to another page.
234             }
235             Roo.log("NavItem - prevent Default?");
236             e.preventDefault();
237             this.scrollToElement(e);
238         }
239         
240         
241         var p =  this.parent();
242    
243         if (['tabs','pills'].indexOf(p.type)!==-1) {
244             if (typeof(p.setActiveItem) !== 'undefined') {
245                 p.setActiveItem(this);
246             }
247         }
248         
249         // if parent is a navbarheader....- and link is probably a '#' page ref.. then remove the expanded menu.
250         if (p.parentType == 'NavHeaderbar' && !this.menu) {
251             // remove the collapsed menu expand...
252             p.parent().el.select('.roo-navbar-collapse',true).removeClass('in');  
253         }
254     },
255     
256     isActive: function () {
257         return this.active
258     },
259     setActive : function(state, fire, is_was_active)
260     {
261         if (this.active && !state && this.navId) {
262             this.was_active = true;
263             var nv = Roo.bootstrap.NavGroup.get(this.navId);
264             if (nv) {
265                 nv.clearWasActive(this);
266             }
267             
268         }
269         this.active = state;
270         
271         if (!state ) {
272             this.el.removeClass('active');
273             this.navLink ? this.navLink.removeClass('active') : false;
274         } else if (!this.el.hasClass('active')) {
275             
276             this.el.addClass('active');
277             if (Roo.bootstrap.version == 4 && this.navLink ) {
278                 this.navLink.addClass('active');
279             }
280             
281         }
282         if (fire) {
283             this.fireEvent('changed', this, state);
284         }
285         
286         // show a panel if it's registered and related..
287         
288         if (!this.navId || !this.tabId || !state || is_was_active) {
289             return;
290         }
291         
292         var tg = Roo.bootstrap.TabGroup.get(this.navId);
293         if (!tg) {
294             return;
295         }
296         var pan = tg.getPanelByName(this.tabId);
297         if (!pan) {
298             return;
299         }
300         // if we can not flip to new panel - go back to old nav highlight..
301         if (false == tg.showPanel(pan)) {
302             var nv = Roo.bootstrap.NavGroup.get(this.navId);
303             if (nv) {
304                 var onav = nv.getWasActive();
305                 if (onav) {
306                     onav.setActive(true, false, true);
307                 }
308             }
309             
310         }
311         
312         
313         
314     },
315      // this should not be here...
316     setDisabled : function(state)
317     {
318         this.disabled = state;
319         if (!state ) {
320             this.el.removeClass('disabled');
321         } else if (!this.el.hasClass('disabled')) {
322             this.el.addClass('disabled');
323         }
324         
325     },
326     
327     /**
328      * Fetch the element to display the tooltip on.
329      * @return {Roo.Element} defaults to this.el
330      */
331     tooltipEl : function()
332     {
333         return this.el.select('' + this.tagtype + '', true).first();
334     },
335     
336     scrollToElement : function(e)
337     {
338         var c = document.body;
339         
340         /*
341          * Firefox / IE places the overflow at the html level, unless specifically styled to behave differently.
342          */
343         if(Roo.isFirefox || Roo.isIE || Roo.isIE11){
344             c = document.documentElement;
345         }
346         
347         var target = Roo.get(c).select('a[name=' + this.href.split('#')[1] +']', true).first();
348         
349         if(!target){
350             return;
351         }
352
353         var o = target.calcOffsetsTo(c);
354         
355         var options = {
356             target : target,
357             value : o[1]
358         };
359         
360         this.fireEvent('scrollto', this, options, e);
361         
362         Roo.get(c).scrollTo('top', options.value, true);
363         
364         return;
365     }
366 });
367  
368
369