tablefilter 0.7.6 → 0.7.8

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,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
+ }
@@ -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
+ }