Skip to content

Commit

Permalink
Change behavior when changing dates in a datepicker group to be more …
Browse files Browse the repository at this point in the history
…intuitive.

Previously when any of the dates were changed, the open, reduced
scoring, close, and answer dates were ensured to be in the correct order
by always changing the input later in the last to a later date if
needed.

This instead acts on the input that was modified.  That input value is
not changed from what the user just changed it to.  Any earlier inputs
in the list are set to the new date in the changed input if they are
later than that date, and any later inputs in the list are set to the
new date in the changed input if they are earlier than that date.

This is to address issue #2581.
  • Loading branch information
drgrice1 committed Oct 3, 2024
1 parent 137ed73 commit 16d9c45
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions htdocs/js/DatePicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,21 @@
rule.push(value ? parseInt(value) * 1000 - timezoneAdjustment : 0);
}

const update = () => {
const update = (input) => {
const activeIndex = groupRules.findIndex((r) => r[0] === input);
if (activeIndex == -1) return;
const activeFieldDate =
groupRules[activeIndex][0]?.parentNode._flatpickr.selectedDates[0]?.getTime() ||
groupRules[activeIndex][1];

for (let i = 1; i < groupRules.length; ++i) {
const prevFieldDate =
groupRules[i - 1][0]?.parentNode._flatpickr.selectedDates[0]?.getTime() || groupRules[i - 1][1];
if (i == activeFieldDate) continue;
const thisFieldDate =
groupRules[i][0]?.parentNode._flatpickr.selectedDates[0]?.getTime() || groupRules[i][1];
if (prevFieldDate && thisFieldDate && prevFieldDate > thisFieldDate) {
groupRules[i][0].parentNode._flatpickr.setDate(prevFieldDate, true);
}
if (i < activeIndex && thisFieldDate > activeFieldDate)
groupRules[i][0].parentNode._flatpickr.setDate(activeFieldDate, true);
else if (i > activeIndex && thisFieldDate < activeFieldDate)
groupRules[i][0].parentNode._flatpickr.setDate(activeFieldDate, true);
}
};

Expand Down Expand Up @@ -115,7 +121,9 @@
if (this.input.value === orig_value) this.altInput.classList.remove('changed');
else this.altInput.classList.add('changed');
},
onClose: update,
onClose() {
return update(this.input);
},
onReady() {
// Flatpickr hides the original input and adds the alternate input after it. That messes up the
// bootstrap input group styling. So move the now hidden original input after the created alternate
Expand All @@ -133,7 +141,7 @@
// Make the alternate input left-to-right even for right-to-left languages.
this.altInput.dir = 'ltr';

this.altInput.addEventListener('blur', update);
this.altInput.addEventListener('blur', () => update(this.input));
},
parseDate(datestr, format) {
// Deal with the case of a unix timestamp. The timezone needs to be adjusted back as this is for
Expand Down

0 comments on commit 16d9c45

Please sign in to comment.