tablefilter 0.7.6 → 0.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15053,6 +15053,12 @@
15053
15053
  * @private
15054
15054
  */
15055
15055
  this.pgAfter = null;
15056
+ /**
15057
+ * <tf-pager> custom element host, when the bridge is active
15058
+ * @type {DOMElement}
15059
+ * @private
15060
+ */
15061
+ this.pagerEl = null;
15056
15062
  let startRow = tf.refRow;
15057
15063
  let nrows = tf.getRowsNb(true);
15058
15064
  this.nbPages = Math.ceil((nrows - startRow) / this.pageLength);
@@ -15093,6 +15099,7 @@
15093
15099
  }
15094
15100
  },
15095
15101
  slcPagesChange: null,
15102
+ pagerPageChange: null,
15096
15103
  nextEvt: null,
15097
15104
  prevEvt: null,
15098
15105
  lastEvt: null,
@@ -15103,9 +15110,7 @@
15103
15110
  * Initialize DOM elements
15104
15111
  */
15105
15112
  init() {
15106
- let slcPages;
15107
15113
  let tf = this.tf;
15108
- let evt = this.evt;
15109
15114
  if (this.initialized) return;
15110
15115
  this.emitter.emit("initializing-feature", this, !isNull(this.tgtId));
15111
15116
  if (this.hasResultsPerPage) {
@@ -15115,6 +15120,60 @@
15115
15120
  this.setResultsPerPage();
15116
15121
  }
15117
15122
  }
15123
+ let targetEl = !this.tgtId ? tf.feature("toolbar").container(this.toolbarPosition) : elm(this.tgtId);
15124
+ if (typeof customElements !== "undefined" && customElements.get("tf-pager") && !tf.enableIcons && !this.hasBtnHtml()) this.initCE(targetEl);
15125
+ else this.initLegacy(targetEl);
15126
+ this.setPagingInfo();
15127
+ if (!tf.fltGrid) {
15128
+ tf.validateAllRows();
15129
+ this.setPagingInfo(tf.validRowsIndex);
15130
+ }
15131
+ this.emitter.on(["after-filtering"], bound(this.resetPagingInfo, this));
15132
+ this.emitter.on(["change-page"], bound(this.changePageHandler, this));
15133
+ this.emitter.on(["change-page-results"], bound(this.changePageResultsHandler, this));
15134
+ /** @inherited */
15135
+ this.initialized = true;
15136
+ this.emitter.emit("feature-initialized", this);
15137
+ }
15138
+ /**
15139
+ * Initialize using the <tf-pager> custom element
15140
+ * @param {DOMElement} targetEl Target container element
15141
+ * @private
15142
+ */
15143
+ initCE(targetEl) {
15144
+ let evt = this.evt;
15145
+ let tf = this.tf;
15146
+ let pagerEl = document.createElement("tf-pager");
15147
+ pagerEl.className = this.pgCssClass();
15148
+ pagerEl.setAttribute("selector-type", this.pageSelectorType);
15149
+ pagerEl.setAttribute("page-text", this.pageText);
15150
+ pagerEl.setAttribute("of-text", this.ofText);
15151
+ if (!this.hasBtns) pagerEl.setAttribute("buttons", "false");
15152
+ this.setPagerButtons(pagerEl);
15153
+ if (!this.isEnabled()) pagerEl.setAttribute("disabled", "");
15154
+ targetEl.appendChild(pagerEl);
15155
+ this.pagerEl = pagerEl;
15156
+ this.pageSlc = pagerEl.shadowRoot.querySelector(this.pageSelectorType === "input" ? "[part=\"page-input\"]" : "[part=\"page-select\"]");
15157
+ this.bindPagerButton("first", evt.first);
15158
+ this.bindPagerButton("prev", evt.prev);
15159
+ this.bindPagerButton("next", evt.next);
15160
+ this.bindPagerButton("last", evt.last);
15161
+ evt.pagerPageChange = (event) => {
15162
+ let page = parseInt(event.detail.page, 10);
15163
+ if (isNaN(page)) return;
15164
+ if (this.pageSelectorType === "input" && tf.sorted) tf.filter();
15165
+ this.changePage(page - 1);
15166
+ };
15167
+ addEvt(pagerEl, "page-change", evt.pagerPageChange);
15168
+ }
15169
+ /**
15170
+ * Initialize using the legacy spans, controls and select
15171
+ * @param {DOMElement} targetEl Target container element
15172
+ * @private
15173
+ */
15174
+ initLegacy(targetEl) {
15175
+ let slcPages;
15176
+ let evt = this.evt;
15118
15177
  evt.slcPagesChange = (event) => {
15119
15178
  let slc = event.target;
15120
15179
  this.changePage(slc.selectedIndex);
@@ -15171,7 +15230,6 @@
15171
15230
  addEvt(btnFirstSpan, "click", evt.first);
15172
15231
  }
15173
15232
  }
