forked from mattheworiordan/jquery.infinite.tabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.infinite.tabs.min.js
1 lines (1 loc) · 7.83 KB
/
jquery.infinite.tabs.min.js
1
(function($){var opts,initialize,adjustToFit,addTab,removeTab,setTabContent,moveNonFixedTabsToScroller,addNav,positionNav,getLeftMostTabIndex,totalElemWidth,shiftTabOffset,shiftScroller,slideTabsAfterResize;$.fn.infiniteTabs=function(command,options,secondParam){if((typeof command==="object")||!command){opts=$.extend({},$.fn.infiniteTabs.defaults,command);return this.each(initialize)}if(typeof command==="string"){switch(command.toLowerCase()){case"adjust-to-fit":return this.each(adjustToFit);case"append-tab":return this.each(function(index,elem){addTab.call(elem,options,"append")});case"prepend-tab":return this.each(function(index,elem){addTab.call(elem,options,"prepend")});case"remove-tab":return this.each(function(index,elem){removeTab.call(elem,options)});case"set-tab-content":return this.each(function(index,elem){setTabContent.call(elem,options,secondParam)})}}};initialize=function(){var scroller=$('<li class="scroller"><div><ul></ul></div></li>'),scrollableTabs=$(this).find("li:not(.locked)"),that=this,scrollableTabOffset=0,windowWidth=$(window).width(),scrollerWidth,nav,lastPinnedTab,ensurePinnedTabCornersAreCorrect,positionNavFunction,setScrollerWidth;moveNonFixedTabsToScroller(this,scrollableTabs,scroller);setScrollerWidth=function(that){lastPinnedTab=$(that).find(">li:not(.navigation):not(.scroller):last");if(lastPinnedTab.length===0){scrollerWidth=$(that).innerWidth()}else{scrollerWidth=$(that).innerWidth()-lastPinnedTab.offset().left-lastPinnedTab.width()}};setScrollerWidth(this);nav=addNav(this);positionNav(this,nav,scroller,scrollerWidth);$(nav).find("a.previous-tab").click(function(e){e.preventDefault();e.stopPropagation();scrollableTabOffset=shiftTabOffset(-1,scrollableTabOffset,scroller,nav,opts);shiftScroller(scrollableTabOffset,scroller,that)});$(nav).find("a.next-tab").click(function(e){e.preventDefault();e.stopPropagation();scrollableTabOffset=shiftTabOffset(1,scrollableTabOffset,scroller,nav,opts);shiftScroller(scrollableTabOffset,scroller,that)});scroller.find("div").css("width",(scrollerWidth-20)+"px");ensurePinnedTabCornersAreCorrect=function(){if(scroller.find("li:not(.hidden)").length===0){$(that).find(">li:not(.navigation):not(.scroller):last").addClass("absolute-last")}else{$(that).find(">li:not(.navigation):not(.scroller):last").removeClass("absolute-last")}};ensurePinnedTabCornersAreCorrect();positionNavFunction=function(){positionNav(that,nav,scroller,scrollerWidth);ensurePinnedTabCornersAreCorrect()};$(this).data("positionNavFunction",positionNavFunction);$(this).data("resetScrollerWidth",function(){setScrollerWidth(that);scroller.find("div").css("width",(scrollerWidth-20)+"px")});$(window).resize(function(e){var resizeWidthBy=$(window).width()-windowWidth,moveTabsBackBy;if(resizeWidthBy){scrollerWidth+=resizeWidthBy;windowWidth=$(window).width();scroller.find("div").css("width",(scrollerWidth-20)+"px");positionNavFunction();moveTabsBackBy=slideTabsAfterResize(scrollableTabOffset,scrollerWidth,scroller);if(moveTabsBackBy){scrollableTabOffset+=moveTabsBackBy;if(scrollableTabOffset>0){scrollableTabOffset=0}shiftScroller(scrollableTabOffset,scroller,that)}}});$(this).find("li.scroller li").click(function(e){var that=this;setTimeout(function(){var scrollerList=scroller.find("ul"),tabRightEdge;if(-scrollableTabOffset>$(that).position().left){scrollableTabOffset=-$(that).position().left+($(that).is(":first-child")?0:opts.initialTabSlideOffset);scrollerList.css("left",scrollableTabOffset+"px")}else{if($(nav).is(":visible")){tabRightEdge=$(that).offset().left+$(that).outerWidth();if(tabRightEdge>$(nav).offset().left){scrollableTabOffset-=tabRightEdge-$(nav).offset().left+opts.initialTabSlideOffset;scrollerList.css("left",scrollableTabOffset+"px")}}}},1)});$(this).find("li.scroller").click(function(e){e.preventDefault();e.stopImmediatePropagation()})};getLeftMostTabIndex=function(scroller,scrollableTabOffset){var tabIndex=0,tabFound=false;scroller.find("ul li").each(function(index,elem){if(!tabFound){if($(elem).position().left>=-scrollableTabOffset){tabFound=true}else{tabIndex+=1}}});return tabIndex};totalElemWidth=function(elems){var width=0;elems.each(function(index,elem){width+=$(elem).outerWidth()});return width};addNav=function(outerMostList){var backArrow=($.browser.msie&&$.browser.version<=8)?"<":"◀",forwardArrow=($.browser.msie&&$.browser.version<=8)?">":"▶",nav=$('<li class="navigation"><div><a href="#next" class="next-tab">'+backArrow+'</a><a href="#previous" class="previous-tab">'+forwardArrow+"</a></div></li>"),navHeightDiff,navPadTop;$(outerMostList).append(nav);navHeightDiff=$(outerMostList).outerHeight()-nav.outerHeight();navPadTop=Math.round(navHeightDiff/2);$(nav).find("a").css("padding-top",navPadTop+"px").css("padding-bottom",(navHeightDiff-navPadTop)+"px");return nav};positionNav=function(outerMostList,nav,scroller,scrollerWidth){var currentPos,navPosition;if($(nav).data("scrollerWidth")){currentPos=parseInt(nav.css("left").replace(/px/,""),10);currentPos+=scrollerWidth-parseInt($(nav).data("scrollerWidth"),10);nav.css("left",currentPos+"px")}else{navPosition=($(outerMostList).width()-$(nav).width()+10);nav.css("left",navPosition+"px")}$(nav).data("scrollerWidth",scrollerWidth);if(totalElemWidth(scroller.find("li"))>scrollerWidth-20){nav.css("display","block")}else{nav.css("display","none")}};slideTabsAfterResize=function(scrollableTabOffset,scrollerWidth,scroller){if(scrollableTabOffset<0){if(scrollableTabOffset+totalElemWidth(scroller.find("li"))+50<scrollerWidth){return -(scrollableTabOffset+totalElemWidth(scroller.find("li"))+50-scrollerWidth)}}return false};moveNonFixedTabsToScroller=function(containerList,scrollableTabs,scroller){scrollableTabs.each(function(index,elem){scroller.find("ul").append(elem)});if($(containerList).find("li.locked").length%2===1){scroller.find("ul").prepend($('<li class="hidden"></li>'))}$(containerList).append(scroller);if($(containerList).find(">li:not(.scroller):not(.nav)").length===0){scroller.find("ul li:first").addClass("absolute-first")}};shiftTabOffset=function(direction,currentScrollableTabOffset,scroller,nav,opts){var currentTabIndex=getLeftMostTabIndex(scroller,currentScrollableTabOffset),scrollerList=scroller.find("ul"),scrollMax;if(direction<0){if(currentScrollableTabOffset===0){currentScrollableTabOffset-=scrollerList.find("li:first").outerWidth()-opts.initialTabSlideOffset}else{currentScrollableTabOffset-=scrollerList.find("li:nth-child("+(currentTabIndex+1)+")").outerWidth()}scrollMax=-scroller.find("li:last").position().left+nav.position().left-scroller.position().left-nav.innerWidth()+8-opts.initialTabSlideOffset;if(currentScrollableTabOffset<scrollMax){currentScrollableTabOffset=scrollMax}}else{currentScrollableTabOffset+=scrollerList.find("li:nth-child("+(currentTabIndex)+")").outerWidth();if(currentScrollableTabOffset>=0){currentScrollableTabOffset=0}}return currentScrollableTabOffset};shiftScroller=function(scrollableTabOffset,scroller,outerMostList){var scrollerList=scroller.find("ul");scrollerList.css("left",scrollableTabOffset+"px");if(scrollableTabOffset<0){$(outerMostList).find("li.locked").filter(":last").addClass("overlay")}else{$(outerMostList).find("li.locked").filter(":last").removeClass("overlay")}};adjustToFit=function(){if($(this).is("ul.infinite-tabs")){$(this).data("resetScrollerWidth")();$(this).data("positionNavFunction")()}};removeTab=function(tab){if($(this).is("ul.infinite-tabs")){$(tab).remove();adjustToFit.call(this)}};addTab=function(tab,position){if($(this).is("ul.infinite-tabs")){if(position==="append"){$(this).find("li.scroller ul").append(tab)}else{$(this).find("li.scroller ul li:not(.hidden):first").before(tab)}adjustToFit.call(this)}};setTabContent=function(tab,newTabContent){if($(this).is("ul.infinite-tabs")){if(typeof newTabContent==="string"){$(tab).find("a").html(newTabContent)}else{$(tab).find("a").empty().append($(newTabContent))}}adjustToFit.call(this)};$.fn.infiniteTabs.defaults={initialTabSlideOffset:10}}(jQuery));