1/*! 2 * Mricode Pagination Plugin 3 * Github: https://github.com/mricle/Mricode.Pagination 4 * Version: 1.4.4 5 * 6 * Required jQuery 7 * 8 * Copyright 2016 Mricle 9 * Released under the MIT license 10 */ 11 12(function (factory) { 13 if (typeof define === 'function' && define.amd) { 14 // AMD 15 define(['jquery'], factory); 16 } else if (typeof exports === 'object') { 17 // Node / CommonJS 18 factory(require('jquery')); 19 } else { 20 // Globals 21 factory(jQuery); 22 } 23})(function ($) { 24 "use strict"; 25 26 var Page = function (element, options) { 27 var defaultOption = { 28 pageIndex: 0, 29 pageSize: 10, 30 total: 0, 31 pageBtnCount: 7, 32 showFirstLastBtn: true, 33 firstBtnText: "é¦é¡µ", 34 lastBtnText: "尾页", 35 prevBtnText: "ä¸ä¸é¡µ", 36 nextBtnText: "ä¸ä¸é¡µ", 37 loadFirstPage: true, 38 remote: { 39 url: null, 40 params: null, 41 pageParams: null, 42 success: null, 43 beforeSend: null, 44 complete: null, 45 pageIndexName: 'pageIndex', 46 pageSizeName: 'pageSize', 47 totalName: 'total', 48 traditional: false 49 }, 50 pageElementSort: ['$page', '$size', '$jump', '$info'], 51 showInfo: true, 52 infoFormat: '{start} ~ {end} of {total} entires', 53 noInfoText: '0 entires', 54 showJump: true, 55 jumpBtnText: 'Go', 56 showPageSizes: true, 57 pageSizeItems: [5, 10, 15, 20], 58 debug: false 59 } 60 this.$element = $(element); 61 this.$page = $('<ul class="ewb-page-items clearfix"></ul>').hide(); 62 this.$size = $('<div class="m-pagination-size"></div>').hide(); 63 this.$jump = $('<li class="ewb-page-li ewb-page-inpt r"></li>').hide(); 64 this.$info = $('<ul class="ewb-page-items clearfix ewb-page-info hidden-xs"></ul>').hide(); 65 this.options = $.extend(true, {}, defaultOption, $.fn.pagination.defaults, options); 66 67 if (options.pageElementSort) { 68 this.options.pageElementSort = options.pageElementSort 69 } else { 70 if ($.fn.pagination.defaults && $.fn.pagination.defaults.pageElementSort) { 71 this.options.pageElementSort = $.fn.pagination.defaults.pageElementSort; 72 } else { 73 this.options.pageElementSort = defaultOption.pageElementSort; 74 } 75 } 76 this.options.pageSizeItems = options.pageSizeItems || (($.fn.pagination.defaults && $.fn.pagination.defaults.pageSizeItems) ? $.fn.pagination.defaults.pageSizeItems : defaultOption.pageSizeItems); 77 this.total = this.options.total; 78 this.currentUrl = this.options.remote.url; 79 this.currentPageIndex = utility.convertInt(this.options.pageIndex); 80 this.currentPageSize = utility.convertInt(this.options.pageSize); 81 this.currentParams = utility.deserializeParams(this.options.remote.params) || {}; 82 this.getLastPageNum = function () { 83 return pagination.core.calLastPageNum(this.total, this.currentPageSize); 84 } 85 if (this.options.remote.success === null) { 86 this.options.remote.success = this.options.remote.callback; 87 } 88 this.init(); 89 } 90 91 Page.prototype = { 92 getPageIndex: function () { 93 return this.currentPageIndex; 94 }, 95 getPageSize: function () { 96 return this.currentPageSize; 97 }, 98 getParams: function () { 99 return this.currentParams; 100 }, 101 setPageIndex: function (pageIndex) { 102 if (pageIndex !== undefined && pageIndex !== null) { 103 this.currentPageIndex = utility.convertInt(pageIndex); 104 } 105 }, 106 setPageSize: function (pageSize) { 107 if (pageSize !== undefined && pageSize !== null) { 108 this.currentPageSize = utility.convertInt(pageSize); 109 } 110 }, 111 setRemoteUrl: function (url) { 112 if (url) { 113 this.currentUrl = url; 114 } 115 }, 116 setParams: function (params) { 117 if (params) {
118 this.currentParams = utility.deserializeParams(params); 119 } 120 }, 121 render: function (total) { 122 if (total !== undefined && total !== null) { 123 this.total = utility.convertInt(total); 124 } 125 this.renderPagination(); 126 this.debug('pagination rendered'); 127 }, 128 init: function () { 129 this.initHtml(); 130 this.initEvent(); 131 if (this.currentUrl && this.options.loadFirstPage) { 132 this.remote(); 133 } else { 134 this.renderPagination(); 135 } 136 this.debug('pagination inited'); 137 }, 138 initHtml: function () { 139 //init size module 140 var sizeHtml = $('<select data-page-btn="size"></select>'); 141 for (var i = 0; i < this.options.pageSizeItems.length; i++) { 142 sizeHtml.append('<option value="' + this.options.pageSizeItems[i] + '" ' + (this.currentPageSize == this.options.pageSizeItems[i] ? "selected" : "") + ' >' + this.options.pageSizeItems[i] + '</option>') 143 } 144 sizeHtml.val(this.currentPageSize); 145 this.$size.append(sizeHtml); 146 147 //init jump module 148 var jumpHtml = '<input data-page-btn="jump" id="pageinput" type="text" class="ewb-page-input hidden-xs" placeholder=""><button data-page-btn="jump" type="button" class="ewb-page-btn hidden-xs">' + this.options.jumpBtnText + '</button>'; 149 this.$jump.append(jumpHtml); 150 151 for (var i = 0; i < this.options.pageElementSort.length; i++) { 152 this.$element.append(this[this.options.pageElementSort[i]]); 153 } 154 }, 155 initEvent: function () { 156 this.$element 157 .on('click', { page: this }, function (event) { 158 if ($(event.target).is('button')) { 159 if ($(event.target).data('pageBtn') == 'jump') { 160 var $input = event.data.page.$element.find('input[data-page-btn=jump]'); 161 event.data.page.jumpEventHandler($input.val(), event); 162 } 163 } else { 164 if ($(event.target).data('pageIndex') !== undefined) 165 eventHandler.call(event.data.page, event); 166 } 167 }).on('change', { page: this }, function (event) { 168 var $this = $(event.target); 169 if ($this.data('pageBtn') == 'jump') { 170 event.data.page.jumpEventHandler($this.val(), event); 171 } 172 if ($this.data('pageBtn') == 'size') { 173 eventHandler.call(event.data.page, event); 174 } 175 }).on('keypress', { page: this }, function (event) { 176 if (event.keyCode == "13") { 177 var $input = event.data.page.$element.find('input[data-page-btn=jump]') 178 event.data.page.jumpEventHandler($input.val(), event); 179 } 180 }); 181 }, 182 jumpEventHandler: function (inputValue, event) { 183 if (!inputValue) { 184 this.$jump.removeClass('error'); 185 } else if (!pagination.check.checkJumpPage(inputValue)) { 186 this.$jump.addClass('error'); 187 $('#pageinput').val(''); 188 Util.layerAlert("页ç é误,请è¾å ¥ææé¡µç ! "); 189 } 190 else if (utility.convertInt(inputValue) > this.getLastPageNum()) { 191 this.$jump.addClass('error'); 192 $('#pageinput').val(''); 193 Util.layerAlert("页ç é误,请è¾å ¥ææé¡µç ! "); 194 } 195 else { 196 this.$jump.removeClass('error'); 197 eventHandler.call(this, event); 198 } 199 }, 200 201 doPagination: function () { 202 if (this.currentUrl) { 203 this.remote(); 204 } else { 205 this.renderPagination(); 206 } 207 }, 208 remote: function () { 209 if (typeof this.options.remote.pageParams === 'function') { 210 var pageParams = this.options.remote.pageParams.call(this, { pageIndex: this.currentPageIndex, pageSize: this.currentPageSize });
211 this.currentParams = $.extend({}, this.currentParams, pageParams); 212 } else { 213 this.currentParams[this.options.remote.pageIndexName] = this.currentPageIndex; 214 this.currentParams[this.options.remote.pageSizeName] = this.currentPageSize; 215 } 216 pagination.remote.getAjax(this, this.currentUrl, this.currentParams, this.ajaxCallBack, this.options.remote.beforeSend, this.options.remote.complete,this.options.remote.traditional); 217 }, 218 219 ajaxCallBack: function (result) { 220 var total = utility.mapObjectNameRecursion(result, this.options.remote.totalName); 221 if (total == null || total == undefined) 222 throw new Error("the response of totalName : '" + this.options.remote.totalName + "' not found."); 223 total = utility.convertInt(total); 224 this.total = total; 225 var lastPageNum = this.getLastPageNum(); 226 if (this.currentPageIndex > 0 && lastPageNum - 1 < this.currentPageIndex) { 227 this.setPageIndex(lastPageNum - 1); 228 this.remote(); 229 } else { 230 if (typeof this.options.remote.success === 'function') this.options.remote.success(result); 231 this.renderPagination(); 232 } 233 }, 234 235 onEvent: function (eventName, pageIndex, pageSize) { 236 if (pageIndex != null) this.currentPageIndex = utility.convertInt(pageIndex); 237 if (pageSize != null) this.currentPageSize = utility.convertInt(pageSize); 238 this.doPagination(); 239 this.$element.trigger(eventName, { 240 pageIndex: this.currentPageIndex, pageSize: this.currentPageSize 241 }); 242 this.debug('pagination ' + eventName); 243 }, 244 //çæå页 245 renderPagination: function () { 246 var option = { 247 showFirstLastBtn: this.options.showFirstLastBtn, 248 firstBtnText: this.options.firstBtnText, 249 prevBtnText: this.options.prevBtnText, 250 nextBtnText: this.options.nextBtnText, 251 lastBtnText: this.options.lastBtnText 252 } 253 var lastPageNum = this.getLastPageNum(); 254 this.currentPageIndex = lastPageNum > 0 && this.currentPageIndex > lastPageNum - 1 ? lastPageNum - 1 : this.currentPageIndex; 255 this.$page.empty().append(pagination.core.renderPages(this.currentPageIndex, this.currentPageSize, this.total, this.options.pageBtnCount, option)).show(); 256 if (this.options.showPageSizes && lastPageNum !== 0) this.$size.show(); else this.$size.hide(); 257 if (this.options.showJump && lastPageNum !== 0) this.$jump.show(); else this.$jump.hide(); 258 this.$info.text(pagination.core.renderInfo(this.currentPageIndex, this.currentPageSize, this.total, this.options.infoFormat, this.options.noInfoText)); 259 if (this.options.showInfo) this.$info.show(); else this.$info.hide(); 260 }, 261 //鿝å页 262 destroy: function () { 263 this.$element.unbind().data("pagination", null).empty(); 264 this.debug('pagination destroyed'); 265 }, 266 debug: function (message, data) { 267 if (this.options.debug && console) { 268 message && console.info(message + ' : pageIndex = ' + this.currentPageIndex + ' , pageSize = ' + this.currentPageSize + ' , total = ' + this.total); 269 data && console.info(data); 270 } 271 } 272 } 273 274 var eventHandler = function (event) { 275 var that = event.data.page; 276 var $target = $(event.target); 277 278 if (event.type === 'click' && $target.data('pageIndex') !== undefined && !$target.parent().hasClass('active')) { 279 that.onEvent(pagination.event.pageClicked, $target.data("pageIndex"), null); 280 } 281 else if ((event.type === 'click' || event.type === 'keypress') && $target.data('pageBtn') === 'jump') { 282 var pageIndexStr = that.$jump.find('input').val(); 283 if (utility.convertInt(pageIndexStr) <= that.getLastPageNum
283()) { 284 that.onEvent(pagination.event.jumpClicked, pageIndexStr - 1, null); 285 that.$jump.find('input').val(null); 286 } 287 } 288 else if (event.type === 'change' && $target.data('pageBtn') === 'size') { 289 var newPageSize = that.$size.find('select').val(); 290 var lastPageNum = pagination.core.calLastPageNum(that.total, newPageSize); 291 if (lastPageNum > 0 && that.currentPageIndex > lastPageNum - 1) { 292 that.currentPageIndex = lastPageNum - 1; 293 } 294 that.onEvent(pagination.event.pageSizeChanged, that.currentPageIndex, newPageSize); 295 } 296 }; 297 298 var pagination = {}; 299 pagination.event = { 300 pageClicked: 'pageClicked', 301 jumpClicked: 'jumpClicked', 302 pageSizeChanged: 'pageSizeChanged' 303 }; 304 pagination.remote = { 305 getAjax: function (pagination, url, data, success, beforeSend, complate,traditional) { 306 $.ajax({ 307 url: url, 308 dataType: 'json', 309 data: data, 310 cache: false, 311 traditional:traditional, 312 contentType: 'application/Json', 313 beforeSend: function (XMLHttpRequest) { 314 if (typeof beforeSend === 'function') beforeSend.call(this, XMLHttpRequest); 315 }, 316 complete: function (XMLHttpRequest, textStatue) { 317 if (typeof complate === 'function') complate.call(this, XMLHttpRequest, textStatue); 318 }, 319 success: function (result) { 320 success.call(pagination, result); 321 } 322 }) 323 } 324 }; 325 pagination.core = { 326 /* 327 options : { 328 showFirstLastBtn 329 firstBtnText: 330 } 331 */ 332 renderPages: function (pageIndex, pageSize, total, pageBtnCount, options) { 333 options = options || {}; 334 var pageNumber = pageIndex + 1; 335 var lastPageNumber = this.calLastPageNum(total, pageSize); 336 var html = []; 337 338 if (lastPageNumber <= pageBtnCount) { 339 html = this.renderGroupPages(1, lastPageNumber, pageNumber); 340 } 341 else { 342 var firstPage = this.renderPerPage(options.firstBtnText || 1, 0); 343 var lastPage = this.renderPerPage(options.lastBtnText || lastPageNumber, lastPageNumber - 1); 344 345 346 //button count of both sides 347 var symmetryBtnCount = (pageBtnCount - 1 - 4) / 2; 348 if (!options.showFirstLastBtn) 349 symmetryBtnCount = symmetryBtnCount + 1; 350 var frontBtnNum = (pageBtnCount + 1) / 2; 351 var behindBtnNum = lastPageNumber - ((pageBtnCount + 1) / 2); 352 353 var prevPage = this.renderPerPage(options.prevBtnText, pageIndex - 1); 354 var nextPage = this.renderPerPage(options.nextBtnText, pageIndex + 1); 355 356 symmetryBtnCount = symmetryBtnCount.toString().indexOf('.') == -1 ? symmetryBtnCount : symmetryBtnCount + 0.5; 357 frontBtnNum = frontBtnNum.toString().indexOf('.') == -1 ? frontBtnNum : frontBtnNum + 0.5; 358 behindBtnNum = behindBtnNum.toString().indexOf('.') == -1 ? behindBtnNum : behindBtnNum + 0.5; 359 if (pageNumber < frontBtnNum) { 360 if (options.showFirstLastBtn) { 361 html = this.renderGroupPages(1, pageBtnCount - 2, pageNumber); 362 html.push(nextPage); 363 html.push(lastPage); 364 } else { 365 html = this.renderGroupPages(1, pageBtnCount - 1, pageNumber); 366 html.push(nextPage); 367 } 368 } 369 else if (pageNumber > behindBtnNum) { 370 if (options.showFirstLastBtn) { 371 html = this.renderGroupPages(lastPageNumber - pageBtnCount + 3, pageBtnCount - 2, pageNumber); 372 html.unshift(prevPage); 373 html.unshift(firstPage); 374 } else { 375 html = this.renderGroupPages(lastPageNumber - pageBtnCount + 2, pageBtnCount - 1, pageNumber); 376 html.unshift(prevPage); 377 } 378 } 379 else { 380 if (options.showFirstLastBtn) { 381 html = this.renderGroupPages(pageNumber - symmetryBtnCount, pageBtnCount - 4, pageNumber); 382 html.unshift(prevPage); 383 html.push(nextPage); 384 html.unshift(firstPage); 385 html.push(lastPage); 386 } else { 387 html = this.renderGroupPages(pageNumber - symmetryBtnCount, pageBtnCount - 2, pageNumber); 388 html.unshift(prevPage); 389 html.push(nextPage); 390 } 391 } 392 } 393 return html; 394 }, 395 renderGroupPages: function (beginPageNum, count, currentPage) { 396 var html = []; 397 for (var i = 0; i < count; i++) { 398 var page = this.renderPerPage(beginPageNum, beginPageNum - 1); 399 if (beginPageNum === currentPage) 400 page.addClass("active"); 401 html.push(page); 402 beginPageNum++; 403 } 404 return html; 405 }, 406 renderPerPage: function (text, value) { 407 return $("<li class='ewb-page-li ewb-page-hover'><a data-page-index='" + value + "'>
407" + text + "</a></li>"); 408 }, 409 renderInfo: function (currentPageIndex, currentPageSize, total, infoFormat, noInfoText) { 410 if (total <= 0) { 411 return noInfoText; 412 } else { 413 var startNum = (currentPageIndex * currentPageSize) + 1; 414 var endNum = (currentPageIndex + 1) * currentPageSize; 415 endNum = endNum >= total ? total : endNum; 416 return infoFormat.replace('{start}', startNum).replace('{end}', endNum).replace('{total}', total); 417 } 418 }, 419 //è®¡ç®æå¤§åé¡µæ° 420 calLastPageNum: function (total, pageSize) { 421 total = utility.convertInt(total); 422 pageSize = utility.convertInt(pageSize); 423 var i = total / pageSize; 424 return utility.isDecimal(i) ? parseInt(i) + 1 : i; 425 } 426 }; 427 pagination.check = { 428 //æ ¡éªè·³è½¬é¡µæ°æææ§ 429 checkJumpPage: function (pageIndex) { 430 var reg = /^\+?[1-9][0-9]*$/; 431 return reg.test(pageIndex); 432 } 433 }; 434 435 var utility = { 436 //转æ¢ä¸ºint 437 convertInt: function (i) { 438 if (typeof i === 'number') { 439 return i; 440 } else { 441 var j = parseInt(i); 442 if (!isNaN(j)) { 443 return j; 444 } else { 445 throw new Error("convertInt Type Error. Type is " + typeof i + ', value = ' + i); 446 } 447 } 448 }, 449 //è¿åæ¯å¦å°æ° 450 isDecimal: function (i) { 451 return parseInt(i) !== i; 452 }, 453 //å¹é 对象åç§°ï¼éå½ï¼ 454 mapObjectNameRecursion: function (object, name) { 455 var obj = object; 456 var arr = name.split('.'); 457 for (var i = 0; i < arr.length; i++) { 458 obj = utility.mapObjectName(obj, arr[i]); 459 } 460 return obj; 461 }, 462 //å¹é 对象åç§° 463 mapObjectName: function (object, name) { 464 var value = null; 465 for (var i in object) { 466 //è¿æ»¤åå屿§ 467 if (object.hasOwnProperty(i)) { 468 if (i == name) { 469 value = object[i]; 470 break; 471 } 472 } 473 } 474 return value; 475 }, 476 deserializeParams: function (params) { 477 var newParams = {}; 478 if (typeof params === 'string') { 479 var arr = params.split('&'); 480 for (var i = 0; i < arr.length; i++) { 481 var a = arr[i].split('='); 482 newParams[a[0]] = decodeURIComponent(a[1]); 483 } 484 } 485 else if (params instanceof Array) { 486 for (var i = 0; i < params.length; i++) { 487 newParams[params[i].name] = decodeURIComponent(params[i].value); 488 } 489 } 490 else if (typeof params === 'object') { 491 newParams = params; 492 } 493 return newParams; 494 } 495 } 496 497 $.fn.pagination = function (option) { 498 if (typeof option === 'undefined') { 499 return $(this).data('pagination') || false; 500 } else { 501 var result; 502 var args = arguments; 503 this.each(function () { 504 var $this = $(this); 505 var data = $this.data('pagination'); 506 if (!data && typeof option === 'string') { 507 throw new Error('MricodePagination is uninitialized.'); 508 } 509 else if (data && typeof option === 'object') { 510 throw new Error('MricodePagination is initialized.'); 511 } 512 //åå§å 513 else if (!data && typeof option === 'object') { 514 var options = typeof option == 'object' && option; 515 var data_api_options = $this.data(); 516 options = $.extend(options, data_api_options); 517 $this.data('pagination', (data = new Page(this, options))); 518 } 519 else if (data && typeof option === 'string') { 520 result = data[option].apply(data, Array.prototype.slice.call(args, 1)); 521 } 522 }); 523 return typeof result === 'undefined' ? this : result; 524 } 525 } 526});
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.