@dile/utils 3.3.21 → 3.4.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.
|
@@ -1,8 +1,27 @@
|
|
|
1
|
-
import { css } from 'lit';
|
|
2
|
-
//import { LionCalendar } from '@lion/ui/components/calendar/src/LionCalendar.js';
|
|
1
|
+
import { css, html, svg, nothing } from 'lit';
|
|
3
2
|
import { LionCalendar } from '@lion/ui/calendar.js';
|
|
3
|
+
import { getMonthNames } from '@lion/ui/localize-no-side-effects.js';
|
|
4
|
+
import '@dile/ui/components/wheel-picker/wheel-picker.js';
|
|
5
|
+
|
|
6
|
+
const DEFAULT_YEAR_RANGE = 100;
|
|
7
|
+
// Lion normalizes dates with new Date(year, month, day), which maps years 0-99 to 1900-1999,
|
|
8
|
+
// so the earliest date that compares correctly is in year 100.
|
|
9
|
+
const EARLIEST_DATE = new Date(100, 0, 1);
|
|
10
|
+
const LATEST_DATE = new Date(8640000000000000);
|
|
11
|
+
const isValidDate = (date) => date instanceof Date && !Number.isNaN(date.getTime());
|
|
12
|
+
const checkIcon = svg`<path d="M9 16.17 4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>`;
|
|
4
13
|
|
|
5
14
|
export class DileCalendar extends LionCalendar {
|
|
15
|
+
static get properties() {
|
|
16
|
+
return {
|
|
17
|
+
...super.properties,
|
|
18
|
+
minYear: { type: Number },
|
|
19
|
+
maxYear: { type: Number },
|
|
20
|
+
monthYearAcceptLabel: { type: String },
|
|
21
|
+
__monthYearOpen: { type: Boolean, state: true },
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
6
25
|
static get styles() {
|
|
7
26
|
return [
|
|
8
27
|
super.styles,
|
|
@@ -23,7 +42,7 @@ export class DileCalendar extends LionCalendar {
|
|
|
23
42
|
align-items: center;
|
|
24
43
|
min-height: auto;
|
|
25
44
|
font-size: 0.9rem;
|
|
26
|
-
|
|
45
|
+
line-height: 1.36;
|
|
27
46
|
}
|
|
28
47
|
@media(min-width: 400px) {
|
|
29
48
|
.calendar__navigation h2 {
|
|
@@ -36,6 +55,14 @@ export class DileCalendar extends LionCalendar {
|
|
|
36
55
|
background-color: var(--dile-calendar-day-background-color, rgba(90, 90, 90, 0.08));
|
|
37
56
|
color: var(--dile-calendar-day-text-color, #303030);
|
|
38
57
|
border-radius: var(--dile-calendar-day-border-radius, 2px);
|
|
58
|
+
font: inherit;
|
|
59
|
+
font-weight: normal;
|
|
60
|
+
line-height: 1;
|
|
61
|
+
}
|
|
62
|
+
.calendar__day-button__text {
|
|
63
|
+
font: inherit;
|
|
64
|
+
font-weight: normal;
|
|
65
|
+
line-height: 1;
|
|
39
66
|
}
|
|
40
67
|
.calendar__day-button[today] {
|
|
41
68
|
background-color: var(--dile-calendar-today-background-color, rgba(90, 90, 90, 0.20));
|
|
@@ -67,7 +94,365 @@ export class DileCalendar extends LionCalendar {
|
|
|
67
94
|
min-height: var(--dile-calendar-day-size, 36px);
|
|
68
95
|
}
|
|
69
96
|
}
|
|
97
|
+
.calendar__navigation-toggle {
|
|
98
|
+
min-height: 30px;
|
|
99
|
+
padding: 0 4px;
|
|
100
|
+
border: 0;
|
|
101
|
+
border-bottom: 1px dotted currentColor;
|
|
102
|
+
border-radius: 0;
|
|
103
|
+
background: transparent;
|
|
104
|
+
color: var(--dile-calendar-day-text-color, #303030);
|
|
105
|
+
font: inherit;
|
|
106
|
+
text-align: center;
|
|
107
|
+
cursor: pointer;
|
|
108
|
+
}
|
|
109
|
+
.calendar__navigation-toggle[aria-expanded="true"] {
|
|
110
|
+
border-bottom-style: solid;
|
|
111
|
+
}
|
|
112
|
+
.calendar__navigation-toggle:focus-visible {
|
|
113
|
+
outline: 2px solid var(--dile-input-focus-border-color, var(--dile-link-color, #6af));
|
|
114
|
+
outline-offset: 2px;
|
|
115
|
+
}
|
|
116
|
+
.calendar-body {
|
|
117
|
+
position: relative;
|
|
118
|
+
}
|
|
119
|
+
.calendar-body.month-year-open > :not(.month-year-layer) {
|
|
120
|
+
visibility: hidden;
|
|
121
|
+
}
|
|
122
|
+
.month-year-layer {
|
|
123
|
+
position: absolute;
|
|
124
|
+
inset: 0;
|
|
125
|
+
display: flex;
|
|
126
|
+
flex-direction: column;
|
|
127
|
+
align-items: stretch;
|
|
128
|
+
gap: 0.25rem;
|
|
129
|
+
padding-bottom: 12px;
|
|
130
|
+
box-sizing: border-box;
|
|
131
|
+
--dile-wheel-picker-item-height: var(--dile-calendar-wheel-item-height, 36px);
|
|
132
|
+
--dile-wheel-picker-font-size: var(--dile-calendar-wheel-font-size, 1.1rem);
|
|
133
|
+
--dile-wheel-picker-text-color: var(--dile-calendar-wheel-text-color, color-mix(in srgb, var(--dile-calendar-day-text-color, #303030) 60%, transparent));
|
|
134
|
+
--dile-wheel-picker-selected-text-color: var(--dile-calendar-wheel-selected-text-color, var(--dile-calendar-day-text-color, #303030));
|
|
135
|
+
--dile-wheel-picker-band-background-color: var(--dile-calendar-wheel-band-background-color, var(--dile-calendar-day-background-color, rgba(90, 90, 90, 0.08)));
|
|
136
|
+
}
|
|
137
|
+
.month-year-columns {
|
|
138
|
+
flex: 1;
|
|
139
|
+
display: flex;
|
|
140
|
+
align-items: center;
|
|
141
|
+
gap: 0.5rem;
|
|
142
|
+
}
|
|
143
|
+
.month-year-accept {
|
|
144
|
+
flex-shrink: 0;
|
|
145
|
+
align-self: center;
|
|
146
|
+
display: inline-flex;
|
|
147
|
+
align-items: center;
|
|
148
|
+
justify-content: center;
|
|
149
|
+
width: 28px;
|
|
150
|
+
height: 28px;
|
|
151
|
+
padding: 0;
|
|
152
|
+
border: 1px solid var(--dile-calendar-month-year-accept-border-color, color-mix(in srgb, currentColor 35%, transparent));
|
|
153
|
+
border-radius: 50%;
|
|
154
|
+
background: transparent;
|
|
155
|
+
color: var(--dile-calendar-month-year-accept-color, var(--dile-calendar-nav-buttons-color, #303030));
|
|
156
|
+
cursor: pointer;
|
|
157
|
+
}
|
|
158
|
+
.month-year-accept:hover {
|
|
159
|
+
background-color: var(--dile-calendar-day-background-color, rgba(90, 90, 90, 0.08));
|
|
160
|
+
}
|
|
161
|
+
.month-year-accept:focus-visible {
|
|
162
|
+
outline: 2px solid var(--dile-input-focus-border-color, var(--dile-link-color, #6af));
|
|
163
|
+
outline-offset: 2px;
|
|
164
|
+
}
|
|
165
|
+
.month-year-accept svg {
|
|
166
|
+
width: 18px;
|
|
167
|
+
height: 18px;
|
|
168
|
+
fill: currentColor;
|
|
169
|
+
}
|
|
170
|
+
.month-wheel {
|
|
171
|
+
flex: 3;
|
|
172
|
+
}
|
|
173
|
+
.year-wheel {
|
|
174
|
+
flex: 2;
|
|
175
|
+
}
|
|
70
176
|
`
|
|
71
177
|
];
|
|
72
178
|
}
|
|
73
|
-
|
|
179
|
+
|
|
180
|
+
constructor() {
|
|
181
|
+
super();
|
|
182
|
+
this.minDate = new Date(EARLIEST_DATE);
|
|
183
|
+
this.monthYearAcceptLabel = 'OK';
|
|
184
|
+
this.__monthYearOpen = false;
|
|
185
|
+
this.__monthItemsCache = { key: '', items: [] };
|
|
186
|
+
this.__yearItemsCache = { key: '', items: [] };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
requestUpdate(name, oldValue, options) {
|
|
190
|
+
// Lion rebuilds the days as soon as these properties change, so empty
|
|
191
|
+
// or invalid values are replaced by the defaults before that happens.
|
|
192
|
+
if (name === 'minDate' && (!isValidDate(this.minDate) || this.minDate < EARLIEST_DATE)) {
|
|
193
|
+
this.minDate = new Date(EARLIEST_DATE);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (name === 'maxDate' && !isValidDate(this.maxDate)) {
|
|
197
|
+
this.maxDate = new Date(LATEST_DATE);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (name === 'disableDates' && typeof this.disableDates !== 'function') {
|
|
201
|
+
this.disableDates = () => false;
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
super.requestUpdate(name, oldValue, options);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
willUpdate(changedProperties) {
|
|
208
|
+
super.willUpdate?.(changedProperties);
|
|
209
|
+
const limitsChanged = ['minDate', 'maxDate', 'disableDates'].some(name => changedProperties.has(name));
|
|
210
|
+
if (this.hasUpdated && limitsChanged && !this.__isEnabledDate(this.centralDate)) {
|
|
211
|
+
this.centralDate = this.findNearestEnabledDate(this.centralDate);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
render() {
|
|
216
|
+
return html`
|
|
217
|
+
<div class="calendar" role="application" @keydown="${this.__onCalendarKeydown}">
|
|
218
|
+
${this.__renderNavigation()}
|
|
219
|
+
<div class="calendar-body ${this.__monthYearOpen ? 'month-year-open' : ''}">
|
|
220
|
+
${this.__renderData()}
|
|
221
|
+
${this.__monthYearOpen ? this.__renderMonthYearLayer() : nothing}
|
|
222
|
+
</div>
|
|
223
|
+
</div>
|
|
224
|
+
`;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Shows the month and year selection layer instead of the days grid.
|
|
229
|
+
* @param {'month'|'year'} focusColumn column that receives the focus
|
|
230
|
+
*/
|
|
231
|
+
async openMonthYearPicker(focusColumn = 'year') {
|
|
232
|
+
this.__monthYearOpen = true;
|
|
233
|
+
await this.updateComplete;
|
|
234
|
+
const wheel = this.shadowRoot.querySelector(focusColumn === 'month' ? '.month-wheel' : '.year-wheel');
|
|
235
|
+
await wheel?.updateComplete;
|
|
236
|
+
wheel?.focus();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Hides the month and year selection layer and shows the days grid again.
|
|
241
|
+
*/
|
|
242
|
+
closeMonthYearPicker() {
|
|
243
|
+
this.__monthYearOpen = false;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Shows the month of the given date, or of the nearest enabled date.
|
|
248
|
+
* @param {Date} [date] defaults to today
|
|
249
|
+
*/
|
|
250
|
+
goToDate(date = new Date()) {
|
|
251
|
+
const target = new Date(date);
|
|
252
|
+
this.centralDate = this.__isEnabledDate(target) ? target : this.findNearestEnabledDate(target);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
get monthYearPickerOpened() {
|
|
256
|
+
return this.__monthYearOpen;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
__renderNavigation() {
|
|
260
|
+
const monthNames = this.__monthNames();
|
|
261
|
+
const monthIndex = this.centralDate.getMonth();
|
|
262
|
+
const month = monthNames[monthIndex];
|
|
263
|
+
const year = this.centralDate.getFullYear();
|
|
264
|
+
const nextMonth = monthNames[(monthIndex + 1) % 12];
|
|
265
|
+
const previousMonth = monthNames[(monthIndex + 11) % 12];
|
|
266
|
+
const nextMonthYear = monthIndex === 11 ? year + 1 : year;
|
|
267
|
+
const previousMonthYear = monthIndex === 0 ? year - 1 : year;
|
|
268
|
+
return html`
|
|
269
|
+
<div class="calendar__navigation">
|
|
270
|
+
<div class="calendar__navigation__month">
|
|
271
|
+
${this.__renderPreviousButton('Month', previousMonth, previousMonthYear)}
|
|
272
|
+
<h2 class="calendar__navigation-heading" id="month" aria-atomic="true">
|
|
273
|
+
${this.__renderToggle('month', month, this.__fieldLabel('month'))}
|
|
274
|
+
</h2>
|
|
275
|
+
${this.__renderNextButton('Month', nextMonth, nextMonthYear)}
|
|
276
|
+
</div>
|
|
277
|
+
<div class="calendar__navigation__year">
|
|
278
|
+
${this.__renderPreviousButton('FullYear', month, year - 1)}
|
|
279
|
+
<h2 class="calendar__navigation-heading" id="year" aria-atomic="true">
|
|
280
|
+
${this.__renderToggle('year', year, this.__fieldLabel('year'))}
|
|
281
|
+
</h2>
|
|
282
|
+
${this.__renderNextButton('FullYear', month, year + 1)}
|
|
283
|
+
</div>
|
|
284
|
+
</div>
|
|
285
|
+
`;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
__renderToggle(column, text, fieldLabel) {
|
|
289
|
+
return html`
|
|
290
|
+
<button
|
|
291
|
+
type="button"
|
|
292
|
+
class="calendar__navigation-toggle calendar__navigation-toggle--${column}"
|
|
293
|
+
aria-expanded="${this.__monthYearOpen ? 'true' : 'false'}"
|
|
294
|
+
aria-controls="month-year-layer"
|
|
295
|
+
aria-label="${fieldLabel}: ${text}"
|
|
296
|
+
title="${fieldLabel}"
|
|
297
|
+
@click="${() => this.__toggleMonthYear(column)}"
|
|
298
|
+
>${text}</button>
|
|
299
|
+
`;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
__renderMonthYearLayer() {
|
|
303
|
+
return html`
|
|
304
|
+
<div
|
|
305
|
+
id="month-year-layer"
|
|
306
|
+
class="month-year-layer"
|
|
307
|
+
@dile-wheel-picker-changed="${this.__stopEvent}"
|
|
308
|
+
@element-changed="${this.__stopEvent}"
|
|
309
|
+
>
|
|
310
|
+
<div class="month-year-columns">
|
|
311
|
+
<dile-wheel-picker
|
|
312
|
+
class="month-wheel"
|
|
313
|
+
label="${this.__fieldLabel('month')}"
|
|
314
|
+
.items="${this.__monthItems()}"
|
|
315
|
+
.value="${this.centralDate.getMonth()}"
|
|
316
|
+
@dile-wheel-picker-changed="${this.__onMonthChanged}"
|
|
317
|
+
></dile-wheel-picker>
|
|
318
|
+
<dile-wheel-picker
|
|
319
|
+
class="year-wheel"
|
|
320
|
+
label="${this.__fieldLabel('year')}"
|
|
321
|
+
.items="${this.__yearItems()}"
|
|
322
|
+
.value="${this.centralDate.getFullYear()}"
|
|
323
|
+
@dile-wheel-picker-changed="${this.__onYearChanged}"
|
|
324
|
+
></dile-wheel-picker>
|
|
325
|
+
</div>
|
|
326
|
+
<button
|
|
327
|
+
type="button"
|
|
328
|
+
class="month-year-accept"
|
|
329
|
+
aria-label="${this.monthYearAcceptLabel}"
|
|
330
|
+
title="${this.monthYearAcceptLabel}"
|
|
331
|
+
@click="${this.__acceptMonthYear}"
|
|
332
|
+
><svg viewBox="0 0 24 24" aria-hidden="true">${checkIcon}</svg></button>
|
|
333
|
+
</div>
|
|
334
|
+
`;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
__monthNames() {
|
|
338
|
+
return getMonthNames({ locale: this.__getLocale() });
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
__fieldLabel(field) {
|
|
342
|
+
const fallback = field === 'year' ? 'Year' : 'Month';
|
|
343
|
+
try {
|
|
344
|
+
const locale = this.__getLocale() || document.documentElement.lang || navigator.language || 'en';
|
|
345
|
+
return new Intl.DisplayNames([locale], { type: 'dateTimeField' }).of(field) || fallback;
|
|
346
|
+
} catch (e) {
|
|
347
|
+
return fallback;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
__monthItems() {
|
|
352
|
+
const year = this.centralDate.getFullYear();
|
|
353
|
+
const names = this.__monthNames();
|
|
354
|
+
const key = [names.join(), year, this.minDate?.getTime(), this.maxDate?.getTime()].join('|');
|
|
355
|
+
if (this.__monthItemsCache.key !== key) {
|
|
356
|
+
const items = names.map((label, month) => {
|
|
357
|
+
const firstDay = new Date(this.centralDate);
|
|
358
|
+
firstDay.setFullYear(year, month, 1);
|
|
359
|
+
firstDay.setHours(0, 0, 0, 0);
|
|
360
|
+
const lastDay = new Date(firstDay);
|
|
361
|
+
lastDay.setFullYear(year, month + 1, 0);
|
|
362
|
+
lastDay.setHours(23, 59, 59, 999);
|
|
363
|
+
const disabled = Boolean((this.minDate && lastDay < this.minDate)
|
|
364
|
+
|| (this.maxDate && firstDay > this.maxDate));
|
|
365
|
+
return { value: month, label, disabled };
|
|
366
|
+
});
|
|
367
|
+
this.__monthItemsCache = { key, items };
|
|
368
|
+
}
|
|
369
|
+
return this.__monthItemsCache.items;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
__yearRange() {
|
|
373
|
+
const currentYear = new Date().getFullYear();
|
|
374
|
+
let from = Number.isFinite(this.minYear) ? this.minYear : currentYear - DEFAULT_YEAR_RANGE;
|
|
375
|
+
let to = Number.isFinite(this.maxYear) ? this.maxYear : currentYear + DEFAULT_YEAR_RANGE;
|
|
376
|
+
if (this.minDate) {
|
|
377
|
+
from = Math.max(from, this.minDate.getFullYear());
|
|
378
|
+
}
|
|
379
|
+
if (this.maxDate) {
|
|
380
|
+
to = Math.min(to, this.maxDate.getFullYear());
|
|
381
|
+
}
|
|
382
|
+
const centralYear = this.centralDate.getFullYear();
|
|
383
|
+
return { from: Math.min(from, centralYear), to: Math.max(to, centralYear) };
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
__yearItems() {
|
|
387
|
+
const { from, to } = this.__yearRange();
|
|
388
|
+
const key = `${from}|${to}`;
|
|
389
|
+
if (this.__yearItemsCache.key !== key) {
|
|
390
|
+
const items = [];
|
|
391
|
+
for (let year = from; year <= to; year++) {
|
|
392
|
+
items.push(year);
|
|
393
|
+
}
|
|
394
|
+
this.__yearItemsCache = { key, items };
|
|
395
|
+
}
|
|
396
|
+
return this.__yearItemsCache.items;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
__toggleMonthYear(column) {
|
|
400
|
+
if (this.__monthYearOpen) {
|
|
401
|
+
this.closeMonthYearPicker();
|
|
402
|
+
} else {
|
|
403
|
+
this.openMonthYearPicker(column);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
__acceptMonthYear() {
|
|
408
|
+
this.closeMonthYearPicker();
|
|
409
|
+
this.updateComplete.then(() => {
|
|
410
|
+
this.shadowRoot.querySelector('.calendar__navigation-toggle--year')?.focus();
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
__stopEvent(e) {
|
|
415
|
+
e.stopPropagation();
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
__onMonthChanged(e) {
|
|
419
|
+
this.__setCentralMonthYear(this.centralDate.getFullYear(), Number(e.detail.value));
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
__onYearChanged(e) {
|
|
423
|
+
this.__setCentralMonthYear(Number(e.detail.value), this.centralDate.getMonth());
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
__setCentralMonthYear(year, month) {
|
|
427
|
+
const lastDayOfMonth = new Date(this.centralDate);
|
|
428
|
+
lastDayOfMonth.setFullYear(year, month + 1, 0);
|
|
429
|
+
const date = new Date(this.centralDate);
|
|
430
|
+
date.setFullYear(year, month, Math.min(this.centralDate.getDate(), lastDayOfMonth.getDate()));
|
|
431
|
+
if (Number.isNaN(date.getTime())) {
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (this.minDate && date < this.minDate) {
|
|
435
|
+
this.centralDate = new Date(this.minDate);
|
|
436
|
+
} else if (this.maxDate && date > this.maxDate) {
|
|
437
|
+
this.centralDate = new Date(this.maxDate);
|
|
438
|
+
} else {
|
|
439
|
+
this.centralDate = date;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
__onCalendarKeydown(e) {
|
|
444
|
+
if (!this.__monthYearOpen) {
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
const inLayer = e.composedPath().some(node => node.id === 'month-year-layer');
|
|
448
|
+
if (e.key === 'Escape' || (e.key === 'Enter' && inLayer)) {
|
|
449
|
+
e.preventDefault();
|
|
450
|
+
e.stopPropagation();
|
|
451
|
+
const column = e.composedPath().some(node => node.classList?.contains('month-wheel')) ? 'month' : 'year';
|
|
452
|
+
this.closeMonthYearPicker();
|
|
453
|
+
this.updateComplete.then(() => {
|
|
454
|
+
this.shadowRoot.querySelector(`.calendar__navigation-toggle--${column}`)?.focus();
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
@@ -6,8 +6,105 @@ import '@dile/ui/components/menu-overlay/menu-overlay.js';
|
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
8
|
formatDate,
|
|
9
|
+
parseDate,
|
|
9
10
|
} from '@lion/ui/localize.js';
|
|
10
11
|
|
|
12
|
+
function deepActiveElement() {
|
|
13
|
+
let active = document.activeElement;
|
|
14
|
+
while (active?.shadowRoot?.activeElement) {
|
|
15
|
+
active = active.shadowRoot.activeElement;
|
|
16
|
+
}
|
|
17
|
+
return active;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Converts the minDate / maxDate values of the datepicker to a Date.
|
|
22
|
+
* Accepts Date objects and strings: "YYYY-MM-DD" is read as a local date.
|
|
23
|
+
* @param {Date|string} value
|
|
24
|
+
* @returns {Date|undefined}
|
|
25
|
+
*/
|
|
26
|
+
export function toCalendarDate(value) {
|
|
27
|
+
if (value === undefined || value === null || value === '') {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
if (value instanceof Date) {
|
|
31
|
+
return Number.isNaN(value.getTime()) ? undefined : value;
|
|
32
|
+
}
|
|
33
|
+
const isoDate = parseIsoDate(value);
|
|
34
|
+
if (isoDate !== null) {
|
|
35
|
+
return isoDate;
|
|
36
|
+
}
|
|
37
|
+
const date = new Date(value);
|
|
38
|
+
return Number.isNaN(date.getTime()) ? undefined : date;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Reads the date part of "YYYY-MM-DD" strings (an optional time after a space or a "T" is ignored)
|
|
43
|
+
* as a local date. Returns null when the string has another format and undefined when the date doesn't exist.
|
|
44
|
+
* @param {string} value
|
|
45
|
+
* @returns {Date|undefined|null}
|
|
46
|
+
*/
|
|
47
|
+
function parseIsoDate(value) {
|
|
48
|
+
const match = /^(\d{1,6})-(\d{1,2})-(\d{1,2})(?:[ T].*)?$/.exec(String(value).trim());
|
|
49
|
+
if (!match) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
const [year, month, day] = match.slice(1).map(Number);
|
|
53
|
+
const date = new Date(2000, 0, 1);
|
|
54
|
+
date.setFullYear(year, month - 1, day);
|
|
55
|
+
return date.getMonth() === month - 1 && date.getDate() === day ? date : undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Converts the text value of the datepicker to a Date: "YYYY-MM-DD" values
|
|
60
|
+
* or dates written in the format of the locale (the page locale by default).
|
|
61
|
+
* @param {string} value
|
|
62
|
+
* @param {string} [locale]
|
|
63
|
+
* @returns {Date|undefined}
|
|
64
|
+
*/
|
|
65
|
+
export function valueToDate(value, locale) {
|
|
66
|
+
if (value === undefined || value === null || String(value).trim() === '') {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
const isoDate = parseIsoDate(value);
|
|
70
|
+
return isoDate !== null ? isoDate : parseDate(String(value).trim(), locale ? { locale } : undefined);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const VALIDATION_MESSAGES = {
|
|
74
|
+
en: {
|
|
75
|
+
invalid: 'Enter a valid date, for example {example}.',
|
|
76
|
+
beforeMin: 'The date must be on or after {date}.',
|
|
77
|
+
afterMax: 'The date must be on or before {date}.',
|
|
78
|
+
disabled: 'This date is not available.',
|
|
79
|
+
},
|
|
80
|
+
es: {
|
|
81
|
+
invalid: 'Introduce una fecha válida, por ejemplo {example}.',
|
|
82
|
+
beforeMin: 'La fecha debe ser igual o posterior al {date}.',
|
|
83
|
+
afterMax: 'La fecha debe ser igual o anterior al {date}.',
|
|
84
|
+
disabled: 'Esta fecha no está disponible.',
|
|
85
|
+
},
|
|
86
|
+
ca: {
|
|
87
|
+
invalid: 'Introdueix una data vàlida, per exemple {example}.',
|
|
88
|
+
beforeMin: 'La data ha de ser igual o posterior al {date}.',
|
|
89
|
+
afterMax: 'La data ha de ser igual o anterior al {date}.',
|
|
90
|
+
disabled: 'Aquesta data no està disponible.',
|
|
91
|
+
},
|
|
92
|
+
fr: {
|
|
93
|
+
invalid: 'Saisissez une date valide, par exemple {example}.',
|
|
94
|
+
beforeMin: 'La date doit être le {date} ou après.',
|
|
95
|
+
afterMax: 'La date doit être le {date} ou avant.',
|
|
96
|
+
disabled: 'Cette date n’est pas disponible.',
|
|
97
|
+
},
|
|
98
|
+
de: {
|
|
99
|
+
invalid: 'Geben Sie ein gültiges Datum ein, z. B. {example}.',
|
|
100
|
+
beforeMin: 'Das Datum muss am oder nach dem {date} liegen.',
|
|
101
|
+
afterMax: 'Das Datum muss am oder vor dem {date} liegen.',
|
|
102
|
+
disabled: 'Dieses Datum ist nicht verfügbar.',
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const dayKey = (date) => date.getFullYear() * 10000 + date.getMonth() * 100 + date.getDate();
|
|
107
|
+
|
|
11
108
|
export class DileDatepicker extends DileCloseOnEscPressed(DileInputIcon) {
|
|
12
109
|
static get styles() {
|
|
13
110
|
return [
|
|
@@ -61,11 +158,20 @@ export class DileDatepicker extends DileCloseOnEscPressed(DileInputIcon) {
|
|
|
61
158
|
return {
|
|
62
159
|
...super.properties,
|
|
63
160
|
firstDayOfWeek: { type: Number },
|
|
161
|
+
weekdayHeaderNotation: { type: String },
|
|
162
|
+
locale: { type: String },
|
|
64
163
|
horizontalAlign: { type: String },
|
|
65
164
|
verticalAlign: { type: String },
|
|
66
165
|
moveTop: { type: Number },
|
|
67
166
|
moveLeft: { type: Number },
|
|
68
167
|
opened: { type: Boolean },
|
|
168
|
+
minYear: { type: Number },
|
|
169
|
+
maxYear: { type: Number },
|
|
170
|
+
minDate: {},
|
|
171
|
+
maxDate: {},
|
|
172
|
+
disableDates: { attribute: false },
|
|
173
|
+
validateDate: { type: Boolean },
|
|
174
|
+
validationMessages: { type: Object },
|
|
69
175
|
};
|
|
70
176
|
}
|
|
71
177
|
|
|
@@ -73,6 +179,9 @@ export class DileDatepicker extends DileCloseOnEscPressed(DileInputIcon) {
|
|
|
73
179
|
super();
|
|
74
180
|
this.icon = 'lucide.calendar';
|
|
75
181
|
this.firstDayOfWeek = 0;
|
|
182
|
+
this.weekdayHeaderNotation = 'short';
|
|
183
|
+
this.validateDate = false;
|
|
184
|
+
this.validationMessages = {};
|
|
76
185
|
this.horizontalAlign = 'under_right';
|
|
77
186
|
this.verticalAlign = 'center';
|
|
78
187
|
this.moveTop = 0;
|
|
@@ -80,6 +189,16 @@ export class DileDatepicker extends DileCloseOnEscPressed(DileInputIcon) {
|
|
|
80
189
|
this.opened = false;
|
|
81
190
|
}
|
|
82
191
|
|
|
192
|
+
willUpdate(changedProperties) {
|
|
193
|
+
super.willUpdate?.(changedProperties);
|
|
194
|
+
if (changedProperties.has('minDate')) {
|
|
195
|
+
this._calendarMinDate = toCalendarDate(this.minDate);
|
|
196
|
+
}
|
|
197
|
+
if (changedProperties.has('maxDate')) {
|
|
198
|
+
this._calendarMaxDate = toCalendarDate(this.maxDate);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
83
202
|
renderIconButton() {
|
|
84
203
|
if (this.disabled) {
|
|
85
204
|
return super.renderIconButton();
|
|
@@ -92,6 +211,7 @@ export class DileDatepicker extends DileCloseOnEscPressed(DileInputIcon) {
|
|
|
92
211
|
horizontalAlign="${this.horizontalAlign}"
|
|
93
212
|
id="menu"
|
|
94
213
|
@overlay-opened="${this.overlayOpenedHandler}"
|
|
214
|
+
@overlay-ready="${this.overlayReadyHandler}"
|
|
95
215
|
@overlay-closed="${this.overlayClosedHandler}"
|
|
96
216
|
>
|
|
97
217
|
<div slot="trigger" class="trigger-container">
|
|
@@ -104,22 +224,151 @@ export class DileDatepicker extends DileCloseOnEscPressed(DileInputIcon) {
|
|
|
104
224
|
`;
|
|
105
225
|
}
|
|
106
226
|
|
|
227
|
+
get calendar() {
|
|
228
|
+
return this.shadowRoot.querySelector('dile-calendar');
|
|
229
|
+
}
|
|
230
|
+
|
|
107
231
|
overlayOpenedHandler() {
|
|
108
232
|
this.opened = true;
|
|
233
|
+
this._focusBeforeOpen = deepActiveElement();
|
|
234
|
+
this.syncCalendarWithValue();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async overlayReadyHandler() {
|
|
238
|
+
const calendar = this.calendar;
|
|
239
|
+
if (!calendar) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
await calendar.updateComplete;
|
|
243
|
+
calendar.focusCentralDate();
|
|
109
244
|
}
|
|
110
245
|
|
|
111
246
|
overlayClosedHandler() {
|
|
112
247
|
this.opened = false;
|
|
248
|
+
this.calendar?.closeMonthYearPicker();
|
|
249
|
+
if (this.shadowRoot.activeElement?.closest('.calendar')) {
|
|
250
|
+
const previous = this._focusBeforeOpen;
|
|
251
|
+
const target = previous?.isConnected && previous !== document.body && !this.calendar?.contains(previous) && !this.calendar?.shadowRoot.contains(previous)
|
|
252
|
+
? previous
|
|
253
|
+
: this.shadowRoot.querySelector('.input-icon-btn');
|
|
254
|
+
target?.focus();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Marks the date of the current value in the calendar and shows its month.
|
|
260
|
+
* Without a valid value, the calendar shows today (or the nearest enabled date).
|
|
261
|
+
*/
|
|
262
|
+
syncCalendarWithValue() {
|
|
263
|
+
const calendar = this.calendar;
|
|
264
|
+
if (!calendar) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const date = valueToDate(this.value, this.locale);
|
|
268
|
+
calendar.selectedDate = date;
|
|
269
|
+
calendar.goToDate(date);
|
|
113
270
|
}
|
|
114
271
|
|
|
115
272
|
showDate(e) {
|
|
116
|
-
|
|
117
|
-
this.
|
|
273
|
+
this.value = this.dateToValue(e.detail.selectedDate);
|
|
274
|
+
if (this.validateDate) {
|
|
275
|
+
this.validate();
|
|
276
|
+
}
|
|
118
277
|
const menu = this.shadowRoot.getElementById('menu');
|
|
119
278
|
if (menu) {
|
|
120
279
|
menu.close();
|
|
121
280
|
}
|
|
122
281
|
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Text written in the input for a date selected in the calendar.
|
|
285
|
+
* @param {Date} date
|
|
286
|
+
* @returns {string}
|
|
287
|
+
*/
|
|
288
|
+
dateToValue(date) {
|
|
289
|
+
return formatDate(date, this.locale ? { locale: this.locale } : undefined);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
doBlur(e) {
|
|
293
|
+
super.doBlur?.(e);
|
|
294
|
+
if (!this.validateDate) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (this.validate()) {
|
|
298
|
+
const { date } = this.getDateValidation();
|
|
299
|
+
const normalized = date ? this.dateToValue(date) : undefined;
|
|
300
|
+
if (normalized && normalized !== this.value) {
|
|
301
|
+
this.value = normalized;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Checks the current value without changing the component state.
|
|
308
|
+
* An empty value is valid (use your form validation to make it required).
|
|
309
|
+
* @param {string} [value]
|
|
310
|
+
* @returns {{valid: boolean, reason?: 'invalid'|'beforeMin'|'afterMax'|'disabled', date?: Date}}
|
|
311
|
+
*/
|
|
312
|
+
getDateValidation(value = this.value) {
|
|
313
|
+
if (value === undefined || value === null || String(value).trim() === '') {
|
|
314
|
+
return { valid: true };
|
|
315
|
+
}
|
|
316
|
+
const date = valueToDate(value, this.locale);
|
|
317
|
+
if (!date) {
|
|
318
|
+
return { valid: false, reason: 'invalid' };
|
|
319
|
+
}
|
|
320
|
+
const minDate = toCalendarDate(this.minDate);
|
|
321
|
+
const maxDate = toCalendarDate(this.maxDate);
|
|
322
|
+
if (minDate && dayKey(date) < dayKey(minDate)) {
|
|
323
|
+
return { valid: false, reason: 'beforeMin', date };
|
|
324
|
+
}
|
|
325
|
+
if (maxDate && dayKey(date) > dayKey(maxDate)) {
|
|
326
|
+
return { valid: false, reason: 'afterMax', date };
|
|
327
|
+
}
|
|
328
|
+
if (typeof this.disableDates === 'function' && this.disableDates(date)) {
|
|
329
|
+
return { valid: false, reason: 'disabled', date };
|
|
330
|
+
}
|
|
331
|
+
return { valid: true, date };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Validates the current value, showing or removing the error message.
|
|
336
|
+
* @returns {boolean} true when the value is valid
|
|
337
|
+
*/
|
|
338
|
+
validate() {
|
|
339
|
+
const result = this.getDateValidation();
|
|
340
|
+
if (result.valid) {
|
|
341
|
+
if (this._dateErrorShown) {
|
|
342
|
+
this.errored = Boolean(this._stateBeforeDateError.errored);
|
|
343
|
+
this.message = this._stateBeforeDateError.message ?? '';
|
|
344
|
+
this._dateErrorShown = false;
|
|
345
|
+
}
|
|
346
|
+
this.internals?.setValidity({});
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
if (!this._dateErrorShown) {
|
|
350
|
+
this._stateBeforeDateError = { errored: this.errored, message: this.message };
|
|
351
|
+
this._dateErrorShown = true;
|
|
352
|
+
}
|
|
353
|
+
this.errored = true;
|
|
354
|
+
this.message = this.getValidationMessage(result.reason);
|
|
355
|
+
this.internals?.setValidity({ customError: true }, this.message, this.el);
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* @param {'invalid'|'beforeMin'|'afterMax'|'disabled'} reason
|
|
361
|
+
* @returns {string}
|
|
362
|
+
*/
|
|
363
|
+
getValidationMessage(reason) {
|
|
364
|
+
const language = (this.locale || document.documentElement.lang || 'en').slice(0, 2).toLowerCase();
|
|
365
|
+
const messages = { ...VALIDATION_MESSAGES.en, ...VALIDATION_MESSAGES[language], ...this.validationMessages };
|
|
366
|
+
const limit = reason === 'beforeMin' ? toCalendarDate(this.minDate) : toCalendarDate(this.maxDate);
|
|
367
|
+
const example = new Date(new Date().getFullYear(), 11, 31);
|
|
368
|
+
return String(messages[reason] || '')
|
|
369
|
+
.replace('{date}', limit ? this.dateToValue(limit) : '')
|
|
370
|
+
.replace('{example}', this.dateToValue(example));
|
|
371
|
+
}
|
|
123
372
|
|
|
124
373
|
close() {
|
|
125
374
|
const menu = this.shadowRoot.getElementById('menu');
|
|
@@ -144,6 +393,13 @@ export class DileDatepicker extends DileCloseOnEscPressed(DileInputIcon) {
|
|
|
144
393
|
<dile-calendar
|
|
145
394
|
class="demo-calendar"
|
|
146
395
|
.firstDayOfWeek="${this.firstDayOfWeek}"
|
|
396
|
+
.weekdayHeaderNotation="${this.weekdayHeaderNotation}"
|
|
397
|
+
.locale="${this.locale}"
|
|
398
|
+
.minYear="${this.minYear}"
|
|
399
|
+
.maxYear="${this.maxYear}"
|
|
400
|
+
.minDate="${this._calendarMinDate}"
|
|
401
|
+
.maxDate="${this._calendarMaxDate}"
|
|
402
|
+
.disableDates="${this.disableDates}"
|
|
147
403
|
@user-selected-date-changed=${this.showDate}
|
|
148
404
|
></dile-calendar>
|
|
149
405
|
`;
|
|
@@ -98,6 +98,13 @@ export class DileDatetimepicker extends DileDatepicker {
|
|
|
98
98
|
<dile-calendar
|
|
99
99
|
class="demo-calendar"
|
|
100
100
|
.firstDayOfWeek="${this.firstDayOfWeek}"
|
|
101
|
+
.weekdayHeaderNotation="${this.weekdayHeaderNotation}"
|
|
102
|
+
.locale="${this.locale}"
|
|
103
|
+
.minYear="${this.minYear}"
|
|
104
|
+
.maxYear="${this.maxYear}"
|
|
105
|
+
.minDate="${this._calendarMinDate}"
|
|
106
|
+
.maxDate="${this._calendarMaxDate}"
|
|
107
|
+
.disableDates="${this.disableDates}"
|
|
101
108
|
@user-selected-date-changed=${this.showDateHandler}
|
|
102
109
|
></dile-calendar>
|
|
103
110
|
<div class="time-area">
|
|
@@ -134,9 +141,23 @@ export class DileDatetimepicker extends DileDatepicker {
|
|
|
134
141
|
let date = formatToIsoOnlyDate(selectedDate);
|
|
135
142
|
let time = this.eltime.value;
|
|
136
143
|
this.value = `${date} ${time}`;
|
|
144
|
+
if (this.validateDate) {
|
|
145
|
+
this.validate();
|
|
146
|
+
}
|
|
137
147
|
this.elmenu.close();
|
|
138
148
|
}
|
|
139
149
|
|
|
150
|
+
dateToValue(date) {
|
|
151
|
+
return formatToIsoOnlyDate(date);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
doBlur(e) {
|
|
155
|
+
// The value includes the time, so it is validated but never rewritten on blur.
|
|
156
|
+
if (this.validateDate) {
|
|
157
|
+
this.validate();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
140
161
|
acceptTime() {
|
|
141
162
|
let date = this.shadowRoot.querySelector('dile-calendar').selectedDate;
|
|
142
163
|
if(!date) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dile/utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"description": "Utility Components of Diverse Uses Based on the Lit Library and Web Components Standard.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"homepage": "https://dile-components.com/",
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@dile/iconlib": "^2.0.2",
|
|
24
|
-
"@dile/ui": "^3.
|
|
25
|
-
"@lion/ui": "^0.
|
|
24
|
+
"@dile/ui": "^3.22.1",
|
|
25
|
+
"@lion/ui": "^0.27.0",
|
|
26
26
|
"linkify-string": "^4.1.3",
|
|
27
27
|
"linkifyjs": "^4.1.3",
|
|
28
28
|
"lit": "^2.7.0 || ^3.0.0",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "cef9349e18ff89a24a206a541f8129fd30ccb553"
|
|
35
35
|
}
|