@nervure/datetime 0.0.0-stage → 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/CHANGELOG.md +28 -0
- package/LICENSE +21 -0
- package/README.md +79 -2
- package/cdn/nervure-datetime.min.js +74 -0
- package/dist/calendar.d.ts +51 -0
- package/dist/calendar.js +125 -0
- package/dist/cdn.d.ts +1 -0
- package/dist/cdn.js +13 -0
- package/dist/core.d.ts +15 -0
- package/dist/core.js +876 -0
- package/dist/element.d.ts +35 -0
- package/dist/element.js +176 -0
- package/dist/i18n.d.ts +43 -0
- package/dist/i18n.js +130 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/model.d.ts +41 -0
- package/dist/model.js +280 -0
- package/dist/react.d.ts +19 -0
- package/dist/react.js +45 -0
- package/dist/style.css +71 -0
- package/dist/styles.d.ts +1 -0
- package/dist/styles.js +74 -0
- package/dist/types.d.ts +128 -0
- package/dist/types.js +1 -0
- package/dist/vue.d.ts +72 -0
- package/dist/vue.js +49 -0
- package/dist/zone.d.ts +22 -0
- package/dist/zone.js +81 -0
- package/package.json +102 -4
package/dist/core.js
ADDED
|
@@ -0,0 +1,876 @@
|
|
|
1
|
+
// The picker: state, drawing and keyboard. Plain DOM, no dependency.
|
|
2
|
+
import { PERIODS, addDays, addMonths, compare, dateText, isoWeek, monthGrid, parseDate, period, quarterOf, same, startOfWeek, weekday, } from "./calendar.js";
|
|
3
|
+
import { digits, firstDayOf, isRtl, labelsFor, monthNames, uses12Hours, weekdayNames } from "./i18n.js";
|
|
4
|
+
import { MIDNIGHT, comparedSpan, complete, emptyPick, granularityOf, innerMode, readValue, selectionOf, spanOf, writeValue, } from "./model.js";
|
|
5
|
+
import { pickerCss } from "./styles.js";
|
|
6
|
+
import { instant, isZone, resolveZone, today, wallClock, zoneLabel } from "./zone.js";
|
|
7
|
+
export { pickerCss } from "./styles.js";
|
|
8
|
+
export { LANGUAGES, labelsFor } from "./i18n.js";
|
|
9
|
+
export { PERIODS, period } from "./calendar.js";
|
|
10
|
+
export { instant, localZone, offsetMinutes, wallClock } from "./zone.js";
|
|
11
|
+
const STYLE_ID = 'nv-datetime-style';
|
|
12
|
+
const ready = new WeakSet();
|
|
13
|
+
/** Adds the stylesheet to a document or a shadow root, once. `createDatePicker` calls it for you. */
|
|
14
|
+
export function injectStyles(root = globalThis.document) {
|
|
15
|
+
if (!root || ready.has(root))
|
|
16
|
+
return;
|
|
17
|
+
const isDoc = root.nodeType === 9;
|
|
18
|
+
const doc = isDoc ? root : root.ownerDocument;
|
|
19
|
+
if (!doc)
|
|
20
|
+
return;
|
|
21
|
+
ready.add(root);
|
|
22
|
+
if (root.querySelector(`#${STYLE_ID}`))
|
|
23
|
+
return;
|
|
24
|
+
const style = doc.createElement('style');
|
|
25
|
+
style.id = STYLE_ID;
|
|
26
|
+
style.textContent = pickerCss;
|
|
27
|
+
(isDoc ? (root.head ?? root.documentElement) : root).append(style);
|
|
28
|
+
}
|
|
29
|
+
const baseView = (mode) => mode === 'month' ? 'months' : mode === 'quarter' ? 'quarters' : mode === 'year' ? 'years' : 'days';
|
|
30
|
+
const GRANULARITIES = ['day', 'week', 'month', 'quarter', 'year', 'custom'];
|
|
31
|
+
// The tab a ready-made period belongs to; the others are free ranges
|
|
32
|
+
const PERIOD_TAB = {
|
|
33
|
+
today: 'day',
|
|
34
|
+
yesterday: 'day',
|
|
35
|
+
thisWeek: 'week',
|
|
36
|
+
lastWeek: 'week',
|
|
37
|
+
thisMonth: 'month',
|
|
38
|
+
lastMonth: 'month',
|
|
39
|
+
thisQuarter: 'quarter',
|
|
40
|
+
lastQuarter: 'quarter',
|
|
41
|
+
thisYear: 'year',
|
|
42
|
+
lastYear: 'year',
|
|
43
|
+
};
|
|
44
|
+
/** `true` lets the reader choose and starts on last year; `false` or nothing compares with nothing. */
|
|
45
|
+
const comparisonOf = (compare) => compare === true ? 'previousYear' : typeof compare === 'string' ? compare : 'none';
|
|
46
|
+
function configure(options, doc, attached, granularity) {
|
|
47
|
+
const period = options.mode === 'period';
|
|
48
|
+
const mode = period ? innerMode(granularity) : (options.mode ?? 'date');
|
|
49
|
+
const tabs = (options.granularities ?? GRANULARITIES).filter((g) => GRANULARITIES.includes(g));
|
|
50
|
+
const presets = options.presets ?? period;
|
|
51
|
+
// An empty lang attribute counts as no language at all
|
|
52
|
+
const locale = [options.locale, doc.documentElement.lang, globalThis.navigator?.language].find(Boolean) ?? 'en';
|
|
53
|
+
const time = !period && (mode === 'time' || (!!options.time && (mode === 'date' || mode === 'range')));
|
|
54
|
+
const zone = resolveZone(options.zone);
|
|
55
|
+
const hour12 = options.hour12 ?? uses12Hours(locale);
|
|
56
|
+
return {
|
|
57
|
+
shape: { mode, time, seconds: !!options.seconds, zone: isZone(zone) ? zone : 'UTC', locale, hour12 },
|
|
58
|
+
labels: labelsFor(locale, options.labels),
|
|
59
|
+
rtl: isRtl(locale),
|
|
60
|
+
// ISO weeks start on Monday: the week mode shows them as they are counted
|
|
61
|
+
firstDay: mode === 'week' ? 0 : (((options.firstDay ?? firstDayOf(locale)) % 7) + 7) % 7,
|
|
62
|
+
min: options.min ? parseDate(options.min) : null,
|
|
63
|
+
max: options.max ? parseDate(options.max) : null,
|
|
64
|
+
months: Math.min(3, Math.max(1, options.months ?? (mode === 'range' ? 2 : 1))),
|
|
65
|
+
weekNumbers: options.weekNumbers ?? mode === 'week',
|
|
66
|
+
presets: (mode !== 'range' && !period) || !presets ? [] : presets === true ? PERIODS : presets,
|
|
67
|
+
inline: options.inline ?? !attached,
|
|
68
|
+
autoClose: options.autoClose ?? (!time && !period),
|
|
69
|
+
period,
|
|
70
|
+
granularities: tabs.length ? tabs : GRANULARITIES,
|
|
71
|
+
hour12,
|
|
72
|
+
minuteStep: Math.max(1, Math.floor(options.minuteStep ?? 1)),
|
|
73
|
+
zones: (options.zones ?? []).map(resolveZone).filter(isZone),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Makes a picker. On a text field, the panel opens under it; on any other element, the calendar is drawn inside it.
|
|
78
|
+
* `target` is an element or a CSS selector.
|
|
79
|
+
*/
|
|
80
|
+
export function createDatePicker(target, options = {}) {
|
|
81
|
+
const host = typeof target === 'string' ? document.querySelector(target) : target;
|
|
82
|
+
if (!host)
|
|
83
|
+
throw new Error(`@nervure/datetime: no element for ${typeof target === 'string' ? target : 'the target'}`);
|
|
84
|
+
const doc = host.ownerDocument;
|
|
85
|
+
const root = host.getRootNode();
|
|
86
|
+
const field = host instanceof doc.defaultView.HTMLInputElement ? host : null;
|
|
87
|
+
injectStyles(root.nodeType === 9 || root.nodeType === 11 ? root : doc);
|
|
88
|
+
let opts = { ...options };
|
|
89
|
+
let gran = 'month';
|
|
90
|
+
let comparison = comparisonOf(opts.compare);
|
|
91
|
+
let cfg = configure(opts, doc, !!field, gran);
|
|
92
|
+
/** Reads a value. In the period mode it also chooses the tab: the one that fits the two days, else the one asked for. */
|
|
93
|
+
function load(value) {
|
|
94
|
+
if (opts.mode !== 'period')
|
|
95
|
+
return readValue(value, cfg.shape);
|
|
96
|
+
const tabs = cfg.granularities;
|
|
97
|
+
const fallback = tabs.includes('custom') ? 'custom' : tabs[0];
|
|
98
|
+
const range = readValue(value, { ...cfg.shape, mode: 'range', time: false });
|
|
99
|
+
const [first, last] = range.dates;
|
|
100
|
+
if (first && last) {
|
|
101
|
+
const fit = granularityOf(first, last);
|
|
102
|
+
gran = tabs.includes(fit) ? fit : fallback;
|
|
103
|
+
}
|
|
104
|
+
else
|
|
105
|
+
gran =
|
|
106
|
+
opts.granularity && tabs.includes(opts.granularity)
|
|
107
|
+
? opts.granularity
|
|
108
|
+
: tabs.includes('month')
|
|
109
|
+
? 'month'
|
|
110
|
+
: tabs[0];
|
|
111
|
+
cfg = configure(opts, doc, !!field, gran);
|
|
112
|
+
if (!first || !last)
|
|
113
|
+
return emptyPick();
|
|
114
|
+
return gran === 'custom' ? range : { dates: [first], times: [MIDNIGHT, MIDNIGHT] };
|
|
115
|
+
}
|
|
116
|
+
let pick = load(opts.value);
|
|
117
|
+
let view = baseView(cfg.shape.mode);
|
|
118
|
+
let cursor = pick.dates[0] ?? today(cfg.shape.zone);
|
|
119
|
+
let shown = { ...cursor, d: 1 };
|
|
120
|
+
let hover = null;
|
|
121
|
+
let isOpen = false;
|
|
122
|
+
let hidden = null;
|
|
123
|
+
const panel = doc.createElement('div');
|
|
124
|
+
panel.className = 'nv-dt';
|
|
125
|
+
const el = (tag, attrs = {}, ...children) => {
|
|
126
|
+
const node = doc.createElement(tag);
|
|
127
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
128
|
+
if (value === null || value === undefined)
|
|
129
|
+
continue;
|
|
130
|
+
// ARIA states are written out: aria-selected="false" is not the same as no attribute
|
|
131
|
+
if (key.startsWith('aria-'))
|
|
132
|
+
node.setAttribute(key, String(value));
|
|
133
|
+
else if (value === false)
|
|
134
|
+
continue;
|
|
135
|
+
else if (key === 'class')
|
|
136
|
+
node.className = String(value);
|
|
137
|
+
else
|
|
138
|
+
node.setAttribute(key, value === true ? '' : String(value));
|
|
139
|
+
}
|
|
140
|
+
node.append(...children);
|
|
141
|
+
return node;
|
|
142
|
+
};
|
|
143
|
+
// ── Rules ──────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
144
|
+
const num = (n, minimum = 1) => digits(n, cfg.shape.locale, minimum);
|
|
145
|
+
const now = () => today(cfg.shape.zone);
|
|
146
|
+
function disabled(date) {
|
|
147
|
+
if ((cfg.min && compare(date, cfg.min) < 0) || (cfg.max && compare(date, cfg.max) > 0))
|
|
148
|
+
return true;
|
|
149
|
+
const rule = opts.disable;
|
|
150
|
+
if (!rule)
|
|
151
|
+
return false;
|
|
152
|
+
const text = dateText(date);
|
|
153
|
+
return typeof rule === 'function' ? rule(text, weekday(date)) : rule.includes(text);
|
|
154
|
+
}
|
|
155
|
+
/** A month, a quarter or a year cannot be picked when all of it is outside the limits. */
|
|
156
|
+
function spanDisabled(first, last) {
|
|
157
|
+
if (cfg.max && compare(first, cfg.max) > 0)
|
|
158
|
+
return true;
|
|
159
|
+
return !!cfg.min && compare(last, cfg.min) < 0;
|
|
160
|
+
}
|
|
161
|
+
function selection() {
|
|
162
|
+
const base = selectionOf(pick, cfg.shape, cfg.labels, opts.format);
|
|
163
|
+
if (!cfg.period)
|
|
164
|
+
return base;
|
|
165
|
+
const span = spanOf(pick, cfg.shape.mode);
|
|
166
|
+
const other = span ? comparedSpan(span[0], span[1], comparison) : null;
|
|
167
|
+
const at = (d) => new Date(instant(d, MIDNIGHT, cfg.shape.zone));
|
|
168
|
+
return {
|
|
169
|
+
...base,
|
|
170
|
+
mode: 'period',
|
|
171
|
+
value: span ? { start: dateText(span[0]), end: dateText(span[1]) } : null,
|
|
172
|
+
granularity: gran,
|
|
173
|
+
compare: other
|
|
174
|
+
? {
|
|
175
|
+
with: comparison,
|
|
176
|
+
start: at(other[0]),
|
|
177
|
+
end: at(addDays(other[1], 1)),
|
|
178
|
+
value: { start: dateText(other[0]), end: dateText(other[1]) },
|
|
179
|
+
}
|
|
180
|
+
: null,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/** Period mode: goes to a tab and keeps what was picked, as the period of that kind that holds its first day. */
|
|
184
|
+
function switchTo(next, span = spanOf(pick, cfg.shape.mode), fromReader = true) {
|
|
185
|
+
gran = cfg.granularities.includes(next)
|
|
186
|
+
? next
|
|
187
|
+
: cfg.granularities.includes('custom')
|
|
188
|
+
? 'custom'
|
|
189
|
+
: cfg.granularities[0];
|
|
190
|
+
cfg = configure(opts, doc, !!field, gran);
|
|
191
|
+
pick = !span
|
|
192
|
+
? emptyPick()
|
|
193
|
+
: gran === 'custom'
|
|
194
|
+
? { dates: [span[0], span[1]], times: [MIDNIGHT, MIDNIGHT] }
|
|
195
|
+
: { dates: [span[0]], times: [MIDNIGHT, MIDNIGHT] };
|
|
196
|
+
hover = null;
|
|
197
|
+
cursor = pick.dates[0] ?? now();
|
|
198
|
+
shown = { ...cursor, d: 1 };
|
|
199
|
+
view = baseView(cfg.shape.mode);
|
|
200
|
+
draw(true);
|
|
201
|
+
place();
|
|
202
|
+
publish(fromReader && !!span);
|
|
203
|
+
}
|
|
204
|
+
function publish(fromReader) {
|
|
205
|
+
const s = selection();
|
|
206
|
+
if (field) {
|
|
207
|
+
field.value = s.text;
|
|
208
|
+
if (hidden)
|
|
209
|
+
hidden.value = s.value === null ? '' : typeof s.value === 'string' ? s.value : JSON.stringify(s.value);
|
|
210
|
+
}
|
|
211
|
+
if (fromReader) {
|
|
212
|
+
opts.onChange?.(s);
|
|
213
|
+
host.dispatchEvent(new CustomEvent('nv-change', { detail: s, bubbles: true, composed: true }));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
// ── Picking ────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
217
|
+
function done() {
|
|
218
|
+
publish(true);
|
|
219
|
+
if (cfg.autoClose && complete(pick, cfg.shape.mode) && cfg.shape.mode !== 'multiple')
|
|
220
|
+
close();
|
|
221
|
+
}
|
|
222
|
+
function choose(date) {
|
|
223
|
+
if (disabled(date))
|
|
224
|
+
return;
|
|
225
|
+
cursor = date;
|
|
226
|
+
const { mode } = cfg.shape;
|
|
227
|
+
if (mode === 'multiple') {
|
|
228
|
+
const at = pick.dates.findIndex((d) => same(d, date));
|
|
229
|
+
if (at >= 0)
|
|
230
|
+
pick.dates.splice(at, 1);
|
|
231
|
+
else
|
|
232
|
+
pick.dates = [...pick.dates, date].sort(compare);
|
|
233
|
+
}
|
|
234
|
+
else if (mode === 'range') {
|
|
235
|
+
const [first, last] = pick.dates;
|
|
236
|
+
if (!first || last)
|
|
237
|
+
pick.dates = [date];
|
|
238
|
+
else
|
|
239
|
+
pick.dates = compare(date, first) < 0 ? [date, first] : [first, date];
|
|
240
|
+
hover = null;
|
|
241
|
+
}
|
|
242
|
+
else
|
|
243
|
+
pick.dates = [date];
|
|
244
|
+
draw();
|
|
245
|
+
if (mode !== 'range' || pick.dates.length === 2)
|
|
246
|
+
done();
|
|
247
|
+
else
|
|
248
|
+
publish(false);
|
|
249
|
+
}
|
|
250
|
+
function chooseSpan(date) {
|
|
251
|
+
cursor = date;
|
|
252
|
+
shown = { ...date, d: 1 };
|
|
253
|
+
if (view === baseView(cfg.shape.mode)) {
|
|
254
|
+
pick.dates = [date];
|
|
255
|
+
draw();
|
|
256
|
+
done();
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
// A month or a year picked on the way: back to the view of the mode
|
|
260
|
+
view = baseView(cfg.shape.mode);
|
|
261
|
+
draw();
|
|
262
|
+
}
|
|
263
|
+
function choosePeriod(item) {
|
|
264
|
+
let first;
|
|
265
|
+
let last;
|
|
266
|
+
if (typeof item === 'string')
|
|
267
|
+
[first, last] = period(item, now(), cfg.firstDay);
|
|
268
|
+
else {
|
|
269
|
+
const [a, b] = item.range(dateText(now()));
|
|
270
|
+
first = parseDate(a);
|
|
271
|
+
last = parseDate(b);
|
|
272
|
+
}
|
|
273
|
+
if (!first || !last)
|
|
274
|
+
return;
|
|
275
|
+
if (cfg.period) {
|
|
276
|
+
switchTo(typeof item === 'string' ? (PERIOD_TAB[item] ?? 'custom') : 'custom', [first, last], false);
|
|
277
|
+
done();
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
pick.dates = [first, last];
|
|
281
|
+
pick.times = [MIDNIGHT, cfg.shape.time ? { h: 23, mi: 59, s: 59 } : MIDNIGHT];
|
|
282
|
+
cursor = first;
|
|
283
|
+
shown = { ...first, d: 1 };
|
|
284
|
+
draw();
|
|
285
|
+
done();
|
|
286
|
+
}
|
|
287
|
+
function setTime(index, change) {
|
|
288
|
+
const next = { ...pick.times[index], ...change };
|
|
289
|
+
pick.times = index === 0 ? [next, pick.times[1]] : [pick.times[0], next];
|
|
290
|
+
if (cfg.shape.mode === 'time' && !pick.dates.length)
|
|
291
|
+
pick.dates = [{ y: 1970, m: 1, d: 1 }];
|
|
292
|
+
if (!pick.dates.length && cfg.shape.mode === 'date')
|
|
293
|
+
pick.dates = [now()];
|
|
294
|
+
publish(true);
|
|
295
|
+
}
|
|
296
|
+
// ── Drawing ────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
297
|
+
function move(to) {
|
|
298
|
+
cursor = to;
|
|
299
|
+
const first = shown;
|
|
300
|
+
const last = addMonths(shown, cfg.months - 1);
|
|
301
|
+
if (to.y * 12 + to.m < first.y * 12 + first.m)
|
|
302
|
+
shown = { y: to.y, m: to.m, d: 1 };
|
|
303
|
+
else if (to.y * 12 + to.m > last.y * 12 + last.m)
|
|
304
|
+
shown = addMonths({ y: to.y, m: to.m, d: 1 }, 1 - cfg.months);
|
|
305
|
+
draw(true);
|
|
306
|
+
}
|
|
307
|
+
function page(months) {
|
|
308
|
+
shown = addMonths(shown, months);
|
|
309
|
+
cursor = addMonths(cursor, months);
|
|
310
|
+
draw();
|
|
311
|
+
}
|
|
312
|
+
/** Marks the days of the pick and of the range under the pointer, without drawing the panel again. */
|
|
313
|
+
function paint() {
|
|
314
|
+
const [first, last] = pick.dates;
|
|
315
|
+
const { mode } = cfg.shape;
|
|
316
|
+
const end = mode === 'range' ? (last ?? hover) : null;
|
|
317
|
+
const [a, b] = first && end ? (compare(first, end) <= 0 ? [first, end] : [end, first]) : [first ?? null, null];
|
|
318
|
+
const week = mode === 'week' && first ? startOfWeek(first, 0) : null;
|
|
319
|
+
for (const cell of panel.querySelectorAll('.nv-dt-day')) {
|
|
320
|
+
const date = parseDate(cell.dataset.date);
|
|
321
|
+
let on = false;
|
|
322
|
+
let start = false;
|
|
323
|
+
let finish = false;
|
|
324
|
+
let inside = false;
|
|
325
|
+
if (mode === 'multiple')
|
|
326
|
+
on = pick.dates.some((d) => same(d, date));
|
|
327
|
+
else if (week) {
|
|
328
|
+
const offset = compare(date, week);
|
|
329
|
+
inside = offset >= 0 && offset < 7;
|
|
330
|
+
start = offset === 0;
|
|
331
|
+
finish = offset === 6;
|
|
332
|
+
}
|
|
333
|
+
else if (mode === 'range' && a) {
|
|
334
|
+
start = same(a, date);
|
|
335
|
+
finish = !!b && same(b, date);
|
|
336
|
+
inside = !!b && compare(date, a) >= 0 && compare(date, b) <= 0;
|
|
337
|
+
}
|
|
338
|
+
else
|
|
339
|
+
on = same(first ?? null, date);
|
|
340
|
+
cell.classList.toggle('is-on', on);
|
|
341
|
+
cell.classList.toggle('is-start', start);
|
|
342
|
+
cell.classList.toggle('is-end', finish);
|
|
343
|
+
cell.classList.toggle('is-in', inside && !(start && finish));
|
|
344
|
+
cell.setAttribute('aria-selected', String(on || start || finish || inside));
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
function header(title, index, count, step, label) {
|
|
348
|
+
const nav = (direction, hide) => {
|
|
349
|
+
const button = el('button', {
|
|
350
|
+
type: 'button',
|
|
351
|
+
class: 'nv-dt-nav',
|
|
352
|
+
'aria-label': `${direction < 0 ? cfg.labels.previous : cfg.labels.next}: ${label}`,
|
|
353
|
+
'data-hide': hide,
|
|
354
|
+
});
|
|
355
|
+
const svg = doc.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
356
|
+
svg.setAttribute('viewBox', '0 0 24 24');
|
|
357
|
+
svg.setAttribute('aria-hidden', 'true');
|
|
358
|
+
const path = doc.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
359
|
+
path.setAttribute('d', direction < 0 ? 'M15 5l-7 7 7 7' : 'M9 5l7 7-7 7');
|
|
360
|
+
svg.append(path);
|
|
361
|
+
button.append(svg);
|
|
362
|
+
button.addEventListener('click', () => page(direction * step));
|
|
363
|
+
return button;
|
|
364
|
+
};
|
|
365
|
+
return el('div', { class: 'nv-dt-head' }, nav(-1, index > 0), el('div', { class: 'nv-dt-title', 'aria-live': 'polite' }, ...title), nav(1, index < count - 1));
|
|
366
|
+
}
|
|
367
|
+
function titleButton(text, to) {
|
|
368
|
+
const button = el('button', { type: 'button' }, text);
|
|
369
|
+
button.addEventListener('click', () => {
|
|
370
|
+
view = view === to ? baseView(cfg.shape.mode) : to;
|
|
371
|
+
draw(true);
|
|
372
|
+
});
|
|
373
|
+
return button;
|
|
374
|
+
}
|
|
375
|
+
function daysView() {
|
|
376
|
+
const { locale } = cfg.shape;
|
|
377
|
+
const names = monthNames(locale);
|
|
378
|
+
const short = weekdayNames(locale, cfg.firstDay, 'short');
|
|
379
|
+
const long = weekdayNames(locale, cfg.firstDay, 'long');
|
|
380
|
+
const full = new Intl.DateTimeFormat(locale, { dateStyle: 'full', timeZone: 'UTC' });
|
|
381
|
+
const current = now();
|
|
382
|
+
const wrap = el('div', { class: 'nv-dt-months' });
|
|
383
|
+
for (let i = 0; i < cfg.months; i++) {
|
|
384
|
+
const month = addMonths(shown, i);
|
|
385
|
+
const grid = el('div', {
|
|
386
|
+
class: 'nv-dt-grid',
|
|
387
|
+
role: 'grid',
|
|
388
|
+
'data-weeks': cfg.weekNumbers,
|
|
389
|
+
'aria-label': `${names[month.m - 1]} ${month.y}`,
|
|
390
|
+
});
|
|
391
|
+
if (cfg.weekNumbers)
|
|
392
|
+
grid.append(el('span', { class: 'nv-dt-dow', 'aria-hidden': 'true' }, cfg.labels.week));
|
|
393
|
+
short.forEach((name, d) => grid.append(el('span', { class: 'nv-dt-dow', role: 'columnheader', 'aria-label': long[d] }, name)));
|
|
394
|
+
monthGrid(month.y, month.m, cfg.firstDay).forEach((date, at) => {
|
|
395
|
+
if (cfg.weekNumbers && at % 7 === 0)
|
|
396
|
+
grid.append(el('span', { class: 'nv-dt-wk', 'aria-hidden': 'true' }, num(isoWeek(addDays(date, (3 - cfg.firstDay + 7) % 7)).week)));
|
|
397
|
+
const out = date.m !== month.m;
|
|
398
|
+
// With several months, a day of the next month is drawn once, in its own month
|
|
399
|
+
if (out && cfg.months > 1) {
|
|
400
|
+
grid.append(el('span', {}));
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
const cell = el('button', {
|
|
404
|
+
type: 'button',
|
|
405
|
+
class: `nv-dt-day${out ? ' is-out' : ''}${same(date, current) ? ' is-today' : ''}`,
|
|
406
|
+
'data-date': dateText(date),
|
|
407
|
+
'aria-label': full.format(Date.UTC(date.y, date.m - 1, date.d, 12)),
|
|
408
|
+
tabindex: same(date, cursor) ? 0 : -1,
|
|
409
|
+
disabled: disabled(date),
|
|
410
|
+
}, num(date.d));
|
|
411
|
+
grid.append(cell);
|
|
412
|
+
});
|
|
413
|
+
wrap.append(el('div', { class: 'nv-dt-month' }, header([titleButton(names[month.m - 1], 'months'), titleButton(num(month.y), 'years')], i, cfg.months, 1, names[month.m - 1]), grid));
|
|
414
|
+
}
|
|
415
|
+
return wrap;
|
|
416
|
+
}
|
|
417
|
+
function cellsView() {
|
|
418
|
+
const { locale, mode } = cfg.shape;
|
|
419
|
+
const [chosen] = pick.dates;
|
|
420
|
+
const current = now();
|
|
421
|
+
const cells = el('div', { class: 'nv-dt-cells', 'data-view': view, role: 'grid' });
|
|
422
|
+
const add = (date, last, text, on, isNow, sub) => {
|
|
423
|
+
const cell = el('button', {
|
|
424
|
+
type: 'button',
|
|
425
|
+
class: `nv-dt-cell${on ? ' is-on' : ''}${isNow ? ' is-now' : ''}`,
|
|
426
|
+
'data-date': dateText(date),
|
|
427
|
+
'aria-pressed': on,
|
|
428
|
+
tabindex: sameSpan(date, cursor) ? 0 : -1,
|
|
429
|
+
disabled: spanDisabled(date, last),
|
|
430
|
+
}, text);
|
|
431
|
+
if (sub)
|
|
432
|
+
cell.append(el('small', {}, sub));
|
|
433
|
+
cell.addEventListener('click', () => chooseSpan(date));
|
|
434
|
+
cells.append(cell);
|
|
435
|
+
};
|
|
436
|
+
const sameSpan = (a, b) => view === 'years'
|
|
437
|
+
? a.y === b.y
|
|
438
|
+
: view === 'quarters'
|
|
439
|
+
? a.y === b.y && quarterOf(a.m) === quarterOf(b.m)
|
|
440
|
+
: a.y === b.y && a.m === b.m;
|
|
441
|
+
const picked = (date) => !!chosen && view === baseView(mode) && sameSpan(date, chosen);
|
|
442
|
+
let title;
|
|
443
|
+
let step = 12;
|
|
444
|
+
if (view === 'years') {
|
|
445
|
+
const first = Math.floor(shown.y / 12) * 12;
|
|
446
|
+
for (let y = first; y < first + 12; y++)
|
|
447
|
+
add({ y, m: 1, d: 1 }, { y, m: 12, d: 31 }, num(y), picked({ y, m: 1, d: 1 }), y === current.y);
|
|
448
|
+
title = [el('span', {}, `${num(first)} – ${num(first + 11)}`)];
|
|
449
|
+
step = 144;
|
|
450
|
+
}
|
|
451
|
+
else if (view === 'quarters') {
|
|
452
|
+
const short = monthNames(locale, 'short');
|
|
453
|
+
for (let q = 0; q < 4; q++) {
|
|
454
|
+
const date = { y: shown.y, m: q * 3 + 1, d: 1 };
|
|
455
|
+
const last = addDays(addMonths(date, 3), -1);
|
|
456
|
+
add(date, last, `${cfg.labels.quarter}${num(q + 1)}`, picked(date), current.y === date.y && quarterOf(current.m) === q + 1, `${short[q * 3]} – ${short[q * 3 + 2]}`);
|
|
457
|
+
}
|
|
458
|
+
title = [titleButton(num(shown.y), 'years')];
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
monthNames(locale, 'short').forEach((name, i) => {
|
|
462
|
+
const date = { y: shown.y, m: i + 1, d: 1 };
|
|
463
|
+
add(date, addDays(addMonths(date, 1), -1), name, picked(date), current.y === date.y && current.m === i + 1);
|
|
464
|
+
});
|
|
465
|
+
title = [titleButton(num(shown.y), 'years')];
|
|
466
|
+
}
|
|
467
|
+
return el('div', { class: 'nv-dt-month' }, header(title, 0, 1, step, view), cells);
|
|
468
|
+
}
|
|
469
|
+
function timeField(index, label) {
|
|
470
|
+
const time = pick.times[index];
|
|
471
|
+
const row = el('div', { class: 'nv-dt-time' }, el('span', {}, label));
|
|
472
|
+
const input = (part, value, min, max, step, name) => {
|
|
473
|
+
const node = el('input', {
|
|
474
|
+
type: 'number',
|
|
475
|
+
inputmode: 'numeric',
|
|
476
|
+
min,
|
|
477
|
+
max,
|
|
478
|
+
step,
|
|
479
|
+
value: String(value).padStart(2, '0'),
|
|
480
|
+
'aria-label': `${label} ${name}`,
|
|
481
|
+
});
|
|
482
|
+
node.addEventListener('change', () => {
|
|
483
|
+
let n = Math.min(max, Math.max(min, Math.round(Number(node.value) || 0)));
|
|
484
|
+
if (part === 'h' && cfg.hour12)
|
|
485
|
+
n = (n % 12) + (pick.times[index].h >= 12 ? 12 : 0);
|
|
486
|
+
node.value = String(part === 'h' && cfg.hour12 ? n % 12 || 12 : n).padStart(2, '0');
|
|
487
|
+
setTime(index, { [part]: n });
|
|
488
|
+
});
|
|
489
|
+
return node;
|
|
490
|
+
};
|
|
491
|
+
const unit = (type) => {
|
|
492
|
+
try {
|
|
493
|
+
return new Intl.DisplayNames(cfg.shape.locale, { type: 'dateTimeField' }).of(type) ?? type;
|
|
494
|
+
}
|
|
495
|
+
catch {
|
|
496
|
+
return type;
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
row.append(cfg.hour12 ? input('h', time.h % 12 || 12, 1, 12, 1, unit('hour')) : input('h', time.h, 0, 23, 1, unit('hour')));
|
|
500
|
+
row.append(el('span', { 'aria-hidden': 'true' }, ':'), input('mi', time.mi, 0, 59, cfg.minuteStep, unit('minute')));
|
|
501
|
+
if (cfg.shape.seconds)
|
|
502
|
+
row.append(el('span', { 'aria-hidden': 'true' }, ':'), input('s', time.s, 0, 59, 1, unit('second')));
|
|
503
|
+
if (cfg.hour12) {
|
|
504
|
+
const period12 = new Intl.DateTimeFormat(cfg.shape.locale, { hour: 'numeric', hour12: true, timeZone: 'UTC' });
|
|
505
|
+
const word = (h) => period12.formatToParts(Date.UTC(2024, 0, 1, h)).find((p) => p.type === 'dayPeriod')?.value ??
|
|
506
|
+
(h < 12 ? 'AM' : 'PM');
|
|
507
|
+
const toggle = el('button', { type: 'button' }, word(time.h));
|
|
508
|
+
toggle.addEventListener('click', () => {
|
|
509
|
+
const h = (pick.times[index].h + 12) % 24;
|
|
510
|
+
toggle.textContent = word(h);
|
|
511
|
+
setTime(index, { h });
|
|
512
|
+
});
|
|
513
|
+
row.append(toggle);
|
|
514
|
+
}
|
|
515
|
+
return row;
|
|
516
|
+
}
|
|
517
|
+
function draw(keepFocus = false) {
|
|
518
|
+
const { mode, time, locale } = cfg.shape;
|
|
519
|
+
const hadFocus = keepFocus || panel.contains(root.activeElement);
|
|
520
|
+
panel.className = `nv-dt${field && !cfg.inline ? ' nv-dt-pop' : ''}${opts.orientation === 'vertical' ? ' nv-dt-vertical' : ''}${opts.class ? ` ${opts.class}` : ''}`;
|
|
521
|
+
panel.dir = cfg.rtl ? 'rtl' : 'ltr';
|
|
522
|
+
panel.lang = locale;
|
|
523
|
+
panel.dataset.theme = opts.theme ?? 'auto';
|
|
524
|
+
panel.dataset.mode = mode;
|
|
525
|
+
if (opts.accent)
|
|
526
|
+
panel.style.setProperty('--nv-dt-accent', opts.accent);
|
|
527
|
+
else
|
|
528
|
+
panel.style.removeProperty('--nv-dt-accent');
|
|
529
|
+
if (field && !cfg.inline) {
|
|
530
|
+
panel.setAttribute('role', 'dialog');
|
|
531
|
+
panel.setAttribute('aria-label', field.getAttribute('aria-label') ?? field.placeholder ?? mode);
|
|
532
|
+
}
|
|
533
|
+
panel.replaceChildren();
|
|
534
|
+
if (cfg.presets.length) {
|
|
535
|
+
const list = el('div', { class: 'nv-dt-presets' });
|
|
536
|
+
const value = JSON.stringify(cfg.period ? selection().value : writeValue(pick, { ...cfg.shape, time: false }));
|
|
537
|
+
for (const item of cfg.presets) {
|
|
538
|
+
const label = typeof item === 'string' ? cfg.labels.periods[item] : item.label;
|
|
539
|
+
const range = typeof item === 'string' ? period(item, now(), cfg.firstDay).map(dateText) : item.range(dateText(now()));
|
|
540
|
+
const button = el('button', { type: 'button', 'aria-pressed': JSON.stringify({ start: range[0], end: range[1] }) === value }, label);
|
|
541
|
+
button.addEventListener('click', () => choosePeriod(item));
|
|
542
|
+
list.append(button);
|
|
543
|
+
}
|
|
544
|
+
panel.append(list);
|
|
545
|
+
}
|
|
546
|
+
const main = el('div', { class: 'nv-dt-main' });
|
|
547
|
+
if (cfg.period && cfg.granularities.length > 1) {
|
|
548
|
+
const names = new Intl.DisplayNames(locale, { type: 'dateTimeField' });
|
|
549
|
+
const field = {
|
|
550
|
+
day: 'day',
|
|
551
|
+
week: 'weekOfYear',
|
|
552
|
+
month: 'month',
|
|
553
|
+
quarter: 'quarter',
|
|
554
|
+
year: 'year',
|
|
555
|
+
custom: '',
|
|
556
|
+
};
|
|
557
|
+
const tabs = el('div', { class: 'nv-dt-tabs', role: 'tablist' });
|
|
558
|
+
for (const g of cfg.granularities) {
|
|
559
|
+
const tab = el('button', { type: 'button', role: 'tab', 'aria-selected': g === gran, 'data-granularity': g }, g === 'custom' ? cfg.labels.custom : (names.of(field[g]) ?? g));
|
|
560
|
+
tab.addEventListener('click', () => switchTo(g));
|
|
561
|
+
tabs.append(tab);
|
|
562
|
+
}
|
|
563
|
+
main.append(tabs);
|
|
564
|
+
}
|
|
565
|
+
if (mode !== 'time')
|
|
566
|
+
main.append(view === 'days' ? daysView() : cellsView());
|
|
567
|
+
if (time) {
|
|
568
|
+
const times = el('div', { class: 'nv-dt-times' });
|
|
569
|
+
if (mode === 'range')
|
|
570
|
+
times.append(timeField(0, cfg.labels.from), timeField(1, cfg.labels.to));
|
|
571
|
+
else
|
|
572
|
+
times.append(timeField(0, cfg.labels.time));
|
|
573
|
+
main.append(times);
|
|
574
|
+
}
|
|
575
|
+
if (cfg.zones.length || (time && cfg.shape.zone !== resolveZone('local'))) {
|
|
576
|
+
const at = Date.now();
|
|
577
|
+
const row = el('label', { class: 'nv-dt-zone' }, el('span', {}, cfg.labels.zone));
|
|
578
|
+
if (cfg.zones.length) {
|
|
579
|
+
const select = el('select', {});
|
|
580
|
+
for (const zone of new Set([cfg.shape.zone, ...cfg.zones]))
|
|
581
|
+
select.append(el('option', { value: zone, selected: zone === cfg.shape.zone }, `${zone.replace(/_/g, ' ')} (${zoneLabel(at, zone, locale)})`));
|
|
582
|
+
select.addEventListener('change', () => api.update({ zone: select.value }));
|
|
583
|
+
row.append(select);
|
|
584
|
+
}
|
|
585
|
+
else
|
|
586
|
+
row.append(el('span', {}, `${cfg.shape.zone.replace(/_/g, ' ')} (${zoneLabel(at, cfg.shape.zone, locale)})`));
|
|
587
|
+
main.append(row);
|
|
588
|
+
}
|
|
589
|
+
if (cfg.period && opts.compare === true) {
|
|
590
|
+
const row = el('label', { class: 'nv-dt-zone nv-dt-compare' }, el('span', {}, cfg.labels.compareTo));
|
|
591
|
+
const select = el('select', {});
|
|
592
|
+
for (const [value, label] of [
|
|
593
|
+
['previousYear', cfg.labels.previousYear],
|
|
594
|
+
['previousPeriod', cfg.labels.previousPeriod],
|
|
595
|
+
['none', cfg.labels.none],
|
|
596
|
+
])
|
|
597
|
+
select.append(el('option', { value, selected: value === comparison }, label));
|
|
598
|
+
select.addEventListener('change', () => {
|
|
599
|
+
comparison = select.value;
|
|
600
|
+
publish(complete(pick, cfg.shape.mode));
|
|
601
|
+
});
|
|
602
|
+
row.append(select);
|
|
603
|
+
main.append(row);
|
|
604
|
+
}
|
|
605
|
+
const foot = el('div', { class: 'nv-dt-foot' });
|
|
606
|
+
const left = el('div', {});
|
|
607
|
+
const right = el('div', {});
|
|
608
|
+
const action = (text, run, cls = '') => {
|
|
609
|
+
const button = el('button', { type: 'button', class: cls }, text);
|
|
610
|
+
button.addEventListener('click', run);
|
|
611
|
+
return button;
|
|
612
|
+
};
|
|
613
|
+
left.append(action(time ? cfg.labels.now : cfg.labels.today, () => {
|
|
614
|
+
const current = now();
|
|
615
|
+
if (time) {
|
|
616
|
+
const w = wallClock(Date.now(), cfg.shape.zone);
|
|
617
|
+
pick.times = [{ h: w.h, mi: w.mi, s: cfg.shape.seconds ? w.s : 0 }, pick.times[1]];
|
|
618
|
+
}
|
|
619
|
+
view = baseView(mode);
|
|
620
|
+
shown = { ...current, d: 1 };
|
|
621
|
+
if (mode === 'range' || mode === 'multiple') {
|
|
622
|
+
cursor = current;
|
|
623
|
+
draw(true);
|
|
624
|
+
}
|
|
625
|
+
else if (mode === 'time') {
|
|
626
|
+
pick.dates = [{ y: 1970, m: 1, d: 1 }];
|
|
627
|
+
draw(true);
|
|
628
|
+
done();
|
|
629
|
+
}
|
|
630
|
+
else if (view === 'days')
|
|
631
|
+
choose(current);
|
|
632
|
+
else
|
|
633
|
+
chooseSpan(current);
|
|
634
|
+
}));
|
|
635
|
+
right.append(action(cfg.labels.clear, () => {
|
|
636
|
+
pick = emptyPick();
|
|
637
|
+
hover = null;
|
|
638
|
+
draw(true);
|
|
639
|
+
publish(true);
|
|
640
|
+
}));
|
|
641
|
+
if (field && !cfg.inline && !cfg.autoClose)
|
|
642
|
+
right.append(action(cfg.labels.apply, close, 'nv-dt-apply'));
|
|
643
|
+
foot.append(left, right);
|
|
644
|
+
main.append(foot);
|
|
645
|
+
panel.append(main);
|
|
646
|
+
paint();
|
|
647
|
+
if (hadFocus)
|
|
648
|
+
panel.querySelector('[tabindex="0"]')?.focus();
|
|
649
|
+
}
|
|
650
|
+
// ── Pointer and keyboard ───────────────────────────────────────────────────────────────────────────────────────
|
|
651
|
+
panel.addEventListener('click', (event) => {
|
|
652
|
+
const cell = event.target.closest('.nv-dt-day');
|
|
653
|
+
if (cell && !cell.disabled)
|
|
654
|
+
choose(parseDate(cell.dataset.date));
|
|
655
|
+
});
|
|
656
|
+
panel.addEventListener('pointerover', (event) => {
|
|
657
|
+
if (cfg.shape.mode !== 'range' || pick.dates.length !== 1)
|
|
658
|
+
return;
|
|
659
|
+
const cell = event.target.closest('.nv-dt-day');
|
|
660
|
+
const next = cell ? parseDate(cell.dataset.date) : null;
|
|
661
|
+
if (!same(next, hover) && (next || hover)) {
|
|
662
|
+
hover = next;
|
|
663
|
+
paint();
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
panel.addEventListener('keydown', (event) => {
|
|
667
|
+
const target = event.target;
|
|
668
|
+
if (event.key === 'Escape' && field && !cfg.inline) {
|
|
669
|
+
event.preventDefault();
|
|
670
|
+
close();
|
|
671
|
+
field.focus();
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
const isDay = target.classList.contains('nv-dt-day');
|
|
675
|
+
if (!isDay && !target.classList.contains('nv-dt-cell'))
|
|
676
|
+
return;
|
|
677
|
+
const side = cfg.rtl ? -1 : 1;
|
|
678
|
+
let to = null;
|
|
679
|
+
if (isDay) {
|
|
680
|
+
const days = { ArrowLeft: -side, ArrowRight: side, ArrowUp: -7, ArrowDown: 7 };
|
|
681
|
+
if (event.key in days)
|
|
682
|
+
to = addDays(cursor, days[event.key]);
|
|
683
|
+
else if (event.key === 'Home')
|
|
684
|
+
to = startOfWeek(cursor, cfg.firstDay);
|
|
685
|
+
else if (event.key === 'End')
|
|
686
|
+
to = addDays(startOfWeek(cursor, cfg.firstDay), 6);
|
|
687
|
+
else if (event.key === 'PageUp')
|
|
688
|
+
to = addMonths(cursor, event.shiftKey ? -12 : -1);
|
|
689
|
+
else if (event.key === 'PageDown')
|
|
690
|
+
to = addMonths(cursor, event.shiftKey ? 12 : 1);
|
|
691
|
+
if (to && cfg.shape.mode === 'range' && pick.dates.length === 1)
|
|
692
|
+
hover = to;
|
|
693
|
+
}
|
|
694
|
+
else {
|
|
695
|
+
const unit = view === 'years' ? 12 : view === 'quarters' ? 3 : 1;
|
|
696
|
+
const columns = view === 'quarters' ? 2 : 3;
|
|
697
|
+
const cells = {
|
|
698
|
+
ArrowLeft: -side,
|
|
699
|
+
ArrowRight: side,
|
|
700
|
+
ArrowUp: -columns,
|
|
701
|
+
ArrowDown: columns,
|
|
702
|
+
PageUp: -12 / unit,
|
|
703
|
+
PageDown: 12 / unit,
|
|
704
|
+
};
|
|
705
|
+
if (event.key in cells) {
|
|
706
|
+
to = addMonths({ ...cursor, d: 1 }, cells[event.key] * unit);
|
|
707
|
+
shown = view === 'years' ? { y: to.y, m: 1, d: 1 } : { y: to.y, m: shown.m, d: 1 };
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
if (!to)
|
|
711
|
+
return;
|
|
712
|
+
event.preventDefault();
|
|
713
|
+
if (isDay)
|
|
714
|
+
move(to);
|
|
715
|
+
else {
|
|
716
|
+
cursor = to;
|
|
717
|
+
draw(true);
|
|
718
|
+
}
|
|
719
|
+
});
|
|
720
|
+
// ── The field and its panel ────────────────────────────────────────────────────────────────────────────────────
|
|
721
|
+
function place() {
|
|
722
|
+
if (!field || cfg.inline || !isOpen)
|
|
723
|
+
return;
|
|
724
|
+
const view0 = doc.defaultView;
|
|
725
|
+
const box = field.getBoundingClientRect();
|
|
726
|
+
const height = panel.offsetHeight;
|
|
727
|
+
const width = panel.offsetWidth;
|
|
728
|
+
const below = view0.innerHeight - box.bottom;
|
|
729
|
+
const top = below < height + 8 && box.top > below ? box.top - height - 4 : box.bottom + 4;
|
|
730
|
+
const left = cfg.rtl ? box.right - width : box.left;
|
|
731
|
+
panel.style.top = `${Math.max(4, top) + view0.scrollY}px`;
|
|
732
|
+
panel.style.left = `${Math.max(4, Math.min(left, view0.innerWidth - width - 4)) + view0.scrollX}px`;
|
|
733
|
+
}
|
|
734
|
+
function open() {
|
|
735
|
+
if (isOpen || cfg.inline)
|
|
736
|
+
return;
|
|
737
|
+
isOpen = true;
|
|
738
|
+
cursor = pick.dates[0] ?? now();
|
|
739
|
+
shown = { ...cursor, d: 1 };
|
|
740
|
+
view = baseView(cfg.shape.mode);
|
|
741
|
+
panel.hidden = false;
|
|
742
|
+
draw();
|
|
743
|
+
place();
|
|
744
|
+
field?.setAttribute('aria-expanded', 'true');
|
|
745
|
+
opts.onOpen?.();
|
|
746
|
+
}
|
|
747
|
+
function close() {
|
|
748
|
+
if (!isOpen || cfg.inline)
|
|
749
|
+
return;
|
|
750
|
+
isOpen = false;
|
|
751
|
+
hover = null;
|
|
752
|
+
if (cfg.shape.mode === 'range' && pick.dates.length === 1) {
|
|
753
|
+
pick = emptyPick();
|
|
754
|
+
publish(false);
|
|
755
|
+
}
|
|
756
|
+
panel.hidden = true;
|
|
757
|
+
field?.setAttribute('aria-expanded', 'false');
|
|
758
|
+
opts.onClose?.();
|
|
759
|
+
}
|
|
760
|
+
const outside = (event) => {
|
|
761
|
+
const path = event.composedPath();
|
|
762
|
+
if (isOpen && !path.includes(panel) && !path.includes(host))
|
|
763
|
+
close();
|
|
764
|
+
};
|
|
765
|
+
const onFieldKey = (event) => {
|
|
766
|
+
if (event.key === 'ArrowDown' || (event.key === 'Enter' && !isOpen)) {
|
|
767
|
+
event.preventDefault();
|
|
768
|
+
open();
|
|
769
|
+
panel.querySelector('[tabindex="0"], input, button')?.focus();
|
|
770
|
+
}
|
|
771
|
+
else if (event.key === 'Escape')
|
|
772
|
+
close();
|
|
773
|
+
};
|
|
774
|
+
const onFieldChange = () => {
|
|
775
|
+
// A date typed by hand: ISO text, or the form of the mode (2026-W49, 2026-Q4, 14:30)
|
|
776
|
+
const typed = readValue(field.value, cfg.shape);
|
|
777
|
+
if (typed.dates.length && !typed.dates.some(disabled)) {
|
|
778
|
+
pick = typed;
|
|
779
|
+
cursor = pick.dates[0];
|
|
780
|
+
shown = { ...cursor, d: 1 };
|
|
781
|
+
if (isOpen)
|
|
782
|
+
draw();
|
|
783
|
+
publish(true);
|
|
784
|
+
}
|
|
785
|
+
else
|
|
786
|
+
publish(false);
|
|
787
|
+
};
|
|
788
|
+
const replace = () => place();
|
|
789
|
+
function mount() {
|
|
790
|
+
if (field && !cfg.inline) {
|
|
791
|
+
panel.hidden = true;
|
|
792
|
+
(root.nodeType === 11 ? root : doc.body).append(panel);
|
|
793
|
+
field.setAttribute('autocomplete', 'off');
|
|
794
|
+
field.setAttribute('aria-haspopup', 'dialog');
|
|
795
|
+
field.setAttribute('aria-expanded', 'false');
|
|
796
|
+
if (opts.placeholder)
|
|
797
|
+
field.placeholder = opts.placeholder;
|
|
798
|
+
if (field.name && !hidden) {
|
|
799
|
+
// The form receives the ISO value; the field keeps the text the reader sees
|
|
800
|
+
hidden = el('input', { type: 'hidden', name: field.name });
|
|
801
|
+
field.removeAttribute('name');
|
|
802
|
+
field.after(hidden);
|
|
803
|
+
}
|
|
804
|
+
field.addEventListener('focus', open);
|
|
805
|
+
field.addEventListener('click', open);
|
|
806
|
+
field.addEventListener('keydown', onFieldKey);
|
|
807
|
+
field.addEventListener('change', onFieldChange);
|
|
808
|
+
doc.addEventListener('pointerdown', outside, true);
|
|
809
|
+
doc.defaultView.addEventListener('resize', replace);
|
|
810
|
+
doc.defaultView.addEventListener('scroll', replace, true);
|
|
811
|
+
}
|
|
812
|
+
else {
|
|
813
|
+
if (field)
|
|
814
|
+
field.after(panel);
|
|
815
|
+
else
|
|
816
|
+
host.append(panel);
|
|
817
|
+
draw();
|
|
818
|
+
}
|
|
819
|
+
publish(false);
|
|
820
|
+
}
|
|
821
|
+
const api = {
|
|
822
|
+
element: panel,
|
|
823
|
+
getValue: () => (cfg.period ? selection().value : writeValue(pick, cfg.shape)),
|
|
824
|
+
getSelection: selection,
|
|
825
|
+
setValue(value) {
|
|
826
|
+
pick = load(value);
|
|
827
|
+
view = baseView(cfg.shape.mode);
|
|
828
|
+
cursor = pick.dates[0] ?? now();
|
|
829
|
+
shown = { ...cursor, d: 1 };
|
|
830
|
+
if (isOpen || cfg.inline)
|
|
831
|
+
draw();
|
|
832
|
+
publish(false);
|
|
833
|
+
},
|
|
834
|
+
update(next) {
|
|
835
|
+
const before = cfg.shape;
|
|
836
|
+
const sameMode = (opts.mode ?? 'date') === (next.mode ?? opts.mode ?? 'date');
|
|
837
|
+
const current = cfg.period ? selection().value : writeValue(pick, before);
|
|
838
|
+
const value = 'value' in next ? next.value : sameMode ? current : null;
|
|
839
|
+
opts = { ...opts, ...next };
|
|
840
|
+
if ('compare' in next)
|
|
841
|
+
comparison = comparisonOf(opts.compare);
|
|
842
|
+
cfg = configure(opts, doc, !!field, gran);
|
|
843
|
+
// A value with its hour carries its offset: read in a new zone, it stays the same instant (Paris 14:30 is Tokyo 22:30)
|
|
844
|
+
pick = load(value);
|
|
845
|
+
cursor = pick.dates[0] ?? now();
|
|
846
|
+
shown = { ...cursor, d: 1 };
|
|
847
|
+
view = baseView(cfg.shape.mode);
|
|
848
|
+
if (isOpen || cfg.inline)
|
|
849
|
+
draw();
|
|
850
|
+
place();
|
|
851
|
+
publish(false);
|
|
852
|
+
},
|
|
853
|
+
open,
|
|
854
|
+
close,
|
|
855
|
+
toggle: () => (isOpen ? close() : open()),
|
|
856
|
+
destroy() {
|
|
857
|
+
panel.remove();
|
|
858
|
+
if (field) {
|
|
859
|
+
field.removeEventListener('focus', open);
|
|
860
|
+
field.removeEventListener('click', open);
|
|
861
|
+
field.removeEventListener('keydown', onFieldKey);
|
|
862
|
+
field.removeEventListener('change', onFieldChange);
|
|
863
|
+
if (hidden) {
|
|
864
|
+
field.name = hidden.name;
|
|
865
|
+
hidden.remove();
|
|
866
|
+
hidden = null;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
doc.removeEventListener('pointerdown', outside, true);
|
|
870
|
+
doc.defaultView?.removeEventListener('resize', replace);
|
|
871
|
+
doc.defaultView?.removeEventListener('scroll', replace, true);
|
|
872
|
+
},
|
|
873
|
+
};
|
|
874
|
+
mount();
|
|
875
|
+
return api;
|
|
876
|
+
}
|