calcite-pagination.cjs.entry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. const index = require('./index-a0010f96.js');
  9. const locale = require('./locale-678ce361.js');
  10. require('./key-6a462411.js');
  11. require('./observers-5706326b.js');
  12. require('./dom-2ec8c9ed.js');
  13. require('./resources-b5a5f8a7.js');
  14. require('./guid-f4f03a7a.js');
  15. const CSS = {
  16. page: "page",
  17. selected: "is-selected",
  18. previous: "previous",
  19. next: "next",
  20. disabled: "is-disabled",
  21. ellipsis: "ellipsis",
  22. ellipsisStart: "ellipsis--start",
  23. ellipsisEnd: "ellipsis--end"
  24. };
  25. const TEXT = {
  26. nextLabel: "Next",
  27. previousLabel: "Previous"
  28. };
  29. 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)}";
  30. const maxPagesDisplayed = 5;
  31. const Pagination = class {
  32. constructor(hostRef) {
  33. index.registerInstance(this, hostRef);
  34. this.calcitePaginationUpdate = index.createEvent(this, "calcitePaginationUpdate", 6);
  35. this.calcitePaginationChange = index.createEvent(this, "calcitePaginationChange", 6);
  36. //--------------------------------------------------------------------------
  37. //
  38. // Public Properties
  39. //
  40. //--------------------------------------------------------------------------
  41. /**
  42. * When `true`, number values are displayed with a group separator corresponding to the language and country format.
  43. */
  44. this.groupSeparator = false;
  45. /** Specifies the number of items per page. */
  46. this.num = 20;
  47. /** Specifies the starting item number. */
  48. this.start = 1;
  49. /** Specifies the total number of items. */
  50. this.total = 0;
  51. /**
  52. * Accessible name for the component's next button.
  53. *
  54. * @default "Next"
  55. */
  56. this.textLabelNext = TEXT.nextLabel;
  57. /**
  58. * Accessible name for the component's previous button.
  59. *
  60. * @default "Previous"
  61. */
  62. this.textLabelPrevious = TEXT.previousLabel;
  63. /** Specifies the size of the component. */
  64. this.scale = "m";
  65. //--------------------------------------------------------------------------
  66. //
  67. // State
  68. //
  69. //--------------------------------------------------------------------------
  70. this.effectiveLocale = "";
  71. this.previousClicked = () => {
  72. this.previousPage().then();
  73. this.emitUpdate();
  74. };
  75. this.nextClicked = () => {
  76. this.nextPage();
  77. this.emitUpdate();
  78. };
  79. /**
  80. * Returns a string representing the localized label value based on groupSeparator prop being on or off.
  81. *
  82. * @param value
  83. */
  84. this.determineGroupSeparator = (value) => {
  85. locale.numberStringFormatter.numberFormatOptions = {
  86. locale: this.effectiveLocale,
  87. numberingSystem: this.numberingSystem,
  88. useGrouping: this.groupSeparator
  89. };
  90. return this.groupSeparator
  91. ? locale.numberStringFormatter.localize(value.toString())
  92. : value.toString();
  93. };
  94. }
  95. // --------------------------------------------------------------------------
  96. //
  97. // Lifecycle
  98. //
  99. // --------------------------------------------------------------------------
  100. connectedCallback() {
  101. locale.connectLocalized(this);
  102. }
  103. disconnectedCallback() {
  104. locale.disconnectLocalized(this);
  105. }
  106. // --------------------------------------------------------------------------
  107. //
  108. // Public Methods
  109. //
  110. // --------------------------------------------------------------------------
  111. /** Go to the next page of results. */
  112. async nextPage() {
  113. this.start = Math.min(this.getLastStart(), this.start + this.num);
  114. }
  115. /** Go to the previous page of results. */
  116. async previousPage() {
  117. this.start = Math.max(1, this.start - this.num);
  118. }
  119. // --------------------------------------------------------------------------
  120. //
  121. // Private Methods
  122. //
  123. // --------------------------------------------------------------------------
  124. getLastStart() {
  125. const { total, num } = this;
  126. const lastStart = total % num === 0 ? total - num : Math.floor(total / num) * num;
  127. return lastStart + 1;
  128. }
  129. showLeftEllipsis() {
  130. return Math.floor(this.start / this.num) > 3;
  131. }
  132. showRightEllipsis() {
  133. return (this.total - this.start) / this.num > 3;
  134. }
  135. emitUpdate() {
  136. const changePayload = {
  137. start: this.start,
  138. total: this.total,
  139. num: this.num
  140. };
  141. this.calcitePaginationChange.emit(changePayload);
  142. this.calcitePaginationUpdate.emit(changePayload);
  143. }
  144. //--------------------------------------------------------------------------
  145. //
  146. // Render Methods
  147. //
  148. //--------------------------------------------------------------------------
  149. renderPages() {
  150. const lastStart = this.getLastStart();
  151. let end;
  152. let nextStart;
  153. // if we don't need ellipses render the whole set
  154. if (this.total / this.num <= maxPagesDisplayed) {
  155. nextStart = 1 + this.num;
  156. end = lastStart - this.num;
  157. }
  158. else {
  159. // if we're within max pages of page 1
  160. if (this.start / this.num < maxPagesDisplayed - 1) {
  161. nextStart = 1 + this.num;
  162. end = 1 + 4 * this.num;
  163. }
  164. else {
  165. // if we're within max pages of last page
  166. if (this.start + 3 * this.num >= this.total) {
  167. nextStart = lastStart - 4 * this.num;
  168. end = lastStart - this.num;
  169. }
  170. else {
  171. nextStart = this.start - this.num;
  172. end = this.start + this.num;
  173. }
  174. }
  175. }
  176. const pages = [];
  177. while (nextStart <= end) {
  178. pages.push(nextStart);
  179. nextStart = nextStart + this.num;
  180. }
  181. return pages.map((page) => this.renderPage(page));
  182. }
  183. renderPage(start) {
  184. const page = Math.floor(start / this.num) + (this.num === 1 ? 0 : 1);
  185. const displayedPage = this.determineGroupSeparator(page);
  186. return (index.h("button", { class: {
  187. [CSS.page]: true,
  188. [CSS.selected]: start === this.start
  189. }, onClick: () => {
  190. this.start = start;
  191. this.emitUpdate();
  192. } }, displayedPage));
  193. }
  194. renderLeftEllipsis() {
  195. if (this.total / this.num > maxPagesDisplayed && this.showLeftEllipsis()) {
  196. return index.h("span", { class: `${CSS.ellipsis} ${CSS.ellipsisStart}` }, "\u2026");
  197. }
  198. }
  199. renderRightEllipsis() {
  200. if (this.total / this.num > maxPagesDisplayed && this.showRightEllipsis()) {
  201. return index.h("span", { class: `${CSS.ellipsis} ${CSS.ellipsisEnd}` }, "\u2026");
  202. }
  203. }
  204. render() {
  205. const { total, num, start } = this;
  206. const prevDisabled = num === 1 ? start <= num : start < num;
  207. const nextDisabled = num === 1 ? start + num > total : start + num > total;
  208. return (index.h(index.Fragment, null, index.h("button", { "aria-label": this.textLabelPrevious, class: {
  209. [CSS.previous]: true,
  210. [CSS.disabled]: prevDisabled
  211. }, 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: {
  212. [CSS.next]: true,
  213. [CSS.disabled]: nextDisabled
  214. }, disabled: nextDisabled, onClick: this.nextClicked }, index.h("calcite-icon", { flipRtl: true, icon: "chevronRight", scale: "s" }))));
  215. }
  216. get el() { return index.getElement(this); }
  217. };
  218. Pagination.style = paginationCss;
  219. exports.calcite_pagination = Pagination;