calcite-pagination.entry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.82
  5. */
  6. import { r as registerInstance, c as createEvent, h, F as Fragment, g as getElement } from './index-8ece2564.js';
  7. const CSS = {
  8. page: "page",
  9. selected: "is-selected",
  10. previous: "previous",
  11. next: "next",
  12. disabled: "is-disabled",
  13. ellipsis: "ellipsis",
  14. ellipsisStart: "ellipsis--start",
  15. ellipsisEnd: "ellipsis--end"
  16. };
  17. const TEXT = {
  18. nextLabel: "Next",
  19. previousLabel: "Previous"
  20. };
  21. const paginationCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host([scale=s]){--calcite-pagination-spacing:0.25rem 0.5rem}:host([scale=s]) .previous,:host([scale=s]) .next,:host([scale=s]) .page{height:1.5rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=s]) .previous,:host([scale=s]) .next{padding-left:0.25rem;padding-right:0.25rem}:host([scale=m]){--calcite-pagination-spacing:0.5rem 0.75rem}:host([scale=m]) .previous,:host([scale=m]) .next,:host([scale=m]) .page{height:2rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=m]) .previous,:host([scale=m]) .next{padding-left:0.5rem;padding-right:0.5rem}:host([scale=l]){--calcite-pagination-spacing:0.75rem 1rem}:host([scale=l]) .previous,:host([scale=l]) .next,:host([scale=l]) .page{height:2.75rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}:host([scale=l]) .previous,:host([scale=l]) .next{padding-left:1rem;padding-right:1rem}:host{display:-ms-flexbox;display:flex;-webkit-writing-mode:horizontal-tb;-ms-writing-mode:lr-tb;writing-mode:horizontal-tb}:host button{outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out}:host button:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}.previous,.next,.page{-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;cursor:pointer;-ms-flex-align:center;align-items:center;border-style:none;--tw-border-opacity:0;background-color:transparent;font-family:inherit;font-size:var(--calcite-font-size-0);line-height:1.25rem;color:var(--calcite-ui-text-3);border-top:2px solid transparent;border-bottom:2px solid transparent}.previous:hover,.next:hover,.page:hover{color:var(--calcite-ui-text-1);-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}.page:hover{border-bottom-color:var(--calcite-ui-border-2)}.page.is-selected{font-weight:var(--calcite-font-weight-medium);color:var(--calcite-ui-text-1);border-bottom-color:var(--calcite-ui-brand)}.previous:hover,.next:hover{background-color:var(--calcite-ui-foreground-2);color:var(--calcite-ui-brand)}.previous:active,.next:active{background-color:var(--calcite-ui-foreground-3)}.previous.is-disabled,.next.is-disabled{pointer-events:none;background-color:transparent}.previous.is-disabled>calcite-icon,.next.is-disabled>calcite-icon{opacity:var(--calcite-ui-opacity-disabled)}.next{margin-right:0px}.page,.ellipsis{padding:var(--calcite-pagination-spacing)}.ellipsis{display:-ms-flexbox;display:flex;-ms-flex-align:end;align-items:flex-end;color:var(--calcite-ui-text-3)}";
  22. const maxPagesDisplayed = 5;
  23. const Pagination = class {
  24. constructor(hostRef) {
  25. registerInstance(this, hostRef);
  26. this.calcitePaginationUpdate = createEvent(this, "calcitePaginationUpdate", 7);
  27. this.calcitePaginationChange = createEvent(this, "calcitePaginationChange", 7);
  28. //--------------------------------------------------------------------------
  29. //
  30. // Public Properties
  31. //
  32. //--------------------------------------------------------------------------
  33. /** number of items per page */
  34. this.num = 20;
  35. /** index of item that should begin the page */
  36. this.start = 1;
  37. /** total number of items */
  38. this.total = 0;
  39. /** Used as an accessible label (aria-label) for the next button
  40. * @default "Next"
  41. */
  42. this.textLabelNext = TEXT.nextLabel;
  43. /** Used as an accessible label (aria-label) of the previous button
  44. * @default "Previous"
  45. */
  46. this.textLabelPrevious = TEXT.previousLabel;
  47. /** The scale of the pagination */
  48. this.scale = "m";
  49. this.previousClicked = () => {
  50. this.previousPage().then();
  51. this.emitUpdate();
  52. };
  53. this.nextClicked = () => {
  54. this.nextPage();
  55. this.emitUpdate();
  56. };
  57. }
  58. // --------------------------------------------------------------------------
  59. //
  60. // Public Methods
  61. //
  62. // --------------------------------------------------------------------------
  63. /** Go to the next page of results */
  64. async nextPage() {
  65. this.start = Math.min(this.getLastStart(), this.start + this.num);
  66. }
  67. /** Go to the previous page of results */
  68. async previousPage() {
  69. this.start = Math.max(1, this.start - this.num);
  70. }
  71. // --------------------------------------------------------------------------
  72. //
  73. // Private Methods
  74. //
  75. // --------------------------------------------------------------------------
  76. getLastStart() {
  77. const { total, num } = this;
  78. const lastStart = total % num === 0 ? total - num : Math.floor(total / num) * num;
  79. return lastStart + 1;
  80. }
  81. showLeftEllipsis() {
  82. return Math.floor(this.start / this.num) > 3;
  83. }
  84. showRightEllipsis() {
  85. return (this.total - this.start) / this.num > 3;
  86. }
  87. emitUpdate() {
  88. const changePayload = {
  89. start: this.start,
  90. total: this.total,
  91. num: this.num
  92. };
  93. this.calcitePaginationChange.emit(changePayload);
  94. this.calcitePaginationUpdate.emit(changePayload);
  95. }
  96. //--------------------------------------------------------------------------
  97. //
  98. // Render Methods
  99. //
  100. //--------------------------------------------------------------------------
  101. renderPages() {
  102. const lastStart = this.getLastStart();
  103. let end;
  104. let nextStart;
  105. // if we don't need ellipses render the whole set
  106. if (this.total / this.num <= maxPagesDisplayed) {
  107. nextStart = 1 + this.num;
  108. end = lastStart - this.num;
  109. }
  110. else {
  111. // if we're within max pages of page 1
  112. if (this.start / this.num < maxPagesDisplayed - 1) {
  113. nextStart = 1 + this.num;
  114. end = 1 + 4 * this.num;
  115. }
  116. else {
  117. // if we're within max pages of last page
  118. if (this.start + 3 * this.num >= this.total) {
  119. nextStart = lastStart - 4 * this.num;
  120. end = lastStart - this.num;
  121. }
  122. else {
  123. nextStart = this.start - this.num;
  124. end = this.start + this.num;
  125. }
  126. }
  127. }
  128. const pages = [];
  129. while (nextStart <= end) {
  130. pages.push(nextStart);
  131. nextStart = nextStart + this.num;
  132. }
  133. return pages.map((page) => this.renderPage(page));
  134. }
  135. renderPage(start) {
  136. const page = Math.floor(start / this.num) + (this.num === 1 ? 0 : 1);
  137. return (h("button", { class: {
  138. [CSS.page]: true,
  139. [CSS.selected]: start === this.start
  140. }, onClick: () => {
  141. this.start = start;
  142. this.emitUpdate();
  143. } }, page));
  144. }
  145. renderLeftEllipsis() {
  146. if (this.total / this.num > maxPagesDisplayed && this.showLeftEllipsis()) {
  147. return h("span", { class: `${CSS.ellipsis} ${CSS.ellipsisStart}` }, "\u2026");
  148. }
  149. }
  150. renderRightEllipsis() {
  151. if (this.total / this.num > maxPagesDisplayed && this.showRightEllipsis()) {
  152. return h("span", { class: `${CSS.ellipsis} ${CSS.ellipsisEnd}` }, "\u2026");
  153. }
  154. }
  155. render() {
  156. const { total, num, start } = this;
  157. const prevDisabled = num === 1 ? start <= num : start < num;
  158. const nextDisabled = num === 1 ? start + num > total : start + num > total;
  159. return (h(Fragment, null, h("button", { "aria-label": this.textLabelPrevious, class: {
  160. [CSS.previous]: true,
  161. [CSS.disabled]: prevDisabled
  162. }, disabled: prevDisabled, onClick: this.previousClicked }, h("calcite-icon", { flipRtl: true, icon: "chevronLeft", scale: "s" })), total > num ? this.renderPage(1) : null, this.renderLeftEllipsis(), this.renderPages(), this.renderRightEllipsis(), this.renderPage(this.getLastStart()), h("button", { "aria-label": this.textLabelNext, class: {
  163. [CSS.next]: true,
  164. [CSS.disabled]: nextDisabled
  165. }, disabled: nextDisabled, onClick: this.nextClicked }, h("calcite-icon", { flipRtl: true, icon: "chevronRight", scale: "s" }))));
  166. }
  167. get el() { return getElement(this); }
  168. };
  169. Pagination.style = paginationCss;
  170. export { Pagination as calcite_pagination };