index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _utils = require("../utils");
  5. var _constant = require("../utils/constant");
  6. var _createNamespace = (0, _utils.createNamespace)('pagination'),
  7. createComponent = _createNamespace[0],
  8. bem = _createNamespace[1],
  9. t = _createNamespace[2];
  10. function makePage(number, text, active) {
  11. return {
  12. number: number,
  13. text: text,
  14. active: active
  15. };
  16. }
  17. var _default = createComponent({
  18. props: {
  19. prevText: String,
  20. nextText: String,
  21. forceEllipses: Boolean,
  22. mode: {
  23. type: String,
  24. default: 'multi'
  25. },
  26. value: {
  27. type: Number,
  28. default: 0
  29. },
  30. pageCount: {
  31. type: [Number, String],
  32. default: 0
  33. },
  34. totalItems: {
  35. type: [Number, String],
  36. default: 0
  37. },
  38. itemsPerPage: {
  39. type: [Number, String],
  40. default: 10
  41. },
  42. showPageSize: {
  43. type: [Number, String],
  44. default: 5
  45. }
  46. },
  47. computed: {
  48. count: function count() {
  49. var count = this.pageCount || Math.ceil(this.totalItems / this.itemsPerPage);
  50. return Math.max(1, count);
  51. },
  52. pages: function pages() {
  53. var pages = [];
  54. var pageCount = this.count;
  55. var showPageSize = +this.showPageSize;
  56. if (this.mode !== 'multi') {
  57. return pages;
  58. } // Default page limits
  59. var startPage = 1;
  60. var endPage = pageCount;
  61. var isMaxSized = showPageSize < pageCount; // recompute if showPageSize
  62. if (isMaxSized) {
  63. // Current page is displayed in the middle of the visible ones
  64. startPage = Math.max(this.value - Math.floor(showPageSize / 2), 1);
  65. endPage = startPage + showPageSize - 1; // Adjust if limit is exceeded
  66. if (endPage > pageCount) {
  67. endPage = pageCount;
  68. startPage = endPage - showPageSize + 1;
  69. }
  70. } // Add page number links
  71. for (var number = startPage; number <= endPage; number++) {
  72. var page = makePage(number, number, number === this.value);
  73. pages.push(page);
  74. } // Add links to move between page sets
  75. if (isMaxSized && showPageSize > 0 && this.forceEllipses) {
  76. if (startPage > 1) {
  77. var previousPageSet = makePage(startPage - 1, '...', false);
  78. pages.unshift(previousPageSet);
  79. }
  80. if (endPage < pageCount) {
  81. var nextPageSet = makePage(endPage + 1, '...', false);
  82. pages.push(nextPageSet);
  83. }
  84. }
  85. return pages;
  86. }
  87. },
  88. watch: {
  89. value: {
  90. handler: function handler(page) {
  91. this.select(page || this.value);
  92. },
  93. immediate: true
  94. }
  95. },
  96. methods: {
  97. select: function select(page, emitChange) {
  98. page = Math.min(this.count, Math.max(1, page));
  99. if (this.value !== page) {
  100. this.$emit('input', page);
  101. if (emitChange) {
  102. this.$emit('change', page);
  103. }
  104. }
  105. }
  106. },
  107. render: function render() {
  108. var _this = this,
  109. _this$slots,
  110. _this$slots3;
  111. var h = arguments[0];
  112. var value = this.value;
  113. var simple = this.mode !== 'multi';
  114. var onSelect = function onSelect(value) {
  115. return function () {
  116. _this.select(value, true);
  117. };
  118. };
  119. return h("ul", {
  120. "class": bem({
  121. simple: simple
  122. })
  123. }, [h("li", {
  124. "class": [bem('item', {
  125. disabled: value === 1
  126. }), bem('prev'), _constant.BORDER],
  127. "on": {
  128. "click": onSelect(value - 1)
  129. }
  130. }, [((_this$slots = this.slots('prev-text')) != null ? _this$slots : this.prevText) || t('prev')]), this.pages.map(function (page) {
  131. var _this$slots2;
  132. return h("li", {
  133. "class": [bem('item', {
  134. active: page.active
  135. }), bem('page'), _constant.BORDER],
  136. "on": {
  137. "click": onSelect(page.number)
  138. }
  139. }, [(_this$slots2 = _this.slots('page', page)) != null ? _this$slots2 : page.text]);
  140. }), simple && h("li", {
  141. "class": bem('page-desc')
  142. }, [this.slots('pageDesc') || value + "/" + this.count]), h("li", {
  143. "class": [bem('item', {
  144. disabled: value === this.count
  145. }), bem('next'), _constant.BORDER],
  146. "on": {
  147. "click": onSelect(value + 1)
  148. }
  149. }, [((_this$slots3 = this.slots('next-text')) != null ? _this$slots3 : this.nextText) || t('next')])]);
  150. }
  151. });
  152. exports.default = _default;