@3mo/date-time-fields 0.16.1 → 0.17.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.
Files changed (71) hide show
  1. package/dist/FieldDateTimePrecision.d.ts.map +1 -1
  2. package/dist/FieldDateTimePrecision.js +0 -1
  3. package/dist/custom-elements.json +999 -1141
  4. package/dist/{FieldDate.d.ts → date-time/FieldDate.d.ts} +1 -1
  5. package/dist/date-time/FieldDate.d.ts.map +1 -0
  6. package/dist/{FieldDate.js → date-time/FieldDate.js} +1 -1
  7. package/dist/date-time/FieldDateTime.d.ts +36 -0
  8. package/dist/date-time/FieldDateTime.d.ts.map +1 -0
  9. package/dist/date-time/FieldDateTime.js +61 -0
  10. package/dist/{FieldDateTimeBase.d.ts → date-time/FieldDateTimeBase.d.ts} +30 -22
  11. package/dist/date-time/FieldDateTimeBase.d.ts.map +1 -0
  12. package/dist/{FieldDateTimeBase.js → date-time/FieldDateTimeBase.js} +103 -105
  13. package/dist/date-time/FieldDateTimeController.d.ts +30 -0
  14. package/dist/date-time/FieldDateTimeController.d.ts.map +1 -0
  15. package/dist/date-time/FieldDateTimeController.js +87 -0
  16. package/dist/date-time/FieldDateTimeControllerBase.d.ts +81 -0
  17. package/dist/date-time/FieldDateTimeControllerBase.d.ts.map +1 -0
  18. package/dist/date-time/FieldDateTimeControllerBase.js +103 -0
  19. package/dist/date-time/index.d.ts +5 -0
  20. package/dist/date-time/index.d.ts.map +1 -0
  21. package/dist/date-time/index.js +4 -0
  22. package/dist/date-time-range/DateRangeParser.d.ts.map +1 -0
  23. package/dist/{FieldDateRange.d.ts → date-time-range/FieldDateRange.d.ts} +1 -1
  24. package/dist/date-time-range/FieldDateRange.d.ts.map +1 -0
  25. package/dist/{FieldDateRange.js → date-time-range/FieldDateRange.js} +1 -1
  26. package/dist/date-time-range/FieldDateTimeRange.d.ts +41 -0
  27. package/dist/date-time-range/FieldDateTimeRange.d.ts.map +1 -0
  28. package/dist/date-time-range/FieldDateTimeRange.js +100 -0
  29. package/dist/date-time-range/FieldDateTimeRangeController.d.ts +44 -0
  30. package/dist/date-time-range/FieldDateTimeRangeController.d.ts.map +1 -0
  31. package/dist/date-time-range/FieldDateTimeRangeController.js +137 -0
  32. package/dist/{FieldToggleableDateTimeRange.d.ts → date-time-range/FieldToggleableDateTimeRange.d.ts} +1 -1
  33. package/dist/date-time-range/FieldToggleableDateTimeRange.d.ts.map +1 -0
  34. package/dist/{FieldToggleableDateTimeRange.js → date-time-range/FieldToggleableDateTimeRange.js} +1 -1
  35. package/dist/date-time-range/index.d.ts +5 -0
  36. package/dist/date-time-range/index.d.ts.map +1 -0
  37. package/dist/date-time-range/index.js +4 -0
  38. package/dist/dateTimeConverter.d.ts +2 -0
  39. package/dist/dateTimeConverter.d.ts.map +1 -0
  40. package/dist/dateTimeConverter.js +10 -0
  41. package/dist/index.d.ts +3 -7
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +3 -7
  44. package/dist/selection/Calendar.d.ts.map +1 -1
  45. package/dist/selection/Calendar.js +4 -3
  46. package/dist/{FieldTime.d.ts → time/FieldTime.d.ts} +2 -2
  47. package/dist/time/FieldTime.d.ts.map +1 -0
  48. package/dist/{FieldTime.js → time/FieldTime.js} +3 -3
  49. package/dist/{FieldTimeController.d.ts → time/FieldTimeController.d.ts} +3 -3
  50. package/dist/time/FieldTimeController.d.ts.map +1 -0
  51. package/dist/{FieldTimeController.js → time/FieldTimeController.js} +2 -2
  52. package/dist/time/index.d.ts +3 -0
  53. package/dist/time/index.d.ts.map +1 -0
  54. package/dist/time/index.js +2 -0
  55. package/dist/tsconfig.tsbuildinfo +1 -1
  56. package/package.json +1 -1
  57. package/dist/DateRangeParser.d.ts.map +0 -1
  58. package/dist/FieldDate.d.ts.map +0 -1
  59. package/dist/FieldDateRange.d.ts.map +0 -1
  60. package/dist/FieldDateTime.d.ts +0 -40
  61. package/dist/FieldDateTime.d.ts.map +0 -1
  62. package/dist/FieldDateTime.js +0 -109
  63. package/dist/FieldDateTimeBase.d.ts.map +0 -1
  64. package/dist/FieldDateTimeRange.d.ts +0 -53
  65. package/dist/FieldDateTimeRange.d.ts.map +0 -1
  66. package/dist/FieldDateTimeRange.js +0 -167
  67. package/dist/FieldTime.d.ts.map +0 -1
  68. package/dist/FieldTimeController.d.ts.map +0 -1
  69. package/dist/FieldToggleableDateTimeRange.d.ts.map +0 -1
  70. /package/dist/{DateRangeParser.d.ts → date-time-range/DateRangeParser.d.ts} +0 -0
  71. /package/dist/{DateRangeParser.js → date-time-range/DateRangeParser.js} +0 -0
