/* ---------------------------------------------------------------------------- * Easy!Appointments - Open Source Web Scheduler * * @package EasyAppointments * @author A.Tselegidis * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 * @link http://easyappointments.org * @since v1.0.0 * ---------------------------------------------------------------------------- */ window.FrontendBook = window.FrontendBook || {}; /** * Frontend Book * * This module contains functions that implement the book appointment page functionality. Once the * initialize() method is called the page is fully functional and can serve the appointment booking * process. * * @module FrontendBook */ (function (exports) { 'use strict'; /** * Contains terms and conditions consent. * * @type {Object} */ var termsAndConditionsConsent; /** * Contains privacy policy consent. * * @type {Object} */ var privacyPolicyConsent; /** * Determines the functionality of the page. * * @type {Boolean} */ exports.manageMode = false; /** * This method initializes the book appointment page. * * @param {Boolean} defaultEventHandlers (OPTIONAL) Determines whether the default * event handlers will be bound to the dom elements. * @param {Boolean} manageMode (OPTIONAL) Determines whether the customer is going * to make changes to an existing appointment rather than booking a new one. */ exports.initialize = function (defaultEventHandlers, manageMode) { defaultEventHandlers = defaultEventHandlers || true; manageMode = manageMode || false; if (GlobalVariables.displayCookieNotice) { cookieconsent.initialise({ palette: { popup: { background: '#ffffffbd', text: '#666666' }, button: { background: '#3DD481', text: '#ffffff' } }, content: { message: EALang.website_using_cookies_to_ensure_best_experience, dismiss: 'OK' }, }); $('.cc-link').replaceWith( $('', { 'data-toggle': 'modal', 'data-target': '#cookie-notice-modal', 'href': '#', 'class': 'cc-link', 'text': $('.cc-link').text() }) ); } FrontendBook.manageMode = manageMode; // Initialize page's components (tooltips, datepickers etc). $('.book-step').qtip({ position: { my: 'top center', at: 'bottom center' }, style: { classes: 'qtip-green qtip-shadow custom-qtip' } }); var fDay = GlobalVariables.firstWeekday; var fDaynum = GeneralFunctions.getWeekDayId(fDay); $('#select-date').datepicker({ dateFormat: 'dd-mm-yy', firstDay: fDaynum, minDate: 0, defaultDate: Date.today(), dayNames: [ EALang.sunday, EALang.monday, EALang.tuesday, EALang.wednesday, EALang.thursday, EALang.friday, EALang.saturday], dayNamesShort: [EALang.sunday.substr(0, 3), EALang.monday.substr(0, 3), EALang.tuesday.substr(0, 3), EALang.wednesday.substr(0, 3), EALang.thursday.substr(0, 3), EALang.friday.substr(0, 3), EALang.saturday.substr(0, 3)], dayNamesMin: [EALang.sunday.substr(0, 2), EALang.monday.substr(0, 2), EALang.tuesday.substr(0, 2), EALang.wednesday.substr(0, 2), EALang.thursday.substr(0, 2), EALang.friday.substr(0, 2), EALang.saturday.substr(0, 2)], monthNames: [EALang.january, EALang.february, EALang.march, EALang.april, EALang.may, EALang.june, EALang.july, EALang.august, EALang.september, EALang.october, EALang.november, EALang.december], prevText: EALang.previous, nextText: EALang.next, currentText: EALang.now, closeText: EALang.close, onSelect: function (dateText, instance) { FrontendBookApi.getAvailableHours($(this).datepicker('getDate').toString('yyyy-MM-dd')); FrontendBook.updateConfirmFrame(); }, onChangeMonthYear: function (year, month, instance) { var currentDate = new Date(year, month - 1, 1); FrontendBookApi.getUnavailableDates($('#select-provider').val(), $('#select-service').val(), currentDate.toString('yyyy-MM-dd')); } }); $('#select-timezone').val(Intl.DateTimeFormat().resolvedOptions().timeZone); // Bind the event handlers (might not be necessary every time we use this class). if (defaultEventHandlers) { bindEventHandlers(); } // If the manage mode is true, the appointments data should be loaded by default. if (FrontendBook.manageMode) { applyAppointmentData(GlobalVariables.appointmentData, GlobalVariables.providerData, GlobalVariables.customerData); } else { var $selectProvider = $('#select-provider'); var $selectService = $('#select-service'); // Check if a specific service was selected (via URL parameter). var selectedServiceId = GeneralFunctions.getUrlParameter(location.href, 'service'); if (selectedServiceId && $selectService.find('option[value="' + selectedServiceId + '"]').length > 0) { $selectService.val(selectedServiceId); } $selectService.trigger('change'); // Load the available hours. // Check if a specific provider was selected. var selectedProviderId = GeneralFunctions.getUrlParameter(location.href, 'provider'); if (selectedProviderId && $selectProvider.find('option[value="' + selectedProviderId + '"]').length === 0) { // Select a service of this provider in order to make the provider available in the select box. for (var index in GlobalVariables.availableProviders) { var provider = GlobalVariables.availableProviders[index]; if (provider.id === selectedProviderId && provider.services.length > 0) { $selectService .val(provider.services[0]) .trigger('change'); } } } if (selectedProviderId && $selectProvider.find('option[value="' + selectedProviderId + '"]').length > 0) { $selectProvider .val(selectedProviderId) .trigger('change'); } } }; /** * This method binds the necessary event handlers for the book appointments page. */ function bindEventHandlers() { /** * Event: Timezone "Changed" */ $('#select-timezone').on('change', function () { var date = $('#select-date').datepicker('getDate'); if (!date) { return; } FrontendBookApi.getAvailableHours(date.toString('yyyy-MM-dd')); FrontendBook.updateConfirmFrame(); }); /** * Event: Selected Provider "Changed" * * Whenever the provider changes the available appointment date - time periods must be updated. */ $('#select-provider').on('change', function () { FrontendBookApi.getUnavailableDates($(this).val(), $('#select-service').val(), $('#select-date').datepicker('getDate').toString('yyyy-MM-dd')); FrontendBook.updateConfirmFrame(); }); /** * Event: Selected Service "Changed" * * When the user clicks on a service, its available providers should * become visible. */ $('#select-service').on('change', function () { var serviceId = $('#select-service').val(); $('#select-provider').empty(); GlobalVariables.availableProviders.forEach(function (provider) { // If the current provider is able to provide the selected service, add him to the list box. var canServeService = provider.services.filter(function (providerServiceId) { return Number(providerServiceId) === Number(serviceId); }).length > 0; if (canServeService) { $('#select-provider').append(new Option(provider.first_name + ' ' + provider.last_name, provider.id)); } }); // Add the "Any Provider" entry. if ($('#select-provider option').length >= 1 && GlobalVariables.displayAnyProvider === '1') { $('#select-provider').append(new Option('- ' + EALang.any_provider + ' -', 'any-provider')); } FrontendBookApi.getUnavailableDates($('#select-provider').val(), $(this).val(), $('#select-date').datepicker('getDate').toString('yyyy-MM-dd')); FrontendBook.updateConfirmFrame(); updateServiceDescription(serviceId); }); /** * Event: Next Step Button "Clicked" * * This handler is triggered every time the user pressed the "next" button on the book wizard. * Some special tasks might be performed, depending the current wizard step. */ $('.button-next').on('click', function () { // If we are on the first step and there is not provider selected do not continue // with the next step. if ($(this).attr('data-step_index') === '1' && !$('#select-provider').val()) { return; } // If we are on the 2nd tab then the user should have an appointment hour // selected. if ($(this).attr('data-step_index') === '2') { if (!$('.selected-hour').length) { if (!$('#select-hour-prompt').length) { $('#available-hours').append('

' + '' + EALang.appointment_hour_missing + ''); } return; } } // If we are on the 3rd tab then we will need to validate the user's // input before proceeding to the next step. if ($(this).attr('data-step_index') === '3') { if (!validateCustomerForm()) { return; // Validation failed, do not continue. } else { FrontendBook.updateConfirmFrame(); var $acceptToTermsAndConditions = $('#accept-to-terms-and-conditions'); if ($acceptToTermsAndConditions.length && $acceptToTermsAndConditions.prop('checked') === true) { var newTermsAndConditionsConsent = { first_name: $('#first-name').val(), last_name: $('#last-name').val(), email: $('#email').val(), type: 'terms-and-conditions' }; if (JSON.stringify(newTermsAndConditionsConsent) !== JSON.stringify(termsAndConditionsConsent)) { termsAndConditionsConsent = newTermsAndConditionsConsent; FrontendBookApi.saveConsent(termsAndConditionsConsent); } } var $acceptToPrivacyPolicy = $('#accept-to-privacy-policy'); if ($acceptToPrivacyPolicy.length && $acceptToPrivacyPolicy.prop('checked') === true) { var newPrivacyPolicyConsent = { first_name: $('#first-name').val(), last_name: $('#last-name').val(), email: $('#email').val(), type: 'privacy-policy' }; if (JSON.stringify(newPrivacyPolicyConsent) !== JSON.stringify(privacyPolicyConsent)) { privacyPolicyConsent = newPrivacyPolicyConsent; FrontendBookApi.saveConsent(privacyPolicyConsent); } } } } // Display the next step tab (uses jquery animation effect). var nextTabIndex = parseInt($(this).attr('data-step_index')) + 1; $(this).parents().eq(1).hide('fade', function () { $('.active-step').removeClass('active-step'); $('#step-' + nextTabIndex).addClass('active-step'); $('#wizard-frame-' + nextTabIndex).show('fade'); }); }); /** * Event: Back Step Button "Clicked" * * This handler is triggered every time the user pressed the "back" button on the * book wizard. */ $('.button-back').on('click', function () { var prevTabIndex = parseInt($(this).attr('data-step_index')) - 1; $(this).parents().eq(1).hide('fade', function () { $('.active-step').removeClass('active-step'); $('#step-' + prevTabIndex).addClass('active-step'); $('#wizard-frame-' + prevTabIndex).show('fade'); }); }); /** * Event: Available Hour "Click" * * Triggered whenever the user clicks on an available hour * for his appointment. */ $('#available-hours').on('click', '.available-hour', function () { $('.selected-hour').removeClass('selected-hour'); $(this).addClass('selected-hour'); FrontendBook.updateConfirmFrame(); }); if (FrontendBook.manageMode) { /** * Event: Cancel Appointment Button "Click" * * When the user clicks the "Cancel" button this form is going to be submitted. We need * the user to confirm this action because once the appointment is cancelled, it will be * delete from the database. * * @param {jQuery.Event} event */ $('#cancel-appointment').on('click', function (event) { var buttons = [ { text: EALang.cancel, click: function () { $('#message-box').dialog('close'); } }, { text: 'OK', click: function () { if ($('#cancel-reason').val() === '') { $('#cancel-reason').css('border', '2px solid #DC3545'); return; } $('#cancel-appointment-form textarea').val($('#cancel-reason').val()); $('#cancel-appointment-form').submit(); } } ]; GeneralFunctions.displayMessageBox(EALang.cancel_appointment_title, EALang.write_appointment_removal_reason, buttons); $('