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