2 * @class Roo.bootstrap.nav.Item
3 * @extends Roo.bootstrap.Component
4 * @children Roo.bootstrap.Container Roo.bootstrap.Button
5 * @parent Roo.bootstrap.nav.Group
7 * Bootstrap Navbar.NavItem class
9 * @cfg {String} href link to
10 * @cfg {String} button_weight (default|primary|secondary|success|info|warning|danger|link|light|dark) default none
11 * @cfg {Boolean} button_outline show and outlined button
12 * @cfg {String} html content of button
13 * @cfg {String} badge text inside badge
14 * @cfg {String} badgecls (bg-green|bg-red|bg-yellow)the extra classes for the badge
15 * @cfg {String} glyphicon DEPRICATED - use fa
16 * @cfg {String} icon DEPRICATED - use fa
17 * @cfg {String} fa - Fontawsome icon name (can add stuff to it like fa-2x)
18 * @cfg {Boolean} active Is item active
19 * @cfg {Boolean} disabled Is item disabled
20 * @cfg {String} linkcls Link Class
21 * @cfg {Boolean} preventDefault (true | false) default false
22 * @cfg {String} tabId the tab that this item activates.
23 * @cfg {String} tagtype (a|span) render as a href or span?
24 * @cfg {Boolean} animateRef (true|false) link to element default false
25 * @cfg {Roo.bootstrap.menu.Menu} menu a Menu
28 * Create a new Navbar Item
29 * @param {Object} config The config object
31 Roo.bootstrap.nav.Item = function(config){
32 Roo.bootstrap.nav.Item.superclass.constructor.call(this, config);
37 * The raw click event for the entire grid.
38 * @param {Roo.EventObject} e
43 * Fires when the active item active state changes
44 * @param {Roo.bootstrap.nav.Item} this
45 * @param {boolean} state the new state
51 * Fires when scroll to element
52 * @param {Roo.bootstrap.nav.Item} this
53 * @param {Object} options
54 * @param {Roo.EventObject} e
62 Roo.extend(Roo.bootstrap.nav.Item, Roo.bootstrap.Component, {
71 preventDefault : false,
79 button_outline : false,
83 getAutoCreate : function(){
90 cfg.cls = typeof(cfg.cls) == 'undefined' ? '' : cfg.cls;
93 cfg.cls += ' active' ;
96 cfg.cls += ' disabled';
100 if (this.button_weight.length) {
101 cfg.tag = this.href ? 'a' : 'button';
102 cfg.html = this.html || '';
103 cfg.cls += ' btn btn' + (this.button_outline ? '-outline' : '') + '-' + this.button_weight;
105 cfg.href = this.href;
108 cfg.html = '<i class="fa fas fa-'+this.fa+'"></i> <span class="nav-html">' + this.html + '</span>';
110 cfg.cls += " nav-html";
113 // menu .. should add dropdown-menu class - so no need for carat..
115 if (this.badge !== '') {
117 cfg.html += ' <span class="badge badge-secondary">' + this.badge + '</span>';
122 if (this.href || this.html || this.glyphicon || this.icon || this.fa) {
126 href : this.href || "#",
127 html: this.html || '',
131 if (this.tagtype == 'a') {
132 cfg.cn[0].cls = 'nav-link' + (this.active ? ' active' : '') + ' ' + this.linkcls;
136 cfg.cn[0].html = '<i class="'+this.icon+'"></i> <span class="nav-html">' + cfg.cn[0].html + '</span>';
137 } else if (this.fa) {
138 cfg.cn[0].html = '<i class="fa fas fa-'+this.fa+'"></i> <span class="nav-html">' + cfg.cn[0].html + '</span>';
139 } else if(this.glyphicon) {
140 cfg.cn[0].html = '<span class="glyphicon glyphicon-' + this.glyphicon + '"></span> ' + cfg.cn[0].html;
142 cfg.cn[0].cls += " nav-html";
146 cfg.cn[0].html += " <span class='caret'></span>";
150 if (this.badge !== '') {
151 cfg.cn[0].html += ' <span class="badge badge-secondary">' + this.badge + '</span>';
159 onRender : function(ct, position)
161 // Roo.log("Call onRender: " + this.xtype);
162 if (Roo.bootstrap.version == 4 && ct.dom.type != 'ul') {
166 var ret = Roo.bootstrap.nav.Item.superclass.onRender.call(this, ct, position);
167 this.navLink = this.el.select('.nav-link',true).first();
168 this.htmlEl = this.el.hasClass('nav-html') ? this.el : this.el.select('.nav-html',true).first();
173 initEvents: function()
175 if (typeof (this.menu) != 'undefined') {
176 this.menu.parentType = this.xtype;
177 this.menu.triggerEl = this.el;
178 this.menu = this.addxtype(Roo.apply({}, this.menu));
181 this.el.on('click', this.onClick, this);
183 //if(this.tagtype == 'span'){
184 // this.el.select('span',true).on('click', this.onClick, this);
187 // at this point parent should be available..
188 this.parent().register(this);
191 onClick : function(e)
193 if (e.getTarget('.dropdown-menu-item')) {
194 // did you click on a menu itemm.... - then don't trigger onclick..
199 this.preventDefault ||
202 Roo.log("NavItem - prevent Default?");
210 var tg = Roo.bootstrap.TabGroup.get(this.navId);
211 if (tg && tg.transition) {
212 Roo.log("waiting for the transitionend");
218 //Roo.log("fire event clicked");
219 if(this.fireEvent('click', this, e) === false){
223 if(this.tagtype == 'span'){
227 //Roo.log(this.href);
228 var ael = this.el.select('a',true).first();
231 if(ael && this.animateRef && this.href.indexOf('#') > -1){
232 //Roo.log(["test:",ael.dom.href.split("#")[0], document.location.toString().split("#")[0]]);
233 if (ael.dom.href.split("#")[0] != document.location.toString().split("#")[0]) {
234 return; // ignore... - it's a 'hash' to another page.
236 Roo.log("NavItem - prevent Default?");
238 this.scrollToElement(e);
242 var p = this.parent();
244 if (['tabs','pills'].indexOf(p.type)!==-1 && p.pilltype) {
245 if (typeof(p.setActiveItem) !== 'undefined') {
246 p.setActiveItem(this);
250 // if parent is a navbarheader....- and link is probably a '#' page ref.. then remove the expanded menu.
251 if (p.parentType == 'NavHeaderbar' && !this.menu) {
252 // remove the collapsed menu expand...
253 p.parent().el.select('.roo-navbar-collapse',true).removeClass('in');
257 isActive: function () {
260 setActive : function(state, fire, is_was_active)
262 if (this.active && !state && this.navId) {
263 this.was_active = true;
264 var nv = Roo.bootstrap.nav.Group.get(this.navId);
266 nv.clearWasActive(this);
273 this.el.removeClass('active');
274 this.navLink ? this.navLink.removeClass('active') : false;
275 } else if (!this.el.hasClass('active')) {
277 this.el.addClass('active');
278 if (Roo.bootstrap.version == 4 && this.navLink ) {
279 this.navLink.addClass('active');
284 this.fireEvent('changed', this, state);
287 // show a panel if it's registered and related..
289 if (!this.navId || !this.tabId || !state || is_was_active) {
293 var tg = Roo.bootstrap.TabGroup.get(this.navId);
297 var pan = tg.getPanelByName(this.tabId);
301 // if we can not flip to new panel - go back to old nav highlight..
302 if (false == tg.showPanel(pan)) {
303 var nv = Roo.bootstrap.nav.Group.get(this.navId);
305 var onav = nv.getWasActive();
307 onav.setActive(true, false, true);
316 // this should not be here...
317 setDisabled : function(state)
319 this.disabled = state;
321 this.el.removeClass('disabled');
322 } else if (!this.el.hasClass('disabled')) {
323 this.el.addClass('disabled');
329 * Fetch the element to display the tooltip on.
330 * @return {Roo.Element} defaults to this.el
332 tooltipEl : function()
334 return this.el; //this.tagtype == 'a' ? this.el : this.el.select('' + this.tagtype + '', true).first();
337 scrollToElement : function(e)
339 var c = document.body;
342 * Firefox / IE places the overflow at the html level, unless specifically styled to behave differently.
344 if(Roo.isFirefox || Roo.isIE || Roo.isIE11){
345 c = document.documentElement;
348 var target = Roo.get(c).select('a[name=' + this.href.split('#')[1] +']', true).first();
354 var o = target.calcOffsetsTo(c);
361 this.fireEvent('scrollto', this, options, e);
363 Roo.get(c).scrollTo('top', options.value, true);
368 * Set the HTML (text content) of the item
369 * @param {string} html content for the nav item
371 setHtml : function(html)
374 this.htmlEl.dom.innerHTML = html;