2 * @class Roo.bootstrap.nav.Item
3 * @extends Roo.bootstrap.Component
4 * @children Roo.bootstrap.Container Roo.bootstrap.Button
6 * Bootstrap Navbar.NavItem class
8 * @cfg {String} href link to
9 * @cfg {String} button_weight (default|primary|secondary|success|info|warning|danger|link|light|dark) default none
10 * @cfg {Boolean} button_outline show and outlined button
11 * @cfg {String} html content of button
12 * @cfg {String} badge text inside badge
13 * @cfg {String} badgecls (bg-green|bg-red|bg-yellow)the extra classes for the badge
14 * @cfg {String} glyphicon DEPRICATED - use fa
15 * @cfg {String} icon DEPRICATED - use fa
16 * @cfg {String} fa - Fontawsome icon name (can add stuff to it like fa-2x)
17 * @cfg {Boolean} active Is item active
18 * @cfg {Boolean} disabled Is item disabled
19 * @cfg {String} linkcls Link Class
20 * @cfg {Boolean} preventDefault (true | false) default false
21 * @cfg {String} tabId the tab that this item activates.
22 * @cfg {String} tagtype (a|span) render as a href or span?
23 * @cfg {Boolean} animateRef (true|false) link to element default false
24 * @cfg {Roo.bootstrap.menu.Menu} menu a Menu
27 * Create a new Navbar Item
28 * @param {Object} config The config object
30 Roo.bootstrap.nav.Item = function(config){
31 Roo.bootstrap.nav.Item.superclass.constructor.call(this, config);
36 * The raw click event for the entire grid.
37 * @param {Roo.EventObject} e
42 * Fires when the active item active state changes
43 * @param {Roo.bootstrap.nav.Item} this
44 * @param {boolean} state the new state
50 * Fires when scroll to element
51 * @param {Roo.bootstrap.nav.Item} this
52 * @param {Object} options
53 * @param {Roo.EventObject} e
61 Roo.extend(Roo.bootstrap.nav.Item, Roo.bootstrap.Component, {
70 preventDefault : false,
78 button_outline : false,
82 getAutoCreate : function(){
89 cfg.cls = typeof(cfg.cls) == 'undefined' ? '' : cfg.cls;
92 cfg.cls += ' active' ;
95 cfg.cls += ' disabled';
99 if (this.button_weight.length) {
100 cfg.tag = this.href ? 'a' : 'button';
101 cfg.html = this.html || '';
102 cfg.cls += ' btn btn' + (this.button_outline ? '-outline' : '') + '-' + this.button_weight;
104 cfg.href = this.href;
107 cfg.html = '<i class="fa fas fa-'+this.fa+'"></i> <span class="nav-html">' + this.html + '</span>';
109 cfg.cls += " nav-html";
112 // menu .. should add dropdown-menu class - so no need for carat..
114 if (this.badge !== '') {
116 cfg.html += ' <span class="badge badge-secondary">' + this.badge + '</span>';
121 if (this.href || this.html || this.glyphicon || this.icon || this.fa) {
125 href : this.href || "#",
126 html: this.html || '',
130 if (this.tagtype == 'a') {
131 cfg.cn[0].cls = 'nav-link' + (this.active ? ' active' : '') + ' ' + this.linkcls;
135 cfg.cn[0].html = '<i class="'+this.icon+'"></i> <span class="nav-html">' + cfg.cn[0].html + '</span>';
136 } else if (this.fa) {
137 cfg.cn[0].html = '<i class="fa fas fa-'+this.fa+'"></i> <span class="nav-html">' + cfg.cn[0].html + '</span>';
138 } else if(this.glyphicon) {
139 cfg.cn[0].html = '<span class="glyphicon glyphicon-' + this.glyphicon + '"></span> ' + cfg.cn[0].html;
141 cfg.cn[0].cls += " nav-html";
145 cfg.cn[0].html += " <span class='caret'></span>";
149 if (this.badge !== '') {
150 cfg.cn[0].html += ' <span class="badge badge-secondary">' + this.badge + '</span>';
158 onRender : function(ct, position)
160 // Roo.log("Call onRender: " + this.xtype);
161 if (Roo.bootstrap.version == 4 && ct.dom.type != 'ul') {
165 var ret = Roo.bootstrap.nav.Item.superclass.onRender.call(this, ct, position);
166 this.navLink = this.el.select('.nav-link',true).first();
167 this.htmlEl = this.el.hasClass('nav-html') ? this.el : this.el.select('.nav-html',true).first();
172 initEvents: function()
174 if (typeof (this.menu) != 'undefined') {
175 this.menu.parentType = this.xtype;
176 this.menu.triggerEl = this.el;
177 this.menu = this.addxtype(Roo.apply({}, this.menu));
180 this.el.on('click', this.onClick, this);
182 //if(this.tagtype == 'span'){
183 // this.el.select('span',true).on('click', this.onClick, this);
186 // at this point parent should be available..
187 this.parent().register(this);
190 onClick : function(e)
192 if (e.getTarget('.dropdown-menu-item')) {
193 // did you click on a menu itemm.... - then don't trigger onclick..
198 this.preventDefault ||
201 Roo.log("NavItem - prevent Default?");
209 var tg = Roo.bootstrap.TabGroup.get(this.navId);
210 if (tg && tg.transition) {
211 Roo.log("waiting for the transitionend");
217 //Roo.log("fire event clicked");
218 if(this.fireEvent('click', this, e) === false){
222 if(this.tagtype == 'span'){
226 //Roo.log(this.href);
227 var ael = this.el.select('a',true).first();
230 if(ael && this.animateRef && this.href.indexOf('#') > -1){
231 //Roo.log(["test:",ael.dom.href.split("#")[0], document.location.toString().split("#")[0]]);
232 if (ael.dom.href.split("#")[0] != document.location.toString().split("#")[0]) {
233 return; // ignore... - it's a 'hash' to another page.
235 Roo.log("NavItem - prevent Default?");
237 this.scrollToElement(e);
241 var p = this.parent();
243 if (['tabs','pills'].indexOf(p.type)!==-1 && p.pilltype) {
244 if (typeof(p.setActiveItem) !== 'undefined') {
245 p.setActiveItem(this);
249 // if parent is a navbarheader....- and link is probably a '#' page ref.. then remove the expanded menu.
250 if (p.parentType == 'NavHeaderbar' && !this.menu) {
251 // remove the collapsed menu expand...
252 p.parent().el.select('.roo-navbar-collapse',true).removeClass('in');
256 isActive: function () {
259 setActive : function(state, fire, is_was_active)
261 if (this.active && !state && this.navId) {
262 this.was_active = true;
263 var nv = Roo.bootstrap.nav.Group.get(this.navId);
265 nv.clearWasActive(this);
272 this.el.removeClass('active');
273 this.navLink ? this.navLink.removeClass('active') : false;
274 } else if (!this.el.hasClass('active')) {
276 this.el.addClass('active');
277 if (Roo.bootstrap.version == 4 && this.navLink ) {
278 this.navLink.addClass('active');
283 this.fireEvent('changed', this, state);
286 // show a panel if it's registered and related..
288 if (!this.navId || !this.tabId || !state || is_was_active) {
292 var tg = Roo.bootstrap.TabGroup.get(this.navId);
296 var pan = tg.getPanelByName(this.tabId);
300 // if we can not flip to new panel - go back to old nav highlight..
301 if (false == tg.showPanel(pan)) {
302 var nv = Roo.bootstrap.nav.Group.get(this.navId);
304 var onav = nv.getWasActive();
306 onav.setActive(true, false, true);
315 // this should not be here...
316 setDisabled : function(state)
318 this.disabled = state;
320 this.el.removeClass('disabled');
321 } else if (!this.el.hasClass('disabled')) {
322 this.el.addClass('disabled');
328 * Fetch the element to display the tooltip on.
329 * @return {Roo.Element} defaults to this.el
331 tooltipEl : function()
333 return this.el; //this.tagtype == 'a' ? this.el : this.el.select('' + this.tagtype + '', true).first();
336 scrollToElement : function(e)
338 var c = document.body;
341 * Firefox / IE places the overflow at the html level, unless specifically styled to behave differently.
343 if(Roo.isFirefox || Roo.isIE || Roo.isIE11){
344 c = document.documentElement;
347 var target = Roo.get(c).select('a[name=' + this.href.split('#')[1] +']', true).first();
353 var o = target.calcOffsetsTo(c);
360 this.fireEvent('scrollto', this, options, e);
362 Roo.get(c).scrollTo('top', options.value, true);
367 * Set the HTML (text content) of the item
368 * @param {string} html content for the nav item
370 setHtml : function(html)
373 this.htmlEl.dom.innerHTML = html;