From 46ef8b15945a5439752168b8ef05873e502f65f3 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 26 Mar 2024 13:38:34 +0000 Subject: [PATCH] Bug 34440: Simplify modal code using isSameOr* functions Sponsored-by: Cuyahoga County Public Library Signed-off-by: Kristi Krueger Signed-off-by: Paul Derscheid Signed-off-by: Katrin Fischer --- .../prog/en/modules/bookings/list.tt | 5 +- .../prog/en/modules/catalogue/ISBDdetail.tt | 5 + .../prog/en/modules/catalogue/MARCdetail.tt | 5 + .../prog/en/modules/catalogue/detail.tt | 6 +- .../prog/en/modules/catalogue/imageviewer.tt | 5 + .../en/modules/catalogue/labeledMARCdetail.tt | 5 + .../prog/en/modules/catalogue/moredetail.tt | 4 + .../prog/js/modals/place_booking.js | 127 ++++++------------ 8 files changed, 75 insertions(+), 87 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/bookings/list.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/bookings/list.tt index 0964226894..ce2aaeb006 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/bookings/list.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/bookings/list.tt @@ -63,7 +63,6 @@ [% INCLUDE modals/cancel_booking.inc %] [% MACRO jsinclude BLOCK %] - [% Asset.js("lib/dayjs/dayjs.min.js") | $raw %] [% Asset.js("lib/vis-timeline/vis-timeline-graph2d.min.js") | $raw %] [% INCLUDE 'datatables.inc' %] [% INCLUDE 'columns_settings.inc' %] @@ -71,6 +70,10 @@ [% INCLUDE 'select2.inc' %] [% INCLUDE 'js-patron-format.inc' %] [% INCLUDE 'js-date-format.inc' %] + [% Asset.js("lib/dayjs/plugin/isSameOrAfter.js") | $raw %] + [% Asset.js("lib/dayjs/plugin/isSameOrBefore.js") | $raw %] + + [% Asset.js("js/modals/place_booking.js") | $raw %] [% Asset.js("js/cancel_booking_modal.js") | $raw %] + [% INCLUDE 'calendar.inc' %] [% INCLUDE 'select2.inc' %] [% Asset.js("js/catalog.js") | $raw %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/MARCdetail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/MARCdetail.tt index 1a81061a54..7ea162a6a0 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/MARCdetail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/MARCdetail.tt @@ -212,6 +212,11 @@ [% MACRO jsinclude BLOCK %] [% INCLUDE 'catalog-strings.inc' %] + [% Asset.js("lib/dayjs/dayjs.min.js") | $raw %] + [% Asset.js("lib/dayjs/plugin/isSameOrAfter.js") | $raw %] + [% Asset.js("lib/dayjs/plugin/isSameOrBefore.js") | $raw %] + + [% INCLUDE 'calendar.inc' %] [% INCLUDE 'select2.inc' %] [% Asset.js("js/catalog.js") | $raw %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt index 6c2048aee8..3dbdda4a30 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt @@ -1207,8 +1207,8 @@ [% END %] [% MACRO jsinclude BLOCK %] - [% INCLUDE 'catalog-strings.inc' %] [% INCLUDE 'calendar.inc' %] + [% INCLUDE 'catalog-strings.inc' %] [% INCLUDE 'select2.inc' %] [% INCLUDE 'js-date-format.inc' %] [% Asset.js("js/catalog.js") | $raw %] @@ -1639,6 +1639,10 @@ [% Asset.js("lib/jquery/plugins/jquery.dataTables.columnFilter.js") | $raw %] [% INCLUDE 'columns_settings.inc' %] [% INCLUDE 'js-date-format.inc' %] + [% Asset.js("lib/dayjs/plugin/isSameOrAfter.js") | $raw %] + [% Asset.js("lib/dayjs/plugin/isSameOrBefore.js") | $raw %] + + [% INCLUDE 'js-patron-format.inc' %] [% INCLUDE 'js-biblio-format.inc' %] [% Asset.js("js/browser.js") | $raw %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/imageviewer.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/imageviewer.tt index c71890fd21..4db3fd50e8 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/imageviewer.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/imageviewer.tt @@ -139,6 +139,11 @@ [% MACRO jsinclude BLOCK %] [% INCLUDE 'catalog-strings.inc' %] + [% Asset.js("lib/dayjs/dayjs.min.js") | $raw %] + [% Asset.js("lib/dayjs/plugin/isSameOrAfter.js") | $raw %] + [% Asset.js("lib/dayjs/plugin/isSameOrBefore.js") | $raw %] + + [% INCLUDE 'calendar.inc' %] [% INCLUDE 'select2.inc' %] [% Asset.js("js/catalog.js") | $raw %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/labeledMARCdetail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/labeledMARCdetail.tt index 1596ab0a18..9e560acc6f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/labeledMARCdetail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/labeledMARCdetail.tt @@ -100,6 +100,11 @@ [% MACRO jsinclude BLOCK %] [% INCLUDE 'catalog-strings.inc' %] + [% Asset.js("lib/dayjs/dayjs.min.js") | $raw %] + [% Asset.js("lib/dayjs/plugin/isSameOrAfter.js") | $raw %] + [% Asset.js("lib/dayjs/plugin/isSameOrBefore.js") | $raw %] + + [% INCLUDE 'calendar.inc' %] [% INCLUDE 'select2.inc' %] [% Asset.js("js/catalog.js") | $raw %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/moredetail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/moredetail.tt index 833b908426..3a73ab0229 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/moredetail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/moredetail.tt @@ -518,6 +518,10 @@ [% INCLUDE 'catalog-strings.inc' %] [% INCLUDE 'modals/checkout_renewals.inc' %] [% INCLUDE 'js-date-format.inc' %] + [% Asset.js("lib/dayjs/plugin/isSameOrAfter.js") | $raw %] + [% Asset.js("lib/dayjs/plugin/isSameOrBefore.js") | $raw %] + + [% INCLUDE 'js-patron-format.inc' %] [% INCLUDE 'calendar.inc' %] [% INCLUDE 'select2.inc' %] diff --git a/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js b/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js index f4b1daf6fd..f2692b4363 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js +++ b/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js @@ -760,46 +760,28 @@ $("#placeBookingModal").on("show.bs.modal", function (e) { } // Add hints for days before the start range and after the end range - let leadDays = 2; - let trailDays = 3; + const leadDays = 2; + const trailDays = 3; periodPicker.calendarContainer.addEventListener( "mouseover", - function (e) { - const target = e.target; - const startDate = periodPicker.selectedDates[0] - ? dayjs(periodPicker.selectedDates[0]).startOf( - "day" - ) - : null; - + function (event) { + const target = event.target; if (target.classList.contains("flatpickr-day")) { const hoverDate = dayjs(target.dateObj).startOf( "day" ); - let leadStart; - let leadEnd; - const trailStart = hoverDate - .add(0, "day") - .startOf("day"); - const trailEnd = hoverDate - .add(trailDays, "day") - .startOf("day"); - - if (!startDate) { - leadStart = hoverDate - .subtract(leadDays, "day") - .startOf("day"); - leadEnd = hoverDate - .subtract(0, "day") - .startOf("day"); - } else { - leadStart = startDate - .subtract(leadDays, "day") - .startOf("day"); - leadEnd = startDate - .subtract(0, "day") - .startOf("day"); - } + const startDate = periodPicker.selectedDates[0] + ? dayjs(periodPicker.selectedDates[0]).startOf( + "day" + ) + : null; + + const leadStart = startDate + ? startDate.subtract(leadDays, "day") + : hoverDate.subtract(leadDays, "day"); + const leadEnd = startDate ? startDate : hoverDate; + const trailStart = hoverDate; + const trailEnd = hoverDate.add(trailDays, "day"); periodPicker.calendarContainer .querySelectorAll(".flatpickr-day") @@ -808,57 +790,32 @@ $("#placeBookingModal").on("show.bs.modal", function (e) { dayElem.dateObj ).startOf("day"); - if (elemDate.isSame(leadStart)) { - dayElem.classList.add("leadRangeStart"); - } else { - dayElem.classList.remove( - "leadRangeStart" - ); - } - - if ( - elemDate >= leadStart && - elemDate < leadEnd - ) { - dayElem.classList.add("leadRange"); - } else { - dayElem.classList.remove("leadRange"); - } - - if (elemDate.isSame(leadEnd)) { - dayElem.classList.add("leadRangeEnd"); - } else { - dayElem.classList.remove( - "leadRangeEnd" - ); - } - - if (elemDate.isSame(trailStart)) { - dayElem.classList.add( - "trailRangeStart" - ); - } else { - dayElem.classList.remove( - "trailRangeStart" - ); - } - - if ( - elemDate > trailStart && - elemDate <= trailEnd - ) { - dayElem.classList.add("trailRange"); - } else { - dayElem.classList.remove("trailRange"); - } - - if (elemDate.isSame(trailEnd)) { - dayElem.classList.add("trailRangeEnd"); - } else { - dayElem.classList.remove( - "trailRangeEnd" - ); - } + dayElem.classList.toggle( + "leadRangeStart", + elemDate.isSame(leadStart) + ); + dayElem.classList.toggle( + "leadRange", + elemDate.isSameOrAfter(leadStart) && + elemDate.isBefore(leadEnd) + ); + dayElem.classList.toggle( + "leadRangeEnd", + elemDate.isSame(leadEnd) + ); + dayElem.classList.toggle( + "trailRangeStart", + elemDate.isSame(trailStart) + ); + dayElem.classList.toggle( + "trailRange", + elemDate.isAfter(trailStart) && + elemDate.isSameOrBefore(trailEnd) + ); + dayElem.classList.toggle( + "trailRangeEnd", + elemDate.isSame(trailEnd) + ); }); } } -- 2.39.5