@@ -0,0 +1,87 @@
1
+ import { Localizer } from '@3mo/localization';
2
+ import { FieldDateTimeControllerBase } from './FieldDateTimeControllerBase.js';
3
+ import { FieldDateTimePrecision } from '../FieldDateTimePrecision.js';
4
+ Localizer.dictionaries.add('de', {
5
+ 'Today': 'Heute',
6
+ 'Yesterday': 'Gestern',
7
+ 'Tomorrow': 'Morgen',
8
+ 'Week start': 'Wochenstart',
9
+ 'Week end': 'Wochenende',
10
+ 'Month start': 'Monatsanfang',
11
+ 'Month end': 'Monatsende',
12
+ 'Year start': 'Jahresanfang',
13
+ 'Year end': 'Jahresende',
14
+ });
15
+ /**
16
+ * Everything a date or date-time field does, for a host which only renders it: the segments at the
17
+ * field's precision, where a picker stands and what picking in it means, the presets, and the validity.
18
+ *
19
+ * ```html
20
+ * <div ${this.controller.group.ref()}>
21
+ * ${this.controller.segments.segments.map(segment => html`<span ${this.controller.segment.ref(segment)}></span>`)}
22
+ * </div>
23
+ * ```
24
+ *
25
+ * A picker is the host's to render: it opens on `handlePickerOpen`, scrolls with `navigationDate`,
26
+ * marks `calendarValue`, and hands what was chosen to `pick`.
27
+ *
28
+ * @ssr false
29
+ */
30
+ export class FieldDateTimeController extends FieldDateTimeControllerBase {
31
+ constructor() {
32
+ super(...arguments);
33
+ this.segments = this.createSegments(controller => ({
34
+ get value() { return controller.selectedDate; },
35
+ handleInput: value => controller.options.handleInput?.(value),
36
+ handleChange: value => controller.options.handleChange?.(value),
37
+ }));
38
+ }
39
+ get group() {
40
+ return this.segments.group;
41
+ }
42
+ get segment() {
43
+ return this.segments.segment;
44
+ }
45
+ get segmentsControllers() {
46
+ return [this.segments];
47
+ }
48
+ get selectedDate() {
49
+ return this.options.value ? new DateTime(this.options.value) : undefined;
50
+ }
51
+ get calendarValue() {
52
+ return new DateTimeRange(this.selectedDate, this.selectedDate);
53
+ }
54
+ /** A picked day keeps the time the picker stands at; the value starts at the precision's unit. */
55
+ pick(date) {
56
+ const { hour, minute, second } = this.navigationDate;
57
+ this.options.handleChange?.(this.precision.getRange(date.with({ hour, minute, second })).start);
58
+ }
59
+ datesOf(value) {
60
+ return [new DateTime(value)];
61
+ }
62
+ get defaultPresets() {
63
+ if (this.precision < FieldDateTimePrecision.Day) {
64
+ return [];
65
+ }
66
+ const now = new DateTime();
67
+ return [
68
+ [
69
+ { label: t('Today'), value: now.dayStart },
70
+ { label: t('Yesterday'), value: now.subtract({ days: 1 }).dayStart },
71
+ { label: t('Tomorrow'), value: now.add({ days: 1 }).dayStart },
72
+ ],
73
+ [
74
+ { label: t('Week start'), value: now.weekStart.dayStart },
75
+ { label: t('Week end'), value: now.weekEnd.dayEnd },
76
+ ],
77
+ [
78
+ { label: t('Month start'), value: now.monthStart.dayStart },
79
+ { label: t('Month end'), value: now.monthEnd.dayEnd },
80
+ ],
81
+ [
82
+ { label: t('Year start'), value: now.yearStart.dayStart },
83
+ { label: t('Year end'), value: now.yearEnd.dayEnd },
84
+ ],
85
+ ];
86
+ }
87
+ }
@@ -0,0 +1,81 @@
1
+ import { Controller, type ReactiveControllerHost } from '@a11d/lit';
2
+ import { FieldDateTimePrecision } from '../FieldDateTimePrecision.js';
3
+ import { DateTimeSegmentsController, type DateTimeSegmentsControllerOptions, type HourCycle } from '../segments/index.js';
4
+ export type FieldDateTimeControllerOptions<T> = {
5
+ readonly value?: T;
6
+ /** Defaults to 'minute'. */
7
+ readonly precision?: FieldDateTimePrecision;
8
+ /** Defaults to the language's convention. */
9
+ readonly hourCycle?: HourCycle;
10
+ /** Where the units the user leaves out are taken from, and what relative shortcuts count from. Defaults to now. */
11
+ readonly referenceDate?: DateTime;
12
+ /** The earliest date which may be chosen, inclusive. */
13
+ readonly min?: DateTime;
14
+ /** The latest date which may be chosen, inclusive. */
15
+ readonly max?: DateTime;
16
+ readonly dateDisabled?: (date: DateTime) => boolean;
17
+ /** The field's name, appended to every segment's own. */
18
+ readonly label?: string;
19
+ readonly disabled?: boolean;
20
+ readonly readonly?: boolean;
21
+ readonly required?: boolean;
22
+ readonly invalid?: boolean;
23
+ /** Fired while editing, once every unit is filled. */
24
+ handleInput?(value: T | undefined): void;
25
+ /** Fired on commit: leaving the segments, Enter, a shortcut, or a date picked. */
26
+ handleChange?(value: T | undefined): void;
27
+ /** Alt+ArrowDown in the segments, as on a native date input. Left out, the field has no picker. */
28
+ handlePickerOpen?(): void;
29
+ };
30
+ export type FieldDateTimePreset<T> = {
31
+ readonly label: string;
32
+ readonly value: T;
33
+ };
34
+ export type FieldDateTimeControllerOptionsOrFactory<T, THost> = FieldDateTimeControllerOptions<T> | ((host: THost) => FieldDateTimeControllerOptions<T>);
35
+ /** What one group of segments is told beyond what the field tells every group. */
36
+ type SegmentsBinding = Pick<DateTimeSegmentsControllerOptions, 'value' | 'handleInput' | 'handleChange' | 'handleFocusChange' | 'handleMoveBeyond' | 'parseShortcut'>;
37
+ /**
38
+ * What every date field does, whether it holds one date or a range: the groups of segments, where a
39
+ * picker stands and what picking in it means, the presets it offers, and the field's validity.
40
+ *
41
+ * @ssr false
42
+ */
43
+ export declare abstract class FieldDateTimeControllerBase<T, THost extends ReactiveControllerHost = ReactiveControllerHost> extends Controller implements EventListenerObject {
44
+ protected readonly host: THost;
45
+ protected readonly options: FieldDateTimeControllerOptions<T>;
46
+ private customValidity;
47
+ private navigatedValue?;
48
+ private _navigationDate?;
49
+ constructor(host: THost, options: FieldDateTimeControllerOptionsOrFactory<T, THost>);
50
+ /** Every group of segments the field renders, in order. */
51
+ protected abstract get segmentsControllers(): ReadonlyArray<DateTimeSegmentsController<THost>>;
52
+ /** The date a picker marks and scrolls to. */
53
+ abstract get selectedDate(): DateTime | undefined;
54
+ /** The dates a calendar marks. */
55
+ abstract get calendarValue(): DateTimeRange | undefined;
56
+ /** Commits a date chosen in a picker, at the precision it was chosen at: a day in a calendar, or an hour in a list. */
57
+ abstract pick(date: DateTime, precision: FieldDateTimePrecision): void;
58
+ protected abstract datesOf(value: T): ReadonlyArray<DateTime>;
59
+ protected abstract get defaultPresets(): ReadonlyArray<ReadonlyArray<FieldDateTimePreset<T>>>;
60
+ get precision(): FieldDateTimePrecision;
61
+ /** Where a picker stands: at the selected date, until it is scrolled elsewhere, and back there whenever the value changes. */
62
+ get navigationDate(): import("@3mo/date-time/DateTime.js").DateTime;
63
+ set navigationDate(value: import("@3mo/date-time/DateTime.js").DateTime);
64
+ protected resetNavigationDate(date?: import("@3mo/date-time/DateTime.js").DateTime | undefined): void;
65
+ /** The presets a picker offers, in groups, leaving out any the field would refuse. */
66
+ get presets(): ReadonlyArray<ReadonlyArray<FieldDateTimePreset<T>>>;
67
+ /** Whether the field shows anything: a value, or units typed on the way to one. */
68
+ get isPopulated(): boolean;
69
+ private get hasValue();
70
+ /** Whether the value holds a date before `min`, after `max`, or refused by `dateDisabled`. */
71
+ isDisabled(value: T): boolean;
72
+ hostUpdate(): void;
73
+ hostUpdated(): void;
74
+ handleEvent(event: Event): void;
75
+ focus(): void;
76
+ setCustomValidity(message: string): void;
77
+ checkValidity(): boolean;
78
+ protected createSegments(binding: (controller: this) => SegmentsBinding): DateTimeSegmentsController<THost>;
79
+ }
80
+ export {};
81
+ //# sourceMappingURL=FieldDateTimeControllerBase.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FieldDateTimeControllerBase.d.ts","sourceRoot":"","sources":["../../date-time/FieldDateTimeControllerBase.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EA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OAAO,CAAC,KAAK,EAAE,CAAC,GAAG,aAAa,CAAC,QAAQ,CAAC;IAE7D,SAAS,CAAC,QAAQ,KAAK,cAAc,IAAI,aAAa,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAE7F,IAAI,SAAS,2BAEZ;IAED,8HAA8H;IAC9H,IAAI,cAAc,kDAEjB;IAED,IAAI,cAAc,CAAC,KAAK,+CAAA,EAGvB;IAED,SAAS,CAAC,mBAAmB,CAAC,IAAI,4DAAoB;IAItD,sFAAsF;IACtF,IAAI,OAAO,IAAI,aAAa,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC,CAIlE;IAED,mFAAmF;IACnF,IAAI,WAAW,YAEd;IAED,OAAO,KAAK,QAAQ,GAEnB;IAED,8FAA8F;IAC9F,UAAU,CAAC,KAAK,EAAE,CAAC;IAUV,UAAU;IAQV,WAAW;IAMpB,WAAW,CAAC,KAAK,EAAE,KAAK;IAQxB,KAAK;IAIL,iBAAiB,CAAC,OAAO,EAAE,MAAM;IAIjC,aAAa;IAOb,SAAS,CAAC,cAAc,CAAC,OAAO,EAAE,CAAC,UAAU,EAAE,IAAI,KAAK,eAAe;CAoBvE"}
@@ -0,0 +1,103 @@
1
+ import { Controller } from '@a11d/lit';
2
+ import { FieldDateTimePrecision } from '../FieldDateTimePrecision.js';
3
+ import { DateTimeSegmentsController } from '../segments/index.js';
4
+ /**
5
+ * What every date field does, whether it holds one date or a range: the groups of segments, where a
6
+ * picker stands and what picking in it means, the presets it offers, and the field's validity.
7
+ *
8
+ * @ssr false
9
+ */
10
+ export class FieldDateTimeControllerBase extends Controller {
11
+ constructor(host, options) {
12
+ super(host);
13
+ this.host = host;
14
+ this.customValidity = '';
15
+ this.options = typeof options === 'function' ? options(host) : options;
16
+ }
17
+ get precision() {
18
+ return this.options.precision ?? FieldDateTimePrecision.Minute;
19
+ }
20
+ /** Where a picker stands: at the selected date, until it is scrolled elsewhere, and back there whenever the value changes. */
21
+ get navigationDate() {
22
+ return this._navigationDate ??= this.selectedDate ?? new DateTime();
23
+ }
24
+ set navigationDate(value) {
25
+ this._navigationDate = value;
26
+ this.host.requestUpdate();
27
+ }
28
+ resetNavigationDate(date = this.selectedDate) {
29
+ this._navigationDate = date;
30
+ }
31
+ /** The presets a picker offers, in groups, leaving out any the field would refuse. */
32
+ get presets() {
33
+ return this.defaultPresets
34
+ .map(group => group.filter(preset => !this.isDisabled(preset.value)))
35
+ .filter(group => group.length > 0);
36
+ }
37
+ /** Whether the field shows anything: a value, or units typed on the way to one. */
38
+ get isPopulated() {
39
+ return this.hasValue || this.segmentsControllers.some(segments => !segments.isEmpty);
40
+ }
41
+ get hasValue() {
42
+ return this.options.value !== undefined && this.datesOf(this.options.value).length > 0;
43
+ }
44
+ /** Whether the value holds a date before `min`, after `max`, or refused by `dateDisabled`. */
45
+ isDisabled(value) {
46
+ const { min, max, dateDisabled } = this.options;
47
+ const precision = this.precision > FieldDateTimePrecision.Day ? FieldDateTimePrecision.Day : this.precision;
48
+ return this.datesOf(value).some(date => (!!min && precision.isSmallerThan(date, min) && !precision.equals(date, min))
49
+ || (!!max && precision.isSmallerThan(max, date) && !precision.equals(max, date))
50
+ || (dateDisabled?.(date) ?? false));
51
+ }
52
+ hostUpdate() {
53
+ if (this.options.value !== this.navigatedValue) {
54
+ this.navigatedValue = this.options.value;
55
+ this.resetNavigationDate();
56
+ }
57
+ }
58
+ // Idempotent for one and the same listener, so re-declaring it on every update costs nothing.
59
+ hostUpdated() {
60
+ for (const segments of this.segmentsControllers) {
61
+ segments.group.value?.addEventListener('keydown', this);
62
+ }
63
+ }
64
+ handleEvent(event) {
65
+ const { key, altKey } = event;
66
+ if (altKey && key === 'ArrowDown' && this.options.handlePickerOpen) {
67
+ event.preventDefault();
68
+ this.options.handlePickerOpen();
69
+ }
70
+ }
71
+ focus() {
72
+ this.segmentsControllers[0]?.focus();
73
+ }
74
+ setCustomValidity(message) {
75
+ this.customValidity = message;
76
+ }
77
+ checkValidity() {
78
+ const { value, required } = this.options;
79
+ return !this.customValidity
80
+ && !(required && !this.hasValue)
81
+ && !(value !== undefined && this.isDisabled(value));
82
+ }
83
+ createSegments(binding) {
84
+ const controller = this;
85
+ const bound = binding(this);
86
+ return new DateTimeSegmentsController(this.host, {
87
+ get value() { return bound.value; },
88
+ get precision() { return controller.precision; },
89
+ get referenceDate() { return controller.options.referenceDate; },
90
+ get hourCycle() { return controller.options.hourCycle; },
91
+ get label() { return controller.options.label; },
92
+ get disabled() { return controller.options.disabled; },
93
+ get readonly() { return controller.options.readonly; },
94
+ get required() { return controller.options.required; },
95
+ get invalid() { return controller.options.invalid; },
96
+ handleInput: bound.handleInput,
97
+ handleChange: bound.handleChange,
98
+ handleFocusChange: bound.handleFocusChange,
99
+ handleMoveBeyond: bound.handleMoveBeyond,
100
+ parseShortcut: bound.parseShortcut,
101
+ });
102
+ }
103
+ }
@@ -0,0 +1,5 @@
1
+ export * from './FieldDateTimeControllerBase.js';
2
+ export * from './FieldDateTimeController.js';
3
+ export * from './FieldDateTime.js';
4
+ export * from './FieldDate.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../date-time/index.ts"],"names":[],"mappings":"AAAA,cAAc,kCAAkC,CAAA;AAChD,cAAc,8BAA8B,CAAA;AAC5C,cAAc,oBAAoB,CAAA;AAClC,cAAc,gBAAgB,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from './FieldDateTimeControllerBase.js';
2
+ export * from './FieldDateTimeController.js';
3
+ export * from './FieldDateTime.js';
4
+ export * from './FieldDate.js';
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DateRangeParser.d.ts","sourceRoot":"","sources":["../../date-time-range/DateRangeParser.ts"],"names":[],"mappings":"AAAA,qBAAa,eAAe;IAC3B,MAAM,CAAC,QAAQ,CAAC,kBAAkB,cAAQ;IAC1C,MAAM,CAAC,QAAQ,CAAC,uBAAuB,WAAsD;IAE7F,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,MAAM,EAAE,aAAa,gDAAe;IAqBhE,OAAO,CAAC,MAAM,CAAC,yBAAyB;CAiBxC"}
@@ -1,4 +1,4 @@
1
- import { FieldDateTimePrecision } from './FieldDateTimePrecision.js';
1
+ import { FieldDateTimePrecision } from '../FieldDateTimePrecision.js';
2
2
  import { FieldDateTimeRange } from './FieldDateTimeRange.js';
