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.
@@ -0,0 +1,181 @@
1
+ /**
2
+ * `<tf-results-per-page>` custom element: the labelled select that picks
3
+ * how many rows a page holds.
4
+ *
5
+ * An open shadow root exposing a `<label>` (`part="results-label"`)
6
+ * wrapping the label text (`part="results-text"`) and a native `<select>`
7
+ * (`part="results-select"`). The select is the label's control: the
8
+ * rendered text is its accessible name, and activating the text focuses
9
+ * the select. Legacy keeps the text in a sibling `<span>` and names the
10
+ * select nowhere (src/modules/paging.js:662-669).
11
+ *
12
+ * Attributes:
13
+ * - `results-text` — label text, rendered as text content.
14
+ * - `page-length` — the selected page length. The matching option is
15
+ * selected, or the first option when the attribute is empty or matches
16
+ * none, and the effective value is reflected back into the attribute,
17
+ * so the attribute always reports what the select shows.
18
+ *
19
+ * The label text names the select. When `results-text` is absent or
20
+ * empty, `aria-label` (`Rows per page`, from ROADMAP
21
+ * v1-webcomponents-investigation.md:313) names it instead.
22
+ *
23
+ * Events:
24
+ * - `page-length-change` — dispatched when the user picks a length, with
25
+ * `detail: {pageLength}`. That value is the select's raw value, a
26
+ * string; the host parses it (paging.js:778).
27
+ *
28
+ * `setPageLengths()` takes the page lengths from the host, and each
29
+ * entry becomes an option whose value and text are both that entry
30
+ * (legacy `new Option(v, v)`, paging.js:681).
31
+ *
32
+ * Styling is left to the theme via `--tf-*` custom properties and
33
+ * `::part()`; the legacy `results_slc_css_class` and
34
+ * `results_span_css_class` keys are `::part()` hooks, and no CSS class or
35
+ * presentational style is shipped.
36
+ *
37
+ * Importing this module registers the element. It is not part of the
38
+ * legacy webpack bundle; consumers load it as a module.
39
+ */
40
+
41
+ // Name used when the host supplies no label text.
42
+ const DEFAULT_RESULTS_LABEL = 'Rows per page';
43
+
44
+ const TEMPLATE = document.createElement('template');
45
+ TEMPLATE.innerHTML = `
46
+ <label part="results-label">
47
+ <span part="results-text"></span><select part="results-select"></select>
48
+ </label>
49
+ `;
50
+
51
+ export class TfResultsPerPageElement extends HTMLElement {
52
+ constructor() {
53
+ super();
54
+ this.attachShadow({ mode: 'open' });
55
+ this.shadowRoot.appendChild(TEMPLATE.content.cloneNode(true));
56
+ this._textEl =
57
+ this.shadowRoot.querySelector('[part="results-text"]');
58
+ this._select =
59
+ this.shadowRoot.querySelector('[part="results-select"]');
60
+ // Page lengths supplied through setPageLengths().
61
+ this._lengths = [];
62
+ // A change inside the shadow root does not reach the host: the
63
+ // native `change` event is not composed, so it is relayed as a
64
+ // composed CustomEvent carrying the chosen value. The pick is
65
+ // written into `page-length` before the event is dispatched.
66
+ this._select.addEventListener('change', () => {
67
+ this.setAttribute('page-length', this._select.value);
68
+ this.dispatchEvent(new CustomEvent('page-length-change', {
69
+ detail: { pageLength: this._select.value },
70
+ bubbles: true,
71
+ composed: true
72
+ }));
73
+ });
74
+ }
75
+
76
+ static get observedAttributes() {
77
+ return ['results-text', 'page-length'];
78
+ }
79
+
80
+ connectedCallback() {
81
+ this._update();
82
+ }
83
+
84
+ attributeChangedCallback() {
85
+ this._update();
86
+ }
87
+
88
+ /**
89
+ * Set the label text, rendered into the `results-text` part.
90
+ * @param {*} text Label text
91
+ */
92
+ setText(text) {
93
+ this.setAttribute('results-text', text == null ? '' : String(text));
94
+ }
95
+
96
+ /**
97
+ * Set the selected page length.
98
+ * @param {*} pageLength Page length
99
+ */
100
+ setPageLength(pageLength) {
101
+ this.setAttribute('page-length',
102
+ pageLength == null ? '' : String(pageLength));
103
+ }
104
+
105
+ /**
106
+ * Supply the option list. Each entry becomes an option whose value
107
+ * and text are both the entry (src/modules/paging.js:681).
108
+ * @param {Array<*>} lengths Page lengths
109
+ */
110
+ setPageLengths(lengths) {
111
+ this._lengths = Array.isArray(lengths) ? lengths : [];
112
+ this._updateOptions();
113
+ }
114
+
115
+ /**
116
+ * Render the label text, name the select and select the option
117
+ * matching `page-length`.
118
+ */
119
+ _update() {
120
+ this._textEl.textContent = this.getAttribute('results-text') || '';
121
+ this._setSelectName();
122
+ this._selectOptions();
123
+ }
124
+
125
+ /**
126
+ * Name the select from the label text, or from the default when
127
+ * `results-text` is absent or empty.
128
+ */
129
+ _setSelectName() {
130
+ if (this.getAttribute('results-text')) {
131
+ this._select.removeAttribute('aria-label');
132
+ } else {
133
+ this._select.setAttribute('aria-label', DEFAULT_RESULTS_LABEL);
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Rebuild the option list from the supplied page lengths.
139
+ */
140
+ _updateOptions() {
141
+ this._select.textContent = '';
142
+ this._lengths.forEach((length) => {
143
+ this._select.add(new Option(length, length, false, false));
144
+ });
145
+ this._selectOptions();
146
+ }
147
+
148
+ /**
149
+ * Select the option whose value matches `page-length`, or the first
150
+ * option when the attribute is empty or matches none, and reflect the
151
+ * option that ended up selected back into the attribute.
152
+ */
153
+ _selectOptions() {
154
+ const options = this._select.options;
155
+ if (options.length === 0) {
156
+ return;
157
+ }
158
+ const pageLength = this.getAttribute('page-length') || '';
159
+ let index = -1;
160
+ for (let i = 0; i < options.length; i++) {
161
+ if (options[i].value === pageLength) {
162
+ index = i;
163
+ break;
164
+ }
165
+ }
166
+ if (index === -1) {
167
+ index = 0;
168
+ }
169
+ options[index].selected = true;
170
+ if (options[index].value !== pageLength) {
171
+ this.setAttribute('page-length', options[index].value);
172
+ }
173
+ }
174
+ }
175
+
176
+ // Register once: loading this module twice (or on a page that already
177
+ // defined the element) must not throw.
178
+ if (typeof customElements !== 'undefined' &&
179
+ !customElements.get('tf-results-per-page')) {
180
+ customElements.define('tf-results-per-page', TfResultsPerPageElement);
181
+ }
@@ -278,6 +278,13 @@ export class Paging extends Feature {
278
278
  */
279
279
  this.pgAfter = null;
280
280
 
281
+ /**
282
+ * <tf-pager> custom element host, when the bridge is active
283
+ * @type {DOMElement}
284
+ * @private
285
+ */
286
+ this.pagerEl = null;
287
+
281
288
  let startRow = tf.refRow;
282
289
  let nrows = tf.getRowsNb(true);
283
290
  //calculates page nb
@@ -328,6 +335,7 @@ export class Paging extends Feature {
328
335
  }
329
336
  },
330
337
  slcPagesChange: null,
338
+ pagerPageChange: null,
331
339
  nextEvt: null,
332
340
  prevEvt: null,
333
341
  lastEvt: null,
@@ -339,9 +347,7 @@ export class Paging extends Feature {
339
347
  * Initialize DOM elements
340
348
  */
341
349
  init() {
342
- let slcPages;
343
350
  let tf = this.tf;
344
- let evt = this.evt;
345
351
 
346
352
  if (this.initialized) {
347
353
  return;
@@ -360,6 +366,104 @@ export class Paging extends Feature {
360
366
  }
361
367
  }
362
368
 
369
+ let targetEl = !this.tgtId ?
370
+ tf.feature('toolbar').container(this.toolbarPosition) :
371
+ elm(this.tgtId);
372
+
373
+ // Render the paging controls with <tf-pager> when the source
374
+ // registers it (Vite / module load) and no button HTML is
375
+ // configured, otherwise with the legacy spans and controls
376
+ // (#926). `enableIcons` and `btn_*_page_html` keep the legacy
377
+ // path: the icon is painted by the `.nextPage`/`.previousPage`/
378
+ // `.firstPage`/`.lastPage` classes (tablefilter.css), and
379
+ // `btn_*_page_html` is user markup. Both render on the legacy
380
+ // button span only (paging.js:399-444).
381
+ // TODO(#965): route enableIcons and btn_*_page_html through the
382
+ // element once theming moves to `::part()`.
383
+ if (typeof customElements !== 'undefined' &&
384
+ customElements.get('tf-pager') && !tf.enableIcons &&
385
+ !this.hasBtnHtml()) {
386
+ this.initCE(targetEl);
387
+ } else {
388
+ this.initLegacy(targetEl);
389
+ }
390
+
391
+ this.setPagingInfo();
392
+
393
+ if (!tf.fltGrid) {
394
+ tf.validateAllRows();
395
+ this.setPagingInfo(tf.validRowsIndex);
396
+ }
397
+
398
+ this.emitter.on(['after-filtering'], bound(this.resetPagingInfo, this));
399
+ this.emitter.on(['change-page'], bound(this.changePageHandler, this));
400
+ this.emitter.on(['change-page-results'],
401
+ bound(this.changePageResultsHandler, this));
402
+
403
+ /** @inherited */
404
+ this.initialized = true;
405
+
406
+ this.emitter.emit('feature-initialized', this);
407
+ }
408
+
409
+ /**
410
+ * Initialize using the <tf-pager> custom element
411
+ * @param {DOMElement} targetEl Target container element
412
+ * @private
413
+ */
414
+ initCE(targetEl) {
415
+ let evt = this.evt;
416
+ let tf = this.tf;
417
+
418
+ let pagerEl = document.createElement('tf-pager');
419
+ pagerEl.className = this.pgCssClass();
420
+ pagerEl.setAttribute('selector-type', this.pageSelectorType);
421
+ pagerEl.setAttribute('page-text', this.pageText);
422
+ pagerEl.setAttribute('of-text', this.ofText);
423
+
424
+ if (!this.hasBtns) {
425
+ pagerEl.setAttribute('buttons', 'false');
426
+ }
427
+
428
+ this.setPagerButtons(pagerEl);
429
+
430
+ if (!this.isEnabled()) {
431
+ pagerEl.setAttribute('disabled', '');
432
+ }
433
+
434
+ targetEl.appendChild(pagerEl);
435
+ this.pagerEl = pagerEl;
436
+ this.pageSlc = pagerEl.shadowRoot.querySelector(
437
+ this.pageSelectorType === INPUT ?
438
+ '[part="page-input"]' : '[part="page-select"]');
439
+
440
+ this.bindPagerButton('first', evt.first);
441
+ this.bindPagerButton('prev', evt.prev);
442
+ this.bindPagerButton('next', evt.next);
443
+ this.bindPagerButton('last', evt.last);
444
+
445
+ evt.pagerPageChange = (event) => {
446
+ let page = parseInt(event.detail.page, 10);
447
+ if (isNaN(page)) {
448
+ return;
449
+ }
450
+ if (this.pageSelectorType === INPUT && tf.sorted) {
451
+ tf.filter();
452
+ }
453
+ this.changePage(page - 1);
454
+ };
455
+ addEvt(pagerEl, 'page-change', evt.pagerPageChange);
456
+ }
457
+
458
+ /**
459
+ * Initialize using the legacy spans, controls and select
460
+ * @param {DOMElement} targetEl Target container element
461
+ * @private
462
+ */
463
+ initLegacy(targetEl) {
464
+ let slcPages;
465
+ let evt = this.evt;
466
+
363
467
  evt.slcPagesChange = (event) => {
364
468
  let slc = event.target;
365
469
  this.changePage(slc.selectedIndex);
@@ -445,9 +549,6 @@ export class Paging extends Feature {
445
549
  }
446
550
 
447
551
  // paging elements (buttons+drop-down list) are added to defined element
448
- let targetEl = !this.tgtId ?
449
- tf.feature('toolbar').container(this.toolbarPosition) :
450
- elm(this.tgtId);
451
552
  targetEl.appendChild(btnFirstSpan);
452
553
  targetEl.appendChild(btnPrevSpan);
453
554
 
@@ -475,23 +576,84 @@ export class Paging extends Feature {
475
576
  this.pgBefore = pgBeforeSpan;
476
577
  this.pgAfter = pgAfterSpan;
477
578
  this.pageSlc = slcPages;
579
+ }
478
580
 
479
- this.setPagingInfo();
581
+ /**
582
+ * The legacy paging classes, applied to the <tf-pager> host
583
+ * @return {String} Space-separated class list
584
+ * @private
585
+ */
586
+ pgCssClass() {
587
+ return [this.btnCssClass, this.pgSlcCssClass, this.pgInpCssClass,
588
+ this.nbPgSpanCssClass].join(' ');
589
+ }
480
590
 
481
- if (!tf.fltGrid) {
482
- tf.validateAllRows();
483
- this.setPagingInfo(tf.validRowsIndex);
484
- }
591
+ /**
592
+ * Whether a `btn_*_page_html` value is set
593
+ * @return {Boolean} True when any button HTML is configured
594
+ * @private
595
+ */
596
+ hasBtnHtml() {
597
+ return Boolean(this.btnFirstPageHtml || this.btnPrevPageHtml ||
598
+ this.btnNextPageHtml || this.btnLastPageHtml);
599
+ }
485
600
 
486
- this.emitter.on(['after-filtering'], bound(this.resetPagingInfo, this));
487
- this.emitter.on(['change-page'], bound(this.changePageHandler, this));
488
- this.emitter.on(['change-page-results'],
489
- bound(this.changePageResultsHandler, this));
601
+ /**
602
+ * Set the `btn_*_page_text` captions on the <tf-pager> buttons
603
+ * (paging.js:390-444).
604
+ * @param {DOMElement} pagerEl <tf-pager> host
605
+ * @private
606
+ */
607
+ setPagerButtons(pagerEl) {
608
+ let btns = [
609
+ { part: 'first', text: this.btnFirstPageText },
610
+ { part: 'prev', text: this.btnPrevPageText },
611
+ { part: 'next', text: this.btnNextPageText },
612
+ { part: 'last', text: this.btnLastPageText }
613
+ ];
614
+
615
+ btns.forEach((btn) => {
616
+ pagerEl.setAttribute(`${btn.part}-text`, btn.text);
617
+ });
618
+ }
490
619
 
491
- /** @inherited */
492
- this.initialized = true;
620
+ /**
621
+ * Bind a paging handler to a <tf-pager> button
622
+ * @param {String} part Button part name
623
+ * @param {Function} handler Paging handler
624
+ * @private
625
+ */
626
+ bindPagerButton(part, handler) {
627
+ addEvt(this.pagerEl.shadowRoot.querySelector(`[part="${part}"]`),
628
+ 'click', handler);
629
+ }
493
630
 
494
- this.emitter.emit('feature-initialized', this);
631
+ /**
632
+ * Remove a paging handler from a <tf-pager> button
633
+ * @param {String} part Button part name
634
+ * @param {Function} handler Paging handler
635
+ * @private
636
+ */
637
+ unbindPagerButton(part, handler) {
638
+ removeEvt(this.pagerEl.shadowRoot.querySelector(`[part="${part}"]`),
639
+ 'click', handler);
640
+ }
641
+
642
+ /**
643
+ * Paging entries shared by the legacy select and <tf-pager>: page
644
+ * number as visible text, start row as value
645
+ * @return {Array<Object>} Page entries
646
+ * @private
647
+ */
648
+ pageEntries() {
649
+ let entries = [];
650
+ for (let z = 0; z < this.nbPages; z++) {
651
+ entries.push({
652
+ text: String(z + 1),
653
+ value: String(z * this.pageLength)
654
+ });
655
+ }
656
+ return entries;
495
657
  }
496
658
 
497
659
  /**
@@ -532,23 +694,32 @@ export class Paging extends Feature {
532
694
 
533
695
  //calculate nb of pages
534
696
  this.nbPages = Math.ceil(tf.validRowsIndex.length / this.pageLength);
535
- //refresh page nb span
536
- this.pgCont.innerHTML = this.nbPages;
537
- //select clearing shortcut
538
- if (this.pageSelectorType === SELECT) {
539
- this.pageSlc.innerHTML = '';
697
+
698
+ if (this.pagerEl) {
699
+ this.pagerEl.setTotalPages(this.nbPages);
700
+ this.pagerEl.setPages(this.pageEntries());
701
+ this.pagerEl.setPage(this.currentPageNb);
702
+ } else {
703
+ //refresh page nb span
704
+ this.pgCont.innerHTML = this.nbPages;
705
+ //select clearing shortcut
706
+ if (this.pageSelectorType === SELECT) {
707
+ this.pageSlc.innerHTML = '';
708
+ }
540
709
  }
541
710
 
542
711
  if (this.nbPages > 0) {
543
712
  cont.style.visibility = 'visible';
544
- if (this.pageSelectorType === SELECT) {
545
- for (let z = 0; z < this.nbPages; z++) {
546
- let opt = createOpt(z + 1, z * this.pageLength, false);
547
- this.pageSlc.options[z] = opt;
713
+ if (!this.pagerEl) {
714
+ if (this.pageSelectorType === SELECT) {
715
+ this.pageEntries().forEach((entry, z) => {
716
+ this.pageSlc.options[z] =
717
+ createOpt(entry.text, entry.value, false);
718
+ });
719
+ } else {
720
+ //input type
721
+ this.pageSlc.value = this.currentPageNb;
548
722
  }
549
- } else {
550
- //input type
551
- this.pageSlc.value = this.currentPageNb;
552
723
  }
553
724
 
554
725
  } else {
@@ -726,7 +897,9 @@ export class Paging extends Feature {
726
897
  this.onBeforeChangePage(this, (index + 1));
727
898
 
728
899
  this.currentPageNb = parseInt(index, 10) + 1;
729
- if (this.pageSelectorType === SELECT) {
900
+ if (this.pagerEl) {
901
+ this.pagerEl.setPage(this.currentPageNb);
902
+ } else if (this.pageSelectorType === SELECT) {
730
903
  this.pageSlc.options[index].selected = true;
731
904
  } else {
732
905
  this.pageSlc.value = this.currentPageNb;
@@ -784,7 +957,10 @@ export class Paging extends Feature {
784
957
  }
785
958
  this.setPagingInfo();
786
959
 
787
- if (pageSelectorType === SELECT) {
960
+ if (this.pagerEl) {
961
+ this.pagerEl.setPage(Math.min(slcPagesSelIndex,
962
+ this.nbPages - 1) + 1);
963
+ } else if (pageSelectorType === SELECT) {
788
964
  let slcIdx = (pageSlc.options.length - 1 <= slcPagesSelIndex) ?
789
965
  (pageSlc.options.length - 1) :
790
966
  slcPagesSelIndex;
@@ -839,6 +1015,26 @@ export class Paging extends Feature {
839
1015
  this.changeResultsPerPage(pageLength);
840
1016
  }
841
1017
 
1018
+ /**
1019
+ * Disable paging and reflect the state on the pager element
1020
+ */
1021
+ disable() {
1022
+ super.disable();
1023
+ if (this.pagerEl) {
1024
+ this.pagerEl.setAttribute('disabled', '');
1025
+ }
1026
+ }
1027
+
1028
+ /**
1029
+ * Enable paging and reflect the state on the pager element
1030
+ */
1031
+ enable() {
1032
+ super.enable();
1033
+ if (this.pagerEl) {
1034
+ this.pagerEl.removeAttribute('disabled');
1035
+ }
1036
+ }
1037
+
842
1038
  /**
843
1039
  * Remove paging feature
844
1040
  */
@@ -847,6 +1043,49 @@ export class Paging extends Feature {
847
1043
  return;
848
1044
  }
849
1045
 
1046
+ if (this.pagerEl) {
1047
+ this.destroyCE();
1048
+ } else {
1049
+ this.destroyLegacy();
1050
+ }
1051
+
1052
+ if (this.hasResultsPerPage) {
1053
+ this.removeResultsPerPage();
1054
+ }
1055
+
1056
+ this.emitter.off(['after-filtering'],
1057
+ bound(this.resetPagingInfo, this));
1058
+ this.emitter.off(['change-page'], bound(this.changePageHandler, this));
1059
+ this.emitter.off(['change-page-results'],
1060
+ bound(this.changePageResultsHandler, this));
1061
+
1062
+ this.pageSlc = null;
1063
+ this.nbPages = 0;
1064
+
1065
+ this.initialized = false;
1066
+ }
1067
+
1068
+ /**
1069
+ * Remove the <tf-pager> element and the handlers bound to it
1070
+ * @private
1071
+ */
1072
+ destroyCE() {
1073
+ let evt = this.evt;
1074
+
1075
+ removeEvt(this.pagerEl, 'page-change', evt.pagerPageChange);
1076
+ this.unbindPagerButton('first', evt.first);
1077
+ this.unbindPagerButton('prev', evt.prev);
1078
+ this.unbindPagerButton('next', evt.next);
1079
+ this.unbindPagerButton('last', evt.last);
1080
+ removeElm(this.pagerEl);
1081
+ this.pagerEl = null;
1082
+ }
1083
+
1084
+ /**
1085
+ * Remove the legacy paging controls and the handlers bound to them
1086
+ * @private
1087
+ */
1088
+ destroyLegacy() {
850
1089
  let evt = this.evt;
851
1090
 
852
1091
  if (this.pageSlc) {
@@ -897,20 +1136,5 @@ export class Paging extends Feature {
897
1136
  removeElm(this.pgCont);
898
1137
  this.pgCont = null;
899
1138
  }
900
-
901
- if (this.hasResultsPerPage) {
902
- this.removeResultsPerPage();
903
- }
904
-
905
- this.emitter.off(['after-filtering'],
906
- bound(this.resetPagingInfo, this));
907
- this.emitter.off(['change-page'], bound(this.changePageHandler, this));
908
- this.emitter.off(['change-page-results'],
909
- bound(this.changePageResultsHandler, this));
910
-
911
- this.pageSlc = null;
912
- this.nbPages = 0;
913
-
914
- this.initialized = false;
915
1139
  }
916
1140
  }