1/*! 2 * jQuery pagination plugin v1.4.1 3 * http://esimakin.github.io/twbs-pagination/ 4 * 5 * Copyright 2014-2016, Eugene Simakin 6 * Released under Apache 2.0 license 7 * http://apache.org/licenses/LICENSE-2.0.html 8 */ 9(function ($, window, document, undefined) { 10 11 'use strict'; 12 13 var old = $.fn.twbsPagination; 14 15 // PROTOTYPE AND CONSTRUCTOR 16 17 var TwbsPagination = function (element, options) { 18 this.$element = $(element); 19 this.options = $.extend({}, $.fn.twbsPagination.defaults, options); 20 21 if (this.options.startPage < 1 || this.options.startPage > this.options.totalPages) { 22 throw new Error('Start page option is incorrect'); 23 } 24 25 this.options.totalPages = parseInt(this.options.totalPages); 26 if (isNaN(this.options.totalPages)) { 27 throw new Error('Total pages option is not correct!'); 28 } 29 30 this.options.visiblePages = parseInt(this.options.visiblePages); 31 if (isNaN(this.options.visiblePages)) { 32 throw new Error('Visible pages option is not correct!'); 33 } 34 35 if (this.options.onPageClick instanceof Function) { 36 this.$element.first().on('page', this.options.onPageClick); 37 } 38 39 // hide if only one page exists 40 if (this.options.hideOnlyOnePage && this.options.totalPages == 1) { 41 this.$element.trigger('page', 1); 42 return this; 43 } 44 45 if (this.options.totalPages < this.options.visiblePages) { 46 this.options.visiblePages = this.options.totalPages; 47 } 48 49 if (this.options.href) { 50 this.options.startPage = this.getPageFromQueryString(); 51 if (!this.options.startPage) { 52 this.options.startPage = 1; 53 } 54 } 55 56 var tagName = (typeof this.$element.prop === 'function') ? 57 this.$element.prop('tagName') : this.$element.attr('tagName'); 58 59 if (tagName === 'UL') { 60 this.$listContainer = this.$element; 61 } else { 62 this.$listContainer = $('<ul></ul>'); 63 } 64 65 this.$listContainer.addClass(this.options.paginationClass); 66 67 if (tagName !== 'UL') { 68 this.$element.append(this.$listContainer); 69 } 70 71 if (this.options.initiateStartPageClick) { 72 this.show(this.options.startPage); 73 } else { 74 this.currentPage = this.options.startPage; 75 this.render(this.getPages(this.options.startPage)); 76 this.setupEvents(); 77 } 78 79 return this; 80 }; 81 82 TwbsPagination.prototype = { 83 84 constructor: TwbsPagination, 85 86 destroy: function () { 87 this.$element.empty(); 88 this.$element.removeData('twbs-pagination'); 89 this.$element.off('page'); 90 91 return this; 92 }, 93 94 show: function (page) { 95 if (page < 1 || page > this.options.totalPages) { 96 throw new Error('Page is incorrect.'); 97 } 98 this.currentPage = page; 99 100 this.render(this.getPages(page)); 101 this.setupEvents(); 102 103 this.$element.trigger('page', page); 104 105 return this; 106 }, 107 108 enable: function () { 109 this.show(this.currentPage); 110 }, 111 112 disable: function () { 113 var _this = this; 114 this.$listContainer.off('click').on('click', 'li', function (evt) { 115 evt.preventDefault(); 116 }); 117 this.$listContainer.children().each(function () { 118 var $this = $(this); 119 if (!$this.hasClass(_this.options.activeClass)) { 120 $(this).addClass(_this.options.disabledClass); 121 } 122 }); 123 }, 124 125 buildListItems: function (pages) { 126 var listItems = []; 127 128 if (this.options.first) { 129 listItems.push(this.buildItem('first', 1)); 130 } 131 132 if (this.options.prev) { 133 var prev = pages.currentPage > 1 ? pages.currentPage - 1 : this.options.loop ? this.options.totalPages : 1; 134 listItems.push(this.buildItem('prev', prev)); 135 } 136 137 for (var i = 0; i < pages.numeric.length; i++) { 138 listItems.push(this.buildItem('page', pages.numeric[i])); 139 } 140 141 if (this.options.next) { 142 var next = pages.currentPage < this.options.totalPages ? pages.currentPage + 1 : this.options.loop ? 1 : this.options.totalPages; 143 listItems.push(this.buildItem('next', next)); 144 } 145 146 if (this.options.last) { 147 listItems.push(this.buildItem('last', this.options.totalPages)); 148 } 149 150 return listItems; 151 }, 152 153 buildItem: function (type, page) { 154 var $itemContainer = $('<li></li>'), 155 $itemContent = $('<a></a>'), 156 itemText = this.options[type] ? this.makeText(this.options[type], page) : page; 157 158 $itemContainer.addClass(this.options[type + 'Class']); 159 $itemContainer.data('page', page); 160 $itemContainer.data('page-type', type); 161 $itemContainer.append($itemContent.attr('href', this.makeHref(page)).addClass(this.options.anchorClass).html(itemText)); 162 163 return $itemContainer; 164 }, 165 166 getPages: function (currentPage) { 167 var pages = []; 168 169 var half = Math.floor(this.options.visiblePages / 2); 170 var start = currentPage - half + 1 - this.options.visiblePages % 2; 171 var end = currentPage + half; 172 173 // handle boundary case 174 if (start <= 0) { 175 start = 1; 176 end = this.options.visiblePages; 177 } 178 if (end > this.options.totalPages) { 179 start = this.options.totalPages - this.options.visiblePages + 1; 180 end = this.options.totalPages; 181 } 182 183 var itPage = start; 184 while (itPage <= end) { 185 pages.push(itPage); 186 itPage++; 187 } 188 189 return { "currentPage": currentPage, "numeric": pages }; 190 }, 191 192 render: function (pages) { 193 var _this = this; 194 this.$listContainer.children().remove(); 195 var items = this.buildListItems(pages); 196 $.each(items, function (key, item) { 197 _this.$listContainer.append(item); 198 }); 199 200 this.$listContainer.children().each(function () { 201 var $this = $(this), 202 pageType = $this.data('page-type'); 203 204 switch (pageType) { 205 case 'page': 206 if ($this.data('page') === pages.currentPage) { 207 $this.addClass(_this.options.activeClass); 208 } 209 break; 210 case 'first': 211 $this.toggleClass(_this.options.disabledClass, pages.currentPage === 1); 212 break; 213 case 'last': 214 $this.toggleClass(_this.options.disabledClass, pages.currentPage === _this.options.totalPages); 215 break; 216 case 'prev': 217 $this.toggleClass(_this.options.disabledClass, !_this.options.loop && pages.currentPage === 1); 218 break; 219 case 'next': 220 $this.toggleClass(_this.options.disabledClass, 221 !_this.options.loop && pages.currentPage === _this.options.totalPages); 222 break; 223 default: 224 break; 225 } 226 227 }); 228 }, 229 230 setupEvents: function () { 231 var _this = this; 232 this.$listContainer.off('click').on('click', 'li', function (evt) { 233 var $this = $(this); 234 if ($this.hasClass(_this.options.disabledClass) || $this.hasClass(_this.options.activeClass)) { 235 return false; 236 } 237 // Prevent click event if href is not set. 238 !_this.options.href && evt.preventDefault(); 239 _this.show(parseInt($this.data('page'))); 240 }); 241 }, 242 243 makeHref: function (page) { 244 return this.options.href ? this.generateQueryString(page) : "#"; 245 }, 246 247 makeText: function (text, page) { 248 return text.replace(this.options.pageVariable, page) 249 .replace(this.options.totalPagesVariable, this.options.totalPages) 250 }, 251 getPageFromQueryString: function (searchStr) {
252 var search = this.getSearchString(searchStr), 253 regex = new RegExp(this.options.pageVariable + '(=([^&#]*)|&|#|$)'), 254 page = regex.exec(search); 255 if (!page || !page[2]) { 256 return null; 257 } 258 page = decodeURIComponent(page[2]); 259 page = parseInt(page); 260 if (isNaN(page)) { 261 return null; 262 } 263 return page; 264 }, 265 generateQueryString: function (pageNumber, searchStr) { 266 var search = this.getSearchString(searchStr), 267 regex = new RegExp(this.options.pageVariable + '=*[^&#]*'); 268 if (!search) return ''; 269 return '?' + search.replace(regex, this.options.pageVariable + '=' + pageNumber); 270 271 }, 272 getSearchString: function (searchStr) { 273 var search = searchStr || window.location.search; 274 if (search === '') { 275 return null; 276 } 277 if (search.indexOf('?') === 0) search = search.substr(1); 278 return search; 279 }, 280 getCurrentPage: function () { 281 return this.currentPage; 282 } 283 }; 284 285 // PLUGIN DEFINITION 286 287 $.fn.twbsPagination = function (option) { 288 var args = Array.prototype.slice.call(arguments, 1); 289 var methodReturn; 290 291 var $this = $(this); 292 var data = $this.data('twbs-pagination'); 293 var options = typeof option === 'object' ? option : {}; 294 295 if (!data) $this.data('twbs-pagination', (data = new TwbsPagination(this, options))); 296 if (typeof option === 'string') methodReturn = data[option].apply(data, args); 297 298 return (methodReturn === undefined) ? $this : methodReturn; 299 }; 300 301 $.fn.twbsPagination.defaults = { 302 totalPages: 1, 303 startPage: 1, 304 visiblePages: 5, 305 initiateStartPageClick: true, 306 hideOnlyOnePage: false, 307 href: false, 308 pageVariable: '{{page}}', 309 totalPagesVariable: '{{total_pages}}', 310 page: null, 311 first: 'First', 312 prev: 'Previous', 313 next: 'Next', 314 last: 'Last', 315 loop: false, 316 onPageClick: null, 317 paginationClass: 'pagination', 318 nextClass: 'page-item next', 319 prevClass: 'page-item prev', 320 lastClass: 'page-item last', 321 firstClass: 'page-item first', 322 pageClass: 'page-item', 323 activeClass: 'active', 324 disabledClass: 'disabled', 325 anchorClass: 'page-link' 326 }; 327 328 $.fn.twbsPagination.Constructor = TwbsPagination; 329 330 $.fn.twbsPagination.noConflict = function () { 331 $.fn.twbsPagination = old; 332 return this; 333 }; 334 335 $.fn.twbsPagination.version = "1.4.1"; 336 337})(window.jQuery, window, document); 338 339
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.