From 9ef266b73cefebe93bb114a6115273dee8127e1f Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Mon, 18 Jul 2016 23:23:53 +0200 Subject: [PATCH] Quick fix for the frontend unavailable dates issue (broken after jquery ui update). --- src/assets/js/frontend_book.js | 7 ++++ src/assets/js/frontend_book_api.js | 67 +++++++++++++++++++----------- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/assets/js/frontend_book.js b/src/assets/js/frontend_book.js index 8d879990..497979fd 100644 --- a/src/assets/js/frontend_book.js +++ b/src/assets/js/frontend_book.js @@ -328,6 +328,13 @@ window.FrontendBook = window.FrontendBook || {}; $('.captcha-title small').click(function(event) { $('.captcha-image').attr('src', GlobalVariables.baseUrl + '/index.php/captcha?' + Date.now()); }); + + + $('#select-date').on('mousedown', '.ui-datepicker-calendar td', function(event) { + setTimeout(function() { + FrontendBookApi.applyPreviousUnavailableDates(); // New jQuery UI version will replace the td elements. + }, 300); // There is no draw event unfortunately. + }) }; /** diff --git a/src/assets/js/frontend_book_api.js b/src/assets/js/frontend_book_api.js index 2457e0c9..7564208e 100644 --- a/src/assets/js/frontend_book_api.js +++ b/src/assets/js/frontend_book_api.js @@ -22,6 +22,9 @@ window.FrontendBookApi = window.FrontendBookApi || {}; 'use strict'; + var unavailableDatesBackup; + var selectedDateStringBackup; + /** * Get Available Hours * @@ -212,33 +215,47 @@ window.FrontendBookApi = window.FrontendBookApi || {}; dataType: 'json' }) .done(function(response) { - // Select first enabled date. - var selectedDate = Date.parse(selectedDateString); - var numberOfDays = new Date(selectedDate.getFullYear(), selectedDate.getMonth() + 1, 0).getDate(); - - for (var i=1; i<=numberOfDays; i++) { - var currentDate = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), i); - if ($.inArray(currentDate.toString('yyyy-MM-dd'), response) === -1) { - $('#select-date').datepicker('setDate', currentDate); - FrontendBookApi.getAvailableHours(currentDate.toString('yyyy-MM-dd')); - break; - } - } - - // If all the days are unavailable then hide the appointments hours. - if (response.length === numberOfDays) { - $('#available-hours').text(EALang['no_available_hours']); - } - - // Grey out unavailable dates. - $('#select-date .ui-datepicker-calendar td:not(.ui-datepicker-other-month)').each(function(index, td) { - selectedDate.set({day: index + 1}); - if ($.inArray(selectedDate.toString('yyyy-MM-dd'), response) != -1) { - $(td).addClass('ui-datepicker-unselectable ui-state-disabled'); - } - }); + unavailableDatesBackup = response; + selectedDateStringBackup = selectedDateString; + _applyUnavailableDates(response, selectedDateString); }) .fail(GeneralFunctions.ajaxFailureHandler); }; + exports.applyPreviousUnavailableDates = function() { + _applyUnavailableDates(unavailableDatesBackup, selectedDateStringBackup); + }; + + function _applyUnavailableDates(unavailableDates, selectedDateString, setDate) { + setDate = setDate || false; + + // Select first enabled date. + var selectedDate = Date.parse(selectedDateString); + var numberOfDays = new Date(selectedDate.getFullYear(), selectedDate.getMonth() + 1, 0).getDate(); + + if (setDate) { + for (var i=1; i<=numberOfDays; i++) { + var currentDate = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), i); + if ($.inArray(currentDate.toString('yyyy-MM-dd'), unavailableDates) === -1) { + $('#select-date').datepicker('setDate', currentDate); + FrontendBookApi.getAvailableHours(currentDate.toString('yyyy-MM-dd')); + break; + } + } + } + + // If all the days are unavailable then hide the appointments hours. + if (unavailableDates.length === numberOfDays) { + $('#available-hours').text(EALang['no_available_hours']); + } + + // Grey out unavailable dates. + $('#select-date .ui-datepicker-calendar td:not(.ui-datepicker-other-month)').each(function(index, td) { + selectedDate.set({day: index + 1}); + if ($.inArray(selectedDate.toString('yyyy-MM-dd'), unavailableDates) != -1) { + $(td).addClass('ui-datepicker-unselectable ui-state-disabled'); + } + }); + } + })(window.FrontendBookApi);