/* ---------------------------------------------------------------------------- * 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 * ---------------------------------------------------------------------------- */ /** * Categories page. * * This module implements the functionality of the categories page. */ App.Pages.Categories = (function () { const $categories = $('#categories'); const $filterCategories = $('#filter-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 */ $categories.on('submit', '#filter-categories form', (event) => { event.preventDefault(); const key = $('#filter-categories .key').val(); $('.selected').removeClass('selected'); resetForm(); filter(key); }); /** * Event: Filter Categories Row "Click" * * Displays the selected row data on the right side of the page. * * @param {jQuery.Event} event */ $categories.on('click', '.category-row', (event) => { if ($('#filter-categories .filter').prop('disabled')) { $('#filter-categories .results').css('color', '#AAA'); return; // exit because we are on edit mode } const categoryId = $(event.currentTarget).attr('data-id'); const category = filterResults.find((filterResult) => Number(filterResult.id) === Number(categoryId)); display(category); $('#filter-categories .selected').removeClass('selected'); $(event.currentTarget).addClass('selected'); $('#edit-category, #delete-category').prop('disabled', false); }); /** * Event: Add Category Button "Click" */ $categories.on('click', '#add-category', () => { resetForm(); $categories.find('.add-edit-delete-group').hide(); $categories.find('.save-cancel-group').show(); $categories.find('.record-details').find('input, select, textarea').prop('disabled', false); $categories.find('.record-details .form-label span').prop('hidden', false); $filterCategories.find('button').prop('disabled', true); $filterCategories.find('.results').css('color', '#AAA'); }); /** * Event: Edit Category Button "Click" */ $categories.on('click', '#edit-category', () => { $categories.find('.add-edit-delete-group').hide(); $categories.find('.save-cancel-group').show(); $categories.find('.record-details').find('input, select, textarea').prop('disabled', false); $categories.find('.record-details .form-label span').prop('hidden', false); $filterCategories.find('button').prop('disabled', true); $filterCategories.find('.results').css('color', '#AAA'); }); /** * Event: Delete Category Button "Click" */ $categories.on('click', '#delete-category', () => { const categoryId = $id.val(); const buttons = [ { text: lang('cancel'), click: () => { $('#message-box').dialog('close'); } }, { text: lang('delete'), click: () => { remove(categoryId); $('#message-box').dialog('close'); } } ]; App.Utils.Message.show(lang('delete_category'), lang('delete_record_prompt'), buttons); }); /** * Event: Categories Save Button "Click" */ $categories.on('click', '#save-category', () => { const category = { name: $name.val(), description: $description.val() }; if ($id.val() !== '') { category.id = $id.val(); } if (!validate()) { return; } save(category); }); /** * Event: Cancel Category Button "Click" */ $categories.on('click', '#cancel-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 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.Categories.search(keyword, filterLimit).then((response) => { filterResults = response; $('#filter-categories .results').empty(); response.forEach((category) => { $('#filter-categories .results').append(getFilterHtml(category)).append($('
')); }); if (response.length === 0) { $('#filter-categories .results').append( $('', { 'text': lang('no_records_found') }) ); } else if (response.length === filterLimit) { $('