paginator.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. function paginatorHelper(options = {}) {
  3. const current = options.current || this.page.current || 0;
  4. const total = options.total || this.page.total || 1;
  5. const endSize = options.hasOwnProperty('end_size') ? +options.end_size : 1;
  6. const midSize = options.hasOwnProperty('mid_size') ? +options.mid_size : 2;
  7. const space = options.hasOwnProperty('space') ? options.space : '…';
  8. const base = options.base || this.page.base || '';
  9. const format = options.format || `${this.config.pagination_dir}/%d/`;
  10. const prevText = options.prev_text || 'Prev';
  11. const nextText = options.next_text || 'Next';
  12. const prevNext = options.hasOwnProperty('prev_next') ? options.prev_next : true;
  13. const transform = options.transform;
  14. const self = this;
  15. let result = '';
  16. let i;
  17. if (!current) return '';
  18. const currentPage = `<span class="page-number current">${transform ? transform(current) : current}</span>`;
  19. function link(i) {
  20. return self.url_for(i === 1 ? base : base + format.replace('%d', i));
  21. }
  22. function pageLink(i) {
  23. return `<a class="page-number" href="${link(i)}">${transform ? transform(i) : i}</a>`;
  24. }
  25. // Display the link to the previous page
  26. if (prevNext && current > 1) {
  27. result += `<a class="extend prev" rel="prev" href="${link(current - 1)}">${prevText}</a>`;
  28. }
  29. if (options.show_all) {
  30. // Display pages on the left side of the current page
  31. for (i = 1; i < current; i++) {
  32. result += pageLink(i);
  33. }
  34. // Display the current page
  35. result += currentPage;
  36. // Display pages on the right side of the current page
  37. for (i = current + 1; i <= total; i++) {
  38. result += pageLink(i);
  39. }
  40. } else {
  41. // It's too complicated. May need refactor.
  42. const leftEnd = current <= endSize ? current - 1 : endSize;
  43. const rightEnd = total - current <= endSize ? current + 1 : total - endSize + 1;
  44. const leftMid = current - midSize <= endSize ? leftEnd + 1 : current - midSize;
  45. const rightMid = current + midSize + endSize > total ? rightEnd - 1 : current + midSize;
  46. const spaceHtml = `<span class="space">${space}</span>`;
  47. // Display pages on the left edge
  48. for (i = 1; i <= leftEnd; i++) {
  49. result += pageLink(i);
  50. }
  51. // Display spaces between edges and middle pages
  52. if (space && current - endSize - midSize > 1) {
  53. result += spaceHtml;
  54. }
  55. // Display left middle pages
  56. if (leftMid > leftEnd) {
  57. for (i = leftMid; i < current; i++) {
  58. result += pageLink(i);
  59. }
  60. }
  61. // Display the current page
  62. result += currentPage;
  63. // Display right middle pages
  64. if (rightMid < rightEnd) {
  65. for (i = current + 1; i <= rightMid; i++) {
  66. result += pageLink(i);
  67. }
  68. }
  69. // Display spaces between edges and middle pages
  70. if (space && total - endSize - midSize > current) {
  71. result += spaceHtml;
  72. }
  73. // Dispaly pages on the right edge
  74. for (i = rightEnd; i <= total; i++) {
  75. result += pageLink(i);
  76. }
  77. }
  78. // Display the link to the next page
  79. if (prevNext && current < total) {
  80. result += `<a class="extend next" rel="next" href="${link(current + 1)}">${nextText}</a>`;
  81. }
  82. return result;
  83. }
  84. module.exports = paginatorHelper;