Debian Codegraph @

jupyter-widgets-controls/lib/widget_datetime.js

raw
1 // Copyright (c) Vidar Tonaas Fauske
2 // Distributed under the terms of the Modified BSD License.
3 import { uuid } from './utils';
4 import { DescriptionView } from './widget_description';
5 import { CoreDescriptionModel } from './widget_core';
6 import { serialize_time } from './widget_time';
7 export function serialize_datetime(value) {
8 if (value === null) {
9 return null;
10 }
11 else {
12 return {
13 year: value.getUTCFullYear(),
14 month: value.getUTCMonth(),
15 date: value.getUTCDate(),
16 hours: value.getUTCHours(),
17 minutes: value.getUTCMinutes(),
18 seconds: value.getUTCSeconds(),
19 milliseconds: value.getUTCMilliseconds(),
20 };
21 }
22 }
23 export function deserialize_datetime(value) {
24 if (value === null) {
25 return null;
26 }
27 else {
28 const date = new Date();
29 date.setUTCFullYear(value.year, value.month, value.date);
30 date.setUTCHours(value.hours, value.minutes, value.seconds, value.milliseconds);
31 return date;
32 }
33 }
34 export const datetime_serializers = {
35 serialize: serialize_datetime,
36 deserialize: deserialize_datetime,
37 };
38 export class DatetimeModel extends CoreDescriptionModel {
39 defaults() {
40 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'DatetimeModel', _view_name: 'DatetimeView', value: null, disabled: false, min: null, max: null });
41 }
42 }
43 DatetimeModel.serializers = Object.assign(Object.assign({}, CoreDescriptionModel.serializers), { value: datetime_serializers, min: datetime_serializers, max: datetime_serializers });
44 export class DatetimeView extends DescriptionView {
45 render() {
46 super.render();
47 this.el.classList.add('jupyter-widgets');
48 this.el.classList.add('widget-inline-hbox');
49 this.el.classList.add('widget-datetimepicker');
50 const test = document.createElement('input');
51 test.type = 'datetime-local';
52 if (test.type === 'text') {
53 // No native support, split into date and time input:
54 this._datepicker = document.createElement('input');
55 this._datepicker.setAttribute('type', 'date');
56 this._datepicker.id = this.label.htmlFor = uuid();
57 this._timepicker = document.createElement('input');
58 this._timepicker.setAttribute('type', 'time');
59 this._timepicker.id = uuid();
60 this.el.appendChild(this._datepicker);
61 this.el.appendChild(this._timepicker);
62 }
63 else {
64 this._datetimepicker = test;
65 this._datetimepicker.id = this.label.htmlFor = uuid();
66 this.el.appendChild(this._datetimepicker);
67 }
68 this.listenTo(this.model, 'change:value', this._update_value);
69 this.listenTo(this.model, 'change', this.update2);
70 this._update_value();
71 this.update2();
72 }
73 /**
74 * Update the contents of this view
75 *
76 * Called when the model is changed. The model may have been
77 * changed by another view or by a state update from the back-end.
78 */
79 update2(model, options) {
80 if (options === undefined || options.updated_view !== this) {
81 const min = this.model.get('min');
82 const max = this.model.get('max');
83 if (this._datetimepicker) {
84 this._datetimepicker.disabled = this.model.get('disabled');
85 this._datetimepicker.min = Private.dt_as_dt_string(min);
86 this._datetimepicker.max = Private.dt_as_dt_string(max);
87 }
88 else {
89 this._datepicker.disabled = this.model.get('disabled');
90 this._datepicker.min = Private.dt_as_date_string(min);
91 this._datepicker.max = Private.dt_as_date_string(max);
92 this._timepicker.disabled = this.model.get('disabled');
93 // Don't support min/max time here.
94 // It could be added by enabling min time if value is min date,
95 // and enabling max time if value is max date, but leave as
96 // TODO for now.
97 }
98 }
99 }
100 events() {
101 // Typescript doesn't understand that these functions are called, so we
102 // specifically use them here so it knows they are being used.
103 void this._picker_change;
104 void this._picker_focusout;
105 return {
106 'change [type="date"]': '_picker_change',
107 'change [type="time"]': '_picker_change',
108 'change [type="datetime-local"]': '_picker_change',
109 'focusout [type="date"]': '_picker_focusout',
110 'focusout [type="datetime-local"]': '_picker_focusout',
111 'focusout [type="time"]': '_picker_focusout',
112 };
113 }
114 _update_value(model, newValue, options) {
115 if (options === undefined || options.updated_view !== this) {
116 const value = this.model.get('value');
117 if (this._datetimepicker) {
118 this._datetimepicker.value = Private.dt_as_dt_string(value);
119 }
120 else {
121 this._datepicker.valueAsDate = value;
122 this._timepicker.value = Private.dt_as_time_string(value);
123 }
124 }
125 }
126 _picker_change() {
127 if (this._datetimepicker) {
128 if (!this._datetimepicker.validity.badInput) {
129 const v = this._datetimepicker.value;
130 let date = v ? new Date(v) : null;
131 if (date && isNaN(date.valueOf())) {
132 date = null;
133 }
134 this.model.set('value', date, { updated_view: this });
135 this.touch();
136 }
137 }
138 else {
139 if (!this._datepicker.validity.badInput &&
140 !this._timepicker.validity.badInput) {
141 const date = this._datepicker.valueAsDate;
142 const time = serialize_time(this._timepicker.value);
143 if (date !== null && time !== null) {
144 // * Use local time *
145 date.setHours(time.hours, time.minutes, time.seconds, time.milliseconds);
146 }
147 this.model.set('value', time !== null && date, { updated_view: this });
148 this.touch();
149 }
150 }
151 }
152 _picker_focusout() {
153 const pickers = [this._datetimepicker, this._datepicker, this._timepicker];
154 if (pickers.some((p) => p && p.validity.badInput)) {
155 this.model.set('value', null);
156 this.touch();
157 }
158 }
159 }
160 var Private;
161 (function (Private) {
162 // eslint-disable-next-line no-inner-declarations
163 function dt_as_dt_string(value) {
164 if (value === null) {
165 return '';
166 }
167 // Replicate `toISOString()` but in local time zone:
168 const parts = [];
169 parts.push(`${value.getFullYear().toString().padStart(4, '0')}`);
170 parts.push(`-${(value.getMonth() + 1).toString().padStart(2, '0')}`);
171 parts.push(`-${value.getDate().toString().padStart(2, '0')}`);
172 parts.push(`T${value.getHours().toString().padStart(2, '0')}`);
173 parts.push(`:${value.getMinutes().toString().padStart(2, '0')}`);
174 if (value.getSeconds() > 0 || value.getMilliseconds() > 0) {
175 parts.push(`:${value.getSeconds().toString().padStart(2, '0')}`);
176 if (value.getMilliseconds() > 0) {
177 parts.push(`.${value.getMilliseconds().toString().padStart(3, '0')}`);
178 }
179 }
180 return parts.join('');
181 }
182 Private.dt_as_dt_string = dt_as_dt_string;
183 // eslint-disable-next-line no-inner-declarations
184 function dt_as_date_string(value) {
185 return value ? dt_as_dt_string(value).split('T', 2)[0] : '';
186 }
187 Private.dt_as_date_string = dt_as_date_string;
188 // eslint-disable-next-line no-inner-declarations
189 function dt_as_time_string(value) {
190 return value ? dt_as_dt_string(value).split('T', 2)[1] : '';
191 }
192 Private.dt_as_time_string = dt_as_time_string;
193 })(Private || (Private = {}));
194 export function serialize_naive(value) {
195 if (value === null) {
196 return null;
197 }
198 else {
199 return {
200 year: value.getFullYear(),
201 month: value.getMonth(),
202 date: value.getDate(),
203 hours: value.getHours(),
204 minutes: value.getMinutes(),
205 seconds: value.getSeconds(),
206 milliseconds: value.getMilliseconds(),
207 };
208 }
209 }
210 export function deserialize_naive(value) {
211 if (value === null) {
212 return null;
213 }
214 else {
215 const date = new Date();
216 date.setFullYear(value.year, value.month, value.date);
217 date.setHours(value.hours, value.minutes, value.seconds, value.milliseconds);
218 return date;
219 }
220 }
221 export const naive_serializers = {
222 serialize: serialize_naive,
223 deserialize: deserialize_naive,
224 };
225 export class NaiveDatetimeModel extends DatetimeModel {
226 defaults() {
227 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'NaiveDatetimeModel' });
228 }
229 }
230 NaiveDatetimeModel.serializers = Object.assign(Object.assign({}, CoreDescriptionModel.serializers), { value: naive_serializers, min: naive_serializers, max: naive_serializers });
231 //# sourceMappingURL=widget_datetime.js.map