calcite-pagination.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 { proxyCustomElement, HTMLElement, createEvent, h, Fragment } from '@stencil/core/internal/client/index.js';
  7. import { n as numberStringFormatter, c as connectLocalized, d as disconnectLocalized } from './locale.js';
  8. import { d as defineCustomElement$2 } from './icon.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 = "@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)}";
  24. const maxPagesDisplayed = 5;
  25. const Pagination = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  26. constructor() {
  27. super();
  28. this.__registerHost();
  29. this.__attachShadow();
  30. this.calcitePaginationUpdate = createEvent(this, "calcitePaginationUpdate", 6);
  31. this.calcitePaginationChange = createEvent(this, "calcitePaginationChange", 6);
  32. //--------------------------------------------------------------------------
  33. //
  34. // Public Properties
  35. //
  36. //--------------------------------------------------------------------------
  37. /**
  38. * When `true`, number values are displayed with a group separator corresponding to the language and country format.
  39. */
  40. this.groupSeparator = false;
  41. /** Specifies the number of items per page. */
  42. this.num = 20;
  43. /** Specifies the starting item number. */
  44. this.start = 1;
  45. /** Specifies the total number of items. */
  46. this.total = 0;
  47. /**
  48. * Accessible name for the component's next button.
  49. *
  50. * @default "Next"
  51. */
  52. this.textLabelNext = TEXT.nextLabel;
  53. /**
  54. * Accessible name for the component's previous button.
  55. *
  56. * @default "Previous"
  57. */
  58. this.textLabelPrevious = TEXT.previousLabel;
  59. /** Specifies the size of the component. */
  60. this.scale = "m";
  61. //--------------------------------------------------------------------------
  62. //
  63. // State
  64. //
  65. //--------------------------------------------------------------------------
  66. this.effectiveLocale = "";
  67. this.previousClicked = () => {
  68. this.previousPage().then();
  69. this.emitUpdate();
  70. };
  71. this.nextClicked = () => {
  72. this.nextPage();
  73. this.emitUpdate();
  74. };
  75. /**
  76. * Returns a string representing the localized label value based on groupSeparator prop being on or off.
  77. *
  78. * @param value
  79. */
  80. this.determineGroupSeparator = (value) => {
  81. numberStringFormatter.numberFormatOptions = {
  82. locale: this.effectiveLocale,
  83. numberingSystem: this.numberingSystem,
  84. useGrouping: this.groupSeparator
  85. };
  86. return this.groupSeparator
  87. ? numberStringFormatter.localize(value.toString())
  88. : value.toString();
  89. };
  90. }
  91. // --------------------------------------------------------------------------
  92. //
  93. // Lifecycle
  94. //
  95. // --------------------------------------------------------------------------
  96. connectedCallback() {
  97. connectLocalized(this);
  98. }
  99. disconnectedCallback() {
  100. disconnectLocalized(this);
  101. }
  102. // --------------------------------------------------------------------------
  103. //
  104. // Public Methods
  105. //
  106. // --------------------------------------------------------------------------
  107. /** Go to the next page of results. */
  108. async nextPage() {
  109. this.start = Math.min(this.getLastStart(), this.start + this.num);
  110. }
  111. /** Go to the previous page of results. */
  112. async previousPage() {
  113. this.start = Math.max(1, this.start - this.num);
  114. }
  115. // --------------------------------------------------------------------------
  116. //
  117. // Private Methods
  118. //
  119. // --------------------------------------------------------------------------
  120. getLastStart() {
  121. const { total, num } = this;
  122. const lastStart = total % num === 0 ? total - num : Math.floor(total / num) * num;
  123. return lastStart + 1;
  124. }
  125. showLeftEllipsis() {
  126. return Math.floor(this.start / this.num) > 3;
  127. }
  128. showRightEllipsis() {
  129. return (this.total - this.start) / this.num > 3;
  130. }
  131. emitUpdate() {
  132. const changePayload = {
  133. start: this.start,
  134. total: this.total,
  135. num: this.num
  136. };
  137. this.calcitePaginationChange.emit(changePayload);
  138. this.calcitePaginationUpdate.emit(changePayload);
  139. }
  140. //--------------------------------------------------------------------------
  141. //
  142. // Render Methods
  143. //
  144. //--------------------------------------------------------------------------
  145. renderPages() {
  146. const lastStart = this.getLastStart();
  147. let end;
  148. let nextStart;
  149. // if we don't need ellipses render the whole set
  150. if (this.total / this.num <= maxPagesDisplayed) {
  151. nextStart = 1 + this.num;
  152. end = lastStart - this.num;
  153. }
  154. else {
  155. // if we're within max pages of page 1
  156. if (this.start / this.num < maxPagesDisplayed - 1) {
  157. nextStart = 1 + this.num;
  158. end = 1 + 4 * this.num;
  159. }
  160. else {
  161. // if we're within max pages of last page
  162. if (this.start + 3 * this.num >= this.total) {
  163. nextStart = lastStart - 4 * this.num;
  164. end = lastStart - this.num;
  165. }
  166. else {
  167. nextStart = this.start - this.num;
  168. end = this.start + this.num;
  169. }
  170. }
  171. }
  172. const pages = [];
  173. while (nextStart <= end) {
  174. pages.push(nextStart);
  175. nextStart = nextStart + this.num;
  176. }
  177. return pages.map((page) => this.renderPage(page));
  178. }
  179. renderPage(start) {
  180. const page = Math.floor(start / this.num) + (this.num === 1 ? 0 : 1);
  181. const displayedPage = this.determineGroupSeparator(page);
  182. return (h("button", { class: {
  183. [CSS.page]: true,
  184. [CSS.selected]: start === this.start
  185. }, onClick: () => {
  186. this.start = start;
  187. this.emitUpdate();
  188. } }, displayedPage));
  189. }
  190. renderLeftEllipsis() {
  191. if (this.total / this.num > maxPagesDisplayed && this.showLeftEllipsis()) {
  192. return h("span", { class: `${CSS.ellipsis} ${CSS.ellipsisStart}` }, "\u2026");
  193. }
  194. }
  195. renderRightEllipsis() {
  196. if (this.total / this.num > maxPagesDisplayed && this.showRightEllipsis()) {
  197. return h("span", { class: `${CSS.ellipsis} ${CSS.ellipsisEnd}` }, "\u2026");
  198. }
  199. }
  200. render() {
  201. const { total, num, start } = this;
  202. const prevDisabled = num === 1 ? start <= num : start < num;
  203. const nextDisabled = num === 1 ? start + num > total : start + num > total;
  204. return (h(Fragment, null, h("button", { "aria-label": this.textLabelPrevious, class: {
  205. [CSS.previous]: true,
  206. [CSS.disabled]: prevDisabled
  207. }, 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: {
  208. [CSS.next]: true,
  209. [CSS.disabled]: nextDisabled
  210. }, disabled: nextDisabled, onClick: this.nextClicked }, h("calcite-icon", { flipRtl: true, icon: "chevronRight", scale: "s" }))));
  211. }
  212. get el() { return this; }
  213. static get style() { return paginationCss; }
  214. }, [1, "calcite-pagination", {
  215. "groupSeparator": [516, "group-separator"],
  216. "num": [514],
  217. "numberingSystem": [1, "numbering-system"],
  218. "start": [1538],
  219. "total": [514],
  220. "textLabelNext": [1, "text-label-next"],
  221. "textLabelPrevious": [1, "text-label-previous"],
  222. "scale": [513],
  223. "effectiveLocale": [32],
  224. "nextPage": [64],
  225. "previousPage": [64]
  226. }]);
  227. function defineCustomElement$1() {
  228. if (typeof customElements === "undefined") {
  229. return;
  230. }
  231. const components = ["calcite-pagination", "calcite-icon"];
  232. components.forEach(tagName => { switch (tagName) {
  233. case "calcite-pagination":
  234. if (!customElements.get(tagName)) {
  235. customElements.define(tagName, Pagination);
  236. }
  237. break;
  238. case "calcite-icon":
  239. if (!customElements.get(tagName)) {
  240. defineCustomElement$2();
  241. }
  242. break;
  243. } });
  244. }
  245. defineCustomElement$1();
  246. const CalcitePagination = Pagination;
  247. const defineCustomElement = defineCustomElement$1;
  248. export { CalcitePagination, defineCustomElement };