2 * @class Roo.bootstrap.NavItem
3 * @extends Roo.bootstrap.Component
5 * Bootstrap Navbar.NavItem class
7 * @cfg {String} href link to
8 * @cfg {String} button_weight (default|primary|secondary|success|info|warning|danger|link|light|dark) default none
9 * @cfg {Boolean} button_outline show and outlined button
10 * @cfg {String} html content of button
11 * @cfg {String} badge text inside badge
12 * @cfg {String} badgecls (bg-green|bg-red|bg-yellow)the extra classes for the badge
13 * @cfg {String} glyphicon DEPRICATED - use fa
14 * @cfg {String} icon DEPRICATED - use fa
15 * @cfg {String} fa - Fontawsome icon name (can add stuff to it like fa-2x)
16 * @cfg {Boolean} active Is item active
17 * @cfg {Boolean} disabled Is item disabled
18 * @cfg {String} linkcls Link Class
19 * @cfg {Boolean} preventDefault (true | false) default false
20 * @cfg {String} tabId the tab that this item activates.
21 * @cfg {String} tagtype (a|span) render as a href or span?
22 * @cfg {Boolean} animateRef (true|false) link to element default false
23 * @cfg {Roo.bootstrap.menu.Menu} menu a Menu
26 * Create a new Navbar Item
27 * @param {Object} config The config object
29 Roo.bootstrap.NavItem = function(config){
30 Roo.bootstrap.NavItem.superclass.constructor.call(this, config);
35 * The raw click event for the entire grid.
36 * @param {Roo.EventObject} e
41 * Fires when the active item active state changes
42 * @param {Roo.bootstrap.NavItem} this
43 * @param {boolean} state the new state
49 * Fires when scroll to element
50 * @param {Roo.bootstrap.NavItem} this
51 * @param {Object} options
52 * @param {Roo.EventObject} e
60 Roo.extend(Roo.bootstrap.NavItem, Roo.bootstrap.Component, {
69 preventDefault : false,
77 button_outline : false,
81 getAutoCreate : function(){
88 cfg.cls = typeof(cfg.cls) == 'undefined' ? '' : cfg.cls;
91 cfg.cls += ' active' ;
94 cfg.cls += ' disabled';
98 if (this.button_weight.length) {
99 cfg.tag = this.href ? 'a' : 'button';
100 cfg.html = this.html || '';
101 cfg.cls += ' btn btn' + (this.button_outline ? '-outline' : '') + '-' + this.button_weight;
103 cfg.href = this.href;
106 cfg.html = '<i class="fa fas fa-'+this.fa+'"></i> <span class="nav-html">' + this.html + '</span>';
108 cfg.cls += " nav-html";
111 // menu .. should add dropdown-menu class - so no need for carat..
113 if (this.badge !== '') {
115 cfg.html += ' <span class="badge badge-secondary">' + this.badge + '</span>';
120 if (this.href || this.html || this.glyphicon || this.icon || this.fa) {
124 href : this.href || "#",
125 html: this.html || '',
129 if (this.tagtype == 'a') {
130 cfg.cn[0].cls = 'nav-link' + (this.active ? ' active' : '') + ' ' + this.linkcls;
134 cfg.cn[0].html = '<i class="'+this.icon+'"></i> <span class="nav-html">' + cfg.cn[0].html + '</span>';
135 } else if (this.fa) {
136 cfg.cn[0].html = '<i class="fa fas fa-'+this.fa+'"></i> <span class="nav-html">' + cfg.cn[0].html + '</span>';
137 } else if(this.glyphicon) {
138 cfg.cn[0].html = '<span class="glyphicon glyphicon-' + this.glyphicon + '"></span> ' + cfg.cn[0].html;
140 cfg.cn[0].cls += " nav-html";
144 cfg.cn[0].html += " <span class='caret'></span>";
148 if (this.badge !== '') {
149 cfg.cn[0].html += ' <span class="badge badge-secondary">' + this.badge + '</span>';
157 onRender : function(ct, position)
159 // Roo.log("Call onRender: " + this.xtype);
160 if (Roo.bootstrap.version == 4 && ct.dom.type != 'ul') {
164 var ret = Roo.bootstrap.NavItem.superclass.onRender.call(this, ct, position);
165 this.navLink = this.el.select('.nav-link',true).first();
166 this.htmlEl = this.el.hasClass('nav-html') ? this.el : this.el.select('.nav-html',true).first();
171 initEvents: function()
173 if (typeof (this.menu) != 'undefined') {
174 this.menu.parentType = this.xtype;
175 this.menu.triggerEl = this.el;
176 this.menu = this.addxtype(Roo.apply({}, this.menu));
179 this.el.on('click', this.onClick, this);
181 //if(this.tagtype == 'span'){
182 // this.el.select('span',true).on('click', this.onClick, this);
185 // at this point parent should be available..
186 this.parent().register(this);
189 onClick : function(e)
191 if (e.getTarget('.dropdown-menu-item')) {
192 // did you click on a menu itemm.... - then don't trigger onclick..
197 this.preventDefault ||
200 Roo.log("NavItem - prevent Default?");
208 var tg = Roo.bootstrap.TabGroup.get(this.navId);
209 if (tg && tg.transition) {
210 Roo.log("waiting for the transitionend");
216 //Roo.log("fire event clicked");
217 if(this.fireEvent('click', this, e) === false){
221 if(this.tagtype == 'span'){
225 //Roo.log(this.href);
226 var ael = this.el.select('a',true).first();
229 if(ael && this.animateRef && this.href.indexOf('#') > -1){
230 //Roo.log(["test:",ael.dom.href.split("#")[0], document.location.toString().split("#")[0]]);
231 if (ael.dom.href.split("#")[0] != document.location.toString().split("#")[0]) {
232 return; // ignore... - it's a 'hash' to another page.
234 Roo.log("NavItem - prevent Default?");
236 this.scrollToElement(e);
240 var p = this.parent();
242 if (['tabs','pills'].indexOf(p.type)!==-1 && p.pilltype) {
243 if (typeof(p.setActiveItem) !== 'undefined') {
244 p.setActiveItem(this);
248 // if parent is a navbarheader....- and link is probably a '#' page ref.. then remove the expanded menu.
249 if (p.parentType == 'NavHeaderbar' && !this.menu) {
250 // remove the collapsed menu expand...
251 p.parent().el.select('.roo-navbar-collapse',true).removeClass('in');
255 isActive: function () {
258 setActive : function(state, fire, is_was_active)
260 if (this.active && !state && this.navId) {
261 this.was_active = true;
262 var nv = Roo.bootstrap.NavGroup.get(this.navId);
264 nv.clearWasActive(this);
271 this.el.removeClass('active');
272 this.navLink ? this.navLink.removeClass('active') : false;
273 } else if (!this.el.hasClass('active')) {
275 this.el.addClass('active');
276 if (Roo.bootstrap.version == 4 && this.navLink ) {
277 this.navLink.addClass('active');
282 this.fireEvent('changed', this, state);
285 // show a panel if it's registered and related..
287 if (!this.navId || !this.tabId || !state || is_was_active) {
291 var tg = Roo.bootstrap.TabGroup.get(this.navId);
295 var pan = tg.getPanelByName(this.tabId);
299 // if we can not flip to new panel - go back to old nav highlight..
300 if (false == tg.showPanel(pan)) {
301 var nv = Roo.bootstrap.NavGroup.get(this.navId);
303 var onav = nv.getWasActive();
305 onav.setActive(true, false, true);
314 // this should not be here...
315 setDisabled : function(state)
317 this.disabled = state;
319 this.el.removeClass('disabled');
320 } else if (!this.el.hasClass('disabled')) {
321 this.el.addClass('disabled');
327 * Fetch the element to display the tooltip on.
328 * @return {Roo.Element} defaults to this.el
330 tooltipEl : function()
332 return this.el; //this.tagtype == 'a' ? this.el : this.el.select('' + this.tagtype + '', true).first();
335 scrollToElement : function(e)
337 var c = document.body;
340 * Firefox / IE places the overflow at the html level, unless specifically styled to behave differently.
342 if(Roo.isFirefox || Roo.isIE || Roo.isIE11){
343 c = document.documentElement;
346 var target = Roo.get(c).select('a[name=' + this.href.split('#')[1] +']', true).first();
352 var o = target.calcOffsetsTo(c);
359 this.fireEvent('scrollto', this, options, e);
361 Roo.get(c).scrollTo('top', options.value, true);
366 * Set the HTML (text content) of the item
367 * @param {string} html content for the nav item
369 setHtml : function(html)
372 this.htmlEl.dom.innerHTML = html;