Debian Codegraph @

jupyter-widgets-controls/lib/widget_time.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 const PARSER = /(\d\d):(\d\d)(:(\d\d)(.(\d{1,3})\d*)?)?/;
7 export function serialize_time(value) {
8 if (value === null) {
9 return null;
10 }
11 else {
12 const res = PARSER.exec(value);
13 if (res === null) {
14 return null;
15 }
16 return {
17 hours: Math.min(23, parseInt(res[1], 10)),
18 minutes: Math.min(59, parseInt(res[2], 10)),
19 seconds: res[4] ? Math.min(59, parseInt(res[4], 10)) : 0,
20 milliseconds: res[6] ? parseInt(res[6], 10) : 0,
21 };
22 }
23 }
24 export function deserialize_time(value) {
25 if (value === null) {
26 return null;
27 }
28 else {
29 const parts = [
30 `${value.hours.toString().padStart(2, '0')}:${value.minutes
31 .toString()
32 .padStart(2, '0')}`,
33 ];
34 if (value.seconds > 0 || value.milliseconds > 0) {
35 parts.push(`:${value.seconds.toString().padStart(2, '0')}`);
36 if (value.milliseconds > 0) {
37 parts.push(`.${value.milliseconds.toString().padStart(3, '0')}`);
38 }
39 }
40 return parts.join('');
41 }
42 }
43 export const time_serializers = {
44 serialize: serialize_time,
45 deserialize: deserialize_time,
46 };
47 export class TimeModel extends CoreDescriptionModel {
48 defaults() {
49 return Object.assign(Object.assign({}, super.defaults()), { _model_name: TimeModel.model_name, _view_name: TimeModel.view_name, value: null, disabled: false, min: null, max: null, step: 60 });
50 }
51 }
52 TimeModel.serializers = Object.assign(Object.assign({}, CoreDescriptionModel.serializers), { value: time_serializers, min: time_serializers, max: time_serializers });
53 TimeModel.model_name = 'TimeModel';
54 TimeModel.view_name = 'TimeView';
55 export class TimeView extends DescriptionView {
56 render() {
57 super.render();
58 this.el.classList.add('jupyter-widgets');
59 this.el.classList.add('widget-inline-hbox');
60 this.el.classList.add('widget-timepicker');
61 this._timepicker = document.createElement('input');
62 this._timepicker.setAttribute('type', 'time');
63 this._timepicker.id = this.label.htmlFor = uuid();
64 this.el.appendChild(this._timepicker);
65 this.listenTo(this.model, 'change:value', this._update_value);
66 this.listenTo(this.model, 'change', this.update2);
67 this._update_value();
68 this.update2();
69 }
70 /**
71 * Update the contents of this view
72 *
73 * Called when the model is changed. The model may have been
74 * changed by another view or by a state update from the back-end.
75 */
76 update2(model, options) {
77 if (options === undefined || options.updated_view !== this) {
78 this._timepicker.disabled = this.model.get('disabled');
79 this._timepicker.min = this.model.get('min');
80 this._timepicker.max = this.model.get('max');
81 this._timepicker.step = this.model.get('step');
82 }
83 return super.update();
84 }
85 events() {
86 // Typescript doesn't understand that these functions are called, so we
87 // specifically use them here so it knows they are being used.
88 void this._picker_change;
89 void this._picker_focusout;
90 return {
91 'change [type="time"]': '_picker_change',
92 'focusout [type="time"]': '_picker_focusout',
93 };
94 }
95 _update_value(model, newValue, options) {
96 if (options === undefined || options.updated_view !== this) {
97 this._timepicker.value = this.model.get('value');
98 }
99 }
100 _picker_change() {
101 if (!this._timepicker.validity.badInput) {
102 this.model.set('value', this._timepicker.value, { updated_view: this });
103 this.touch();
104 }
105 }
106 _picker_focusout() {
107 if (this._timepicker.validity.badInput) {
108 this.model.set('value', null, { updated_view: this });
109 this.touch();
110 }
111 }
112 }
113 //# sourceMappingURL=widget_time.js.map