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