jquery.fn.twbs-responsive-pagination
Version:
jQuery plugin to make Twitter Bootstrap pagination responsive
9 lines (8 loc) • 2.03 kB
JavaScript
/*!
* twbsResponsivePagination.js v0.1.2
* https://github.com/SpikedKira/jQuery.fn.twbsResponsivePagination
* MIT licensed
*
* Copyright (C) 2015 J Corley
*/
!function(e){function i(i){return e(i).width()>=e(i).parent().width()?!1:!0}var t={},o={init:function(i){var o={};i&&(o=i),t=e.extend(!0,{},e.fn.twbsResponsivePagination.defaults,o);var n=e(this).find(" > li");t.$overflowBefore=e("<li><span></span></li>").insertAfter(n.filter(":nth-child("+t.endcapCount+")")),t.$overflowAfter=e("<li><span></span></li>").insertBefore(n.filter(":nth-last-child("+t.endcapCount+")")),t.$overflowBefore.add(t.$overflowAfter).addClass("disabled removable").addClass(t.overflowClass).attr("aria-hidden",!0),e(this).data("twbsResponsivePagination",t);var s=this;e(window).bind("resize.twbsResponsivePagination",function(){e(s).twbsResponsivePagination("update")}),e(s).twbsResponsivePagination("update")},update:function(){var o=t.overflowClass,n=e(this).find(" > li");n.filter("."+o+" ~ li").hide().filter("."+o+" ~ ."+o+" ~ li").show(),n.filter("."+o).show(),n.filter(".active").show();for(var s=n.filter(".active").index(),a=t.$overflowBefore.show().index(),r=t.$overflowAfter.show().index(),f=s-1,l=s+1;f>a||r>l;f--,l++){if(a+1>=f){if(t.$overflowBefore.hide().next().show(),!i(this)){t.$overflowBefore.show().next().hide();break}}else if(n.eq(f).show(),!i(this)){n.eq(f).hide();break}if(l>=r-1){if(t.$overflowAfter.hide().prev().show(),!i(this)){t.$overflowAfter.show().prev().hide();break}}else if(n.eq(l).show(),!i(this)){n.eq(l).hide();break}}}};e.fn.twbsResponsivePagination=function(i){var n=arguments;return this.each(function(){var s;return o[i]?(t=e(this).data("twbsResponsivePagination"),s=o[i].apply(this,Array.prototype.slice.call(n,1))):"object"!=typeof i&&i?e.error("Method "+i+" does not exist on jQuery.twbsResponsivePagination"):s=o.init.apply(this,n),void 0!==typeof s?(e(this).data("twbsResponsivePagination",t),s):void 0})},e.fn.twbsResponsivePagination.defaults={endcapCount:2,overflowClass:"overflow"}}(jQuery);