-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDateTimeInputModel.js
More file actions
133 lines (116 loc) · 3.48 KB
/
DateTimeInputModel.js
File metadata and controls
133 lines (116 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import {
extractTimestampFromDateTimeInput,
formatTimestampForDateTimeInput,
} from '../../../../utilities/formatting/dateTimeInputFormatters.mjs';
import { Observable } from '/js/src/index.js';
/**
* @typedef DateTimeInputRawData
* @property {string} date the raw date value
* @property {string} time the raw time value
*/
/**
* @typedef DateTimeInputConfiguration
* @property {Partial<DateTimeInputRawData>} [defaults] the default raw values to use when partially updating inputs
* @property {boolean} [required] states if the input can be null
*/
/**
* Store the state of a date time model
*/
export class DateTimeInputModel extends Observable {
/**
* Constructor
* @param {object} [configuration] the model configuration
* @param {boolean} [configuration.seconds=false] states if the date time input granularity is seconds (else, it is minutes)
*/
constructor(configuration) {
super();
const { seconds } = configuration || {};
this._seconds = seconds;
/**
* @type {DateTimeInputRawData}
* @private
*/
this._raw = { date: '', time: '' };
/**
* @type {number|null}
* @private
*/
this._value = null;
}
/**
* Update the inputs raw values
*
* @param {DateTimeInputRawData} raw the input raw values
* @return {void}
*/
update(raw) {
this._raw = raw;
const hasDateAndTime = raw.date && raw.time;
try {
this._value = hasDateAndTime ? extractTimestampFromDateTimeInput(raw, { seconds: this._seconds }) : null;
} catch {
this._value = null;
}
hasDateAndTime && this.notify();
}
/**
* Reset the inputs to its initial state
* @return {void}
*/
clear() {
this.setValue(null, true);
}
/**
* States if the input model is empty (has no defined value)
*
* @return {boolean} true if the model is empty
*/
get isEmpty() {
return this._value === null;
}
/**
* Returns the raw input values
*
* @return {DateTimeInputRawData} the raw values
*/
get raw() {
return this._raw;
}
/**
* Returns the current date represented by the inputs (null if no valid value is represented)
*
* @return {number|null} the current value
*/
get value() {
return this._value;
}
/**
* Set the current date represented by the inputs and update the raw values accordingly
*
* @param {number|null} value the new value
* @param {boolean} [silent] if true, observers will not be notified of the change
* @return {void}
*/
setValue(value, silent) {
if (value === this._value) {
return;
}
this._value = value;
this._raw = value !== null
? formatTimestampForDateTimeInput(value, this._seconds)
: { date: '', time: '' };
!silent && this.notify();
}
}