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