calcite-pagination.entry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.97
  5. */
  6. import { r as registerInstance, c as createEvent, h, F as Fragment, g as getElement } from './index-1f9b54dc.js';
  7. import { n as numberStringFormatter, c as connectLocalized, d as disconnectLocalized } from './locale-35f81208.js';
  8. import './key-acc755b7.js';
  9. import './observers-9f44e9b3.js';
  10. import './dom-8f0a9ff2.js';
  11. import './resources-9c476cb6.js';
  12. import './guid-9f15e57a.js';
  13. const CSS = {
  14. page: "page",
  15. selected: "is-selected",
  16. previous: "previous",
  17. next: "next",
  18. disabled: "is-disabled",
  19. ellipsis: "ellipsis",
  20. ellipsisStart: "ellipsis--start",
  21. ellipsisEnd: "ellipsis--end"
  22. };
  23. const TEXT = {
  24. nextLabel: "Next",
  25. previousLabel: "Previous"
  26. };
  27. const paginationCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity: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;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-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{block-size:1.5rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=s]) .previous,:host([scale=s]) .next{padding-inline:0.25rem}:host([scale=m]){--calcite-pagination-spacing:0.5rem 0.75rem}:host([scale=m]) .previous,:host([scale=m]) .next,:host([scale=m]) .page{block-size:2rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=m]) .previous,:host([scale=m]) .next{padding-inline:0.5rem}:host([scale=l]){--calcite-pagination-spacing:0.75rem 1rem}:host([scale=l]) .previous,:host([scale=l]) .next,:host([scale=l]) .page{block-size:2.75rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}:host([scale=l]) .previous,:host([scale=l]) .next{padding-inline:1rem}:host{display:flex;writing-mode:horizontal-tb}:host button{outline-color:transparent}:host button:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}.previous,.next,.page{box-sizing:border-box;display:flex;cursor:pointer;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-block:2px solid transparent}.previous:hover,.next:hover,.page:hover{color:var(--calcite-ui-text-1);transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.page:hover{border-block-end-color:var(--calcite-ui-border-2)}.page.is-selected{font-weight:var(--calcite-font-weight-medium);color:var(--calcite-ui-text-1);border-block-end-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-inline-end:0px}.page,.ellipsis{padding:var(--calcite-pagination-spacing)}.ellipsis{display:flex;align-items:flex-end;color:var(--calcite-ui-text-3)}";
  28. const maxPagesDisplayed = 5;
  29. const Pagination = class {
  30. constructor(hostRef) {
  31. registerInstance(this, hostRef);
  32. this.calcitePaginationUpdate = createEvent(this, "calcitePaginationUpdate", 6);
  33. this.calcitePaginationChange = createEvent(this, "calcitePaginationChange", 6);
  34. //--------------------------------------------------------------------------
  35. //
  36. // Public Properties
  37. //
  38. //--------------------------------------------------------------------------
  39. /**
  40. * When `true`, number values are displayed with a group separator corresponding to the language and country format.
  41. */
  42. this.groupSeparator = false;
  43. /** Specifies the number of items per page. */
  44. this.num = 20;
  45. /** Specifies the starting item number. */
  46. this.start = 1;
  47. /** Specifies the total number of items. */
  48. this.total = 0;
  49. /**
  50. * Accessible name for the component's next button.
  51. *
  52. * @default "Next"
  53. */
  54. this.textLabelNext = TEXT.nextLabel;
  55. /**
  56. * Accessible name for the component's previous button.
  57. *
  58. * @default "Previous"
  59. */
  60. this.textLabelPrevious = TEXT.previousLabel;
  61. /** Specifies the size of the component. */
  62. this.scale = "m";
  63. //--------------------------------------------------------------------------
  64. //
  65. // State
  66. //
  67. //--------------------------------------------------------------------------
  68. this.effectiveLocale = "";
  69. this.previousClicked = () => {
  70. this.previousPage().then();
  71. this.emitUpdate();
  72. };
  73. this.nextClicked = () => {
  74. this.nextPage();
  75. this.emitUpdate();
  76. };
  77. /**
  78. * Returns a string representing the localized label value based on groupSeparator prop being on or off.
  79. *
  80. * @param value
  81. */
  82. this.determineGroupSeparator = (value) => {
  83. numberStringFormatter.numberFormatOptions = {
  84. locale: this.effectiveLocale,
  85. numberingSystem: this.numberingSystem,
  86. useGrouping: this.groupSeparator
  87. };
  88. return this.groupSeparator
  89. ? numberStringFormatter.localize(value.toString())
  90. : value.toString();
  91. };
  92. }
  93. // --------------------------------------------------------------------------
  94. //
  95. // Lifecycle
  96. //
  97. // --------------------------------------------------------------------------
  98. connectedCallback() {
  99. connectLocalized(this);
  100. }
  101. disconnectedCallback() {
  102. disconnectLocalized(this);
  103. }
  104. // --------------------------------------------------------------------------
  105. //
  106. // Public Methods
  107. //
  108. // --------------------------------------------------------------------------
  109. /** Go to the next page of results. */
  110. async nextPage() {
  111. this.start = Math.min(this.getLastStart(), this.start + this.num);
  112. }
  113. /** Go to the previous page of results. */
  114. async previousPage() {
  115. this.start = Math.max(1, this.start - this.num);
  116. }
  117. // --------------------------------------------------------------------------
  118. //
  119. // Private Methods
  120. //
  121. // --------------------------------------------------------------------------
  122. getLastStart() {
  123. const { total, num } = this;
  124. const lastStart = total % num === 0 ? total - num : Math.floor(total / num) * num;
  125. return lastStart + 1;
  126. }
  127. showLeftEllipsis() {
  128. return Math.floor(this.start / this.num) > 3;
  129. }
  130. showRightEllipsis() {
  131. return (this.total - this.start) / this.num > 3;
  132. }
  133. emitUpdate() {
  134. const changePayload = {
  135. start: this.start,
  136. total: this.total,
  137. num: this.num
  138. };
  139. this.calcitePaginationChange.emit(changePayload);
  140. this.calcitePaginationUpdate.emit(changePayload);
  141. }
  142. //--------------------------------------------------------------------------
  143. //
  144. // Render Methods
  145. //
  146. //--------------------------------------------------------------------------
  147. renderPages() {
  148. const lastStart = this.getLastStart();
  149. let end;
  150. let nextStart;
  151. // if we don't need ellipses render the whole set
  152. if (this.total / this.num <= maxPagesDisplayed) {
  153. nextStart = 1 + this.num;
  154. end = lastStart - this.num;
  155. }
  156. else {
  157. // if we're within max pages of page 1
  158. if (this.start / this.num < maxPagesDisplayed - 1) {
  159. nextStart = 1 + this.num;
  160. end = 1 + 4 * this.num;
  161. }
  162. else {
  163. // if we're within max pages of last page
  164. if (this.start + 3 * this.num >= this.total) {
  165. nextStart = lastStart - 4 * this.num;
  166. end = lastStart - this.num;
  167. }
  168. else {
  169. nextStart = this.start - this.num;
  170. end = this.start + this.num;
  171. }
  172. }
  173. }
  174. const pages = [];
  175. while (nextStart <= end) {
  176. pages.push(nextStart);
  177. nextStart = nextStart + this.num;
  178. }
  179. return pages.map((page) => this.renderPage(page));
  180. }
  181. renderPage(start) {
  182. const page = Math.floor(start / this.num) + (this.num === 1 ? 0 : 1);
  183. const displayedPage = this.determineGroupSeparator(page);
  184. return (h("button", { class: {
  185. [CSS.page]: true,
  186. [CSS.selected]: start === this.start
  187. }, onClick: () => {
  188. this.start = start;
  189. this.emitUpdate();
  190. } }, displayedPage));
  191. }
  192. renderLeftEllipsis() {
  193. if (this.total / this.num > maxPagesDisplayed && this.showLeftEllipsis()) {
  194. return h("span", { class: `${CSS.ellipsis} ${CSS.ellipsisStart}` }, "\u2026");
  195. }
  196. }
  197. renderRightEllipsis() {
  198. if (this.total / this.num > maxPagesDisplayed && this.showRightEllipsis()) {
  199. return h("span", { class: `${CSS.ellipsis} ${CSS.ellipsisEnd}` }, "\u2026");
  200. }
  201. }
  202. render() {
  203. const { total, num, start } = this;
  204. const prevDisabled = num === 1 ? start <= num : start < num;
  205. const nextDisabled = num === 1 ? start + num > total : start + num > total;
  206. return (h(Fragment, null, h("button", { "aria-label": this.textLabelPrevious, class: {
  207. [CSS.previous]: true,
  208. [CSS.disabled]: prevDisabled
  209. }, 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: {
  210. [CSS.next]: true,
  211. [CSS.disabled]: nextDisabled
  212. }, disabled: nextDisabled, onClick: this.nextClicked }, h("calcite-icon", { flipRtl: true, icon: "chevronRight", scale: "s" }))));
  213. }
  214. get el() { return getElement(this); }
  215. };
  216. Pagination.style = paginationCss;
  217. export { Pagination as calcite_pagination };