Skip to content

Commit 9f6af04

Browse files
Ahmet OeztuerkAhmet Oeztuerk
authored andcommitted
datetime picker: parseDate function checks if the given date string is typed up until the hour and minute.
Only then it returns a valid javascript Date. This prevents it from padding the missing temporal fields with zeros. It only calls the input field update if the date object exists . Since the returned date is undefined if its invalid, this prevents updating the input field back. Typing feels smoother now since it does not pad the field with zeros.
1 parent 6933547 commit 9f6af04

1 file changed

Lines changed: 42 additions & 20 deletions

File tree

root/thruk/javascript/thruk-3.26.js

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8873,24 +8873,37 @@ function show_cal(ev) {
88738873
}
88748874
}
88758875

8876-
var _parseDate = function(date_val) {
8877-
var date_time = date_val.split(" ");
8878-
if(date_time.length == 1) { date_time[1] = "0:0:0"; }
8879-
if(date_time.length == 2) {
8880-
var dates = date_time[0].split('-');
8881-
if(dates[2] == undefined) {
8882-
return;
8883-
}
8884-
var times = date_time[1].split(':');
8885-
if(times[1] == undefined) {
8886-
times = new Array(0,0,0);
8887-
}
8888-
if(times[2] == undefined) {
8889-
times[2] = 0;
8890-
}
8891-
return(new Date(dates[0], (dates[1]-1), dates[2], times[0], times[1], times[2]));
8876+
// require_time: if toggled, function needs typing the hour and minute fully to return a date
8877+
var _parseDate = function(date_val, require_time) {
8878+
if(!date_val) {
8879+
return;
88928880
}
8893-
return;
8881+
date_val = jQuery.trim(date_val);
8882+
// four digits (year), slash , two digits (month), shash , two digits (day) , at least one spacing ,
8883+
// two digits (hour) , semicolon , two digits (minute)
8884+
// optional: semicolon , two digits (second)
8885+
var match = date_val.match(/^(\d{4})-(\d{2})-(\d{2})(?:\s+(\d{2}):(\d{2})(?::(\d{2}))?)?$/);
8886+
if(!match) {
8887+
return;
8888+
}
8889+
if(require_time && (match[4] == undefined || match[5] == undefined)) {
8890+
return;
8891+
}
8892+
8893+
var year = parseInt(match[1], 10);
8894+
var month = parseInt(match[2], 10);
8895+
var day = parseInt(match[3], 10);
8896+
var hour = match[4] != undefined ? parseInt(match[4], 10) : 0;
8897+
var minute = match[5] != undefined ? parseInt(match[5], 10) : 0;
8898+
var second = match[6] != undefined ? parseInt(match[6], 10) : 0;
8899+
var date = new Date(year, (month-1), day, hour, minute, second);
8900+
8901+
// javascript keeps months indexed from zero
8902+
if(date.getFullYear() !== year || date.getMonth() !== (month-1) || date.getDate() !== day
8903+
|| date.getHours() !== hour || date.getMinutes() !== minute || date.getSeconds() !== second) {
8904+
return;
8905+
}
8906+
return(date);
88948907
};
88958908

88968909
var date1 = _parseDate(document.getElementById(id1).value);
@@ -8986,9 +8999,10 @@ function show_cal(ev) {
89868999
ev.stopImmediatePropagation();
89879000
ev.stopPropagation();
89889001
var picker = ev.data[0];
8989-
var date1 = _parseDate(document.getElementById(id1).value);
9002+
var date1 = _parseDate(document.getElementById(id1).value, true);
9003+
var changed = false;
89909004
if(hasRange) {
8991-
date2 = _parseDate(document.getElementById(id2).value);
9005+
date2 = _parseDate(document.getElementById(id2).value, true);
89929006
}
89939007
// reverse dates, because we always click on the end date when having ranges
89949008
if(date1 && date2 && date1 > date2) {
@@ -8999,6 +9013,7 @@ function show_cal(ev) {
89999013
if(date1) {
90009014
var date = moment(date1);
90019015
picker.setStartDate(date);
9016+
changed = true;
90029017
if(!hasRange) {
90039018
picker.setEndDate(date);
90049019
}
@@ -9007,9 +9022,16 @@ function show_cal(ev) {
90079022
if(date2) {
90089023
var date = moment(date2);
90099024
picker.setEndDate(date);
9025+
changed = true;
9026+
}
9027+
}
9028+
if(changed) {
9029+
if(picker._updateView) {
9030+
picker._updateView();
9031+
} else {
9032+
picker.updateView();
90109033
}
90119034
}
9012-
picker.updateView();
90139035
};
90149036

90159037
var _onShow = function(ev, picker) {

0 commit comments

Comments
 (0)