15174
- let targetEl = !this.tgtId ? tf.feature("toolbar").container(this.toolbarPosition) : elm(this.tgtId);
15175
15233
  targetEl.appendChild(btnFirstSpan);
15176
15234
  targetEl.appendChild(btnPrevSpan);
15177
15235
  let pgBeforeSpan = createElm("span");
@@ -15197,17 +15255,87 @@
15197
15255
  this.pgBefore = pgBeforeSpan;
15198
15256
  this.pgAfter = pgAfterSpan;
15199
15257
  this.pageSlc = slcPages;
15200
- this.setPagingInfo();
15201
- if (!tf.fltGrid) {
15202
- tf.validateAllRows();
15203
- this.setPagingInfo(tf.validRowsIndex);
15204
- }
15205
- this.emitter.on(["after-filtering"], bound(this.resetPagingInfo, this));
15206
- this.emitter.on(["change-page"], bound(this.changePageHandler, this));
15207
- this.emitter.on(["change-page-results"], bound(this.changePageResultsHandler, this));
15208
- /** @inherited */
15209
- this.initialized = true;
15210
- this.emitter.emit("feature-initialized", this);
15258
+ }
15259
+ /**
15260
+ * The legacy paging classes, applied to the <tf-pager> host
15261
+ * @return {String} Space-separated class list
15262
+ * @private
15263
+ */
15264
+ pgCssClass() {
15265
+ return [
15266
+ this.btnCssClass,
15267
+ this.pgSlcCssClass,
15268
+ this.pgInpCssClass,
15269
+ this.nbPgSpanCssClass
15270
+ ].join(" ");
15271
+ }
15272
+ /**
15273
+ * Whether a `btn_*_page_html` value is set
15274
+ * @return {Boolean} True when any button HTML is configured
15275
+ * @private
15276
+ */
15277
+ hasBtnHtml() {
15278
+ return Boolean(this.btnFirstPageHtml || this.btnPrevPageHtml || this.btnNextPageHtml || this.btnLastPageHtml);
15279
+ }
15280
+ /**
15281
+ * Set the `btn_*_page_text` captions on the <tf-pager> buttons
15282
+ * (paging.js:390-444).
15283
+ * @param {DOMElement} pagerEl <tf-pager> host
15284
+ * @private
15285
+ */
15286
+ setPagerButtons(pagerEl) {
15287
+ [
15288
+ {
15289
+ part: "first",
15290
+ text: this.btnFirstPageText
15291
+ },
15292
+ {
15293
+ part: "prev",
15294
+ text: this.btnPrevPageText
15295
+ },
15296
+ {
15297
+ part: "next",
15298
+ text: this.btnNextPageText
15299
+ },
15300
+ {
15301
+ part: "last",
15302
+ text: this.btnLastPageText
15303
+ }
15304
+ ].forEach((btn) => {
15305
+ pagerEl.setAttribute(`${btn.part}-text`, btn.text);
15306
+ });
15307
+ }
15308
+ /**
15309
+ * Bind a paging handler to a <tf-pager> button
15310
+ * @param {String} part Button part name
15311
+ * @param {Function} handler Paging handler
15312
+ * @private
15313
+ */
15314
+ bindPagerButton(part, handler) {
15315
+ addEvt(this.pagerEl.shadowRoot.querySelector(`[part="${part}"]`), "click", handler);
15316
+ }
15317
+ /**
15318
+ * Remove a paging handler from a <tf-pager> button
15319
+ * @param {String} part Button part name
15320
+ * @param {Function} handler Paging handler
15321
+ * @private
15322
+ */
15323
+ unbindPagerButton(part, handler) {
15324
+ removeEvt(this.pagerEl.shadowRoot.querySelector(`[part="${part}"]`), "click", handler);
15325
+ }
15326
+ /**
15327
+ * Paging entries shared by the legacy select and <tf-pager>: page
15328
+ * number as visible text, start row as value
15329
+ * @return {Array<Object>} Page entries
15330
+ * @private
15331
+ */
15332
+ pageEntries() {
15333
+ let entries = [];
15334
+ for (let z = 0; z < this.nbPages; z++) entries.push({
15335
+ text: String(z + 1),
15336
+ value: String(z * this.pageLength)
15337
+ });
15338
+ return entries;
15211
15339
  }
