calcite-pagination.js 12 KB

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