From f51371360a025f5a922ce318537914774b713cc2 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 | 19 ++++++++++++++----- 1 file changed, 14 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..1636cf41dc 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc @@ -74,22 +74,31 @@ var thisInput = instance.input; if ( thisInput.hasAttribute('data-date_to') ) { var endPicker = document.querySelector("#"+thisInput.dataset.date_to)._flatpickr; + if ( typeof endPicker === 'undefined' ) { + console.log("Found non-existant picker"); + 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