|
| 1 | +/* ************************************************************************ |
| 2 | +
|
| 3 | + osparc - the simcore frontend |
| 4 | +
|
| 5 | + https://osparc.io |
| 6 | +
|
| 7 | + Copyright: |
| 8 | + 2022 IT'IS Foundation, https://itis.swiss |
| 9 | +
|
| 10 | + License: |
| 11 | + MIT: https://opensource.org/licenses/MIT |
| 12 | +
|
| 13 | + Authors: |
| 14 | + * Odei Maiz (odeimaiz) |
| 15 | +
|
| 16 | +************************************************************************ */ |
| 17 | + |
| 18 | +qx.Class.define("demo.widget.DateTimeField", { |
| 19 | + extend: qx.ui.core.Widget, |
| 20 | + include: [qx.ui.form.MForm], |
| 21 | + implement: [qx.ui.form.IForm, qx.ui.form.IStringForm], |
| 22 | + |
| 23 | + construct: function() { |
| 24 | + this.base(arguments); |
| 25 | + |
| 26 | + // Layout |
| 27 | + var layout = new qx.ui.layout.HBox(5); |
| 28 | + this._setLayout(layout); |
| 29 | + |
| 30 | + // Date selector |
| 31 | + this.__dateField = new qx.ui.form.DateField(); |
| 32 | + this._add(this.__dateField, {flex: 1}); |
| 33 | + |
| 34 | + // Hour selector |
| 35 | + this.__hourSpinner = new qx.ui.form.Spinner(0, 12, 23); |
| 36 | + this.__hourSpinner.setWidth(60); |
| 37 | + this._add(this.__hourSpinner); |
| 38 | + |
| 39 | + // Minute selector |
| 40 | + this.__minuteSpinner = new qx.ui.form.Spinner(0, 0, 59); |
| 41 | + this.__minuteSpinner.setWidth(60); |
| 42 | + this._add(this.__minuteSpinner); |
| 43 | + |
| 44 | + // Sync changes back to value |
| 45 | + this.__dateField.addListener("changeValue", this.__updateValue, this); |
| 46 | + this.__hourSpinner.addListener("changeValue", this.__updateValue, this); |
| 47 | + this.__minuteSpinner.addListener("changeValue", this.__updateValue, this); |
| 48 | + }, |
| 49 | + |
| 50 | + properties: { |
| 51 | + // The combined Date value |
| 52 | + value: { |
| 53 | + check: "Date", |
| 54 | + nullable: true, |
| 55 | + event: "changeValue", |
| 56 | + apply: "_applyValue" |
| 57 | + } |
| 58 | + }, |
| 59 | + |
| 60 | + members: { |
| 61 | + __dateField: null, |
| 62 | + __hourSpinner: null, |
| 63 | + __minuteSpinner: null, |
| 64 | + |
| 65 | + _applyValue: function(value, old) { |
| 66 | + if (value) { |
| 67 | + this.__dateField.setValue(value); |
| 68 | + this.__hourSpinner.setValue(value.getHours()); |
| 69 | + this.__minuteSpinner.setValue(value.getMinutes()); |
| 70 | + } else { |
| 71 | + this.__dateField.resetValue(); |
| 72 | + this.__hourSpinner.resetValue(); |
| 73 | + this.__minuteSpinner.resetValue(); |
| 74 | + } |
| 75 | + }, |
| 76 | + |
| 77 | + __updateValue: function() { |
| 78 | + const date = this.__dateField.getValue(); |
| 79 | + if (date) { |
| 80 | + const newDate = new Date(date.getTime()); |
| 81 | + newDate.setHours(this.__hourSpinner.getValue()); |
| 82 | + newDate.setMinutes(this.__minuteSpinner.getValue()); |
| 83 | + this.setValue(newDate); |
| 84 | + } else { |
| 85 | + this.resetValue(); |
| 86 | + } |
| 87 | + }, |
| 88 | + |
| 89 | + // Interface methods (IStringForm) |
| 90 | + setValueAsString: function(str) { |
| 91 | + const d = new Date(str); |
| 92 | + if (!isNaN(d.getTime())) { |
| 93 | + this.setValue(d); |
| 94 | + } |
| 95 | + }, |
| 96 | + |
| 97 | + getValueAsString: function() { |
| 98 | + const v = this.getValue(); |
| 99 | + return v ? v.toISOString() : ""; |
| 100 | + } |
| 101 | + }, |
| 102 | + |
| 103 | + destruct: function() { |
| 104 | + this.__dateField = null; |
| 105 | + this.__hourSpinner = null; |
| 106 | + this.__minuteSpinner = null; |
| 107 | + } |
| 108 | +}); |
0 commit comments