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