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