/* ---------------------------------------------------------------------------- * Easy!Appointments - Online Appointment Scheduler * * @package EasyAppointments * @author A.Tselegidis * @copyright Copyright (c) Alex Tselegidis * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 * @link https://easyappointments.org * @since v1.5.0 * ---------------------------------------------------------------------------- */ /** * Service-categories page. * * This module implements the functionality of the service-categories page. */ App.Pages.Categories = (function () { const $serviceCategories = $('#service-categories'); const $filterCategories = $('#filter-service-categories'); const $id = $('#id'); const $name = $('#name'); const $description = $('#description'); let filterResults = {}; let filterLimit = 20; /** * Add the page event listeners. */ function addEventListeners() { /** * Event: Filter Categories Form "Submit" * * @param {jQuery.Event} event */ $serviceCategories.on('submit', '#filter-service-categories form', (event) => { event.preventDefault(); const key = $('#filter-service-categories .key').val(); $('.selected').removeClass('selected'); resetForm(); filter(key); }); /** * Event: Filter Service-Categories Row "Click" * * Displays the selected row data on the right side of the page. * * @param {jQuery.Event} event */ $serviceCategories.on('click', '.service-category-row', (event) => { if ($('#filter-service-categories .filter').prop('disabled')) { $('#filter-service-categories .results').css('color', '#AAA'); return; // exit because we are on edit mode } const serviceCategoryId = $(event.currentTarget).attr('data-id'); const serviceCategory = filterResults.find((filterResult) => Number(filterResult.id) === Number(serviceCategoryId)); display(serviceCategory); $('#filter-service-categories .selected').removeClass('selected'); $(event.currentTarget).addClass('selected'); $('#edit-service-category, #delete-service-category').prop('disabled', false); }); /** * Event: Add Service-Category Button "Click" */ $serviceCategories.on('click', '#add-service-category', () => { resetForm(); $serviceCategories.find('.add-edit-delete-group').hide(); $serviceCategories.find('.save-cancel-group').show(); $serviceCategories.find('.record-details').find('input, select, textarea').prop('disabled', false); $serviceCategories.find('.record-details .form-label span').prop('hidden', false); $filterCategories.find('button').prop('disabled', true); $filterCategories.find('.results').css('color', '#AAA'); }); /** * Event: Edit Service-Category Button "Click" */ $serviceCategories.on('click', '#edit-service-category', () => { $serviceCategories.find('.add-edit-delete-group').hide(); $serviceCategories.find('.save-cancel-group').show(); $serviceCategories.find('.record-details').find('input, select, textarea').prop('disabled', false); $serviceCategories.find('.record-details .form-label span').prop('hidden', false); $filterCategories.find('button').prop('disabled', true); $filterCategories.find('.results').css('color', '#AAA'); }); /** * Event: Delete Service-Category Button "Click" */ $serviceCategories.on('click', '#delete-service-category', () => { const serviceCategoryId = $id.val(); const buttons = [ { text: lang('cancel'), click: (event, messageModal) => { messageModal.dispose(); } }, { text: lang('delete'), click: (event, messageModal) => { remove(serviceCategoryId); messageModal.dispose(); } } ]; App.Utils.Message.show(lang('delete_service_category'), lang('delete_record_prompt'), buttons); }); /** * Event: Categories Save Button "Click" */ $serviceCategories.on('click', '#save-service-category', () => { const serviceCategory = { name: $name.val(), description: $description.val() }; if ($id.val() !== '') { serviceCategory.id = $id.val(); } if (!validate()) { return; } save(serviceCategory); }); /** * Event: Cancel Service-Category Button "Click" */ $serviceCategories.on('click', '#cancel-service-category', () => { const id = $id.val(); resetForm(); if (id !== '') { select(id, true); } }); } /** * Filter service categories records. * * @param {String} keyword This key string is used to filter the service-category records. * @param {Number} selectId Optional, if set then after the filter operation the record with the given * ID will be selected (but not displayed). * @param {Boolean} show Optional (false), if true then the selected record will be displayed on the form. */ function filter(keyword, selectId = null, show = false) { App.Http.ServiceCategories.search(keyword, filterLimit).then((response) => { filterResults = response; $('#filter-service-categories .results').empty(); response.forEach((serviceCategory) => { $('#filter-service-categories .results').append(getFilterHtml(serviceCategory)).append($('
')); }); if (response.length === 0) { $('#filter-service-categories .results').append( $('', { 'text': lang('no_records_found') }) ); } else if (response.length === filterLimit) { $('