Debian Codegraph @

jupyter-widgets-controls/lib/widget_date.js

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 import { DescriptionView } from './widget_description';
4 import { CoreDescriptionModel } from './widget_core';
5 import { uuid } from './utils';
6 export function serialize_date(value) {
7 if (value === null) {
8 return null;
9 }
10 else {
11 return {
12 year: value.getUTCFullYear(),
13 month: value.getUTCMonth(),
14 date: value.getUTCDate(),
15 };
16 }
17 }
18 export function deserialize_date(value) {
19 if (value === null) {
20 return null;
21 }
22 else {
23 const date = new Date();
24 date.setUTCFullYear(value.year, value.month, value.date);
25 date.setUTCHours(0, 0, 0, 0);
26 return date;
27 }
28 }
29 export class DatePickerModel extends CoreDescriptionModel {
30 defaults() {
31 return Object.assign(Object.assign({}, super.defaults()), { value: null, _model_name: 'DatePickerModel', _view_name: 'DatePickerView' });
32 }
33 }
34 DatePickerModel.serializers = Object.assign(Object.assign({}, CoreDescriptionModel.serializers), { value: {
35 serialize: serialize_date,
36 deserialize: deserialize_date,
37 } });
38 export class DatePickerView extends DescriptionView {
39 render() {
40 super.render();
41 this.el.classList.add('jupyter-widgets');
42 this.el.classList.add('widget-inline-hbox');
43 this.el.classList.add('widget-datepicker');
44 this._datepicker = document.createElement('input');
45 this._datepicker.setAttribute('type', 'date');
46 this._datepicker.id = this.label.htmlFor = uuid();
47 this.el.appendChild(this._datepicker);
48 this.listenTo(this.model, 'change:value', this._update_value);
49 this._update_value();
50 this.update();
51 }
52 /**
53 * Update the contents of this view
54 *
55 * Called when the model is changed. The model may have been
56 * changed by another view or by a state update from the back-end.
57 */
58 update(options) {
59 if (options === undefined || options.updated_view !== this) {
60 this._datepicker.disabled = this.model.get('disabled');
61 }
62 return super.update();
63 }
64 events() {
65 // Typescript doesn't understand that these functions are called, so we
66 // specifically use them here so it knows they are being used.
67 void this._picker_change;
68 void this._picker_focusout;
69 return {
70 'change [type="date"]': '_picker_change',
71 'focusout [type="date"]': '_picker_focusout',
72 };
73 }
74 _update_value() {
75 const value = this.model.get('value');
76 this._datepicker.valueAsDate = value;
77 }
78 _picker_change() {
79 if (!this._datepicker.validity.badInput) {
80 this.model.set('value', this._datepicker.valueAsDate);
81 this.touch();
82 }
83 }
84 _picker_focusout() {
85 if (this._datepicker.validity.badInput) {
86 this.model.set('value', null);
87 this.touch();
88 }
89 }
90 }
91 //# sourceMappingURL=widget_date.js.map