calcite-pagination.cjs.entry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. const index = require('./index-5c65e149.js');
  9. const CSS = {
  10. page: "page",
  11. selected: "is-selected",
  12. previous: "previous",
  13. next: "next",
  14. disabled: "is-disabled",
  15. ellipsis: "ellipsis",
  16. ellipsisStart: "ellipsis--start",
  17. ellipsisEnd: "ellipsis--end"
  18. };
  19. const TEXT = {
  20. nextLabel: "Next",
  21. previousLabel: "Previous"
  22. };
  23. 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)}";
  24. const maxPagesDisplayed = 5;
  25. const Pagination = class {
  26. constructor(hostRef) {
  27. index.registerInstance(this, hostRef);
  28. this.calcitePaginationUpdate = index.createEvent(this, "calcitePaginationUpdate", 7);
  29. this.calcitePaginationChange = index.createEvent(this, "calcitePaginationChange", 7);
  30. //--------------------------------------------------------------------------
  31. //
  32. // Public Properties
  33. //
  34. //--------------------------------------------------------------------------
  35. /** number of items per page */
  36. this.num = 20;
  37. /** index of item that should begin the page */
  38. this.start = 1;
  39. /** total number of items */
  40. this.total = 0;
  41. /** Used as an accessible label (aria-label) for the next button
  42. * @default "Next"
  43. */
  44. this.textLabelNext = TEXT.nextLabel;
  45. /** Used as an accessible label (aria-label) of the previous button
  46. * @default "Previous"
  47. */
  48. this.textLabelPrevious = TEXT.previousLabel;
  49. /** The scale of the pagination */
  50. this.scale = "m";
  51. this.previousClicked = () => {
  52. this.previousPage().then();
  53. this.emitUpdate();
  54. };
  55. this.nextClicked = () => {
  56. this.nextPage();
  57. this.emitUpdate();
  58. };
  59. }
  60. // --------------------------------------------------------------------------
  61. //
  62. // Public Methods
  63. //
  64. // --------------------------------------------------------------------------
  65. /** Go to the next page of results */
  66. async nextPage() {
  67. this.start = Math.min(this.getLastStart(), this.start + this.num);
  68. }
  69. /** Go to the previous page of results */
  70. async previousPage() {
  71. this.start = Math.max(1, this.start - this.num);
  72. }
  73. // --------------------------------------------------------------------------
  74. //
  75. // Private Methods
  76. //
  77. // --------------------------------------------------------------------------
  78. getLastStart() {
  79. const { total, num } = this;
  80. const lastStart = total % num === 0 ? total - num : Math.floor(total / num) * num;
  81. return lastStart + 1;
  82. }
  83. showLeftEllipsis() {
  84. return Math.floor(this.start / this.num) > 3;
  85. }
  86. showRightEllipsis() {
  87. return (this.total - this.start) / this.num > 3;
  88. }
  89. emitUpdate() {
  90. const changePayload = {
  91. start: this.start,
  92. total: this.total,
  93. num: this.num
  94. };
  95. this.calcitePaginationChange.emit(changePayload);
  96. this.calcitePaginationUpdate.emit(changePayload);
  97. }
  98. //--------------------------------------------------------------------------
  99. //
  100. // Render Methods
  101. //
  102. //--------------------------------------------------------------------------
  103. renderPages() {
  104. const lastStart = this.getLastStart();
  105. let end;
  106. let nextStart;
  107. // if we don't need ellipses render the whole set
  108. if (this.total / this.num <= maxPagesDisplayed) {
  109. nextStart = 1 + this.num;
  110. end = lastStart - this.num;
  111. }
  112. else {
  113. // if we're within max pages of page 1
  114. if (this.start / this.num < maxPagesDisplayed - 1) {
  115. nextStart = 1 + this.num;
  116. end = 1 + 4 * this.num;
  117. }
  118. else {
  119. // if we're within max pages of last page
  120. if (this.start + 3 * this.num >= this.total) {
  121. nextStart = lastStart - 4 * this.num;
  122. end = lastStart - this.num;
  123. }
  124. else {
  125. nextStart = this.start - this.num;
  126. end = this.start + this.num;
  127. }
  128. }
  129. }
  130. const pages = [];
  131. while (nextStart <= end) {
  132. pages.push(nextStart);
  133. nextStart = nextStart + this.num;
  134. }
  135. return pages.map((page) => this.renderPage(page));
  136. }
  137. renderPage(start) {
  138. const page = Math.floor(start / this.num) + (this.num === 1 ? 0 : 1);
  139. return (index.h("button", { class: {
  140. [CSS.page]: true,
  141. [CSS.selected]: start === this.start
  142. }, onClick: () => {
  143. this.start = start;
  144. this.emitUpdate();
  145. } }, page));
  146. }
  147. renderLeftEllipsis() {
  148. if (this.total / this.num > maxPagesDisplayed && this.showLeftEllipsis()) {
  149. return index.h("span", { class: `${CSS.ellipsis} ${CSS.ellipsisStart}` }, "\u2026");
  150. }
  151. }
  152. renderRightEllipsis() {
  153. if (this.total / this.num > maxPagesDisplayed && this.showRightEllipsis()) {
  154. return index.h("span", { class: `${CSS.ellipsis} ${CSS.ellipsisEnd}` }, "\u2026");
  155. }
  156. }
  157. render() {
  158. const { total, num, start } = this;
  159. const prevDisabled = num === 1 ? start <= num : start < num;
  160. const nextDisabled = num === 1 ? start + num > total : start + num > total;
  161. return (index.h(index.Fragment, null, index.h("button", { "aria-label": this.textLabelPrevious, class: {
  162. [CSS.previous]: true,
  163. [CSS.disabled]: prevDisabled
  164. }, disabled: prevDisabled, onClick: this.previousClicked }, index.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()), index.h("button", { "aria-label": this.textLabelNext, class: {
  165. [CSS.next]: true,
  166. [CSS.disabled]: nextDisabled
  167. }, disabled: nextDisabled, onClick: this.nextClicked }, index.h("calcite-icon", { flipRtl: true, icon: "chevronRight", scale: "s" }))));
  168. }
  169. get el() { return index.getElement(this); }
  170. };
  171. Pagination.style = paginationCss;
  172. exports.calcite_pagination = Pagination;