15212
15340
  /**
15213
15341
  * Reset paging when filters are already instantiated
@@ -15236,15 +15364,22 @@
15236
15364
  let cont = !this.tgtId ? tf.feature("toolbar").container(this.toolbarPosition) : elm(this.tgtId);
15237
15365
  tf.validRowsIndex = validRows || tf.getValidRows(true);
15238
15366
  this.nbPages = Math.ceil(tf.validRowsIndex.length / this.pageLength);
15239
- this.pgCont.innerHTML = this.nbPages;
15240
- if (this.pageSelectorType === "select") this.pageSlc.innerHTML = "";
15367
+ if (this.pagerEl) {
15368
+ this.pagerEl.setTotalPages(this.nbPages);
15369
+ this.pagerEl.setPages(this.pageEntries());
15370
+ this.pagerEl.setPage(this.currentPageNb);
15371
+ } else {
15372
+ this.pgCont.innerHTML = this.nbPages;
15373
+ if (this.pageSelectorType === "select") this.pageSlc.innerHTML = "";
15374
+ }
15241
15375
  if (this.nbPages > 0) {
15242
15376
  cont.style.visibility = "visible";
15243
- if (this.pageSelectorType === "select") for (let z = 0; z < this.nbPages; z++) {
15244
- let opt = createOpt(z + 1, z * this.pageLength, false);
15245
- this.pageSlc.options[z] = opt;
15377
+ if (!this.pagerEl) {
15378
+ if (this.pageSelectorType === "select") this.pageEntries().forEach((entry, z) => {
15379
+ this.pageSlc.options[z] = createOpt(entry.text, entry.value, false);
15380
+ });
15381
+ else this.pageSlc.value = this.currentPageNb;
15246
15382
  }
15247
- else this.pageSlc.value = this.currentPageNb;
15248
15383
  } else
15249
15384
  /*** if no results paging select and buttons are hidden ***/
15250
15385
  cont.style.visibility = "hidden";
