[pmg-devel] [PATCH widget-toolkit 1/1] DateTimeField: implement set{Max, Min}Value

Dominik Csapak d.csapak at proxmox.com
Fri Apr 24 15:57:52 CEST 2020


this sets the max/min value for the underlying date and time fields,
and allowing to only change the value respecting both current date
and time fields
e.g.
if the new maxValue is 2020-10-10 12:00
and the current value is 2020-10-09 15:00
do not allow to set the date to 2020-10-10
and vice versa (if the new limit is on the same day, limit the time range)

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 form/DateTimeField.js | 86 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)

diff --git a/form/DateTimeField.js b/form/DateTimeField.js
index 0362229..daca22c 100644
--- a/form/DateTimeField.js
+++ b/form/DateTimeField.js
@@ -49,6 +49,84 @@ Ext.define('Proxmox.DateTimeField', {
 	}
     ],
 
+    setMinValue: function(value) {
+	let me = this;
+	let current = me.getValue();
+	if (!value || !current) {
+	    return;
+	}
+
+	let minhours = value.getHours();
+	let minminutes = value.getMinutes();
+
+	let hours = current.getHours();
+	let minutes = current.getMinutes();
+
+	value.setHours(0);
+	value.setMinutes(0);
+	value.setSeconds(0);
+	current.setHours(0);
+	current.setMinutes(0);
+	current.setSeconds(0);
+
+	let time = new Date();
+	if (current-value > 0) {
+	    time.setHours(0);
+	    time.setMinutes(0);
+	    time.setSeconds(0);
+	    time.setMilliseconds(0);
+	} else {
+	    time.setHours(minhours);
+	    time.setMinutes(minminutes);
+	}
+	me.lookup('timeentry').setMinValue(time);
+
+	// current time is smaller than the time part of the new minimum
+	// so we have to add 1 to the day
+	if ((minhours*60+minminutes) > (hours*60+minutes)) {
+	    value.setDate(value.getDate()+1);
+	}
+	me.lookup('dateentry').setMinValue(value);
+    },
+
+    setMaxValue: function(value) {
+	let me = this;
+	let current = me.getValue();
+	if (!value || !current) {
+	    return;
+	}
+
+	let maxhours = value.getHours();
+	let maxminutes = value.getMinutes();
+
+	let hours = current.getHours();
+	let minutes = current.getMinutes();
+
+	value.setHours(0);
+	value.setMinutes(0);
+	current.setHours(0);
+	current.setMinutes(0);
+
+	let time = new Date();
+	if (value-current > 0) {
+	    time.setHours(23);
+	    time.setMinutes(59);
+	    time.setSeconds(59);
+	} else {
+	    time.setHours(maxhours);
+	    time.setMinutes(maxminutes);
+	}
+	me.lookup('timeentry').setMaxValue(time);
+
+	// current time is biger than the time part of the new maximum
+	// so we have to subtract 1 to the day
+	if ((maxhours*60+maxminutes) < (hours*60+minutes)) {
+	    value.setDate(value.getDate()-1);
+	}
+
+	me.lookup('dateentry').setMaxValue(value);
+    },
+
     initComponent: function() {
 	var me = this;
 
@@ -59,6 +137,14 @@ Ext.define('Proxmox.DateTimeField', {
 	me.lookupReference('dateentry').setValue(value);
 	me.lookupReference('timeentry').setValue(value);
 
+	if (me.minValue) {
+	    me.setMinValue(me.minValue);
+	}
+
+	if (me.maxValue) {
+	    me.setMaxValue(me.maxValue);
+	}
+
 	me.relayEvents(me.lookupReference('dateentry'), ['change']);
 	me.relayEvents(me.lookupReference('timeentry'), ['change']);
     }
-- 
2.20.1




More information about the pmg-devel mailing list