@ismailnakkar/ui-behaviours 0.1.0 → 0.2.0
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.
- package/README.md +87 -75
- package/package.json +1 -1
- package/src/index.js +7 -7
- package/src/range-picker.js +20 -12
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @ismailnakkar/ui-behaviours
|
|
2
2
|
|
|
3
3
|
Page behaviours from plain HTML attributes: copy to the clipboard, confirm a submit, send a form once, open, close and
|
|
4
|
-
light-dismiss a native `<dialog>`, check every row from a header box, and select a field's text on a click.
|
|
5
|
-
|
|
4
|
+
light-dismiss a native `<dialog>`, check every row from a header box, and select a field's text on a click. They listen
|
|
5
|
+
on the document, so markup added later works too.
|
|
6
6
|
|
|
7
7
|
A date range picker over [Cally](https://wicky.nillia.ms/cally/) has its own entry, so pages without one never load the
|
|
8
8
|
calendar. The package ships no styles and no wording: the app styles the states it sets, and every word comes from the
|
|
@@ -14,12 +14,13 @@ app's markup.
|
|
|
14
14
|
npm i @ismailnakkar/ui-behaviours
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
`npm i cally
|
|
17
|
+
The behaviours have no dependencies. The [range picker](#range-picker) needs Cally 0.9.2 or a later 0.9 release, an
|
|
18
|
+
optional peer dependency (`npm i cally`). Type declarations are included.
|
|
19
19
|
|
|
20
20
|
## Usage
|
|
21
21
|
|
|
22
|
-
Call
|
|
22
|
+
Call `installUiBehaviours()` once from the layout's script, on every page, after the page is parsed (a module or a
|
|
23
|
+
deferred script): it also opens the page's `data-open-on-load` dialogs. A second call does nothing.
|
|
23
24
|
|
|
24
25
|
```js
|
|
25
26
|
import {installUiBehaviours} from '@ismailnakkar/ui-behaviours';
|
|
@@ -27,22 +28,7 @@ import {installUiBehaviours} from '@ismailnakkar/ui-behaviours';
|
|
|
27
28
|
installUiBehaviours();
|
|
28
29
|
```
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
then. A second call does nothing.
|
|
32
|
-
|
|
33
|
-
`copyText(text)` is the clipboard write the copy buttons use, for a page's own script (copying a link it just made):
|
|
34
|
-
it resolves to `true` when the clipboard took the text, `false` where `navigator.clipboard` is absent (plain http) or
|
|
35
|
-
refuses.
|
|
36
|
-
|
|
37
|
-
```js
|
|
38
|
-
import {copyText} from '@ismailnakkar/ui-behaviours';
|
|
39
|
-
|
|
40
|
-
if (!(await copyText(url))) {
|
|
41
|
-
field.select();
|
|
42
|
-
}
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
## The attributes
|
|
31
|
+
Then mark up the page:
|
|
46
32
|
|
|
47
33
|
| Attribute | On | Does |
|
|
48
34
|
|---|---|---|
|
|
@@ -59,9 +45,11 @@ if (!(await copyText(url))) {
|
|
|
59
45
|
|
|
60
46
|
### `data-copy="id"`
|
|
61
47
|
|
|
62
|
-
On a button: a click copies the element with that id
|
|
63
|
-
|
|
64
|
-
|
|
48
|
+
On a button: a click copies the element with that id, taking the first of:
|
|
49
|
+
|
|
50
|
+
1. its `data-copy-value`, for text grouped on screen for reading, such as a setup key;
|
|
51
|
+
2. an input's or a textarea's current value;
|
|
52
|
+
3. its text as laid out (`innerText`: one line per block, so a list copies one item a line), trimmed.
|
|
65
53
|
|
|
66
54
|
```html
|
|
67
55
|
<input id="short-url" value="https://example.com/abcd" readonly data-autoselect>
|
|
@@ -71,12 +59,12 @@ screen for reading, such as a setup key), else an input's or a textarea's curren
|
|
|
71
59
|
<button type="button" data-copy="setup-key">Copy key</button>
|
|
72
60
|
```
|
|
73
61
|
|
|
74
|
-
The button gets `data-copied`
|
|
75
|
-
the
|
|
76
|
-
|
|
62
|
+
The button gets `data-copied` when the copy worked, or `data-copy-failed` when the browser refused, for 1.5 seconds; a
|
|
63
|
+
second click restarts the 1.5 seconds and replaces the other mark. On a refusal the field or text is selected, a field
|
|
64
|
+
focused too, so the user copies it by hand. A missing source copies nothing.
|
|
77
65
|
|
|
78
|
-
To refuse a copy (asking for a missing field first), call `event.preventDefault()` in a click listener of
|
|
79
|
-
|
|
66
|
+
To refuse a copy (asking for a missing field first), call `event.preventDefault()` in a click listener of your own on
|
|
67
|
+
the button or an element around it: the copy skips a cancelled click.
|
|
80
68
|
|
|
81
69
|
### `data-confirm="question"`
|
|
82
70
|
|
|
@@ -93,9 +81,10 @@ form's. A submit the page already cancelled asks nothing.
|
|
|
93
81
|
### `data-submit-once`
|
|
94
82
|
|
|
95
83
|
On a form: the first submit goes and marks the form `data-submitting`; every later one is refused, so a double click on
|
|
96
|
-
a payment button pays once. The buttons stay enabled, as a disabled submitter would drop its `name` and `value
|
|
97
|
-
the
|
|
98
|
-
restored from the back-forward cache (Back from a provider's page) drops the mark, so its
|
|
84
|
+
a payment button pays once. The buttons stay enabled, as a disabled submitter would drop its `name` and `value`; style
|
|
85
|
+
the mark to show the wait. A submit refused by `data-confirm`, or cancelled by a listener on the form, was never sent
|
|
86
|
+
and leaves no mark. A page restored from the back-forward cache (Back from a provider's page) drops the mark, so its
|
|
87
|
+
forms send again.
|
|
99
88
|
|
|
100
89
|
```html
|
|
101
90
|
<form method="POST" action="/subscription" data-submit-once>
|
|
@@ -106,9 +95,9 @@ restored from the back-forward cache (Back from a provider's page) drops the mar
|
|
|
106
95
|
|
|
107
96
|
### `data-show-modal="id"`
|
|
108
97
|
|
|
109
|
-
On a button or a link: a click opens the `<dialog>` with that id with `showModal()
|
|
110
|
-
|
|
111
|
-
|
|
98
|
+
On a button or a link: a click opens the `<dialog>` with that id with `showModal()`. On a link the `href` is the
|
|
99
|
+
fallback: a modified click (new tab, new window) follows it, as does any click on a page without that dialog. A click
|
|
100
|
+
the page cancelled opens nothing, and an open dialog stays open.
|
|
112
101
|
|
|
113
102
|
```html
|
|
114
103
|
<a href="/links/create" data-show-modal="shorten-dialog">Shorten a link</a>
|
|
@@ -116,20 +105,20 @@ does any click on a page without that dialog. A click the page cancelled opens n
|
|
|
116
105
|
|
|
117
106
|
### `data-close-modal`
|
|
118
107
|
|
|
119
|
-
On a button inside a dialog: a click closes that dialog
|
|
120
|
-
without sending the form, so a Cancel button may sit in the dialog's form.
|
|
108
|
+
On a button inside a dialog: a click closes that dialog, and the native `close` event fires. On a submit button it
|
|
109
|
+
closes without sending the form, so a Cancel button may sit in the dialog's form.
|
|
121
110
|
|
|
122
111
|
```html
|
|
123
112
|
<button type="button" data-close-modal>Cancel</button>
|
|
124
113
|
```
|
|
125
114
|
|
|
126
|
-
|
|
115
|
+
Without script, `<form method="dialog">` and Escape close a dialog natively.
|
|
127
116
|
|
|
128
117
|
### `closedby="any"`
|
|
129
118
|
|
|
130
|
-
On a dialog: the native
|
|
131
|
-
dragged out onto the backdrop (a text selection) does not.
|
|
132
|
-
|
|
119
|
+
On a dialog: the native light-dismiss attribute. A click on the backdrop closes the dialog; a press inside its box
|
|
120
|
+
dragged out onto the backdrop (a text selection) does not. A browser that supports `closedby` does this itself;
|
|
121
|
+
elsewhere the package does.
|
|
133
122
|
|
|
134
123
|
```html
|
|
135
124
|
<dialog id="shorten-dialog" aria-labelledby="shorten-dialog-title" closedby="any">…</dialog>
|
|
@@ -154,11 +143,11 @@ changes, the header follows: checked when every row is, indeterminate when some
|
|
|
154
143
|
<input type="checkbox" name="ids[]" value="5">
|
|
155
144
|
```
|
|
156
145
|
|
|
157
|
-
|
|
146
|
+
The header sets the rows without firing `change` on them, so a page counting selected rows listens to the header too.
|
|
158
147
|
|
|
159
148
|
### `data-autoselect`
|
|
160
149
|
|
|
161
|
-
On an input or a textarea: a click selects all of its text, as tabbing in already does
|
|
150
|
+
On an input or a textarea: a click selects all of its text, as tabbing in already does.
|
|
162
151
|
|
|
163
152
|
```html
|
|
164
153
|
<input value="https://example.com/abcd" readonly data-autoselect>
|
|
@@ -168,8 +157,9 @@ On an input or a textarea: a click selects all of its text, as tabbing in alread
|
|
|
168
157
|
|
|
169
158
|
The package sets attributes and the browser's own states; the app styles them:
|
|
170
159
|
|
|
171
|
-
- `[data-copied]` and `[data-copy-failed]` on a copy button: show its "Copied" or "Copy it by hand" label
|
|
160
|
+
- `[data-copied]` and `[data-copy-failed]` on a copy button: show its "Copied" or "Copy it by hand" label instead of
|
|
172
161
|
"Copy". The words are the app's, in the button.
|
|
162
|
+
- `[data-submitting]` on a form sent once, to show the wait.
|
|
173
163
|
- `dialog`, `dialog::backdrop`, and `dialog[open]` for an opening transition.
|
|
174
164
|
- `:indeterminate` on a header checkbox, if the app restyles checkboxes.
|
|
175
165
|
|
|
@@ -195,10 +185,6 @@ The package sets attributes and the browser's own states; the app styles them:
|
|
|
195
185
|
}
|
|
196
186
|
```
|
|
197
187
|
|
|
198
|
-
## Events
|
|
199
|
-
|
|
200
|
-
The package fires none of its own. A dialog's native `close` event fires when a close button or the backdrop closes it.
|
|
201
|
-
|
|
202
188
|
## Accessibility
|
|
203
189
|
|
|
204
190
|
- Dialogs are native `<dialog>` opened with `showModal()`: focus moves in, the page behind is inert, Escape closes,
|
|
@@ -208,17 +194,10 @@ The package fires none of its own. A dialog's native `close` event fires when a
|
|
|
208
194
|
- The copy marks change an attribute only, so screen readers announce nothing. A page that needs it adds a
|
|
209
195
|
`role="status"` line and fills it when `data-copied` appears (a `MutationObserver`).
|
|
210
196
|
|
|
211
|
-
## Testing
|
|
212
|
-
|
|
213
|
-
The listeners need a DOM, so run your app's tests in happy-dom or jsdom (Vitest: `environment: 'happy-dom'`). They sit
|
|
214
|
-
on the document and outlive a test's markup: install them once per test file (`beforeAll`).
|
|
215
|
-
|
|
216
|
-
Run this package's suite with `npm test`.
|
|
217
|
-
|
|
218
197
|
## Range picker
|
|
219
198
|
|
|
220
199
|
A date range for a GET form: a button opens a native popover with the app's preset ranges beside a Cally
|
|
221
|
-
`<calendar-range>`, and the picks fill the form's `from` and `to` fields.
|
|
200
|
+
`<calendar-range>`, and the picks fill the form's `from` and `to` fields. Its own entry loads Cally:
|
|
222
201
|
|
|
223
202
|
```js
|
|
224
203
|
import {installRangePicker} from '@ismailnakkar/ui-behaviours/range-picker';
|
|
@@ -226,9 +205,9 @@ import {installRangePicker} from '@ismailnakkar/ui-behaviours/range-picker';
|
|
|
226
205
|
installRangePicker();
|
|
227
206
|
```
|
|
228
207
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
208
|
+
Call it after the page is parsed; a second call does nothing. For each picker already on the page it rewrites the
|
|
209
|
+
labels from the fields and moves the calendar to the end day. A picker added later works too, but keeps the server's
|
|
210
|
+
labels and view until its first pick.
|
|
232
211
|
|
|
233
212
|
It needs the browser's Popover API (Chrome 114, Safari 17, Firefox 125), which npm cannot check: without it the button
|
|
234
213
|
opens nothing and the box shows in place, where its radios and fields still submit.
|
|
@@ -245,8 +224,8 @@ opens nothing and the box shows in place, where its radios and fields still subm
|
|
|
245
224
|
|
|
246
225
|
<div id="report-range" popover data-range-picker data-range-state="preset">
|
|
247
226
|
<div role="radiogroup" aria-label="Range">
|
|
248
|
-
<label><input type="radio" name="range" value="today" data-
|
|
249
|
-
<label><input type="radio" name="range" value="last7" data-
|
|
227
|
+
<label><input type="radio" name="range" value="today" data-range-preset="2026-11-20/2026-11-20"> Today</label>
|
|
228
|
+
<label><input type="radio" name="range" value="last7" data-range-preset="2026-11-14/2026-11-20" checked> Last 7 days</label>
|
|
250
229
|
<label><input type="radio" name="range" value="custom" data-range-custom> Custom range</label>
|
|
251
230
|
</div>
|
|
252
231
|
|
|
@@ -274,9 +253,10 @@ opens nothing and the box shows in place, where its radios and fields still subm
|
|
|
274
253
|
|
|
275
254
|
- **`data-range-picker`** marks the box holding the rest, a `popover` here. Its buttons are the elements whose
|
|
276
255
|
`popovertarget` names its id.
|
|
277
|
-
- **`data-from
|
|
278
|
-
package does no calendar maths. Picking it sets the calendar's
|
|
279
|
-
the end day's, with keyboard focus on that day (this needs
|
|
256
|
+
- **`data-range-preset="from/to"`** on a preset's radio gives its days as `YYYY-MM-DD/YYYY-MM-DD`, the format of
|
|
257
|
+
Cally's `value`; the server works them out, the package does no calendar maths. Picking it sets the calendar's
|
|
258
|
+
`value` and moves its view so the two months end on the end day's, with keyboard focus on that day (this needs
|
|
259
|
+
`page-by="single"`), then fills the fields and labels.
|
|
280
260
|
- **`data-range-custom`** on the Custom range radio: two days picked on the calendar check it and fill the fields.
|
|
281
261
|
While only the start is picked, the picker shows that day alone, named as Custom range, and the radios, the fields and
|
|
282
262
|
the buttons keep the range the form sends. Paging the calendar or changing its year or month only moves the view and
|
|
@@ -296,7 +276,7 @@ opens nothing and the box shows in place, where its radios and fields still subm
|
|
|
296
276
|
Render the labels and the states on the server too: they are what shows before the script runs, and on a picker added
|
|
297
277
|
later until its first pick.
|
|
298
278
|
|
|
299
|
-
### Styling
|
|
279
|
+
### Styling the picker
|
|
300
280
|
|
|
301
281
|
The words for each state are the app's, in the markup; the app shows the ones `data-range-state` calls for:
|
|
302
282
|
|
|
@@ -316,22 +296,44 @@ The words for each state are the app's, in the markup; the app shows the ones `d
|
|
|
316
296
|
While `pending` the checked radio is still the range the form sends. To show Custom range as the one being picked,
|
|
317
297
|
style `[data-range-state="pending"] label:has([data-range-custom])` as checked, and the checked label as not.
|
|
318
298
|
|
|
319
|
-
###
|
|
320
|
-
|
|
321
|
-
The package fires none of its own. Cally's `change` and `rangestart` do not bubble, so listen to them in the capture
|
|
322
|
-
phase, as the package does. The radios fire the native `change`.
|
|
323
|
-
|
|
324
|
-
### Accessibility
|
|
299
|
+
### The picker's accessibility
|
|
325
300
|
|
|
326
301
|
- Label the radio group (`aria-label`) and the button (`aria-labelledby` on its field label and itself), and give the
|
|
327
302
|
summary `aria-live="polite"`, so a pick is read out.
|
|
328
303
|
- Give Cally's previous and next slots an `aria-label`, and its year select a `label` slot.
|
|
329
304
|
|
|
330
|
-
|
|
305
|
+
## API
|
|
331
306
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
`
|
|
307
|
+
| Import | Does |
|
|
308
|
+
|---|---|
|
|
309
|
+
| `installUiBehaviours()` | listens on the document for every behaviour above and opens the `data-open-on-load` dialogs; later calls do nothing |
|
|
310
|
+
| `copyText(text)` | the copy buttons' clipboard write: resolves to `true` when the clipboard took the text, `false` where `navigator.clipboard` is absent (plain http) or refuses |
|
|
311
|
+
| `installRangePicker()`, from `@ismailnakkar/ui-behaviours/range-picker` | listens on the document for range pickers and writes those already on the page; later calls do nothing |
|
|
312
|
+
|
|
313
|
+
`copyText()` serves a page's own script, copying a link it just made:
|
|
314
|
+
|
|
315
|
+
```js
|
|
316
|
+
import {copyText} from '@ismailnakkar/ui-behaviours';
|
|
317
|
+
|
|
318
|
+
if (!(await copyText(url))) {
|
|
319
|
+
field.select();
|
|
320
|
+
}
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
## Events
|
|
324
|
+
|
|
325
|
+
The package fires none of its own. A dialog's native `close` fires when a close button or the backdrop closes it, and
|
|
326
|
+
the range picker's radios fire the native `change`. Cally's `change` and `rangestart` do not bubble: listen to them in
|
|
327
|
+
the capture phase, as the package does.
|
|
328
|
+
|
|
329
|
+
## Testing
|
|
330
|
+
|
|
331
|
+
The listeners need a DOM, so run your app's tests in happy-dom or jsdom (Vitest: `environment: 'happy-dom'`). They sit
|
|
332
|
+
on the document and outlive a test's markup: install them once per test file (`beforeAll`).
|
|
333
|
+
|
|
334
|
+
happy-dom and jsdom cannot run Cally. For the range picker, mock it, and define a `calendar-range` of your own that
|
|
335
|
+
holds `value`, `focusedDate` and `tentative`, and dispatches Cally's events, not bubbling: `change` once `value` holds
|
|
336
|
+
both days, `rangestart` with the start day as a `Date` in `detail`.
|
|
335
337
|
|
|
336
338
|
```js
|
|
337
339
|
vi.mock('cally', () => ({}));
|
|
@@ -342,3 +344,13 @@ customElements.define('calendar-range', class extends HTMLElement {
|
|
|
342
344
|
tentative = '';
|
|
343
345
|
});
|
|
344
346
|
```
|
|
347
|
+
|
|
348
|
+
Run this package's suite with `npm test`.
|
|
349
|
+
|
|
350
|
+
## Upgrading
|
|
351
|
+
|
|
352
|
+
### From 0.1
|
|
353
|
+
|
|
354
|
+
- A preset radio gives its days in one attribute, `data-range-preset="from/to"`, in place of `data-from` and `data-to`:
|
|
355
|
+
`data-from="2026-11-14" data-to="2026-11-20"` becomes `data-range-preset="2026-11-14/2026-11-20"`. The old pair is
|
|
356
|
+
no longer read: picking a radio marked with it fills nothing, and checked it shows as `custom`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ismailnakkar/ui-behaviours",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Page behaviours from plain HTML attributes (copy, confirm, submit once, native dialogs, check all, autoselect), and a date range picker over Cally.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"clipboard",
|
package/src/index.js
CHANGED
|
@@ -21,7 +21,7 @@ export const installUiBehaviours = once(() => {
|
|
|
21
21
|
window.addEventListener('pageshow', releaseSubmitted);
|
|
22
22
|
document.addEventListener('change', checkAll);
|
|
23
23
|
document.addEventListener('click', autoselect);
|
|
24
|
-
document.addEventListener('click',
|
|
24
|
+
document.addEventListener('click', copyOnClick);
|
|
25
25
|
document.addEventListener('click', showModal);
|
|
26
26
|
document.addEventListener('click', closeModal);
|
|
27
27
|
document.addEventListener('pointerdown', pressBackdrop);
|
|
@@ -88,7 +88,7 @@ function releaseSubmitted({persisted}) {
|
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
function checkboxesNamed(name) {
|
|
91
|
-
return [...document.
|
|
91
|
+
return [...document.getElementsByName(name)].filter((box) => box.type === 'checkbox' && !box.disabled);
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
function checkAll({target}) {
|
|
@@ -134,10 +134,10 @@ function copiedTextOf(source) {
|
|
|
134
134
|
return source.value;
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
return (source.innerText ?? source.textContent
|
|
137
|
+
return (source.innerText ?? source.textContent).trim();
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
-
async function
|
|
140
|
+
async function copyOnClick(event) {
|
|
141
141
|
const button = closestTo(event, '[data-copy]');
|
|
142
142
|
const source = button ? document.getElementById(button.dataset.copy) : null;
|
|
143
143
|
|
|
@@ -147,14 +147,14 @@ async function copyButton(event) {
|
|
|
147
147
|
|
|
148
148
|
const copied = await copyText(copiedTextOf(source));
|
|
149
149
|
|
|
150
|
-
|
|
150
|
+
flashCopyResult(button, copied);
|
|
151
151
|
|
|
152
152
|
if (!copied) {
|
|
153
153
|
selectForManualCopy(source);
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
-
function
|
|
157
|
+
function flashCopyResult(button, copied) {
|
|
158
158
|
clearTimeout(copyTimers.get(button));
|
|
159
159
|
button.toggleAttribute(COPIED, copied);
|
|
160
160
|
button.toggleAttribute(COPY_FAILED, !copied);
|
|
@@ -164,7 +164,7 @@ function flashCopied(button, copied) {
|
|
|
164
164
|
}, COPY_FLASH_MS));
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
-
//
|
|
167
|
+
// select() alone may leave the field unfocused; focused, Ctrl/Cmd+C copies it by hand.
|
|
168
168
|
function selectForManualCopy(source) {
|
|
169
169
|
if (isField(source)) {
|
|
170
170
|
source.focus();
|
package/src/range-picker.js
CHANGED
|
@@ -11,12 +11,13 @@ const State = Object.freeze({preset: 'preset', custom: 'custom', pending: 'pendi
|
|
|
11
11
|
/** data-range-dates="compact" leaves out a year both ends share. */
|
|
12
12
|
const COMPACT = 'compact';
|
|
13
13
|
|
|
14
|
-
/** Between the two days of a Cally range value: "2026-11-14/2026-11-20". */
|
|
14
|
+
/** Between the two days of a Cally range value, and of a preset's data-range-preset: "2026-11-14/2026-11-20". */
|
|
15
15
|
const VALUE_SEPARATOR = '/';
|
|
16
16
|
|
|
17
17
|
/** A spaced en dash: "14 Nov – 20 Nov 2026". */
|
|
18
18
|
const DATES_SEPARATOR = ' – ';
|
|
19
19
|
|
|
20
|
+
// In UTC, as dayOf() reads each day as its UTC midnight.
|
|
20
21
|
const DAY = {day: 'numeric', month: 'short', timeZone: 'UTC'};
|
|
21
22
|
const DAY_AND_YEAR = {...DAY, year: 'numeric'};
|
|
22
23
|
|
|
@@ -43,12 +44,13 @@ function isRadio(element) {
|
|
|
43
44
|
|
|
44
45
|
function pickRange(event) {
|
|
45
46
|
const picker = closestTo(event, PICKER);
|
|
46
|
-
const target = event.target;
|
|
47
47
|
|
|
48
48
|
if (!picker) {
|
|
49
49
|
return;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
const target = event.target;
|
|
53
|
+
|
|
52
54
|
if (isCalendar(target)) {
|
|
53
55
|
const [from, to] = target.value.split(VALUE_SEPARATOR);
|
|
54
56
|
|
|
@@ -77,17 +79,22 @@ function pickStart(event) {
|
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
const start = isoOf(event.detail);
|
|
82
|
+
const range = {state: State.pending, name: nameOf(customRadioOf(picker)), from: start, to: start};
|
|
80
83
|
|
|
81
|
-
showIn(picker,
|
|
84
|
+
showIn(picker, range, localeOf(picker));
|
|
82
85
|
}
|
|
83
86
|
|
|
84
|
-
/** A preset radio's days, worked out by the server; null for Custom range. */
|
|
87
|
+
/** A preset radio's days, worked out by the server, from its data-range-preset="from/to"; null for Custom range. */
|
|
85
88
|
function presetOf(radio) {
|
|
86
|
-
const
|
|
89
|
+
const [from, to] = radio?.dataset.rangePreset?.split(VALUE_SEPARATOR) ?? [];
|
|
87
90
|
|
|
88
91
|
return from && to ? {from, to} : null;
|
|
89
92
|
}
|
|
90
93
|
|
|
94
|
+
function calendarOf(picker) {
|
|
95
|
+
return picker.querySelector(CALENDAR);
|
|
96
|
+
}
|
|
97
|
+
|
|
91
98
|
function checkedRadioOf(picker) {
|
|
92
99
|
return picker.querySelector('input[type="radio"]:checked');
|
|
93
100
|
}
|
|
@@ -106,7 +113,7 @@ function checkCustom(picker) {
|
|
|
106
113
|
|
|
107
114
|
/** Drops a start still waiting for its end day. */
|
|
108
115
|
function dropStart(picker) {
|
|
109
|
-
const calendar = picker
|
|
116
|
+
const calendar = calendarOf(picker);
|
|
110
117
|
|
|
111
118
|
if (calendar) {
|
|
112
119
|
calendar.tentative = '';
|
|
@@ -114,7 +121,7 @@ function dropStart(picker) {
|
|
|
114
121
|
}
|
|
115
122
|
|
|
116
123
|
function moveCalendar(picker, from, to) {
|
|
117
|
-
const calendar = picker
|
|
124
|
+
const calendar = calendarOf(picker);
|
|
118
125
|
|
|
119
126
|
if (calendar) {
|
|
120
127
|
calendar.value = from + VALUE_SEPARATOR + to;
|
|
@@ -124,11 +131,11 @@ function moveCalendar(picker, from, to) {
|
|
|
124
131
|
|
|
125
132
|
/**
|
|
126
133
|
* Ends the two months on the end day's and focuses that day. Cally pages to a far day by whole pages, which can leave
|
|
127
|
-
* it in the first month
|
|
128
|
-
*
|
|
134
|
+
* it in the first month. So the view goes to the month before first, then, a task later once Cally has redrawn, to the
|
|
135
|
+
* day itself: one page on with page-by="single".
|
|
129
136
|
*/
|
|
130
137
|
function viewEndDay(picker, to) {
|
|
131
|
-
const calendar = picker
|
|
138
|
+
const calendar = calendarOf(picker);
|
|
132
139
|
const monthBefore = dayOf(to);
|
|
133
140
|
|
|
134
141
|
if (!calendar || !monthBefore) {
|
|
@@ -204,12 +211,13 @@ function openersOf(picker) {
|
|
|
204
211
|
return [];
|
|
205
212
|
}
|
|
206
213
|
|
|
207
|
-
return [...document.querySelectorAll('[popovertarget]')]
|
|
214
|
+
return [...document.querySelectorAll('[popovertarget]')]
|
|
215
|
+
.filter((opener) => opener.getAttribute('popovertarget') === picker.id);
|
|
208
216
|
}
|
|
209
217
|
|
|
210
218
|
/** The calendar's locale, so the labels match its months; else the page's lang. */
|
|
211
219
|
function localeOf(picker) {
|
|
212
|
-
return picker
|
|
220
|
+
return calendarOf(picker)?.getAttribute('locale') || picker.closest('[lang]')?.lang || undefined;
|
|
213
221
|
}
|
|
214
222
|
|
|
215
223
|
function isoOf(day) {
|