@@ -15363,7 +15498,8 @@
15363
15498
  if (index >= 0 && index <= this.nbPages - 1) {
15364
15499
  this.onBeforeChangePage(this, index + 1);
15365
15500
  this.currentPageNb = parseInt(index, 10) + 1;
15366
- if (this.pageSelectorType === "select") this.pageSlc.options[index].selected = true;
15501
+ if (this.pagerEl) this.pagerEl.setPage(this.currentPageNb);
15502
+ else if (this.pageSelectorType === "select") this.pageSlc.options[index].selected = true;
15367
15503
  else this.pageSlc.value = this.currentPageNb;
15368
15504
  this.startPagingRow = this.pageSelectorType === "select" ? this.pageSlc.value : index * this.pageLength;
15369
15505
  this.groupByPage();
@@ -15395,7 +15531,8 @@
15395
15531
  if (!isNaN(this.pageLength)) {
15396
15532
  if (this.startPagingRow >= tf.nbFilterableRows) this.startPagingRow = tf.nbFilterableRows - this.pageLength;
15397
15533
  this.setPagingInfo();
15398
- if (pageSelectorType === "select") {
15534
+ if (this.pagerEl) this.pagerEl.setPage(Math.min(slcPagesSelIndex, this.nbPages - 1) + 1);
15535
+ else if (pageSelectorType === "select") {
15399
15536
  let slcIdx = pageSlc.options.length - 1 <= slcPagesSelIndex ? pageSlc.options.length - 1 : slcPagesSelIndex;
15400
15537
  pageSlc.options[slcIdx].selected = true;
15401
15538
  }
@@ -15436,10 +15573,53 @@
15436
15573
  this.changeResultsPerPage(pageLength);
15437
15574
  }
15438
15575
  /**
15576
+ * Disable paging and reflect the state on the pager element
15577
+ */
15578
+ disable() {
15579
+ super.disable();
15580
+ if (this.pagerEl) this.pagerEl.setAttribute("disabled", "");
15581
+ }
15582
+ /**
15583
+ * Enable paging and reflect the state on the pager element
15584
+ */
15585
+ enable() {
15586
+ super.enable();
15587
+ if (this.pagerEl) this.pagerEl.removeAttribute("disabled");
15588
+ }
15589
+ /**
15439
15590
  * Remove paging feature
15440
15591
  */
15441
15592
  destroy() {
15442
15593
  if (!this.initialized) return;
15594
+ if (this.pagerEl) this.destroyCE();
15595
+ else this.destroyLegacy();
15596
+ if (this.hasResultsPerPage) this.removeResultsPerPage();
15597
+ this.emitter.off(["after-filtering"], bound(this.resetPagingInfo, this));
15598
+ this.emitter.off(["change-page"], bound(this.changePageHandler, this));
15599
+ this.emitter.off(["change-page-results"], bound(this.changePageResultsHandler, this));
15600
+ this.pageSlc = null;
15601
+ this.nbPages = 0;
15602
+ this.initialized = false;
15603
+ }
15604
+ /**
15605
+ * Remove the <tf-pager> element and the handlers bound to it
15606
+ * @private
15607
+ */
15608
+ destroyCE() {
15609
+ let evt = this.evt;
15610
+ removeEvt(this.pagerEl, "page-change", evt.pagerPageChange);
15611
+ this.unbindPagerButton("first", evt.first);
15612
+ this.unbindPagerButton("prev", evt.prev);
15613
+ this.unbindPagerButton("next", evt.next);
15614
+ this.unbindPagerButton("last", evt.last);
15615
+ removeElm(this.pagerEl);
15616
+ this.pagerEl = null;
15617
+ }
15618
+ /**
15619
+ * Remove the legacy paging controls and the handlers bound to them
15620
+ * @private
15621
+ */
15622
+ destroyLegacy() {
15443
15623
  let evt = this.evt;
15444
15624
  if (this.pageSlc) {
15445
15625
  if (this.pageSelectorType === "select") removeEvt(this.pageSlc, "change", evt.slcPagesChange);
@@ -15478,13 +15658,6 @@
15478
15658
  removeElm(this.pgCont);
15479
15659
  this.pgCont = null;
15480
15660
  }
15481
- if (this.hasResultsPerPage) this.removeResultsPerPage();
15482
- this.emitter.off(["after-filtering"], bound(this.resetPagingInfo, this));
15483
- this.emitter.off(["change-page"], bound(this.changePageHandler, this));
15484
- this.emitter.off(["change-page-results"], bound(this.changePageResultsHandler, this));
15485
- this.pageSlc = null;
15486
- this.nbPages = 0;
15487
- this.initialized = false;
15488
15661
  }
15489
15662
  };
15490
15663
  //#endregion
@@ -15547,7 +15720,7 @@
15547
15720
  * Current version
15548
15721
  * @type {String}
15549
15722
  */
15550
- this.version = "0.7.6";
15723
+ this.version = "0.7.7";
15551
15724
  /**
15552
15725
  * Current year
15553
15726
  * @type {Number}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tablefilter",
3
- "version": "0.7.6",
3
+ "version": "0.7.7",
4
4
  "description": "A Javascript library making HTML tables filterable and a bit more",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -0,0 +1,325 @@
1
+ /**
2
+ * `<tf-pager>` custom element: accessible pagination controls.
3
+ *
4
+ * An open shadow root exposing a `nav` landmark (`part="nav"`) with four
5
+ * semantic `<button type="button">` elements in first/prev/next/last
6
+ * order — each via its own `part`, a named slot and a text hook — plus a
7
+ * page indicator (`part="page-status"`) carrying `aria-current="page"`.
8
+ *
9
+ * Attributes:
10
+ * - `label` — `aria-label` on the landmark.
11
+ * - `first-label`, `prev-label`, `next-label`, `last-label` — `aria-label`
12
+ * on each button.
13
+ * - `first-text`, `prev-text`, `next-text`, `last-text` — button text.
14
+ * - `page` — current page number, 1-based.
15
+ * - `total-pages` — page count, rendered as supplied. The host resolves
16
+ * it from the legacy Paging feature.
17
+ * - `page-text`, `of-text` — text rendered either side of the selector.
18
+ * - `selector-type` — `select` (default) or `input`.
19
+ * - `selector-label` — `aria-label` on the page selector; defaults to
20
+ * `Current page`, and an empty value opts out of the name.
21
+ * - `disabled` — disables all four buttons.
22
+ * - `buttons` — `false` hides the four buttons, any other value renders
23
+ * them.
24
+ * No accessible name is rendered unless its attribute is set.
25
+ *
26
+ * Events:
27
+ * - `page-change` — dispatched when the user picks a page, with
28
+ * `detail: {page}`, the page number as a string. Bubbles and crosses
29
+ * the shadow boundary.
30
+ *
31
+ * The select is populated through `setPages()`, which takes `{text,
32
+ * value}` entries from the host — the legacy option model: page number
33
+ * as text, start row as value.
34
+ *
35
+ * Content is projected through the slots (`slot="first"`, `slot="prev"`,
36
+ * `slot="next"`, `slot="last"`). Styling is left to the theme via `--tf-*`
37
+ * custom properties and `::part()`; no CSS class or presentational style
38
+ * is shipped.
39
+ *
40
+ * Importing this module registers the element. It is not part of the
41
+ * legacy webpack bundle; consumers load it as a module.
42
+ */
43
+
44
+ const BUTTONS = [
45
+ { part: 'first' },
46
+ { part: 'prev' },
47
+ { part: 'next' },
48
+ { part: 'last' }
49
+ ];
50
+
51
+ const DEFAULT_SELECTOR_LABEL = 'Current page';
52
+
53
+ // Cloned into every instance's shadow root. Structure and `part` hooks
54
+ // only; no class or presentational style is baked in. The indicator sits
55
+ // between the prev and next buttons, mirroring the legacy markup order
56
+ // (src/modules/paging.js: first, prev, page text, selector, of text,
57
+ // total, next, last).
58
+ const TEMPLATE = document.createElement('template');
59
+ TEMPLATE.innerHTML = `
60
+ <nav part="nav">
61
+ <button type="button" part="first">
62
+ <slot name="first"></slot><span part="first-text"></span>
63
+ </button>
64
+ <button type="button" part="prev">
65
+ <slot name="prev"></slot><span part="prev-text"></span>
66
+ </button>
67
+ <span part="page-status" aria-current="page">
68
+ <span part="page-text"></span>
69
+ <span part="page-selector"></span>
70
+ <span part="of-text"></span>
71
+ <span part="total-pages"></span>
72
+ </span>
73
+ <button type="button" part="next">
74
+ <slot name="next"></slot><span part="next-text"></span>
75
+ </button>
76
+ <button type="button" part="last">
77
+ <slot name="last"></slot><span part="last-text"></span>
78
+ </button>
79
+ </nav>
80
+ `;
81
+
82
+ export class TfPagerElement extends HTMLElement {
83
+ constructor() {
84
+ super();
85
+ this.attachShadow({ mode: 'open' });
86
+ this.shadowRoot.appendChild(TEMPLATE.content.cloneNode(true));
87
+ this._nav = this.shadowRoot.querySelector('nav');
88
+ this._buttons = BUTTONS.map(
89
+ (btn) => this.shadowRoot.querySelector(`[part="${btn.part}"]`));
90
+ this._texts = BUTTONS.map(
91
+ (btn) =>
92
+ this.shadowRoot.querySelector(`[part="${btn.part}-text"]`));
93
+ this._pageText = this.shadowRoot.querySelector('[part="page-text"]');
94
+ this._pageSelector =
95
+ this.shadowRoot.querySelector('[part="page-selector"]');
96
+ this._ofText = this.shadowRoot.querySelector('[part="of-text"]');
97
+ this._totalPagesEl =
98
+ this.shadowRoot.querySelector('[part="total-pages"]');
99
+ // Page entries supplied through setPages().
100
+ this._pages = [];
101
+ }
102
+
103
+ static get observedAttributes() {
104
+ return ['label', 'disabled', 'buttons', 'page', 'total-pages',
105
+ 'page-text', 'of-text', 'selector-type', 'selector-label']
106
+ .concat(BUTTONS.map((btn) => `${btn.part}-label`))
107
+ .concat(BUTTONS.map((btn) => `${btn.part}-text`));
108
+ }
109
+
110
+ connectedCallback() {
111
+ this._update();
112
+ }
113
+
114
+ attributeChangedCallback() {
115
+ this._update();
116
+ }
117
+
118
+ /**
119
+ * Set the landmark's accessible name, rendered as `aria-label` on
120
+ * the `nav` part.
121
+ * @param {*} label Accessible name
122
+ */
123
+ setLabel(label) {
124
+ this.setAttribute('label', label == null ? '' : String(label));
125
+ }
126
+
127
+ /**
128
+ * Set the current page number.
129
+ * @param {*} page Page number, 1-based
130
+ */
131
+ setPage(page) {
132
+ this.setAttribute('page', page == null ? '' : String(page));
133
+ }
134
+
135
+ /**
136
+ * Set the page count, resolved by the host from the legacy Paging
137
+ * feature.
138
+ * @param {*} totalPages Page count
139
+ */
140
+ setTotalPages(totalPages) {
141
+ this.setAttribute('total-pages',
142
+ totalPages == null ? '' : String(totalPages));
143
+ }
144
+
145
+ /**
146
+ * Render the landmark, button attributes and page indicator into the
147
+ * shadow parts.
148
+ */
149
+ _update() {
150
+ if (!this._nav) {
151
+ return;
152
+ }
153
+
154
+ this._setAriaLabel(this._nav, 'label');
155
+
156
+ const showButtons = this.getAttribute('buttons') !== 'false';
157
+
158
+ BUTTONS.forEach((btn, idx) => {
159
+ this._setAriaLabel(this._buttons[idx], `${btn.part}-label`);
160
+ this._texts[idx].textContent =
161
+ this.getAttribute(`${btn.part}-text`) || '';
162
+
163
+ if (this.hasAttribute('disabled')) {
164
+ this._buttons[idx].setAttribute('disabled', '');
165
+ } else {
166
+ this._buttons[idx].removeAttribute('disabled');
167
+ }
168
+
169
+ if (showButtons) {
170
+ this._buttons[idx].removeAttribute('hidden');
171
+ } else {
172
+ this._buttons[idx].setAttribute('hidden', '');
173
+ }
174
+ });
175
+
176
+ this._pageText.textContent = this.getAttribute('page-text') || '';
177
+ this._ofText.textContent = this.getAttribute('of-text') || '';
178
+ this._totalPagesEl.textContent = this._totalPages();
179
+
180
+ this._updateSelector(this._syncSelector());
181
+ }
182
+
183
+ /**
184
+ * The `total-pages` value supplied by the host, or empty when it has
185
+ * not been set.
186
+ * @return {String} Total pages, or an empty string
187
+ */
188
+ _totalPages() {
189
+ return this.getAttribute('total-pages') || '';
190
+ }
191
+
192
+ /**
193
+ * Supply the page list rendered into the select. Each entry is
194
+ * `{text, value}` — the legacy option model, page number as visible
195
+ * text and start row as value.
196
+ * @param {Array<Object>} pages Page entries
197
+ */
198
+ setPages(pages) {
199
+ this._pages = Array.isArray(pages) ? pages : [];
200
+ this._update();
201
+ }
202
+
203
+ /**
204
+ * Ensure the page selector matches `selector-type`, creating it on
205
+ * first use and replacing it when the type changes. Exactly one
206
+ * control exists at a time, so no presentational attribute is needed
207
+ * to hide the other.
208
+ * @return {Element} The page selector control
209
+ */
210
+ _syncSelector() {
211
+ const isInput = this.getAttribute('selector-type') === 'input';
212
+ const part = isInput ? 'page-input' : 'page-select';
213
+ const current = this._pageSelector.firstElementChild;
214
+
215
+ if (current && current.getAttribute('part') === part) {
216
+ return current;
217
+ }
218
+
219
+ this._pageSelector.textContent = '';
220
+ const el = document.createElement(isInput ? 'input' : 'select');
221
+ el.setAttribute('part', part);
222
+ if (isInput) {
223
+ el.setAttribute('type', 'number');
224
+ el.setAttribute('min', '1');
225
+ el.addEventListener('keydown', (event) => {
226
+ if (event.key === 'Enter' && el.value !== '') {
227
+ this._dispatchPageChange(el.value);
228
+ }
229
+ });
230
+ } else {
231
+ el.addEventListener('change', () => {
232
+ const option = el.options[el.selectedIndex];
233
+ if (option) {
234
+ this._dispatchPageChange(option.textContent);
235
+ }
236
+ });
237
+ }
238
+ this._pageSelector.appendChild(el);
239
+ return el;
240
+ }
241
+
242
+ /**
243
+ * Dispatch `page-change` with the page number a user action resolved
244
+ * to.
245
+ * @param {String} page Page number
246
+ */
247
+ _dispatchPageChange(page) {
248
+ this.dispatchEvent(new CustomEvent('page-change', {
249
+ detail: { page: String(page) },
250
+ bubbles: true,
251
+ composed: true
252
+ }));
253
+ }
254
+
255
+ /**
256
+ * Render the host-supplied page entries, page count and accessible
257
+ * name into the selector control; the input variant holds the page
258
+ * number itself.
259
+ * @param {Element} el The selector control
260
+ */
261
+ _updateSelector(el) {
262
+ this._setSelectorLabel(el);
263
+
264
+ const page = this.getAttribute('page') || '';
265
+ const total = this._totalPages();
266
+ const isInput = el.getAttribute('part') === 'page-input';
267
+
268
+ if (isInput) {
269
+ el.value = page;
270
+ if (total !== '') {
271
+ el.setAttribute('max', total);
272
+ } else {
273
+ el.removeAttribute('max');
274
+ }
275
+ return;
276
+ }
277
+
278
+ el.textContent = '';
279
+ this._pages.forEach((entry) => {
280
+ const option = document.createElement('option');
281
+ option.value = String(entry.value);
282
+ option.textContent = String(entry.text);
283
+ if (String(entry.text) === page) {
284
+ option.setAttribute('selected', 'true');
285
+ }
286
+ el.appendChild(option);
287
+ });
288
+ }
289
+
290
+ /**
291
+ * Name the page selector. Absent `selector-label` renders the
292
+ * ROADMAP default, a non-empty value overrides it, and an explicit
293
+ * empty value removes the name so it can be opted out of.
294
+ * @param {Element} el The selector control
295
+ */
296
+ _setSelectorLabel(el) {
297
+ if (!this.hasAttribute('selector-label')) {
298
+ el.setAttribute('aria-label', DEFAULT_SELECTOR_LABEL);
299
+ return;
300
+ }
301
+ this._setAriaLabel(el, 'selector-label');
302
+ }
303
+
304
+ /**
305
+ * Render an attribute as `aria-label` on a shadow element, or remove
306
+ * it when the attribute is absent.
307
+ * @param {Element} el Shadow element to name
308
+ * @param {String} attr Attribute holding the accessible name
309
+ */
310
+ _setAriaLabel(el, attr) {
311
+ const label = this.getAttribute(attr);
312
+ if (label) {
313
+ el.setAttribute('aria-label', label);
314
+ } else {
315
+ el.removeAttribute('aria-label');
316
+ }
317
+ }
318
+ }
319
+
320
+ // Register once: loading this module twice (or on a page that already
321
+ // defined the element) must not throw.
322
+ if (typeof customElements !== 'undefined' &&
323
+ !customElements.get('tf-pager')) {
324
+ customElements.define('tf-pager', TfPagerElement);
325
+ }