Bug 29478: Today should select now for pastinclusive

If Today is clicked when we only allow dates in the past and today/now,
we should select the current date/time
We need to update the maxDate to make it up-to-date, or the maxDate may
be set to the minute before and clicking Today will blank the input.

Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Jonathan Druart 2021-11-17 12:19:50 +01:00
parent 58cec67dbd
commit 03884cc9f9

View file

@ -101,18 +101,23 @@
label: __("or"), label: __("or"),
onClick: (index, fp) => { onClick: (index, fp) => {
let date; let date;
let hh = 23, mm = 59;
switch (index) { switch (index) {
case 0: case 0:
date = new Date().fp_incr(-1); date = new Date().fp_incr(-1);
break; break;
case 1: case 1:
date = new Date(); date = new Date();
if ( $(fp.input).data("flatpickr-pastinclusive") === true ) {
hh = date.getHours();
mm = date.getMinutes();
}
break; break;
case 2: case 2:
date = new Date().fp_incr(1); date = new Date().fp_incr(1);
break; break;
} }
date.setHours(23, 59, 0, 0); date.setHours(hh, mm, 0, 0);
fp.setDate(date); fp.setDate(date);
} }
}) })
@ -121,6 +126,7 @@
$(document).ready(function(){ $(document).ready(function(){
$(".flatpickr").each(function(){ $(".flatpickr").each(function(){
let options = {}; let options = {};
let refresh_max_date = 0;
if( $(this).data("flatpickr-futuredate") === true ) { if( $(this).data("flatpickr-futuredate") === true ) {
let original_date = $(this).val(); let original_date = $(this).val();
@ -139,7 +145,8 @@
} }
} }
if( $(this).data("flatpickr-pastinclusive") === true ) { if( $(this).data("flatpickr-pastinclusive") === true ) {
options['maxDate'] = "today"; options['maxDate'] = new Date(); /* Not today or hh:mm will be 00:00 */
refresh_max_date = 1;
} }
if( $(this).data("flatpickr-pastdate") === true ) { if( $(this).data("flatpickr-pastdate") === true ) {
options['maxDate'] = new Date().fp_incr(-1); options['maxDate'] = new Date().fp_incr(-1);
@ -149,7 +156,12 @@
options['dateFormat'] = flatpickr_dateformat_string + " " + flatpickr_timeformat_string; options['dateFormat'] = flatpickr_dateformat_string + " " + flatpickr_timeformat_string;
} }
$(this).flatpickr(options); let fp = $(this).flatpickr(options);
if ( refresh_max_date ) {
/* Refresh the maxDate every 30 secondes to make sure the user will not
be stuck with the minute passed */
setInterval(() => { fp.set("maxDate", new Date()) }, 30000);
}
}); });
}); });
</script> </script>