From 04ec3adaa0448407f6f17571cb9a30c8ad5405a6 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Mon, 17 Jan 2022 05:10:26 +0100 Subject: [PATCH] Code refactoring and improvements for the default calendar view utility. --- assets/js/utils/calendar_default_view.js | 283 ++++++++++++----------- 1 file changed, 145 insertions(+), 138 deletions(-) diff --git a/assets/js/utils/calendar_default_view.js b/assets/js/utils/calendar_default_view.js index 19fb6a77..d68be725 100755 --- a/assets/js/utils/calendar_default_view.js +++ b/assets/js/utils/calendar_default_view.js @@ -10,35 +10,43 @@ * ---------------------------------------------------------------------------- */ /** - * Working plan utility. + * Default calendar view utility. * * This module implements the functionality of the default calendar view. * * Old Name: BackendCalendarDefaultView */ App.Utils.CalendarDefaultView = (function () { + const $calendarPage = $('#calendar-page'); + const $reloadAppointments = $('#reload-appointments'); + const $calendar = $('#calendar'); + const $selectFilterItem = $('#select-filter-item'); + const $appointmentsModal = $('#appointments-modal'); + const $unavailabilitiesModal = $('#unavailabilities-modal'); + const $header = $('#header'); + const $footer = $('#footer'); + const $notification = $('#notification'); + const $calendarToolbar = $('#calendar-toolbar'); const FILTER_TYPE_PROVIDER = 'provider'; const FILTER_TYPE_SERVICE = 'service'; let lastFocusedEventData; // Contains event data for later use. /** - * Bind the event handlers. + * Add the utility event listeners. */ - function bindEventHandlers() { - const $calendarPage = $('#calendar-page'); - + function addEventListeners() { /** * Event: Reload Button "Click" * * When the user clicks the reload button, the calendar items need to be refreshed. */ - $('#reload-appointments').on('click', () => { - const calendarView = $('#calendar').fullCalendar('getView'); + $reloadAppointments.on('click', () => { + const calendarView = $calendar.fullCalendar('getView'); refreshCalendarAppointments( - $('#calendar'), - $('#select-filter-item').val(), - $('#select-filter-item').find('option:selected').attr('type'), + $calendar, + $selectFilterItem.val(), + $selectFilterItem.find('option:selected').attr('type'), calendarView.start, calendarView.end ); @@ -57,11 +65,14 @@ App.Utils.CalendarDefaultView = (function () { * Event: Popover Edit Button "Click" * * Enables the edit dialog of the selected calendar event. + * + * @param {jQuery.Event} event */ - $calendarPage.on('click', '.edit-popover', () => { - $(event.target).parents('.popover').popover('dispose'); + $calendarPage.on('click', '.edit-popover', (event) => { + const $target = $(event.target); + + $target.closest('.popover').popover('dispose'); - let $dialog; let startMoment; let endMoment; @@ -91,7 +102,7 @@ App.Utils.CalendarDefaultView = (function () { } } - $('#select-filter-item').trigger('change'); // Update the calendar. + $selectFilterItem.trigger('change'); // Update the calendar. }; App.Http.Calendar.saveWorkingPlanException( @@ -104,36 +115,35 @@ App.Utils.CalendarDefaultView = (function () { }); } else if (!lastFocusedEventData.data.is_unavailable) { const appointment = lastFocusedEventData.data; - $dialog = $('#appointments-modal'); App.Components.AppointmentsModal.resetModal(); // Apply appointment data and show modal dialog. - $dialog.find('.modal-header h3').text(App.Lang.edit_appointment_title); - $dialog.find('#appointment-id').val(appointment.id); - $dialog.find('#select-service').val(appointment.id_services).trigger('change'); - $dialog.find('#select-provider').val(appointment.id_users_provider); + $appointmentsModal.find('.modal-header h3').text(App.Lang.edit_appointment_title); + $appointmentsModal.find('#appointment-id').val(appointment.id); + $appointmentsModal.find('#select-service').val(appointment.id_services).trigger('change'); + $appointmentsModal.find('#select-provider').val(appointment.id_users_provider); // Set the start and end datetime of the appointment. startMoment = moment(appointment.start_datetime); - $dialog.find('#start-datetime').datetimepicker('setDate', startMoment.toDate()); + $appointmentsModal.find('#start-datetime').datetimepicker('setDate', startMoment.toDate()); endMoment = moment(appointment.end_datetime); - $dialog.find('#end-datetime').datetimepicker('setDate', endMoment.toDate()); + $appointmentsModal.find('#end-datetime').datetimepicker('setDate', endMoment.toDate()); const customer = appointment.customer; - $dialog.find('#customer-id').val(appointment.id_users_customer); - $dialog.find('#first-name').val(customer.first_name); - $dialog.find('#last-name').val(customer.last_name); - $dialog.find('#email').val(customer.email); - $dialog.find('#phone-number').val(customer.phone_number); - $dialog.find('#address').val(customer.address); - $dialog.find('#city').val(customer.city); - $dialog.find('#zip-code').val(customer.zip_code); - $dialog.find('#appointment-location').val(appointment.location); - $dialog.find('#appointment-notes').val(appointment.notes); - $dialog.find('#customer-notes').val(customer.notes); - $dialog.modal('show'); + $appointmentsModal.find('#customer-id').val(appointment.id_users_customer); + $appointmentsModal.find('#first-name').val(customer.first_name); + $appointmentsModal.find('#last-name').val(customer.last_name); + $appointmentsModal.find('#email').val(customer.email); + $appointmentsModal.find('#phone-number').val(customer.phone_number); + $appointmentsModal.find('#address').val(customer.address); + $appointmentsModal.find('#city').val(customer.city); + $appointmentsModal.find('#zip-code').val(customer.zip_code); + $appointmentsModal.find('#appointment-location').val(appointment.location); + $appointmentsModal.find('#appointment-notes').val(appointment.notes); + $appointmentsModal.find('#customer-notes').val(customer.notes); + $appointmentsModal.modal('show'); } else { const unavailable = lastFocusedEventData.data; @@ -143,17 +153,16 @@ App.Utils.CalendarDefaultView = (function () { unavailable.end_datetime = lastFocusedEventData.end.format('YYYY-MM-DD HH:mm:ss'); endMoment = moment(unavailable.end_datetime); - $dialog = $('#unavailabilities-modal'); App.Components.UnavailabilitiesModal.resetModal(); // Apply unavailable data to dialog. - $dialog.find('.modal-header h3').text('Edit Unavailable Period'); - $dialog.find('#unavailable-start').datetimepicker('setDate', startMoment.toDate()); - $dialog.find('#unavailable-id').val(unavailable.id); - $dialog.find('#unavailable-provider').val(unavailable.id_users_provider); - $dialog.find('#unavailable-end').datetimepicker('setDate', endMoment.toDate()); - $dialog.find('#unavailable-notes').val(unavailable.notes); - $dialog.modal('show'); + $unavailabilitiesModal.find('.modal-header h3').text('Edit Unavailable Period'); + $unavailabilitiesModal.find('#unavailable-start').datetimepicker('setDate', startMoment.toDate()); + $unavailabilitiesModal.find('#unavailable-id').val(unavailable.id); + $unavailabilitiesModal.find('#unavailable-provider').val(unavailable.id_users_provider); + $unavailabilitiesModal.find('#unavailable-end').datetimepicker('setDate', endMoment.toDate()); + $unavailabilitiesModal.find('#unavailable-notes').val(unavailable.notes); + $unavailabilitiesModal.modal('show'); } }); @@ -162,15 +171,19 @@ App.Utils.CalendarDefaultView = (function () { * * Displays a prompt on whether the user wants the appointment to be deleted. If he confirms the * deletion then an AJAX call is made to the server and deletes the appointment from the database. + * + * @param {jQuery.Event} event */ $calendarPage.on('click', '.delete-popover', (event) => { - $(event.target).parents('.popover').popover('dispose'); + const $target = $(event.target); + + $target.parents('.popover').popover('dispose'); let url; let data; if (lastFocusedEventData.data.workingPlanException) { - const providerId = $('#select-filter-item').val(); + const providerId = $selectFilterItem.val(); const provider = App.Vars.available_providers.find( (availableProvider) => Number(availableProvider.id) === Number(providerId) @@ -195,7 +208,7 @@ App.Utils.CalendarDefaultView = (function () { } } - $('#select-filter-item').trigger('change'); // Update the calendar. + $selectFilterItem.trigger('change'); // Update the calendar. }; const date = lastFocusedEventData.start.format('YYYY-MM-DD'); @@ -224,7 +237,7 @@ App.Utils.CalendarDefaultView = (function () { $('#message-box').dialog('close'); // Refresh calendar event items. - $('#select-filter-item').trigger('change'); + $selectFilterItem.trigger('change'); }); } } @@ -254,7 +267,7 @@ App.Utils.CalendarDefaultView = (function () { $('#message-box').dialog('close'); // Refresh calendar event items. - $('#select-filter-item').trigger('change'); + $selectFilterItem.trigger('change'); }); } }); @@ -264,22 +277,22 @@ App.Utils.CalendarDefaultView = (function () { * * Load the appointments that correspond to the select filter item and display them on the calendar. */ - $('#select-filter-item').on('change', () => { + $selectFilterItem.on('change', () => { // If current value is service, then the sync buttons must be disabled. - if ($('#select-filter-item option:selected').attr('type') === FILTER_TYPE_SERVICE) { + if ($selectFilterItem.find('option:selected').attr('type') === FILTER_TYPE_SERVICE) { $('#google-sync, #enable-sync, #insert-appointment, #insert-dropdown').prop('disabled', true); - $('#calendar').fullCalendar('option', { + $calendar.fullCalendar('option', { selectable: false, editable: false }); } else { $('#google-sync, #enable-sync, #insert-appointment, #insert-dropdown').prop('disabled', false); - $('#calendar').fullCalendar('option', { + $calendar.fullCalendar('option', { selectable: true, editable: true }); - const providerId = $('#select-filter-item').val(); + const providerId = $selectFilterItem.val(); const provider = App.Vars.available_providers.find(function (availableProvider) { return Number(availableProvider.id) === Number(providerId); @@ -290,7 +303,7 @@ App.Utils.CalendarDefaultView = (function () { } // If the user has already the sync enabled then apply the proper style changes. - if ($('#select-filter-item option:selected').attr('google-sync') === 'true') { + if ($selectFilterItem('option:selected').attr('google-sync') === 'true') { $('#enable-sync').removeClass('btn-light').addClass('btn-secondary enabled'); $('#enable-sync span').text(App.Lang.disable_sync); $('#google-sync').prop('disabled', false); @@ -313,11 +326,7 @@ App.Utils.CalendarDefaultView = (function () { */ function getCalendarHeight() { const result = - window.innerHeight - - $('#footer').outerHeight() - - $('#header').outerHeight() - - $('#calendar-toolbar').outerHeight() - - 60; // 60 for fine tuning + window.innerHeight - $footer.outerHeight() - $header.outerHeight() - $calendarToolbar.outerHeight() - 60; // 60 for fine tuning return result > 500 ? result : 500; // Minimum height is 500px } @@ -343,7 +352,9 @@ App.Utils.CalendarDefaultView = (function () { * above the calendar item. */ function calendarEventClick(event, jsEvent) { - $('.popover').popover('dispose'); // Close all open popovers. + const $target = $(jsEvent.target); + + $calendarPage.find('.popover').popover('dispose'); // Close all open popovers. let $html; let displayEdit; @@ -355,7 +366,7 @@ App.Utils.CalendarDefaultView = (function () { const $altParent = $(jsEvent.target).parents().eq(1); if ( - $(this).hasClass('fc-unavailable') || + $target.hasClass('fc-unavailable') || $parent.hasClass('fc-unavailable') || $altParent.hasClass('fc-unavailable') ) { @@ -451,7 +462,7 @@ App.Utils.CalendarDefaultView = (function () { ] }); } else if ( - $(this).hasClass('fc-working-plan-exception') || + $target.hasClass('fc-working-plan-exception') || $parent.hasClass('fc-working-plan-exception') || $altParent.hasClass('fc-working-plan-exception') ) { @@ -459,7 +470,7 @@ App.Utils.CalendarDefaultView = (function () { ($parent.hasClass('fc-custom') || $altParent.hasClass('fc-custom')) && App.Vars.privileges.appointments.delete === true ? 'me-2' - : 'd-none'; // Same value at the time. + : 'd-none'; $html = $('
', { 'html': [ @@ -537,7 +548,7 @@ App.Utils.CalendarDefaultView = (function () { ] }), $('