8becd8cf2b995791971b950c2c7724ba5af47a0d
[roojs1] / Roo / bootstrap / nav / Item.js
1 /**
2  * @class Roo.bootstrap.nav.Item
3  * @extends Roo.bootstrap.Component
4  * @children Roo.bootstrap.Container Roo.bootstrap.Button
5  * @parent Roo.bootstrap.nav.Group
6  * @licence LGPL
7  * Bootstrap Navbar.NavItem class
8  * 
9  * @cfg {String} href  link to
10  * @cfg {String} button_weight (default|primary|secondary|success|info|warning|danger|link|light|dark) default none
11  * @cfg {Boolean} button_outline show and outlined button
12  * @cfg {String} html content of button
13  * @cfg {String} badge text inside badge
14  * @cfg {String} badgecls (bg-green|bg-red|bg-yellow)the extra classes for the badge
15  * @cfg {String} glyphicon DEPRICATED - use fa
16  * @cfg {String} icon DEPRICATED - use fa
17  * @cfg {String} fa - Fontawsome icon name (can add stuff to it like fa-2x)
18  * @cfg {Boolean} active Is item active
19  * @cfg {Boolean} disabled Is item disabled
20  * @cfg {String} linkcls  Link Class
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  * @cfg {Roo.bootstrap.menu.Menu} menu a Menu 
26   
27  * @constructor
28  * Create a new Navbar Item
29  * @param {Object} config The config object
30  */
31 Roo.bootstrap.nav.Item = function(config){
32     Roo.bootstrap.nav.Item.superclass.constructor.call(this, config);
33     this.addEvents({
34         // raw events
35         /**
36          * @event click
37          * The raw click event for the entire grid.
38          * @param {Roo.EventObject} e
39          */
40         "click" : true,
41          /**
42             * @event changed
43             * Fires when the active item active state changes
44             * @param {Roo.bootstrap.nav.Item} this
45             * @param {boolean} state the new state
46              
47          */
48         'changed': true,
49         /**
50             * @event scrollto
51             * Fires when scroll to element
52             * @param {Roo.bootstrap.nav.Item} this
53             * @param {Object} options
54             * @param {Roo.EventObject} e
55              
56          */
57         'scrollto': true
58     });
59    
60 };
61
62 Roo.extend(Roo.bootstrap.nav.Item, Roo.bootstrap.Component,  {
63     
64     href: false,
65     html: '',
66     badge: '',
67     icon: false,
68     fa : false,
69     glyphicon: false,
70     active: false,
71     preventDefault : false,
72     tabId : false,
73     tagtype : 'a',
74     tag: 'li',
75     disabled : false,
76     animateRef : false,
77     was_active : false,
78     button_weight : '',
79     button_outline : false,
80     linkcls : '',
81     navLink: false,
82     
83     getAutoCreate : function(){
84          
85         var cfg = {
86             tag: this.tag,
87             cls: 'nav-item'
88         };
89         
90         cfg.cls =  typeof(cfg.cls) == 'undefined'  ? '' : cfg.cls;
91         
92         if (this.active) {
93             cfg.cls +=  ' active' ;
94         }
95         if (this.disabled) {
96             cfg.cls += ' disabled';
97         }
98         
99         // BS4 only?
100         if (this.button_weight.length) {
101             cfg.tag = this.href ? 'a' : 'button';
102             cfg.html = this.html || '';
103             cfg.cls += ' btn btn' + (this.button_outline ? '-outline' : '') + '-' + this.button_weight;
104             if (this.href) {
105                 cfg.href = this.href;
106             }
107             if (this.fa) {
108                 cfg.html = '<i class="fa fas fa-'+this.fa+'"></i> <span class="nav-html">' + this.html + '</span>';
109             } else {
110                 cfg.cls += " nav-html";
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                     cls : ''
129                 }
130             ];
131             if (this.tagtype == 'a') {
132                 cfg.cn[0].cls = 'nav-link' +  (this.active ?  ' active'  : '') + ' ' + this.linkcls;
133         
134             }
135             if (this.icon) {
136                 cfg.cn[0].html = '<i class="'+this.icon+'"></i> <span class="nav-html">' + cfg.cn[0].html + '</span>';
137             } else  if (this.fa) {
138                 cfg.cn[0].html = '<i class="fa fas fa-'+this.fa+'"></i> <span class="nav-html">' + cfg.cn[0].html + '</span>';
139             } else if(this.glyphicon) {
140                 cfg.cn[0].html = '<span class="glyphicon glyphicon-' + this.glyphicon + '"></span> '  + cfg.cn[0].html;
141             } else {
142                 cfg.cn[0].cls += " nav-html";
143             }
144             
145             if (this.menu) {
146                 cfg.cn[0].html += " <span class='caret'></span>";
147              
148             }
149             
150             if (this.badge !== '') {
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.nav.Item.superclass.onRender.call(this, ct, position);
167         this.navLink = this.el.select('.nav-link',true).first();
168         this.htmlEl = this.el.hasClass('nav-html') ? this.el : this.el.select('.nav-html',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.nav.Group.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.nav.Group.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      * Set the HTML (text content) of the item
369      * @param {string} html  content for the nav item
370      */
371     setHtml : function(html)
372     {
373         this.html = html;
374         this.htmlEl.dom.innerHTML = html;
375         
376     } 
377 });
378  
379
380