Spaces:
Runtime error
Runtime error
File size: 37,966 Bytes
b82d373 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 |
/*
* pagination.js 2.6.0
* A jQuery plugin to provide simple yet fully customisable pagination.
* https://github.com/superRaytin/paginationjs
*
* Homepage: http://pagination.js.org
*
* Copyright 2014-2100, superRaytin
* Released under the MIT license.
*/
(function(global, $) {
if (typeof $ === 'undefined') {
throwError('Pagination requires jQuery.');
}
var pluginName = 'pagination';
var pluginHookMethod = 'addHook';
var eventPrefix = '__pagination-';
if ($.fn.pagination) {
throwError('plugin conflicted, the name "pagination" has been taken by another jQuery plugin.');
}
$.fn[pluginName] = function(options) {
if (typeof options === 'undefined') {
return this;
}
var container = $(this);
var attributes = $.extend({}, $.fn[pluginName].defaults, options);
var pagination = {
initialize: function() {
var self = this;
// Cache data for current instance
if (!container.data('pagination')) {
container.data('pagination', {});
}
if (self.callHook('beforeInit') === false) return;
// Pagination has been initialized, destroy it
if (container.data('pagination').initialized) {
$('.paginationjs', container).remove();
}
// Whether to disable Pagination at the initialization
self.disabled = !!attributes.disabled;
// Model will be passed to the callback function
var model = self.model = {
pageRange: attributes.pageRange,
pageSize: attributes.pageSize
};
// Parse dataSource to find available paging data
self.parseDataSource(attributes.dataSource, function(dataSource) {
// Asynchronous mode
self.isAsync = Helpers.isString(dataSource);
if (Helpers.isArray(dataSource)) {
model.totalNumber = attributes.totalNumber = dataSource.length;
}
// Asynchronous mode and a 'totalNumberLocator' has been specified
self.isDynamicTotalNumber = self.isAsync && attributes.totalNumberLocator;
var el = self.render(true);
// Add extra className to the pagination element
if (attributes.className) {
el.addClass(attributes.className);
}
model.el = el;
// Append / prepend pagination element to the container
container[attributes.position === 'bottom' ? 'append' : 'prepend'](el);
// Bind events
self.observer();
// Mark pagination has been initialized
container.data('pagination').initialized = true;
// Call hook after initialization
self.callHook('afterInit', el);
});
},
render: function(isBoot) {
var self = this;
var model = self.model;
var el = model.el || $('<div class="paginationjs"></div>');
var isForced = isBoot !== true;
self.callHook('beforeRender', isForced);
var currentPage = model.pageNumber || attributes.pageNumber;
var pageRange = attributes.pageRange || 0;
var totalPage = self.getTotalPage();
var rangeStart = currentPage - pageRange;
var rangeEnd = currentPage + pageRange;
if (rangeEnd > totalPage) {
rangeEnd = totalPage;
rangeStart = totalPage - pageRange * 2;
rangeStart = rangeStart < 1 ? 1 : rangeStart;
}
if (rangeStart <= 1) {
rangeStart = 1;
rangeEnd = Math.min(pageRange * 2 + 1, totalPage);
}
el.html(self.generateHTML({
currentPage: currentPage,
pageRange: pageRange,
rangeStart: rangeStart,
rangeEnd: rangeEnd
}));
// Whether to hide pagination when there is only one page
if (attributes.hideOnlyOnePage) {
el[totalPage <= 1 ? 'hide' : 'show']();
}
self.callHook('afterRender', isForced);
return el;
},
getPageLinkTag: function(index) {
var pageLink = attributes.pageLink;
return pageLink ? `<a href="${pageLink}">${index}</a>` : `<a>${index}</a>`;
},
// Generate HTML for page numbers
generatePageNumbersHTML: function(args) {
var self = this;
var currentPage = args.currentPage;
var totalPage = self.getTotalPage();
var getPageLinkTag = self.getPageLinkTag;
var rangeStart = args.rangeStart;
var rangeEnd = args.rangeEnd;
var html = '';
var i;
var ellipsisText = attributes.ellipsisText;
var classPrefix = attributes.classPrefix;
var pageClassName = attributes.pageClassName || '';
var activeClassName = attributes.activeClassName || '';
var disableClassName = attributes.disableClassName || '';
// Display all page numbers if page range disabled
if (attributes.pageRange === null) {
for (i = 1; i <= totalPage; i++) {
if (i == currentPage) {
html += `<li class="${classPrefix}-page J-paginationjs-page ${pageClassName} ${activeClassName}" data-num="${i}"><a>${i}</a></li>`;
} else {
html += `<li class="${classPrefix}-page J-paginationjs-page ${pageClassName}" data-num="${i}">${getPageLinkTag(i)}</li>`;
}
}
return html;
}
if (rangeStart <= 3) {
for (i = 1; i < rangeStart; i++) {
if (i == currentPage) {
html += `<li class="${classPrefix}-page J-paginationjs-page ${pageClassName} ${activeClassName}" data-num="${i}"><a>${i}</a></li>`;
} else {
html += `<li class="${classPrefix}-page J-paginationjs-page ${pageClassName}" data-num="${i}">${getPageLinkTag(i)}</li>`;
}
}
} else {
if (!attributes.hideFirstOnEllipsisShow) {
html += `<li class="${classPrefix}-page ${classPrefix}-first J-paginationjs-page ${pageClassName}" data-num="1">${getPageLinkTag(1)}</li>`;
}
html += `<li class="${classPrefix}-ellipsis ${disableClassName}"><a>${ellipsisText}</a></li>`;
}
for (i = rangeStart; i <= rangeEnd; i++) {
if (i == currentPage) {
html += `<li class="${classPrefix}-page J-paginationjs-page ${pageClassName} ${activeClassName}" data-num="${i}"><a>${i}</a></li>`;
} else {
html += `<li class="${classPrefix}-page J-paginationjs-page ${pageClassName}" data-num="${i}">${getPageLinkTag(i)}</li>`;
}
}
if (rangeEnd >= totalPage - 2) {
for (i = rangeEnd + 1; i <= totalPage; i++) {
html += `<li class="${classPrefix}-page J-paginationjs-page ${pageClassName}" data-num="${i}">${getPageLinkTag(i)}</li>`;
}
} else {
html += `<li class="${classPrefix}-ellipsis ${disableClassName}"><a>${ellipsisText}</a></li>`;
if (!attributes.hideLastOnEllipsisShow) {
html += `<li class="${classPrefix}-page ${classPrefix}-last J-paginationjs-page ${pageClassName}" data-num="${totalPage}">${getPageLinkTag(totalPage)}</li>`;
}
}
return html;
},
// Generate HTML content
generateHTML: function(args) {
var self = this;
var currentPage = args.currentPage;
var totalPage = self.getTotalPage();
var getPageLinkTag = self.getPageLinkTag;
var totalNumber = self.getTotalNumber();
var pageSize = attributes.pageSize;
var showPrevious = attributes.showPrevious;
var showNext = attributes.showNext;
var showPageNumbers = attributes.showPageNumbers;
var showNavigator = attributes.showNavigator;
var showSizeChanger = attributes.showSizeChanger;
var sizeChangerOptions = attributes.sizeChangerOptions;
var showGoInput = attributes.showGoInput;
var showGoButton = attributes.showGoButton;
var prevText = attributes.prevText;
var nextText = attributes.nextText;
var goButtonText = attributes.goButtonText;
var classPrefix = attributes.classPrefix;
var disableClassName = attributes.disableClassName || '';
var ulClassName = attributes.ulClassName || '';
var prevClassName = attributes.prevClassName || '';
var nextClassName = attributes.nextClassName || '';
var html = '';
var sizeSelect = `<select class="J-paginationjs-size-select">`;
var goInput = '<input type="text" class="J-paginationjs-go-pagenumber">';
var goButton = `<input type="button" class="J-paginationjs-go-button" value="${goButtonText}">`;
var formattedString;
var formatSizeChanger = typeof attributes.formatSizeChanger === 'function' ? attributes.formatSizeChanger(currentPage, totalPage, totalNumber) : attributes.formatSizeChanger;
var formatNavigator = typeof attributes.formatNavigator === 'function' ? attributes.formatNavigator(currentPage, totalPage, totalNumber) : attributes.formatNavigator;
var formatGoInput = typeof attributes.formatGoInput === 'function' ? attributes.formatGoInput(goInput, currentPage, totalPage, totalNumber) : attributes.formatGoInput;
var formatGoButton = typeof attributes.formatGoButton === 'function' ? attributes.formatGoButton(goButton, currentPage, totalPage, totalNumber) : attributes.formatGoButton;
var autoHidePrevious = typeof attributes.autoHidePrevious === 'function' ? attributes.autoHidePrevious() : attributes.autoHidePrevious;
var autoHideNext = typeof attributes.autoHideNext === 'function' ? attributes.autoHideNext() : attributes.autoHideNext;
var header = typeof attributes.header === 'function' ? attributes.header(currentPage, totalPage, totalNumber) : attributes.header;
var footer = typeof attributes.footer === 'function' ? attributes.footer(currentPage, totalPage, totalNumber) : attributes.footer;
// Prepend extra contents to the pagination buttons
if (header) {
formattedString = self.replaceVariables(header, {
currentPage: currentPage,
totalPage: totalPage,
totalNumber: totalNumber
});
html += formattedString;
}
// Whether to display navigator
if (showNavigator) {
if (formatNavigator) {
formattedString = self.replaceVariables(formatNavigator, {
currentPage: currentPage,
totalPage: totalPage,
totalNumber: totalNumber,
rangeStart: (currentPage - 1) * pageSize + 1,
rangeEnd: Math.min(currentPage * pageSize, totalNumber)
});
html += `<div class="${classPrefix}-nav J-paginationjs-nav">${formattedString}</div>`;
}
}
if (showPrevious || showPageNumbers || showNext) {
html += '<div class="paginationjs-pages">';
if (ulClassName) {
html += `<ul class="${ulClassName}">`;
} else {
html += '<ul>';
}
// Whether to display Previous button
if (showPrevious) {
if (currentPage <= 1) {
if (!autoHidePrevious) {
html += `<li class="${classPrefix}-prev ${disableClassName} ${prevClassName}"><a>${prevText}</a></li>`;
}
} else {
html += `<li class="${classPrefix}-prev J-paginationjs-previous ${prevClassName}" data-num="${currentPage - 1}" title="Previous page">${getPageLinkTag(prevText)}</li>`;
}
}
// Whether to display page numbers
if (showPageNumbers) {
html += self.generatePageNumbersHTML(args);
}
// Whether to display Next button
if (showNext) {
if (currentPage >= totalPage) {
if (!autoHideNext) {
html += `<li class="${classPrefix}-next ${disableClassName} ${nextClassName}"><a>${nextText}</a></li>`;
}
} else {
html += `<li class="${classPrefix}-next J-paginationjs-next ${nextClassName}" data-num="${currentPage + 1}" title="Next page">${getPageLinkTag(nextText)}</li>`;
}
}
html += `</ul></div>`;
}
if (showSizeChanger) {
if (Helpers.isArray(sizeChangerOptions)) {
if (sizeChangerOptions.indexOf(pageSize) === -1) {
sizeChangerOptions.unshift(pageSize);
sizeChangerOptions.sort((a, b) => a - b);
}
for (let i = 0; i < sizeChangerOptions.length; i++) {
sizeSelect += `<option value="${sizeChangerOptions[i]}"${(sizeChangerOptions[i] === pageSize ? ' selected' : '')}>${sizeChangerOptions[i]} / page</option>`;
}
sizeSelect += `</select>`;
formattedString = sizeSelect;
if (formatSizeChanger) {
formattedString = self.replaceVariables(formatSizeChanger, {
length: sizeSelect,
total: totalNumber
});
}
html += `<div class="paginationjs-size-changer">${formattedString}</div>`;
}
}
// Whether to display Go input
if (showGoInput) {
if (formatGoInput) {
formattedString = self.replaceVariables(formatGoInput, {
currentPage: currentPage,
totalPage: totalPage,
totalNumber: totalNumber,
input: goInput
});
html += `<div class="${classPrefix}-go-input">${formattedString}</div>`;
}
}
// Whether to display Go button
if (showGoButton) {
if (formatGoButton) {
formattedString = self.replaceVariables(formatGoButton, {
currentPage: currentPage,
totalPage: totalPage,
totalNumber: totalNumber,
button: goButton
});
html += `<div class="${classPrefix}-go-button">${formattedString}</div>`;
}
}
// Append extra contents to the pagination buttons
if (footer) {
formattedString = self.replaceVariables(footer, {
currentPage: currentPage,
totalPage: totalPage,
totalNumber: totalNumber
});
html += formattedString;
}
return html;
},
// dataSource is a request URL and a 'totalNumberLocator' function specified
// execute it to find out 'totalNumber' from the response
findTotalNumberFromRemoteResponse: function(response) {
var self = this;
self.model.totalNumber = attributes.totalNumberLocator(response);
},
// Go to the specified page
go: function(number, callback) {
var self = this;
var model = self.model;
if (self.disabled) return;
var pageNumber = number;
pageNumber = parseInt(pageNumber);
if (!pageNumber || pageNumber < 1) return;
var pageSize = attributes.pageSize;
var totalNumber = self.getTotalNumber();
var totalPage = self.getTotalPage();
if (totalNumber > 0 && pageNumber > totalPage) return;
// Pick paging data in synchronous mode
if (!self.isAsync) {
render(self.getPagingData(pageNumber));
return;
}
var postData = {};
var alias = attributes.alias || {};
var pageSizeName = alias.pageSize ? alias.pageSize : 'pageSize';
var pageNumberName = alias.pageNumber ? alias.pageNumber : 'pageNumber';
postData[pageSizeName] = pageSize;
postData[pageNumberName] = pageNumber;
var ajaxParams = typeof attributes.ajax === 'function' ? attributes.ajax() : attributes.ajax;
// If the pageNumber's value starts with 0 via Ajax
if (ajaxParams && ajaxParams.pageNumberStartWithZero) {
postData[pageNumberName] = pageNumber - 1;
}
var formatAjaxParams = {
type: 'get',
cache: false,
data: {},
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
dataType: 'json',
async: true
};
$.extend(true, formatAjaxParams, ajaxParams);
$.extend(formatAjaxParams.data, postData);
formatAjaxParams.url = attributes.dataSource;
formatAjaxParams.success = function(response) {
try {
self.model.originalResponse = response;
if (self.isDynamicTotalNumber) {
self.findTotalNumberFromRemoteResponse(response);
} else {
self.model.totalNumber = attributes.totalNumber;
}
var finalData = self.filterDataWithLocator(response);
render(finalData);
} catch (e) {
if(typeof attributes.onError === 'function') {
attributes.onError(e, 'ajaxSuccessHandlerError');
} else {
throw e;
}
}
};
formatAjaxParams.error = function(jqXHR, textStatus, errorThrown) {
attributes.formatAjaxError && attributes.formatAjaxError(jqXHR, textStatus, errorThrown);
self.enable();
};
self.disable();
if (attributes.ajaxFunction) {
attributes.ajaxFunction(formatAjaxParams);
} else {
$.ajax(formatAjaxParams);
}
function render(data) {
if (self.callHook('beforePaging', pageNumber) === false) return false;
// Pagination direction
model.direction = typeof model.pageNumber === 'undefined' ? 0 : (pageNumber > model.pageNumber ? 1 : -1);
model.pageNumber = pageNumber;
self.render();
if (self.disabled && self.isAsync) {
// enable pagination
self.enable();
}
// cache model data
container.data('pagination').model = model;
// format result data before callback invoked
if (attributes.formatResult) {
var cloneData = $.extend(true, [], data);
if (!Helpers.isArray(data = attributes.formatResult(cloneData))) {
data = cloneData;
}
}
container.data('pagination').currentPageData = data;
self.doCallback(data, callback);
self.callHook('afterPaging', pageNumber);
if (pageNumber == 1) {
self.callHook('afterIsFirstPage');
} else if (pageNumber == self.getTotalPage()) {
self.callHook('afterIsLastPage');
}
}
},
doCallback: function(data, customCallback) {
var self = this;
var model = self.model;
if (typeof customCallback === 'function') {
customCallback(data, model);
} else if (typeof attributes.callback === 'function') {
attributes.callback(data, model);
}
},
destroy: function() {
if (this.callHook('beforeDestroy') === false) return;
this.model.el.remove();
container.off();
// Remove style element
$('#paginationjs-style').remove();
this.callHook('afterDestroy');
},
previous: function(callback) {
this.go(this.model.pageNumber - 1, callback);
},
next: function(callback) {
this.go(this.model.pageNumber + 1, callback);
},
disable: function() {
var self = this;
var source = self.isAsync ? 'async' : 'sync';
if (self.callHook('beforeDisable', source) === false) return;
self.disabled = true;
self.model.disabled = true;
self.callHook('afterDisable', source);
},
enable: function() {
var self = this;
var source = self.isAsync ? 'async' : 'sync';
if (self.callHook('beforeEnable', source) === false) return;
self.disabled = false;
self.model.disabled = false;
self.callHook('afterEnable', source);
},
refresh: function(callback) {
this.go(this.model.pageNumber, callback);
},
show: function() {
var self = this;
if (self.model.el.is(':visible')) return;
self.model.el.show();
},
hide: function() {
var self = this;
if (!self.model.el.is(':visible')) return;
self.model.el.hide();
},
// Replace variables for template string
replaceVariables: function(template, variables) {
var formattedString;
for (var key in variables) {
var value = variables[key];
var regexp = new RegExp('<%=\\s*' + key + '\\s*%>', 'img');
formattedString = (formattedString || template).replace(regexp, value);
}
return formattedString;
},
getPagingData: function(number) {
var pageSize = attributes.pageSize;
var dataSource = attributes.dataSource;
var totalNumber = this.getTotalNumber();
var start = pageSize * (number - 1) + 1;
var end = Math.min(number * pageSize, totalNumber);
return dataSource.slice(start - 1, end);
},
getTotalNumber: function() {
return this.model.totalNumber || attributes.totalNumber || 0;
},
getTotalPage: function() {
return Math.ceil(this.getTotalNumber() / attributes.pageSize);
},
getLocator: function(locator) {
var result;
if (typeof locator === 'string') {
result = locator;
} else if (typeof locator === 'function') {
result = locator();
} else {
throwError('"locator" is incorrect. Expect string or function type.');
}
return result;
},
// Filter data with "locator"
filterDataWithLocator: function(dataSource) {
var locator = this.getLocator(attributes.locator);
var filteredData;
// Datasource is an Object, use "locator" to locate available data
if (Helpers.isObject(dataSource)) {
try {
$.each(locator.split('.'), function(index, item) {
filteredData = (filteredData ? filteredData : dataSource)[item];
});
}
catch (e) {
// ignore
}
if (!filteredData) {
throwError('dataSource.' + locator + ' is undefined.');
} else if (!Helpers.isArray(filteredData)) {
throwError('dataSource.' + locator + ' should be an Array.');
}
}
return filteredData || dataSource;
},
parseDataSource: function(dataSource, callback) {
var self = this;
if (Helpers.isObject(dataSource)) {
callback(attributes.dataSource = self.filterDataWithLocator(dataSource));
} else if (Helpers.isArray(dataSource)) {
callback(attributes.dataSource = dataSource);
} else if (typeof dataSource === 'function') {
attributes.dataSource(function(data) {
if (!Helpers.isArray(data)) {
throwError('The parameter of "done" Function should be an Array.');
}
self.parseDataSource.call(self, data, callback);
});
} else if (typeof dataSource === 'string') {
if (/^https?|file:/.test(dataSource)) {
attributes.ajaxDataType = 'jsonp';
}
callback(dataSource);
} else {
throwError('Unexpected dataSource type');
}
},
callHook: function(hook) {
var paginationData = container.data('pagination') || {};
var result;
var args = Array.prototype.slice.apply(arguments);
args.shift();
if (attributes[hook] && typeof attributes[hook] === 'function') {
if (attributes[hook].apply(global, args) === false) {
result = false;
}
}
if (paginationData.hooks && paginationData.hooks[hook]) {
$.each(paginationData.hooks[hook], function(index, item) {
if (item.apply(global, args) === false) {
result = false;
}
});
}
return result !== false;
},
observer: function() {
var self = this;
var el = self.model.el;
// Go to specified page number
container.on(eventPrefix + 'go', function(event, pageNumber, done) {
if (typeof pageNumber === 'string') {
pageNumber = parseInt(pageNumber.trim());
}
if (!pageNumber) return;
if (typeof pageNumber !== 'number') {
throwError('"pageNumber" is incorrect. (Number)');
}
self.go(pageNumber, done);
});
// Page number button click listener
el.on('click', '.J-paginationjs-page', function(event) {
var current = $(event.currentTarget);
var pageNumber = current.attr('data-num').trim();
if (!pageNumber || current.hasClass(attributes.disableClassName) || current.hasClass(attributes.activeClassName)) return;
if (self.callHook('beforePageOnClick', event, pageNumber) === false) return false;
self.go(pageNumber);
self.callHook('afterPageOnClick', event, pageNumber);
if (!attributes.pageLink) return false;
});
// Previous button click listener
el.on('click', '.J-paginationjs-previous', function(event) {
var current = $(event.currentTarget);
var pageNumber = current.attr('data-num').trim();
if (!pageNumber || current.hasClass(attributes.disableClassName)) return;
if (self.callHook('beforePreviousOnClick', event, pageNumber) === false) return false;
self.go(pageNumber);
self.callHook('afterPreviousOnClick', event, pageNumber);
if (!attributes.pageLink) return false;
});
// Next button click listener
el.on('click', '.J-paginationjs-next', function(event) {
var current = $(event.currentTarget);
var pageNumber = current.attr('data-num').trim();
if (!pageNumber || current.hasClass(attributes.disableClassName)) return;
if (self.callHook('beforeNextOnClick', event, pageNumber) === false) return false;
self.go(pageNumber);
self.callHook('afterNextOnClick', event, pageNumber);
if (!attributes.pageLink) return false;
});
// Go button click listener
el.on('click', '.J-paginationjs-go-button', function(event) {
var pageNumber = $('.J-paginationjs-go-pagenumber', el).val();
if (self.callHook('beforeGoButtonOnClick', event, pageNumber) === false) return false;
container.trigger(eventPrefix + 'go', pageNumber);
self.callHook('afterGoButtonOnClick', event, pageNumber);
});
// go input enter keyup listener
el.on('keyup', '.J-paginationjs-go-pagenumber', function(event) {
if (event.which === 13) {
var pageNumber = $(event.currentTarget).val();
if (self.callHook('beforeGoInputOnEnter', event, pageNumber) === false) return false;
container.trigger(eventPrefix + 'go', pageNumber);
// Maintain the cursor
$('.J-paginationjs-go-pagenumber', el).focus();
self.callHook('afterGoInputOnEnter', event, pageNumber);
}
});
el.on('change', '.J-paginationjs-size-select', function(event) {
var current = $(event.currentTarget);
var size = parseInt(current.val());
var currentPage = self.model.pageNumber || attributes.pageNumber;
if (typeof size !== 'number') return;
if (self.callHook('beforeSizeSelectorChange', event, size) === false) return false;
attributes.pageSize = size;
self.model.pageSize = size;
self.model.totalPage = self.getTotalPage();
if (currentPage > self.model.totalPage) {
currentPage = self.model.totalPage;
}
self.go(currentPage);
self.callHook('afterSizeSelectorChange', event, size);
if (!attributes.pageLink) return false;
});
// Previous page
container.on(eventPrefix + 'previous', function(event, done) {
self.previous(done);
});
// Next page
container.on(eventPrefix + 'next', function(event, done) {
self.next(done);
});
// Disable
container.on(eventPrefix + 'disable', function() {
self.disable();
});
// Enable
container.on(eventPrefix + 'enable', function() {
self.enable();
});
// Refresh
container.on(eventPrefix + 'refresh', function(event, done) {
self.refresh(done);
});
// Show
container.on(eventPrefix + 'show', function() {
self.show();
});
// Hide
container.on(eventPrefix + 'hide', function() {
self.hide();
});
// Destroy
container.on(eventPrefix + 'destroy', function() {
self.destroy();
});
// Whether to load the default page
var validTotalPage = Math.max(self.getTotalPage(), 1)
var defaultPageNumber = attributes.pageNumber;
// Default pageNumber should be 1 when totalNumber is dynamic
if (self.isDynamicTotalNumber) {
if (attributes.resetPageNumberOnInit) defaultPageNumber = 1;
}
if (attributes.triggerPagingOnInit) {
container.trigger(eventPrefix + 'go', Math.min(defaultPageNumber, validTotalPage));
}
}
};
// Pagination has been initialized
if (container.data('pagination') && container.data('pagination').initialized === true) {
// Handle events
if (isNumeric(options)) {
// eg: container.pagination(5)
container.trigger.call(this, eventPrefix + 'go', options, arguments[1]);
return this;
} else if (typeof options === 'string') {
var args = Array.prototype.slice.apply(arguments);
args[0] = eventPrefix + args[0];
switch (options) {
case 'previous':
case 'next':
case 'go':
case 'disable':
case 'enable':
case 'refresh':
case 'show':
case 'hide':
case 'destroy':
container.trigger.apply(this, args);
break;
case 'getSelectedPageNum':
case 'getCurrentPageNum':
if (container.data('pagination').model) {
return container.data('pagination').model.pageNumber;
} else {
return container.data('pagination').attributes.pageNumber;
}
case 'getTotalPage':
return Math.ceil(container.data('pagination').model.totalNumber / container.data('pagination').model.pageSize);
case 'getSelectedPageData':
case 'getCurrentPageData':
return container.data('pagination').currentPageData;
// Whether pagination has been disabled
case 'isDisabled':
return container.data('pagination').model.disabled === true;
default:
throwError('Unknown action: ' + options);
}
return this;
} else {
// Uninstall the old instance before initializing a new one
uninstallPlugin(container);
}
} else {
if (!Helpers.isObject(options)) throwError('Illegal options');
}
// Check parameters
parameterChecker(attributes);
pagination.initialize();
return this;
};
// Instance defaults
$.fn[pluginName].defaults = {
// Data source
// Array | String | Function | Object
//dataSource: '',
// String | Function
//locator: 'data',
// Function
//totalNumberLocator: function() {},
// Total number of data items
totalNumber: 0,
// Default page number
pageNumber: 1,
// Number of data items per page
pageSize: 10,
// Page range (pages around current page)
pageRange: 2,
// Whether to display the 'Previous' button
showPrevious: true,
// Whether to display the 'Next' button
showNext: true,
// Whether to display the page buttons
showPageNumbers: true,
showNavigator: false,
// Whether to display the 'Go' input
showGoInput: false,
// Whether to display the 'Go' button
showGoButton: false,
showSizeChanger: false,
sizeChangerOptions: [10, 20, 50, 100],
// Page link
pageLink: '',
// 'Previous' text
prevText: '‹',
// 'Next' text
nextText: '›',
// Ellipsis text
ellipsisText: '...',
// 'Go' button text
goButtonText: 'Go',
// Additional class name(s) for the Pagination container
//className: '',
classPrefix: 'paginationjs',
activeClassName: 'active',
// class name when disabled
disableClassName: 'disabled',
//ulClassName: '',
//pageClassName: '',
//prevClassName: '',
//nextClassName: '',
formatNavigator: 'Total <%= totalNumber %> items',
formatGoInput: '<%= input %>',
formatGoButton: '<%= button %>',
// position in the container
position: 'bottom',
// Auto hide previous button when current page is the first
autoHidePrevious: false,
// Auto hide next button when current page is the last
autoHideNext: false,
//header: '',
//footer: '',
//alias: {},
// Whether to trigger pagination at initialization
triggerPagingOnInit: true,
// Whether to reset page number at initialization, it works only if dataSource is a URL and totalNumberLocator is specified
resetPageNumberOnInit: true,
// Whether to hide pagination when less than one page
hideOnlyOnePage: false,
hideFirstOnEllipsisShow: false,
hideLastOnEllipsisShow: false,
// Customize item's innerHTML
callback: function() {}
};
// Hook register
$.fn[pluginHookMethod] = function(hook, callback) {
if (arguments.length < 2) {
throwError('Expect 2 arguments at least.');
}
if (typeof callback !== 'function') {
throwError('callback should be a function.');
}
var container = $(this);
var paginationData = container.data('pagination');
if (!paginationData) {
container.data('pagination', {});
paginationData = container.data('pagination');
}
!paginationData.hooks && (paginationData.hooks = {});
//paginationData.hooks[hook] = callback;
paginationData.hooks[hook] = paginationData.hooks[hook] || [];
paginationData.hooks[hook].push(callback);
};
// Static method
$[pluginName] = function(selector, options) {
if (arguments.length < 2) {
throwError('Requires two parameters.');
}
var container;
// 'selector' is a jQuery object
if (typeof selector !== 'string' && selector instanceof jQuery) {
container = selector;
} else {
container = $(selector);
}
if (!container.length) return;
container.pagination(options);
return container;
};
// ============================================================
// helpers
// ============================================================
var Helpers = {};
// Throw error
function throwError(content) {
throw new Error('Pagination: ' + content);
}
// Check parameters
function parameterChecker(args) {
if (!args.dataSource) {
throwError('"dataSource" is required.');
}
if (typeof args.dataSource === 'string') {
if (args.totalNumberLocator === undefined) {
if (args.totalNumber === undefined) {
throwError('"totalNumber" is required.');
} else if (!isNumeric(args.totalNumber)) {
throwError('"totalNumber" is incorrect. Expect numberic type');
}
} else {
if (typeof args.totalNumberLocator !== 'function') {
throwError('"totalNumberLocator" should be a Function.');
}
}
} else if (Helpers.isObject(args.dataSource)) {
if (typeof args.locator === 'undefined') {
throwError('"dataSource" is an Object, please specify a "locator".');
} else if (typeof args.locator !== 'string' && typeof args.locator !== 'function') {
throwError('' + args.locator + ' is incorrect. Expect string or function type');
}
}
if (args.formatResult !== undefined && typeof args.formatResult !== 'function') {
throwError('"formatResult" should be a Function.');
}
if (args.onError !== undefined && typeof args.onError !== 'function') {
throwError('"onError" should be a Function.');
}
}
// uninstall plugin
function uninstallPlugin(target) {
var events = ['go', 'previous', 'next', 'disable', 'enable', 'refresh', 'show', 'hide', 'destroy'];
// off all events
$.each(events, function(index, value) {
target.off(eventPrefix + value);
});
// reset pagination data
target.data('pagination', {});
// remove pagination element
$('.paginationjs', target).remove();
}
// Object type detection
function getObjectType(object, tmp) {
return ( (tmp = typeof(object)) == "object" ? object == null && "null" || Object.prototype.toString.call(object).slice(8, -1) : tmp ).toLowerCase();
}
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
$.each(['Object', 'Array', 'String'], function(index, name) {
Helpers['is' + name] = function(object) {
return getObjectType(object) === name.toLowerCase();
};
});
/*
* export via AMD or CommonJS
* */
if (typeof define === 'function' && define.amd) {
define(function() {
return $;
});
}
})(this, window.jQuery);
|