From 6795ab236a9bb5ebe44779af4050fdb5783cbb02 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 4 Nov 2021 13:22:20 +0000 Subject: [PATCH] Bug 29450: Allow flatpickr to instantiate in any order This patch updates the flatpickr wrapper code to allow for cases where the startdate/enddate pickers are pre-populated and thus the onClose event of the startdate pickr may be fired before the enddate flatpickr is instantiated.. which leads to an error. --- .../prog/en/includes/calendar.inc | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc index 6464b8a0f5..aa4e13a6e2 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc @@ -74,22 +74,30 @@ var thisInput = instance.input; if ( thisInput.hasAttribute('data-date_to') ) { var endPicker = document.querySelector("#"+thisInput.dataset.date_to)._flatpickr; + if ( typeof endPicker === 'undefined' ) { + endPicker = document.querySelector("#"+thisInput.dataset.date_to).flatpickr(); + } endPicker.set('minDate', selectedDates[0]); } }, }); $(document).ready(function(){ $(".flatpickr").each(function(){ - let options = {}; + + var thisPicker; + if ( $(this).get(0)._flatpickr ) { + thisPicker = $(this).get(0)._flatpickr; + } else { + thisPicker = $(this).flatpickr(); + } if( $(this).hasClass("futuredate") ) { - options['minDate'] = new Date().fp_incr(1); - options['allowInvalidPreload'] = true; + thisPicker.set('minDate', new Date().fp_incr(1)); + thisPicker.set('allowInvalidPreload', true); } if( $(this).hasClass("pastdate") ) { - options['maxDate'] = new Date().fp_incr(-1); + thisPicker.set('maxDate', new Date().fp_incr(-1)); } - $(this).flatpickr(options); }); }); -- 2.20.1