/*! * Copyright (c) 2015 Adam Jimenez * * Dual licensed under the MIT (MIT_LICENSE.txt) * and GPL (GPL_LICENSE.txt) licenses * * https://github.com/adamjimenez/ui.tabs.overflowResize */ (function($) { // overridden ui.tabs functions var uiTabsFuncs = { option: $.ui.tabs.prototype.option, _ui: $.ui.tabs.prototype._ui, }; uiTabsFuncs = $.extend( uiTabsFuncs, { add: $.ui.tabs.prototype.add, remove: $.ui.tabs.prototype.remove, resize: $.ui.tabs.prototype.resize } ); $.extend($.ui.tabs.prototype, { overflowResize: function(options) { var self = this, initialized = false, buttonWidth, containerWidth, resizeTimer = null, hover = false; // initialize overflow function init() { destroy(); $(window).on('resize', resize); // $(self._getList()).on('mouseover', mouseenter); // $(self._getList()).on('mouseout', mouseout); initialized = true; resize(); } function destroy() { $(window).off('resize', resize); // $(self._getList()).off('mouseenter', mouseenter); // $(self._getList()).off('mouseout', mouseout); initialized = false; } function doResize(animate) { // get button width var totalButtonWidth = 0; self.tabs.each(function(i) { var tab = self.tabs.eq(i); if (tab.hasClass('button')) { totalButtonWidth += tab.outerWidth(true); } }); // calc new widths if (blnTabsScrollable){ var elem_container = $('.contenedor_tabs_scroller').filter(function() {return !! $(this).find(self._getList().children(':not(.button):visible')).length;}); if (elem_container.length > 0) var containerWidth = elem_container.width()-100; else var containerWidth = parseFloat(window.getComputedStyle(self._getList()[0]).width)-100; } else var containerWidth = parseFloat(window.getComputedStyle(self._getList()[0]).width)-100; /*JESM - Se agrega el que solo obtenga el calculo de tabs visibles*/ var item = self._getList().children(':not(.button):visible'); if(!item.length) return; var totalMargin = (item[0].getBoundingClientRect().width - parseFloat(window.getComputedStyle(item[0]).width)) * self.tabs.length; var availableWidth = containerWidth - totalButtonWidth - totalMargin; tabMaxWidth = availableWidth / item.length; //JESM if (tabMaxWidth < TABMAXWIDTH_DEFINED){ tabMaxWidth = TABMAXWIDTH_DEFINED; var css = {'max-width' : TABMAXWIDTH_DEFINED}; } else var css = {'max-width' : tabMaxWidth}; if(animate) { /*JESM - Se agrega el que solo obtenga el calculo de tabs visibles*/ self._getList().children(':not(.button):visible').animate(css, 'fast'); //subtract padding between tabs } else { /*JESM - Se agrega el que solo obtenga el calculo de tabs visibles*/ self._getList().children(':not(.button):visible').css(css); } } function resize(e, animate) { if (resizeTimer) clearTimeout(resizeTimer); resizeTimer = setTimeout(function(){ doResize(animate) }, 50); } self._ui = function( tab, panel ) { return { tab: tab, panel: panel, index: this.anchors.index( tab ) }; }; self.resize = function(animate) { // get button width var totalButtonWidth = 0; self.tabs.each(function(i) { var tab = self.tabs.eq(i); if (tab.hasClass('button')) { totalButtonWidth += tab.outerWidth(true); } }); // calc new widths if (blnTabsScrollable){ var elem_container = $('.contenedor_tabs_scroller').filter(function() {return !! $(this).find(self._getList().children(':not(.button):visible')).length;}); if (elem_container.length > 0) var containerWidth = elem_container.width()-100; else var containerWidth = parseFloat(window.getComputedStyle(self._getList()[0]).width)-50; } else var containerWidth = parseFloat(window.getComputedStyle(self._getList()[0]).width)-50; /*JESM - Se agrega el que solo obtenga el calculo de tabs visibles*/ var item = self._getList().children(':not(.button):visible'); if(!item.length) return; //JESM - INICIA - Se modifica para obtener los datos del tab activo, porque si el primero está NO VISIBLE "NO" obtiene el width y lo deja sin ajustar var idx = -1; for (var i = 0; i < item.length; i++) { if (item[i].className.indexOf("ui-tabs-active") >= 0) { idx = i break; } } if (idx > -1) var totalMargin = (item[idx].getBoundingClientRect().width - parseFloat(window.getComputedStyle(item[idx]).width)) * self.tabs.length; else //JESM - FIN var totalMargin = (item[0].getBoundingClientRect().width - parseFloat(window.getComputedStyle(item[0]).width)) * self.tabs.length; var availableWidth = containerWidth - totalButtonWidth - totalMargin; tabMaxWidth = availableWidth / item.length; //JESM if (tabMaxWidth < TABMAXWIDTH_DEFINED){ tabMaxWidth = TABMAXWIDTH_DEFINED; var css = {'max-width' : TABMAXWIDTH_DEFINED}; } else var css = {'max-width' : tabMaxWidth}; if(animate) { /*JESM - Se agrega el que solo obtenga el calculo de tabs visibles*/ self._getList().children(':not(.button):visible').animate(css, 'fast'); //subtract padding between tabs } else { /*JESM - Se agrega el que solo obtenga el calculo de tabs visibles*/ self._getList().children(':not(.button):visible').css(css); } } // temporarily remove overflow buttons before adding a tab self.add = function(name, content, iconCls) { var newTab = false; if (!name) { name = 'New tab'; newTab = true; } if (!content) content = ''; var id = $( "
"+content+"
" ).appendTo( this.element.children(':last-child') ).uniqueId().attr('id'); var ul = self._getList(); var li = $( '
  • '+name+'
  • ' ).insertBefore( $(ul).children('li.addTab') ); if (iconCls) li.children('a').prepend(''); li.uniqueId(); if(newTab) li.attr('data-newtab', 1); var index = li.index(); this.refresh(this); doResize(true); //middle click close li.on('mouseup', function(e){ if(e.which === 2) { var li = $( this ).closest( "li" ); self.remove(li.index()); } }); this.option( "active", index ); this._trigger( "add", null, this._ui( this.tabs[ index ], this.panels[ index ] ) ); return li; }; self.remove = function(index) { var result = this._trigger( "beforeRemove", null, this._ui( this.tabs[ index ], this.panels[ index ] ) ); if(result===false) return; index = this._getIndex( index ); var options = this.options, tab = this.tabs.eq( index ).remove(), panel = this._getPanelForTab( tab ).remove(); // If selected tab was removed focus tab to the right or // in case the last tab was removed the tab to the left. // We check for more than 2 tabs, because if there are only 2, // then when we remove this tab, there will only be one tab left // so we don't need to detect which tab to activate. if ( tab.hasClass( "ui-tabs-active" ) && this.anchors.length > 2 ) { this._activate( index + ( index + 1 < this.anchors.length ? 1 : -1 ) ); } this.refresh(this); //if(!hover) doResize(); this._trigger( "remove", null, this._ui( this.tabs[ index ], this.panels[ index ] ) ); return this; }; self.getPanelForTab = function( tab ) { return this._getPanelForTab( tab ); }; init(); } }); })(jQuery);