3
3
  /** @element mo-field-date */
4
4
  export declare class FieldDateRange extends FieldDateTimeRange {
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FieldDateRange.d.ts","sourceRoot":"","sources":["../../date-time-range/FieldDateRange.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,MAAM,8BAA8B,CAAA;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAE5D,6BAA6B;AAC7B,qBACa,cAAe,SAAQ,kBAAkB;IAC5C,SAAS,yBAA6B;CAC/C;AAED,OAAO,CAAC,MAAM,CAAC;IACd,UAAU,qBAAqB;QAC9B,qBAAqB,EAAE,cAAc,CAAA;KACrC;CACD"}
@@ -1,6 +1,6 @@
1
1
  import { __decorate } from "tslib";
2
2
  import { component } from '@a11d/lit';
3
- import { FieldDateTimePrecision } from './FieldDateTimePrecision.js';
3
+ import { FieldDateTimePrecision } from '../FieldDateTimePrecision.js';
4
4
  import { FieldDateTimeRange } from './FieldDateTimeRange.js';
5
5
  /** @element mo-field-date */
6
6
  let FieldDateRange = class FieldDateRange extends FieldDateTimeRange {
@@ -0,0 +1,41 @@
1
+ import { type MaterialIcon } from '@3mo/icon';
2
+ import { FieldDateTimeBase } from '../date-time/FieldDateTimeBase.js';
3
+ import { FieldDateTimeRangeController } from './FieldDateTimeRangeController.js';
4
+ /**
5
+ * Its behaviour is {@link FieldDateTimeRangeController}, for a date range field of another design.
6
+ *
7
+ * @element mo-field-date-range
8
+ *
9
+ * @attr value - The selected date range.
10
+ *
11
+ * @i18n "Period"
12
+ * @i18n "Start"
13
+ * @i18n "End"
14
+ * @i18n "Last ${count:number} days"
15
+ * @i18n "Last week"
16
+ * @i18n "This week"
17
+ * @i18n "Next week"
18
+ * @i18n "Last month"
19
+ * @i18n "This month"
20
+ * @i18n "Next month"
21
+ * @i18n "Last year"
22
+ * @i18n "This year"
23
+ * @i18n "Next year"
24
+ */
25
+ export declare class FieldDateTimeRange extends FieldDateTimeBase<DateTimeRange | undefined> {
26
+ value?: DateTimeRange;
27
+ readonly controller: FieldDateTimeRangeController<this>;
28
+ protected get segmentsTemplate(): import("lit-html").HTMLTemplateResult;
29
+ protected get _label(): string;
30
+ static get styles(): import("@a11d/lit").CSSResult;
31
+ protected calendarIconButtonIcon: MaterialIcon;
32
+ protected get popoverSelectionTemplate(): import("lit-html").HTMLTemplateResult;
33
+ protected get popoverToolbarTemplate(): import("lit-html").HTMLTemplateResult;
34
+ private get startEndTabBarTemplate();
35
+ }
36
+ declare global {
37
+ interface HTMLElementTagNameMap {
38
+ 'mo-field-date-time-range': FieldDateTimeRange;
39
+ }
40
+ }
41
+ //# sourceMappingURL=FieldDateTimeRange.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FieldDateTimeRange.d.ts","sourceRoot":"","sources":["../../date-time-range/FieldDateTimeRange.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,WAAW,CAAA;AAE7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAA;AACrE,OAAO,EAAE,4BAA4B,EAAoC,MAAM,mCAAmC,CAAA;AAQlH;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBACa,kBAAmB,SAAQ,iBAAiB,CAAC,aAAa,GAAG,SAAS,CAAC;IACvD,KAAK,CAAC,EAAE,aAAa,CAAA;IAEjD,QAAQ,CAAC,UAAU,qCAAiE;IAEpF,SAAS,KAAK,gBAAgB,0CAQ7B;IAED,cAAuB,MAAM,WAE5B;IAED,WAAoB,MAAM,kCAmBzB;IAED,UAAmB,sBAAsB,EAAE,YAAY,CAAe;IAEtE,cAAuB,wBAAwB,0CAO9C;IAED,SAAS,KAAK,sBAAsB,0CAInC;IAED,OAAO,KAAK,sBAAsB,GASjC;CACD;AAED,OAAO,CAAC,MAAM,CAAC;IACd,UAAU,qBAAqB;QAC9B,0BAA0B,EAAE,kBAAkB,CAAA;KAC9C;CACD"}
@@ -0,0 +1,100 @@
1
+ import { __decorate } from "tslib";
2
+ import { component, css, html, property } from '@a11d/lit';
3
+ import { Localizer } from '@3mo/localization';
4
+ import { FieldDateTimeBase } from '../date-time/FieldDateTimeBase.js';
5
+ import { FieldDateTimeRangeController } from './FieldDateTimeRangeController.js';
6
+ Localizer.dictionaries.add('de', {
7
+ 'Period': 'Zeitraum',
8
+ 'Start': 'Start',
9
+ 'End': 'Ende',
10
+ });
11
+ /**
12
+ * Its behaviour is {@link FieldDateTimeRangeController}, for a date range field of another design.
13
+ *
14
+ * @element mo-field-date-range
15
+ *
16
+ * @attr value - The selected date range.
17
+ *
18
+ * @i18n "Period"
19
+ * @i18n "Start"
20
+ * @i18n "End"
21
+ * @i18n "Last ${count:number} days"
22
+ * @i18n "Last week"
23
+ * @i18n "This week"
24
+ * @i18n "Next week"
25
+ * @i18n "Last month"
26
+ * @i18n "This month"
27
+ * @i18n "Next month"
28
+ * @i18n "Last year"
29
+ * @i18n "This year"
30
+ * @i18n "Next year"
31
+ */
32
+ let FieldDateTimeRange = class FieldDateTimeRange extends FieldDateTimeBase {
33
+ constructor() {
34
+ super(...arguments);
35
+ this.controller = new FieldDateTimeRangeController(this, this.controllerOptions);
36
+ this.calendarIconButtonIcon = 'date_range';
37
+ }
38
+ get segmentsTemplate() {
39
+ return html `
40
+ <div part='segments-range' dir=${this.controller.startSegments.segmenter.direction}>
41
+ ${this.getSegmentsTemplate(this.controller.startSegments, 'start')}
42
+ <span part='literal' aria-hidden='true'> ${DateTimeRange.getUntilDelimiter()} </span>
43
+ ${this.getSegmentsTemplate(this.controller.endSegments, 'end')}
44
+ </div>
45
+ `;
46
+ }
47
+ get _label() {
48
+ return super._label || t('Period');
49
+ }
50
+ static get styles() {
51
+ return css `
52
+ ${super.styles}
53
+
54
+ mo-tab-bar {
55
+ max-width: 200px;
56
+ place-self: center;
57
+ padding-block: 4px;
58
+ mo-tab {
59
+ &:not([active]) {
60
+ opacity: 0.5;
61
+ }
62
+ }
63
+ }
64
+
65
+ #selector {
66
+ flex: none !important;
67
+ }
68
+ `;
69
+ }
70
+ get popoverSelectionTemplate() {
71
+ return html `
72
+ <mo-flex style='flex: 1'>
73
+ ${this.popoverToolbarTemplate}
74
+ ${super.popoverSelectionTemplate}
75
+ </mo-flex>
76
+ `;
77
+ }
78
+ get popoverToolbarTemplate() {
79
+ return html `
80
+ ${this.startEndTabBarTemplate}
81
+ `;
82
+ }
83
+ get startEndTabBarTemplate() {
84
+ return html `
85
+ <mo-flex style='border-inline-start: 1px solid var(--mo-color-transparent-gray-3)'>
86
+ <mo-tab-bar .value=${this.controller.selection} @change=${(e) => this.controller.selection = e.detail}>
87
+ <mo-tab value='start'>${t('Start')}</mo-tab>
88
+ <mo-tab value='end'>${t('End')}</mo-tab>
89
+ </mo-tab-bar>
90
+ </mo-flex>
91
+ `;
92
+ }
93
+ };
94
+ __decorate([
95
+ property({ type: Object })
96
+ ], FieldDateTimeRange.prototype, "value", void 0);
97
+ FieldDateTimeRange = __decorate([
98
+ component('mo-field-date-time-range')
99
+ ], FieldDateTimeRange);
100
+ export { FieldDateTimeRange };
@@ -0,0 +1,44 @@
1
+ import { type ReactiveControllerHost } from '@a11d/lit';
2
+ import { FieldDateTimeControllerBase, type FieldDateTimePreset } from '../date-time/FieldDateTimeControllerBase.js';
3
+ import { FieldDateTimePrecision } from '../FieldDateTimePrecision.js';
4
+ /** The end of the range a picker edits. */
5
+ export type FieldDateTimeRangeSelection = 'start' | 'end';
6
+ /**
7
+ * Everything a date range field does, for a host which only renders it: a group of segments for
8
+ * each end with the arrow keys crossing between them, the end a picker edits, range shortcuts, the
9
+ * presets, and the validity.
10
+ *
11
+ * ```html
12
+ * <div ${this.controller.startSegments.group.ref()}>
13
+ * ${this.controller.startSegments.segments.map(segment => html`<span ${this.controller.startSegments.segment.ref(segment)}></span>`)}
14
+ * </div>
15
+ * <div ${this.controller.endSegments.group.ref()}>…</div>
16
+ * ```
17
+ *
18
+ * A picker is the host's to render: it opens on `handlePickerOpen`, edits the `selection` end, scrolls
19
+ * with `navigationDate`, marks `calendarValue`, and hands what was chosen to `pick`. Picking an end at
20
+ * the field's precision moves the selection on to the other end.
21
+ *
22
+ * @ssr false
23
+ */
24
+ export declare class FieldDateTimeRangeController<THost extends ReactiveControllerHost = ReactiveControllerHost> extends FieldDateTimeControllerBase<DateTimeRange, THost> {
25
+ private _selection;
26
+ readonly startSegments: import("../index.js").DateTimeSegmentsController<THost>;
27
+ readonly endSegments: import("../index.js").DateTimeSegmentsController<THost>;
28
+ /** The end a picker edits. Entering an end's segments selects it. */
29
+ get selection(): FieldDateTimeRangeSelection;
30
+ set selection(value: FieldDateTimeRangeSelection);
31
+ protected get segmentsControllers(): import("../index.js").DateTimeSegmentsController<THost>[];
32
+ get selectedDate(): import("@3mo/date-time/DateTime.js").DateTime | undefined;
33
+ get calendarValue(): import("@3mo/date-time/DateTimeRange.js").DateTimeRange | undefined;
34
+ /** An end without a date leaves the picker where it stands. */
35
+ protected resetNavigationDate(): void;
36
+ /** A picked day keeps the time the picker stands at; the start begins the precision's unit and the end ends it. */
37
+ pick(date: DateTime, precision: FieldDateTimePrecision): void;
38
+ private withEnd;
39
+ /** Range keywords ("m", "lw") set both ends at once; anything else resolves to the focused end. */
40
+ private parseShortcut;
41
+ protected datesOf(value: DateTimeRange): import("@3mo/date-time/DateTime.js").DateTime[];
42
+ protected get defaultPresets(): ReadonlyArray<ReadonlyArray<FieldDateTimePreset<DateTimeRange>>>;
43
+ }
44
+ //# sourceMappingURL=FieldDateTimeRangeController.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FieldDateTimeRangeController.d.ts","sourceRoot":"","sources":["../../date-time-range/FieldDateTimeRangeController.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,sBAAsB,EAAE,MAAM,WAAW,CAAA;AAEvD,OAAO,EAAE,2BAA2B,EAAE,KAAK,mBAAmB,EAAE,MAAM,6CAA6C,CAAA;AACnH,OAAO,EAAE,sBAAsB,EAAE,MAAM,8BAA8B,CAAA;AAgBrE,2CAA2C;AAC3C,MAAM,MAAM,2BAA2B,GAAG,OAAO,GAAG,KAAK,CAAA;AAEzD;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,4BAA4B,CAAC,KAAK,SAAS,sBAAsB,GAAG,sBAAsB,CAAE,SAAQ,2BAA2B,CAAC,aAAa,EAAE,KAAK,CAAC;IACjK,OAAO,CAAC,UAAU,CAAuC;IAEzD,QAAQ,CAAC,aAAa,0DAOnB;IAEH,QAAQ,CAAC,WAAW,0DAOjB;IAEH,qEAAqE;IACrE,IAAI,SAAS,gCAEZ;IAED,IAAI,SAAS,CAAC,KAAK,6BAAA,EAMlB;IAED,SAAS,KAAK,mBAAmB,8DAEhC;IAED,IAAI,YAAY,8DAEf;IAED,IAAI,aAAa,wEAEhB;IAED,+DAA+D;cAC5C,mBAAmB;IAItC,mHAAmH;IACnH,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,sBAAsB;IAYtD,OAAO,CAAC,OAAO;IAMf,mGAAmG;IACnG,OAAO,CAAC,aAAa;IASrB,SAAS,CAAC,OAAO,CAAC,KAAK,EAAE,aAAa;IAItC,SAAS,KAAK,cAAc,IAAI,aAAa,CAAC,aAAa,CAAC,mBAAmB,CAAC,aAAa,CAAC,CAAC,CAAC,CAuB/F;CACD"}
@@ -0,0 +1,137 @@
1
+ import { Localizer } from '@3mo/localization';
2
+ import { FieldDateTimeControllerBase } from '../date-time/FieldDateTimeControllerBase.js';
3
+ import { FieldDateTimePrecision } from '../FieldDateTimePrecision.js';
4
+ import { DateRangeParser } from './DateRangeParser.js';
5
+ Localizer.dictionaries.add('de', {
6
+ 'Last ${count:number} days': 'Letzte ${count} Tage',
7
+ 'Last week': 'Letzte Woche',
8
+ 'This week': 'Diese Woche',
9
+ 'Next week': 'Nächste Woche',
10
+ 'Last month': 'Letzter Monat',
11
+ 'This month': 'Dieser Monat',
12
+ 'Next month': 'Nächster Monat',
13
+ 'Last year': 'Letztes Jahr',
14
+ 'This year': 'Dieses Jahr',
15
+ 'Next year': 'Nächstes Jahr',
16
+ });
17
+ /**
18
+ * Everything a date range field does, for a host which only renders it: a group of segments for
19
+ * each end with the arrow keys crossing between them, the end a picker edits, range shortcuts, the
20
+ * presets, and the validity.
21
+ *
22
+ * ```html
23
+ * <div ${this.controller.startSegments.group.ref()}>
24
+ * ${this.controller.startSegments.segments.map(segment => html`<span ${this.controller.startSegments.segment.ref(segment)}></span>`)}
25
+ * </div>
26
+ * <div ${this.controller.endSegments.group.ref()}>…</div>
27
+ * ```
28
+ *
29
+ * A picker is the host's to render: it opens on `handlePickerOpen`, edits the `selection` end, scrolls
30
+ * with `navigationDate`, marks `calendarValue`, and hands what was chosen to `pick`. Picking an end at
31
+ * the field's precision moves the selection on to the other end.
32
+ *
33
+ * @ssr false
34
+ */
35
+ export class FieldDateTimeRangeController extends FieldDateTimeControllerBase {
36
+ constructor() {
37
+ super(...arguments);
38
+ this._selection = 'start';
39
+ this.startSegments = this.createSegments(controller => ({
40
+ get value() { return controller.options.value?.start; },
41
+ handleInput: value => controller.options.handleInput?.(controller.withEnd('start', value)),
42
+ handleChange: value => controller.options.handleChange?.(controller.withEnd('start', value)),
43
+ handleFocusChange: focused => { if (focused) {
44
+ controller.selection = 'start';
45
+ } },
46
+ handleMoveBeyond: direction => direction > 0 && controller.endSegments.focusFirst(),
47
+ parseShortcut: (text, referenceDate) => controller.parseShortcut(text, referenceDate),
48
+ }));
49
+ this.endSegments = this.createSegments(controller => ({
50
+ get value() { return controller.options.value?.end; },
51
+ handleInput: value => controller.options.handleInput?.(controller.withEnd('end', value)),
52
+ handleChange: value => controller.options.handleChange?.(controller.withEnd('end', value)),
53
+ handleFocusChange: focused => { if (focused) {
54
+ controller.selection = 'end';
55
+ } },
56
+ handleMoveBeyond: direction => direction < 0 && controller.startSegments.focusLast(),
57
+ parseShortcut: (text, referenceDate) => controller.parseShortcut(text, referenceDate),
58
+ }));
59
+ }
60
+ /** The end a picker edits. Entering an end's segments selects it. */
61
+ get selection() {
62
+ return this._selection;
63
+ }
64
+ set selection(value) {
65
+ if (this._selection !== value) {
66
+ this._selection = value;
67
+ this.resetNavigationDate();
68
+ this.host.requestUpdate();
69
+ }
70
+ }
71
+ get segmentsControllers() {
72
+ return [this.startSegments, this.endSegments];
73
+ }
74
+ get selectedDate() {
75
+ return this.selection === 'start' ? this.options.value?.start : this.options.value?.end;
76
+ }
77
+ get calendarValue() {
78
+ return this.options.value;
79
+ }
80
+ /** An end without a date leaves the picker where it stands. */
81
+ resetNavigationDate() {
82
+ super.resetNavigationDate(this.selectedDate ?? this.navigationDate);
83
+ }
84
+ /** A picked day keeps the time the picker stands at; the start begins the precision's unit and the end ends it. */
85
+ pick(date, precision) {
86
+ const { hour, minute, second } = this.navigationDate;
87
+ const { start, end } = this.precision.getRange(date.with({ hour, minute, second }));
88
+ const value = this.options.value;
89
+ this.options.handleChange?.(this.selection === 'start'
90
+ ? new DateTimeRange(start, value?.end)
91
+ : new DateTimeRange(value?.start, end));
92
+ if (precision === this.precision) {
93
+ this.selection = this.selection === 'start' ? 'end' : 'start';
94
+ }
95
+ }
96
+ withEnd(selection, value) {
97
+ const start = selection === 'start' ? value : this.options.value?.start;
98
+ const end = selection === 'end' ? (value ? this.precision.getRange(value).end : undefined) : this.options.value?.end;
99
+ return !start && !end ? undefined : new DateTimeRange(start, end);
100
+ }
101
+ /** Range keywords ("m", "lw") set both ends at once; anything else resolves to the focused end. */
102
+ parseShortcut(text, referenceDate) {
103
+ const range = DateRangeParser.parse(text, referenceDate);
104
+ if (range?.start && range.end) {
105
+ this.options.handleChange?.(range);
106
+ return undefined;
107
+ }
108
+ return range?.start ?? range?.end;
109
+ }
110
+ datesOf(value) {
111
+ return [value.start, value.end].filter((date) => !!date);
112
+ }
113
+ get defaultPresets() {
114
+ const now = new DateTime();
115
+ const lastDays = (count) => ({ label: t('Last ${count:number} days', { count }), value: new DateTimeRange(now.subtract({ days: count - 1 }).dayStart, now.dayEnd) });
116
+ return [
117
+ this.precision < FieldDateTimePrecision.Day ? [] : [
118
+ { label: t('Last week'), value: new DateTimeRange(now.add({ weeks: -1 }).weekStart.dayStart, now.add({ weeks: -1 }).weekEnd.dayEnd) },
119
+ { label: t('This week'), value: new DateTimeRange(now.weekStart.dayStart, now.weekEnd.dayEnd) },
120
+ { label: t('Next week'), value: new DateTimeRange(now.add({ weeks: 1 }).weekStart.dayStart, now.add({ weeks: 1 }).weekEnd.dayEnd) },
121
+ lastDays(7),
122
+ lastDays(30),
123
+ lastDays(90),
124
+ ],
125
+ this.precision < FieldDateTimePrecision.Month ? [] : [
126
+ { label: t('Last month'), value: new DateTimeRange(now.add({ months: -1 }).monthStart.dayStart, now.add({ months: -1 }).monthEnd.dayEnd) },
127
+ { label: t('This month'), value: new DateTimeRange(now.monthStart.dayStart, now.monthEnd.dayEnd) },
128
+ { label: t('Next month'), value: new DateTimeRange(now.add({ months: 1 }).monthStart.dayStart, now.add({ months: 1 }).monthEnd.dayEnd) },
129
+ ],
130
+ [
131
+ { label: t('Last year'), value: new DateTimeRange(now.add({ years: -1 }).yearStart.dayStart, now.add({ years: -1 }).yearEnd.dayEnd) },
132
+ { label: t('This year'), value: new DateTimeRange(now.yearStart.dayStart, now.yearEnd.dayEnd) },
133
+ { label: t('Next year'), value: new DateTimeRange(now.add({ years: 1 }).yearStart.dayStart, now.add({ years: 1 }).yearEnd.dayEnd) },
134
+ ],
135
+ ];
136
+ }
137
+ }