loseta 0.18.0 → 0.19.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 +5 -1
- package/package.json +1 -1
- package/registry/behaviors/calendar.js +855 -0
- package/registry/behaviors/carousel.js +280 -0
- package/registry/behaviors/hover-overlay.js +180 -0
- package/registry/behaviors/message-scroller.js +147 -0
- package/registry/behaviors/questionnaire.js +91 -0
- package/registry/behaviors/resizable.js +463 -0
- package/registry/components/carousel.css +1 -0
- package/registry/components/hover-card.css +2 -2
- package/registry/components/resizable.css +49 -0
- package/registry/components/tooltip.css +4 -1
- package/registry/examples/calendar.html +387 -326
- package/registry/examples/carousel.html +80 -2
- package/registry/examples/questionnaire.html +2 -2
- package/registry/examples/resizable.html +88 -15
- package/registry/examples/sidebar.html +10 -10
- package/registry/registry.json +85 -1
- package/registry/styles/luma/resizable.css +10 -0
- package/registry/styles/lyra/resizable.css +10 -0
- package/registry/styles/maia/resizable.css +10 -0
- package/registry/styles/mira/resizable.css +10 -0
- package/registry/styles/nova/resizable.css +10 -0
- package/registry/styles/rhea/resizable.css +10 -0
- package/registry/styles/sera/resizable.css +10 -0
- package/registry/styles/vega/resizable.css +10 -0
|
@@ -0,0 +1,855 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { emit, init, isRTL, on } from "./core.js";
|
|
4
|
+
|
|
5
|
+
const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
|
|
6
|
+
const ISO_MONTH = /^\d{4}-\d{2}$/;
|
|
7
|
+
const DAY_SELECTOR = "button[data-date]";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {"single" | "range" | "multiple"} SelectionMode
|
|
11
|
+
* @typedef {object} CalendarState
|
|
12
|
+
* @property {SelectionMode} selection
|
|
13
|
+
* @property {number} year
|
|
14
|
+
* @property {number} month
|
|
15
|
+
* @property {string | null} value
|
|
16
|
+
* @property {string[]} values
|
|
17
|
+
* @property {string | null} rangeStart
|
|
18
|
+
* @property {string | null} rangeEnd
|
|
19
|
+
* @property {Set<string>} unavailable
|
|
20
|
+
* @property {string | null} min
|
|
21
|
+
* @property {string | null} max
|
|
22
|
+
* @property {string | null} today
|
|
23
|
+
* @property {boolean} weekNumbers
|
|
24
|
+
* @property {number[]} weekOrder
|
|
25
|
+
* @property {string} locale
|
|
26
|
+
* @property {string | null} focused
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** @type {WeakMap<HTMLElement, CalendarState>} */
|
|
30
|
+
const states = new WeakMap();
|
|
31
|
+
/** @type {WeakSet<HTMLElement>} */
|
|
32
|
+
const skipped = new WeakSet();
|
|
33
|
+
|
|
34
|
+
/** @param {number} value */
|
|
35
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {number} year
|
|
39
|
+
* @param {number} month
|
|
40
|
+
* @param {number} day
|
|
41
|
+
*/
|
|
42
|
+
function isoDate(year, month, day) {
|
|
43
|
+
return `${year}-${pad(month + 1)}-${pad(day)}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** @param {string} value */
|
|
47
|
+
function parseDate(value) {
|
|
48
|
+
if (!ISO_DATE.test(value)) return null;
|
|
49
|
+
const [yearText, monthText, dayText] = value.split("-");
|
|
50
|
+
const year = Number(yearText);
|
|
51
|
+
const month = Number(monthText) - 1;
|
|
52
|
+
const day = Number(dayText);
|
|
53
|
+
if (
|
|
54
|
+
!Number.isInteger(year) ||
|
|
55
|
+
!Number.isInteger(month) ||
|
|
56
|
+
!Number.isInteger(day)
|
|
57
|
+
) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
if (month < 0 || month > 11 || day < 1 || day > 31) return null;
|
|
61
|
+
const date = new Date(Date.UTC(year, month, day));
|
|
62
|
+
if (
|
|
63
|
+
date.getUTCFullYear() !== year ||
|
|
64
|
+
date.getUTCMonth() !== month ||
|
|
65
|
+
date.getUTCDate() !== day
|
|
66
|
+
) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
return { year, month, day };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** @param {string} value */
|
|
73
|
+
function parseMonth(value) {
|
|
74
|
+
if (!ISO_MONTH.test(value)) return null;
|
|
75
|
+
const [yearText, monthText] = value.split("-");
|
|
76
|
+
const year = Number(yearText);
|
|
77
|
+
const month = Number(monthText) - 1;
|
|
78
|
+
if (!Number.isInteger(year) || month < 0 || month > 11) return null;
|
|
79
|
+
return { year, month };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** @param {string} value */
|
|
83
|
+
function dateFromIso(value) {
|
|
84
|
+
return new Date(`${value}T00:00:00Z`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** @param {string} value @param {number} days */
|
|
88
|
+
function addDays(value, days) {
|
|
89
|
+
const date = dateFromIso(value);
|
|
90
|
+
date.setUTCDate(date.getUTCDate() + days);
|
|
91
|
+
return isoDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** @param {number} year @param {number} month */
|
|
95
|
+
function daysInMonth(year, month) {
|
|
96
|
+
return new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** @param {number} year @param {number} month @param {number} day */
|
|
100
|
+
function weekdayOf(year, month, day) {
|
|
101
|
+
return new Date(Date.UTC(year, month, day)).getUTCDay();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** @param {string} value @param {number} year @param {number} month */
|
|
105
|
+
function sameMonth(value, year, month) {
|
|
106
|
+
const parsed = parseDate(value);
|
|
107
|
+
return parsed !== null && parsed.year === year && parsed.month === month;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** @param {Date} date */
|
|
111
|
+
function isoWeek(date) {
|
|
112
|
+
const target = new Date(
|
|
113
|
+
Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate())
|
|
114
|
+
);
|
|
115
|
+
const dayNumber = (target.getUTCDay() + 6) % 7;
|
|
116
|
+
target.setUTCDate(target.getUTCDate() - dayNumber + 3);
|
|
117
|
+
const firstThursday = new Date(Date.UTC(target.getUTCFullYear(), 0, 4));
|
|
118
|
+
const firstDayNumber = (firstThursday.getUTCDay() + 6) % 7;
|
|
119
|
+
firstThursday.setUTCDate(firstThursday.getUTCDate() - firstDayNumber + 3);
|
|
120
|
+
return (
|
|
121
|
+
1 +
|
|
122
|
+
Math.round((target.getTime() - firstThursday.getTime()) / 604800000)
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** @param {string} locale */
|
|
127
|
+
function latin(locale) {
|
|
128
|
+
const tag = locale.trim() || "en";
|
|
129
|
+
return tag.includes("-u-") ? tag : `${tag}-u-nu-latn`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** @param {string} locale */
|
|
133
|
+
function dayLabelFormatter(locale) {
|
|
134
|
+
return new Intl.DateTimeFormat(latin(locale), {
|
|
135
|
+
year: "numeric",
|
|
136
|
+
month: "long",
|
|
137
|
+
day: "numeric",
|
|
138
|
+
timeZone: "UTC"
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** @param {string} locale @param {string} value */
|
|
143
|
+
function dayLabel(locale, value) {
|
|
144
|
+
return dayLabelFormatter(locale).format(dateFromIso(value));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** @param {string} locale @param {number} year @param {number} month */
|
|
148
|
+
function monthLabel(locale, year, month) {
|
|
149
|
+
return new Intl.DateTimeFormat(latin(locale), {
|
|
150
|
+
month: "long",
|
|
151
|
+
year: "numeric",
|
|
152
|
+
timeZone: "UTC"
|
|
153
|
+
}).format(new Date(Date.UTC(year, month, 1)));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** @param {string} locale */
|
|
157
|
+
function weekdayLabels(locale) {
|
|
158
|
+
const full = new Intl.DateTimeFormat(latin(locale), {
|
|
159
|
+
weekday: "long",
|
|
160
|
+
timeZone: "UTC"
|
|
161
|
+
});
|
|
162
|
+
const short = new Intl.DateTimeFormat(latin(locale), {
|
|
163
|
+
weekday: "short",
|
|
164
|
+
timeZone: "UTC"
|
|
165
|
+
});
|
|
166
|
+
const names = { full: /** @type {string[]} */ ([]), short: /** @type {string[]} */ ([]) };
|
|
167
|
+
|
|
168
|
+
for (let index = 0; index < 7; index += 1) {
|
|
169
|
+
const date = new Date(Date.UTC(2024, 0, 7 + index));
|
|
170
|
+
names.full.push(full.format(date));
|
|
171
|
+
names.short.push(short.format(date));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return names;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** @param {string} locale */
|
|
178
|
+
function firstDayOfWeek(locale) {
|
|
179
|
+
try {
|
|
180
|
+
const info = /** @type {{ weekInfo?: { firstDay?: number } }} */ (
|
|
181
|
+
/** @type {unknown} */ (new Intl.Locale(locale))
|
|
182
|
+
);
|
|
183
|
+
const firstDay = info.weekInfo?.firstDay;
|
|
184
|
+
if (typeof firstDay === "number") return firstDay % 7;
|
|
185
|
+
} catch {
|
|
186
|
+
// Intl.Locale or weekInfo is unavailable; fall back to Sunday.
|
|
187
|
+
}
|
|
188
|
+
return 0;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** @param {Element} calendar @param {string} locale */
|
|
192
|
+
function weekOrderFor(calendar, locale) {
|
|
193
|
+
const cells = [...calendar.querySelectorAll("thead th[scope='col']")];
|
|
194
|
+
const labels = cells
|
|
195
|
+
.map(
|
|
196
|
+
(cell) =>
|
|
197
|
+
cell.getAttribute("aria-label") ?? cell.textContent?.trim() ?? ""
|
|
198
|
+
)
|
|
199
|
+
.filter((label) => label !== "" && label !== "Week");
|
|
200
|
+
const names = weekdayLabels(locale);
|
|
201
|
+
const matched = labels
|
|
202
|
+
.map((label) => {
|
|
203
|
+
const fullIndex = names.full.indexOf(label);
|
|
204
|
+
if (fullIndex !== -1) return fullIndex;
|
|
205
|
+
return names.short.indexOf(label);
|
|
206
|
+
})
|
|
207
|
+
.filter((index) => index !== -1);
|
|
208
|
+
|
|
209
|
+
if (matched.length === 7) return matched;
|
|
210
|
+
|
|
211
|
+
const first = firstDayOfWeek(locale);
|
|
212
|
+
return Array.from({ length: 7 }, (_, index) => (first + index) % 7);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** @param {Element} calendar */
|
|
216
|
+
function hasWeekNumbers(calendar) {
|
|
217
|
+
return [...calendar.querySelectorAll("thead th[scope='col']")].some((cell) => {
|
|
218
|
+
if (cell.getAttribute("aria-label") === "Week") return true;
|
|
219
|
+
return (cell.textContent ?? "").trim() === "";
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** @param {string | null} value */
|
|
224
|
+
function parseDateAttribute(value) {
|
|
225
|
+
if (!value) return null;
|
|
226
|
+
return parseDate(value) ? value : null;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** @param {string | null} value */
|
|
230
|
+
function parseList(value) {
|
|
231
|
+
if (!value) return [];
|
|
232
|
+
return value
|
|
233
|
+
.split(",")
|
|
234
|
+
.map((part) => part.trim())
|
|
235
|
+
.filter((part) => parseDate(part) !== null);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** @param {Element} calendar */
|
|
239
|
+
function initialMonth(calendar) {
|
|
240
|
+
const explicit = parseMonth(calendar.getAttribute("data-month") ?? "");
|
|
241
|
+
if (explicit) return explicit;
|
|
242
|
+
|
|
243
|
+
const first = calendar.querySelector(DAY_SELECTOR)?.getAttribute("data-date");
|
|
244
|
+
if (first) {
|
|
245
|
+
const parsed = parseDate(first);
|
|
246
|
+
if (parsed) return { year: parsed.year, month: parsed.month };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const monthSelect = calendar.querySelector(
|
|
250
|
+
"[data-slot='month'], select[aria-label='Month']"
|
|
251
|
+
);
|
|
252
|
+
const yearSelect = calendar.querySelector(
|
|
253
|
+
"[data-slot='year'], select[aria-label='Year']"
|
|
254
|
+
);
|
|
255
|
+
if (monthSelect instanceof HTMLSelectElement) {
|
|
256
|
+
const month = Number(monthSelect.value) - 1;
|
|
257
|
+
const year =
|
|
258
|
+
yearSelect instanceof HTMLSelectElement
|
|
259
|
+
? Number(yearSelect.value)
|
|
260
|
+
: new Date().getFullYear();
|
|
261
|
+
if (Number.isInteger(year) && month >= 0 && month < 12) {
|
|
262
|
+
return { year, month };
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const today = new Date();
|
|
267
|
+
return { year: today.getFullYear(), month: today.getMonth() };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** @param {Element} calendar */
|
|
271
|
+
function initialValues(calendar) {
|
|
272
|
+
const values = [...calendar.querySelectorAll("td[aria-selected='true']")]
|
|
273
|
+
.map((cell) => cell.querySelector(DAY_SELECTOR)?.getAttribute("data-date"))
|
|
274
|
+
.filter((value) => typeof value === "string" && parseDate(value) !== null);
|
|
275
|
+
|
|
276
|
+
return /** @type {string[]} */ ([...new Set(values)]).sort();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** @param {Element} calendar @param {number} year @param {number} month */
|
|
280
|
+
function initialUnavailable(calendar, year, month) {
|
|
281
|
+
const unavailable = new Set(parseList(calendar.getAttribute("data-unavailable")));
|
|
282
|
+
|
|
283
|
+
for (const button of calendar.querySelectorAll(DAY_SELECTOR)) {
|
|
284
|
+
if (button.getAttribute("aria-disabled") !== "true") continue;
|
|
285
|
+
const value = button.getAttribute("data-date");
|
|
286
|
+
if (!value) continue;
|
|
287
|
+
const parsed = parseDate(value);
|
|
288
|
+
if (parsed && parsed.year === year && parsed.month === month) {
|
|
289
|
+
unavailable.add(value);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return unavailable;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** @param {Element} calendar */
|
|
297
|
+
function initialToday(calendar) {
|
|
298
|
+
const current = calendar
|
|
299
|
+
.querySelector("button[aria-current='date'][data-date]")
|
|
300
|
+
?.getAttribute("data-date");
|
|
301
|
+
if (current && parseDate(current)) return current;
|
|
302
|
+
|
|
303
|
+
const today = new Date();
|
|
304
|
+
return isoDate(today.getFullYear(), today.getMonth(), today.getDate());
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** @param {Element} calendar */
|
|
308
|
+
function readSelection(calendar) {
|
|
309
|
+
const value = calendar.getAttribute("data-selection");
|
|
310
|
+
if (value === "range" || value === "multiple") return value;
|
|
311
|
+
|
|
312
|
+
const grid = calendar.querySelector("table[role='grid']");
|
|
313
|
+
if (grid?.getAttribute("aria-multiselectable") === "true") return "range";
|
|
314
|
+
return "single";
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** @param {Element} calendar @param {string | null} focus */
|
|
318
|
+
function setRovingTabindex(calendar, focus) {
|
|
319
|
+
for (const button of calendar.querySelectorAll(DAY_SELECTOR)) {
|
|
320
|
+
button.setAttribute(
|
|
321
|
+
"tabindex",
|
|
322
|
+
focus !== null && button.getAttribute("data-date") === focus ? "0" : "-1"
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** @param {Element} calendar */
|
|
328
|
+
function hasCustomDayContent(calendar) {
|
|
329
|
+
for (const cell of calendar.querySelectorAll("td[role='gridcell']")) {
|
|
330
|
+
const button = cell.querySelector(":scope > button");
|
|
331
|
+
if (!button) return true;
|
|
332
|
+
if (button.children.length > 0) return true;
|
|
333
|
+
if (cell.children.length !== 1) return true;
|
|
334
|
+
}
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* @param {Element} calendar
|
|
340
|
+
* @returns {CalendarState | null}
|
|
341
|
+
*/
|
|
342
|
+
function ensure(calendar) {
|
|
343
|
+
if (!(calendar instanceof HTMLElement)) {
|
|
344
|
+
throw new Error("calendar root must be an HTMLElement");
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const existing = states.get(calendar);
|
|
348
|
+
if (existing) return existing;
|
|
349
|
+
if (skipped.has(calendar)) return null;
|
|
350
|
+
if (hasCustomDayContent(calendar)) {
|
|
351
|
+
skipped.add(calendar);
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const month = initialMonth(calendar);
|
|
356
|
+
const values = initialValues(calendar);
|
|
357
|
+
const selection = readSelection(calendar);
|
|
358
|
+
const locale =
|
|
359
|
+
calendar.closest("[lang]")?.getAttribute("lang") ??
|
|
360
|
+
document.documentElement.lang ??
|
|
361
|
+
"en";
|
|
362
|
+
const state = /** @type {CalendarState} */ ({
|
|
363
|
+
selection,
|
|
364
|
+
year: month.year,
|
|
365
|
+
month: month.month,
|
|
366
|
+
value: null,
|
|
367
|
+
values: [],
|
|
368
|
+
rangeStart: null,
|
|
369
|
+
rangeEnd: null,
|
|
370
|
+
unavailable: initialUnavailable(calendar, month.year, month.month),
|
|
371
|
+
min: parseDateAttribute(calendar.getAttribute("data-min")),
|
|
372
|
+
max: parseDateAttribute(calendar.getAttribute("data-max")),
|
|
373
|
+
today: initialToday(calendar),
|
|
374
|
+
weekNumbers: hasWeekNumbers(calendar),
|
|
375
|
+
weekOrder: weekOrderFor(calendar, locale),
|
|
376
|
+
locale,
|
|
377
|
+
focused: null
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
if (selection === "single") {
|
|
381
|
+
state.value = values[0] ?? null;
|
|
382
|
+
state.values = state.value ? [state.value] : [];
|
|
383
|
+
} else if (selection === "multiple") {
|
|
384
|
+
state.values = values;
|
|
385
|
+
} else {
|
|
386
|
+
state.rangeStart = values[0] ?? null;
|
|
387
|
+
state.rangeEnd = values.length > 1 ? values[values.length - 1] ?? null : null;
|
|
388
|
+
state.value = state.rangeStart;
|
|
389
|
+
if (state.rangeStart) state.values.push(state.rangeStart);
|
|
390
|
+
if (state.rangeEnd) state.values.push(state.rangeEnd);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const focused =
|
|
394
|
+
calendar
|
|
395
|
+
.querySelector("button[tabindex='0'][data-date]")
|
|
396
|
+
?.getAttribute("data-date") ?? null;
|
|
397
|
+
state.focused =
|
|
398
|
+
focused && parseDate(focused)
|
|
399
|
+
? focused
|
|
400
|
+
: state.value ?? state.today ?? isoDate(state.year, state.month, 1);
|
|
401
|
+
|
|
402
|
+
states.set(calendar, state);
|
|
403
|
+
|
|
404
|
+
const grid = calendar.querySelector("table[role='grid']");
|
|
405
|
+
if (grid) {
|
|
406
|
+
if (state.selection === "single") {
|
|
407
|
+
grid.removeAttribute("aria-multiselectable");
|
|
408
|
+
} else {
|
|
409
|
+
grid.setAttribute("aria-multiselectable", "true");
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
setRovingTabindex(calendar, state.focused);
|
|
414
|
+
return state;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** @param {CalendarState} state @param {string} value */
|
|
418
|
+
function isDisabled(state, value) {
|
|
419
|
+
const parsed = parseDate(value);
|
|
420
|
+
if (!parsed) return true;
|
|
421
|
+
if (state.min && value < state.min) return true;
|
|
422
|
+
if (state.max && value > state.max) return true;
|
|
423
|
+
return state.unavailable.has(value);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** @param {CalendarState} state @param {string} value */
|
|
427
|
+
function isSelected(state, value) {
|
|
428
|
+
if (state.selection === "single") return state.value === value;
|
|
429
|
+
if (state.selection === "multiple") return state.values.includes(value);
|
|
430
|
+
if (!state.rangeStart) return false;
|
|
431
|
+
if (!state.rangeEnd) return state.rangeStart === value;
|
|
432
|
+
return value >= state.rangeStart && value <= state.rangeEnd;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* @param {Element} calendar
|
|
437
|
+
* @param {CalendarState} state
|
|
438
|
+
* @param {string} value
|
|
439
|
+
* @param {boolean} extend
|
|
440
|
+
*/
|
|
441
|
+
function selectDate(calendar, state, value, extend) {
|
|
442
|
+
if (state.selection === "single") {
|
|
443
|
+
state.value = value;
|
|
444
|
+
state.values = [value];
|
|
445
|
+
state.rangeStart = null;
|
|
446
|
+
state.rangeEnd = null;
|
|
447
|
+
} else if (state.selection === "multiple") {
|
|
448
|
+
const index = state.values.indexOf(value);
|
|
449
|
+
if (index === -1) {
|
|
450
|
+
state.values = [...state.values, value];
|
|
451
|
+
} else {
|
|
452
|
+
state.values = state.values.filter((candidate) => candidate !== value);
|
|
453
|
+
}
|
|
454
|
+
state.value = null;
|
|
455
|
+
state.rangeStart = null;
|
|
456
|
+
state.rangeEnd = null;
|
|
457
|
+
} else if (extend && state.rangeStart) {
|
|
458
|
+
const start = state.rangeStart;
|
|
459
|
+
state.rangeStart = value < start ? value : start;
|
|
460
|
+
state.rangeEnd = value < start ? start : value;
|
|
461
|
+
state.values = [state.rangeStart, state.rangeEnd];
|
|
462
|
+
state.value = state.rangeStart;
|
|
463
|
+
} else if (state.rangeStart && state.rangeEnd) {
|
|
464
|
+
state.rangeStart = value;
|
|
465
|
+
state.rangeEnd = null;
|
|
466
|
+
state.values = [value];
|
|
467
|
+
state.value = value;
|
|
468
|
+
} else if (state.rangeStart) {
|
|
469
|
+
const start = state.rangeStart;
|
|
470
|
+
state.rangeStart = value < start ? value : start;
|
|
471
|
+
state.rangeEnd = value < start ? start : value;
|
|
472
|
+
state.values = [state.rangeStart, state.rangeEnd];
|
|
473
|
+
state.value = state.rangeStart;
|
|
474
|
+
} else {
|
|
475
|
+
state.rangeStart = value;
|
|
476
|
+
state.rangeEnd = null;
|
|
477
|
+
state.values = [value];
|
|
478
|
+
state.value = value;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
state.focused = value;
|
|
482
|
+
emit(calendar, "change", {
|
|
483
|
+
value: state.value,
|
|
484
|
+
values: [...state.values],
|
|
485
|
+
range:
|
|
486
|
+
state.rangeStart && state.rangeEnd
|
|
487
|
+
? { start: state.rangeStart, end: state.rangeEnd }
|
|
488
|
+
: null
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* @param {Element} calendar
|
|
494
|
+
* @param {CalendarState} state
|
|
495
|
+
* @param {string | null} focusDate
|
|
496
|
+
*/
|
|
497
|
+
function render(calendar, state, focusDate) {
|
|
498
|
+
const table = calendar.querySelector("table[role='grid']");
|
|
499
|
+
if (!table) return;
|
|
500
|
+
|
|
501
|
+
for (const value of parseList(calendar.getAttribute("data-unavailable"))) {
|
|
502
|
+
state.unavailable.add(value);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const names = weekdayLabels(state.locale);
|
|
506
|
+
const thead = table.querySelector("thead");
|
|
507
|
+
const tbody = table.querySelector("tbody");
|
|
508
|
+
if (!thead || !tbody) return;
|
|
509
|
+
|
|
510
|
+
const headerRow = document.createElement("tr");
|
|
511
|
+
if (state.weekNumbers) {
|
|
512
|
+
const week = document.createElement("th");
|
|
513
|
+
week.scope = "col";
|
|
514
|
+
week.setAttribute("aria-label", "Week");
|
|
515
|
+
headerRow.append(week);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
for (const index of state.weekOrder) {
|
|
519
|
+
const cell = document.createElement("th");
|
|
520
|
+
cell.scope = "col";
|
|
521
|
+
cell.setAttribute("aria-label", names.full[index] ?? "");
|
|
522
|
+
cell.textContent = names.short[index] ?? "";
|
|
523
|
+
headerRow.append(cell);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
thead.replaceChildren(headerRow);
|
|
527
|
+
tbody.replaceChildren();
|
|
528
|
+
|
|
529
|
+
const firstWeekday = weekdayOf(state.year, state.month, 1);
|
|
530
|
+
const startOffset =
|
|
531
|
+
(firstWeekday - (state.weekOrder[0] ?? 0) + 7) % 7;
|
|
532
|
+
const days = daysInMonth(state.year, state.month);
|
|
533
|
+
const cellCount = Math.ceil((startOffset + days) / 7) * 7;
|
|
534
|
+
|
|
535
|
+
let focus =
|
|
536
|
+
focusDate && sameMonth(focusDate, state.year, state.month)
|
|
537
|
+
? focusDate
|
|
538
|
+
: state.focused && sameMonth(state.focused, state.year, state.month)
|
|
539
|
+
? state.focused
|
|
540
|
+
: state.value && sameMonth(state.value, state.year, state.month)
|
|
541
|
+
? state.value
|
|
542
|
+
: state.today && sameMonth(state.today, state.year, state.month)
|
|
543
|
+
? state.today
|
|
544
|
+
: isoDate(state.year, state.month, 1);
|
|
545
|
+
|
|
546
|
+
if (isDisabled(state, focus)) {
|
|
547
|
+
for (let day = 1; day <= days; day += 1) {
|
|
548
|
+
const candidate = isoDate(state.year, state.month, day);
|
|
549
|
+
if (!isDisabled(state, candidate)) {
|
|
550
|
+
focus = candidate;
|
|
551
|
+
break;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
state.focused = focus;
|
|
556
|
+
|
|
557
|
+
for (let index = 0; index < cellCount; index += 7) {
|
|
558
|
+
const row = document.createElement("tr");
|
|
559
|
+
const rowStart = new Date(
|
|
560
|
+
Date.UTC(state.year, state.month, 1 - startOffset + index)
|
|
561
|
+
);
|
|
562
|
+
|
|
563
|
+
if (state.weekNumbers) {
|
|
564
|
+
const week = document.createElement("th");
|
|
565
|
+
week.scope = "row";
|
|
566
|
+
const number = isoWeek(rowStart);
|
|
567
|
+
week.setAttribute("aria-label", `Week ${number}`);
|
|
568
|
+
week.textContent = pad(number);
|
|
569
|
+
row.append(week);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
for (let offset = 0; offset < 7; offset += 1) {
|
|
573
|
+
const date = new Date(rowStart);
|
|
574
|
+
date.setUTCDate(date.getUTCDate() + offset);
|
|
575
|
+
const year = date.getUTCFullYear();
|
|
576
|
+
const month = date.getUTCMonth();
|
|
577
|
+
const day = date.getUTCDate();
|
|
578
|
+
const value = isoDate(year, month, day);
|
|
579
|
+
const currentMonth = year === state.year && month === state.month;
|
|
580
|
+
const selected = isSelected(state, value);
|
|
581
|
+
const disabled = !currentMonth || isDisabled(state, value);
|
|
582
|
+
const current = value === state.today;
|
|
583
|
+
|
|
584
|
+
const cell = document.createElement("td");
|
|
585
|
+
cell.setAttribute("role", "gridcell");
|
|
586
|
+
cell.setAttribute("aria-selected", String(selected));
|
|
587
|
+
|
|
588
|
+
const button = document.createElement("button");
|
|
589
|
+
button.type = "button";
|
|
590
|
+
button.setAttribute("data-date", value);
|
|
591
|
+
button.setAttribute("aria-label", dayLabel(state.locale, value));
|
|
592
|
+
button.setAttribute("tabindex", value === focus ? "0" : "-1");
|
|
593
|
+
if (disabled) button.setAttribute("aria-disabled", "true");
|
|
594
|
+
if (current) button.setAttribute("aria-current", "date");
|
|
595
|
+
button.textContent = String(day);
|
|
596
|
+
|
|
597
|
+
cell.append(button);
|
|
598
|
+
row.append(cell);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
tbody.append(row);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
const caption = table.querySelector("caption");
|
|
605
|
+
if (caption) {
|
|
606
|
+
caption.textContent = monthLabel(state.locale, state.year, state.month);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
const title = calendar.querySelector(
|
|
610
|
+
":scope > header > [data-slot='title']"
|
|
611
|
+
);
|
|
612
|
+
if (title) {
|
|
613
|
+
title.textContent = monthLabel(state.locale, state.year, state.month);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
const monthSelect = calendar.querySelector(
|
|
617
|
+
"[data-slot='month'], select[aria-label='Month']"
|
|
618
|
+
);
|
|
619
|
+
if (monthSelect instanceof HTMLSelectElement) {
|
|
620
|
+
const monthFormatter = new Intl.DateTimeFormat(latin(state.locale), {
|
|
621
|
+
month: "long",
|
|
622
|
+
timeZone: "UTC"
|
|
623
|
+
});
|
|
624
|
+
const namesByMonth = Array.from({ length: 12 }, (_, index) =>
|
|
625
|
+
monthFormatter.format(new Date(Date.UTC(2024, index, 1)))
|
|
626
|
+
);
|
|
627
|
+
|
|
628
|
+
if (monthSelect.options.length !== 12) {
|
|
629
|
+
monthSelect.replaceChildren();
|
|
630
|
+
for (let index = 0; index < 12; index += 1) {
|
|
631
|
+
const option = document.createElement("option");
|
|
632
|
+
option.value = String(index + 1);
|
|
633
|
+
option.textContent = namesByMonth[index] ?? "";
|
|
634
|
+
monthSelect.append(option);
|
|
635
|
+
}
|
|
636
|
+
} else {
|
|
637
|
+
for (let index = 0; index < 12; index += 1) {
|
|
638
|
+
const option = monthSelect.options[index];
|
|
639
|
+
if (!option) continue;
|
|
640
|
+
option.value = String(index + 1);
|
|
641
|
+
option.textContent = namesByMonth[index] ?? "";
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
monthSelect.value = String(state.month + 1);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
const yearSelect = calendar.querySelector(
|
|
648
|
+
"[data-slot='year'], select[aria-label='Year']"
|
|
649
|
+
);
|
|
650
|
+
if (yearSelect instanceof HTMLSelectElement) {
|
|
651
|
+
const value = String(state.year);
|
|
652
|
+
if (![...yearSelect.options].some((option) => option.value === value)) {
|
|
653
|
+
const option = document.createElement("option");
|
|
654
|
+
option.value = value;
|
|
655
|
+
option.textContent = value;
|
|
656
|
+
yearSelect.append(option);
|
|
657
|
+
}
|
|
658
|
+
yearSelect.value = value;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
calendar.setAttribute("data-month", `${state.year}-${pad(state.month + 1)}`);
|
|
662
|
+
|
|
663
|
+
const target = calendar.querySelector(`button[data-date="${focus}"]`);
|
|
664
|
+
if (target instanceof HTMLElement) {
|
|
665
|
+
target.focus({ preventScroll: true });
|
|
666
|
+
target.scrollIntoView({ block: "nearest" });
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* @param {Element} calendar
|
|
672
|
+
* @param {CalendarState} state
|
|
673
|
+
* @param {string} value
|
|
674
|
+
*/
|
|
675
|
+
function moveFocus(calendar, state, value) {
|
|
676
|
+
const parsed = parseDate(value);
|
|
677
|
+
if (!parsed) return;
|
|
678
|
+
|
|
679
|
+
if (parsed.year !== state.year || parsed.month !== state.month) {
|
|
680
|
+
state.year = parsed.year;
|
|
681
|
+
state.month = parsed.month;
|
|
682
|
+
render(calendar, state, value);
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
state.focused = value;
|
|
687
|
+
setRovingTabindex(calendar, value);
|
|
688
|
+
const target = calendar.querySelector(`button[data-date="${value}"]`);
|
|
689
|
+
if (target instanceof HTMLElement) {
|
|
690
|
+
target.focus({ preventScroll: true });
|
|
691
|
+
target.scrollIntoView({ block: "nearest" });
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* @param {Element} calendar
|
|
697
|
+
* @param {CalendarState} state
|
|
698
|
+
* @param {number} months
|
|
699
|
+
*/
|
|
700
|
+
function shiftMonth(calendar, state, months) {
|
|
701
|
+
const date = new Date(Date.UTC(state.year, state.month + months, 1));
|
|
702
|
+
state.year = date.getUTCFullYear();
|
|
703
|
+
state.month = date.getUTCMonth();
|
|
704
|
+
const focus =
|
|
705
|
+
state.focused && sameMonth(state.focused, state.year, state.month)
|
|
706
|
+
? state.focused
|
|
707
|
+
: isoDate(state.year, state.month, 1);
|
|
708
|
+
render(calendar, state, focus);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
on("focusin", ".calendar", (event, element) => {
|
|
712
|
+
if (!(element instanceof HTMLElement)) return;
|
|
713
|
+
const state = ensure(element);
|
|
714
|
+
if (!state) return;
|
|
715
|
+
if (!(event.target instanceof HTMLElement)) return;
|
|
716
|
+
if (!event.target.matches(DAY_SELECTOR)) return;
|
|
717
|
+
const value = event.target.getAttribute("data-date");
|
|
718
|
+
if (value && parseDate(value)) state.focused = value;
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
on("click", ".calendar button[data-date]", (event, element) => {
|
|
722
|
+
if (!(element instanceof HTMLElement)) return;
|
|
723
|
+
const calendar = element.closest(".calendar");
|
|
724
|
+
if (!(calendar instanceof HTMLElement)) return;
|
|
725
|
+
const value = element.getAttribute("data-date");
|
|
726
|
+
if (!value) return;
|
|
727
|
+
|
|
728
|
+
const state = ensure(calendar);
|
|
729
|
+
if (!state) return;
|
|
730
|
+
const parsed = parseDate(value);
|
|
731
|
+
if (
|
|
732
|
+
!parsed ||
|
|
733
|
+
parsed.year !== state.year ||
|
|
734
|
+
parsed.month !== state.month ||
|
|
735
|
+
isDisabled(state, value)
|
|
736
|
+
) {
|
|
737
|
+
event.preventDefault();
|
|
738
|
+
return true;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
selectDate(calendar, state, value, event instanceof MouseEvent && event.shiftKey);
|
|
742
|
+
render(calendar, state, value);
|
|
743
|
+
event.preventDefault();
|
|
744
|
+
return true;
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
on(
|
|
748
|
+
"click",
|
|
749
|
+
".calendar [data-slot='previous'], .calendar [data-slot='next']",
|
|
750
|
+
(event, element) => {
|
|
751
|
+
if (!(element instanceof HTMLElement)) return;
|
|
752
|
+
const calendar = element.closest(".calendar");
|
|
753
|
+
if (!(calendar instanceof HTMLElement)) return;
|
|
754
|
+
const state = ensure(calendar);
|
|
755
|
+
if (!state) return;
|
|
756
|
+
shiftMonth(
|
|
757
|
+
calendar,
|
|
758
|
+
state,
|
|
759
|
+
element.getAttribute("data-slot") === "next" ? 1 : -1
|
|
760
|
+
);
|
|
761
|
+
event.preventDefault();
|
|
762
|
+
return true;
|
|
763
|
+
}
|
|
764
|
+
);
|
|
765
|
+
|
|
766
|
+
on(
|
|
767
|
+
"change",
|
|
768
|
+
".calendar [data-slot='month'], .calendar [data-slot='year']",
|
|
769
|
+
(_event, element) => {
|
|
770
|
+
if (!(element instanceof HTMLElement)) return;
|
|
771
|
+
const calendar = element.closest(".calendar");
|
|
772
|
+
if (!(calendar instanceof HTMLElement)) return;
|
|
773
|
+
const state = ensure(calendar);
|
|
774
|
+
if (!state) return;
|
|
775
|
+
const monthSelect = calendar.querySelector(
|
|
776
|
+
"[data-slot='month'], select[aria-label='Month']"
|
|
777
|
+
);
|
|
778
|
+
const yearSelect = calendar.querySelector(
|
|
779
|
+
"[data-slot='year'], select[aria-label='Year']"
|
|
780
|
+
);
|
|
781
|
+
|
|
782
|
+
if (monthSelect instanceof HTMLSelectElement) {
|
|
783
|
+
const month = Number(monthSelect.value) - 1;
|
|
784
|
+
if (Number.isInteger(month) && month >= 0 && month < 12) {
|
|
785
|
+
state.month = month;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
if (yearSelect instanceof HTMLSelectElement) {
|
|
789
|
+
const year = Number(yearSelect.value);
|
|
790
|
+
if (Number.isInteger(year)) state.year = year;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
render(calendar, state, isoDate(state.year, state.month, 1));
|
|
794
|
+
}
|
|
795
|
+
);
|
|
796
|
+
|
|
797
|
+
on("keydown", ".calendar", (event, element) => {
|
|
798
|
+
if (!(event instanceof KeyboardEvent)) return;
|
|
799
|
+
if (!(element instanceof HTMLElement)) return;
|
|
800
|
+
if (!(event.target instanceof HTMLElement)) return;
|
|
801
|
+
if (!event.target.matches(DAY_SELECTOR)) return;
|
|
802
|
+
|
|
803
|
+
const value = event.target.getAttribute("data-date");
|
|
804
|
+
if (!value) return;
|
|
805
|
+
const parsed = parseDate(value);
|
|
806
|
+
if (!parsed) return;
|
|
807
|
+
|
|
808
|
+
const calendar = element;
|
|
809
|
+
const state = ensure(calendar);
|
|
810
|
+
if (!state) return;
|
|
811
|
+
const rtl = isRTL(calendar);
|
|
812
|
+
let next = value;
|
|
813
|
+
|
|
814
|
+
if (event.key === "ArrowRight") {
|
|
815
|
+
next = addDays(value, rtl ? -1 : 1);
|
|
816
|
+
} else if (event.key === "ArrowLeft") {
|
|
817
|
+
next = addDays(value, rtl ? 1 : -1);
|
|
818
|
+
} else if (event.key === "ArrowDown") {
|
|
819
|
+
next = addDays(value, 7);
|
|
820
|
+
} else if (event.key === "ArrowUp") {
|
|
821
|
+
next = addDays(value, -7);
|
|
822
|
+
} else if (event.key === "Home" || event.key === "End") {
|
|
823
|
+
const offset =
|
|
824
|
+
(weekdayOf(parsed.year, parsed.month, parsed.day) -
|
|
825
|
+
(state.weekOrder[0] ?? 0) +
|
|
826
|
+
7) %
|
|
827
|
+
7;
|
|
828
|
+
const day =
|
|
829
|
+
event.key === "Home"
|
|
830
|
+
? Math.max(1, parsed.day - offset)
|
|
831
|
+
: Math.min(daysInMonth(parsed.year, parsed.month), parsed.day + (6 - offset));
|
|
832
|
+
next = isoDate(parsed.year, parsed.month, day);
|
|
833
|
+
} else if (event.key === "PageUp" || event.key === "PageDown") {
|
|
834
|
+
const direction = event.key === "PageDown" ? 1 : -1;
|
|
835
|
+
const target = new Date(
|
|
836
|
+
Date.UTC(
|
|
837
|
+
parsed.year + (event.shiftKey ? direction : 0),
|
|
838
|
+
parsed.month + (event.shiftKey ? 0 : direction),
|
|
839
|
+
1
|
|
840
|
+
)
|
|
841
|
+
);
|
|
842
|
+
const year = target.getUTCFullYear();
|
|
843
|
+
const month = target.getUTCMonth();
|
|
844
|
+
const day = Math.min(parsed.day, daysInMonth(year, month));
|
|
845
|
+
next = isoDate(year, month, day);
|
|
846
|
+
} else {
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
event.preventDefault();
|
|
851
|
+
moveFocus(calendar, state, next);
|
|
852
|
+
return true;
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
init();
|