2016-04-02 16:36:55 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2016-04-02 16:36:55 +03:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2022-01-18 15:01:22 +03:00
|
|
|
* @since v1.5.0
|
2016-04-02 16:36:55 +03:00
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2022-01-14 11:26:44 +03:00
|
|
|
/**
|
2023-10-26 05:54:13 +03:00
|
|
|
* Service-categories page.
|
2022-01-14 11:26:44 +03:00
|
|
|
*
|
2023-10-26 05:54:13 +03:00
|
|
|
* This module implements the functionality of the service-categories page.
|
2022-01-14 11:26:44 +03:00
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
App.Pages.Categories = (function () {
|
2023-10-26 05:54:13 +03:00
|
|
|
const $serviceCategories = $('#service-categories');
|
|
|
|
const $filterCategories = $('#filter-service-categories');
|
2022-01-18 11:31:19 +03:00
|
|
|
const $id = $('#id');
|
|
|
|
const $name = $('#name');
|
|
|
|
const $description = $('#description');
|
2022-01-10 11:26:28 +03:00
|
|
|
let filterResults = {};
|
|
|
|
let filterLimit = 20;
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
2022-01-17 08:40:48 +03:00
|
|
|
* Add the page event listeners.
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2022-01-17 08:40:48 +03:00
|
|
|
function addEventListeners() {
|
2016-04-02 16:36:55 +03:00
|
|
|
/**
|
|
|
|
* Event: Filter Categories Form "Submit"
|
2020-12-08 14:23:37 +03:00
|
|
|
*
|
|
|
|
* @param {jQuery.Event} event
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2023-10-26 05:54:13 +03:00
|
|
|
$serviceCategories.on('submit', '#filter-service-categories form', (event) => {
|
2020-12-08 14:23:37 +03:00
|
|
|
event.preventDefault();
|
2023-10-26 05:54:13 +03:00
|
|
|
const key = $('#filter-service-categories .key').val();
|
2016-07-15 22:09:38 +03:00
|
|
|
$('.selected').removeClass('selected');
|
2022-01-10 11:26:28 +03:00
|
|
|
resetForm();
|
|
|
|
filter(key);
|
2016-04-02 16:36:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2023-10-26 05:54:13 +03:00
|
|
|
* Event: Filter Service-Categories Row "Click"
|
2016-04-02 16:36:55 +03:00
|
|
|
*
|
|
|
|
* Displays the selected row data on the right side of the page.
|
2022-01-17 08:40:48 +03:00
|
|
|
*
|
|
|
|
* @param {jQuery.Event} event
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2023-10-26 05:54:13 +03:00
|
|
|
$serviceCategories.on('click', '.service-category-row', (event) => {
|
|
|
|
if ($('#filter-service-categories .filter').prop('disabled')) {
|
|
|
|
$('#filter-service-categories .results').css('color', '#AAA');
|
2016-04-02 16:36:55 +03:00
|
|
|
return; // exit because we are on edit mode
|
|
|
|
}
|
|
|
|
|
2023-10-26 05:54:13 +03:00
|
|
|
const serviceCategoryId = $(event.currentTarget).attr('data-id');
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2023-10-26 05:54:13 +03:00
|
|
|
const serviceCategory = filterResults.find((filterResult) => Number(filterResult.id) === Number(serviceCategoryId));
|
2016-04-02 16:36:55 +03:00
|
|
|
|
2023-10-26 05:54:13 +03:00
|
|
|
display(serviceCategory);
|
|
|
|
$('#filter-service-categories .selected').removeClass('selected');
|
2022-01-18 11:31:19 +03:00
|
|
|
$(event.currentTarget).addClass('selected');
|
2023-10-26 05:54:13 +03:00
|
|
|
$('#edit-service-category, #delete-service-category').prop('disabled', false);
|
2016-04-02 16:36:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2023-10-26 05:54:13 +03:00
|
|
|
* Event: Add Service-Category Button "Click"
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2023-10-26 05:54:13 +03:00
|
|
|
$serviceCategories.on('click', '#add-service-category', () => {
|
2022-01-10 11:26:28 +03:00
|
|
|
resetForm();
|
2023-10-26 05:54:13 +03:00
|
|
|
$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);
|
2022-01-17 08:40:48 +03:00
|
|
|
$filterCategories.find('button').prop('disabled', true);
|
|
|
|
$filterCategories.find('.results').css('color', '#AAA');
|
2016-04-02 16:36:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2023-10-26 05:54:13 +03:00
|
|
|
* Event: Edit Service-Category Button "Click"
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2023-10-26 05:54:13 +03:00
|
|
|
$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);
|
2022-01-17 08:40:48 +03:00
|
|
|
$filterCategories.find('button').prop('disabled', true);
|
|
|
|
$filterCategories.find('.results').css('color', '#AAA');
|
2016-04-02 16:36:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2023-10-26 05:54:13 +03:00
|
|
|
* Event: Delete Service-Category Button "Click"
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2023-10-26 05:54:13 +03:00
|
|
|
$serviceCategories.on('click', '#delete-service-category', () => {
|
|
|
|
const serviceCategoryId = $id.val();
|
2016-04-02 16:36:55 +03:00
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
const buttons = [
|
2017-06-16 09:51:05 +03:00
|
|
|
{
|
2022-01-18 10:22:25 +03:00
|
|
|
text: lang('cancel'),
|
2023-02-20 10:24:49 +03:00
|
|
|
click: (event, messageModal) => {
|
|
|
|
messageModal.dispose();
|
2017-06-16 09:51:05 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2022-01-18 10:22:25 +03:00
|
|
|
text: lang('delete'),
|
2023-02-20 10:24:49 +03:00
|
|
|
click: (event, messageModal) => {
|
2023-10-26 05:54:13 +03:00
|
|
|
remove(serviceCategoryId);
|
2023-02-20 10:24:49 +03:00
|
|
|
messageModal.dispose();
|
2017-06-16 09:51:05 +03:00
|
|
|
}
|
2021-11-06 19:38:37 +03:00
|
|
|
}
|
2017-06-16 09:51:05 +03:00
|
|
|
];
|
2016-04-02 16:36:55 +03:00
|
|
|
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Utils.Message.show(lang('delete_category'), lang('delete_record_prompt'), buttons);
|
2016-04-02 16:36:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Categories Save Button "Click"
|
|
|
|
*/
|
2023-10-26 05:54:13 +03:00
|
|
|
$serviceCategories.on('click', '#save-service-category', () => {
|
|
|
|
const serviceCategory = {
|
2022-01-17 08:40:48 +03:00
|
|
|
name: $name.val(),
|
|
|
|
description: $description.val()
|
2016-04-02 16:36:55 +03:00
|
|
|
};
|
|
|
|
|
2022-01-17 08:40:48 +03:00
|
|
|
if ($id.val() !== '') {
|
2023-10-26 05:54:13 +03:00
|
|
|
serviceCategory.id = $id.val();
|
2016-04-02 16:36:55 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
if (!validate()) {
|
2016-04-02 16:36:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-26 05:54:13 +03:00
|
|
|
save(serviceCategory);
|
2016-04-02 16:36:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2023-10-26 05:54:13 +03:00
|
|
|
* Event: Cancel Service-Category Button "Click"
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2023-10-26 05:54:13 +03:00
|
|
|
$serviceCategories.on('click', '#cancel-service-category', () => {
|
2022-01-17 08:40:48 +03:00
|
|
|
const id = $id.val();
|
2022-01-10 11:26:28 +03:00
|
|
|
resetForm();
|
2016-04-02 16:36:55 +03:00
|
|
|
if (id !== '') {
|
2022-01-10 11:26:28 +03:00
|
|
|
select(id, true);
|
2016-04-02 16:36:55 +03:00
|
|
|
}
|
|
|
|
});
|
2022-01-10 11:26:28 +03:00
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter service categories records.
|
|
|
|
*
|
2023-10-26 05:54:13 +03:00
|
|
|
* @param {String} keyword This key string is used to filter the service-category records.
|
2016-05-15 13:20:37 +03:00
|
|
|
* @param {Number} selectId Optional, if set then after the filter operation the record with the given
|
|
|
|
* ID will be selected (but not displayed).
|
2022-01-10 11:26:28 +03:00
|
|
|
* @param {Boolean} show Optional (false), if true then the selected record will be displayed on the form.
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
function filter(keyword, selectId = null, show = false) {
|
2023-10-26 05:54:13 +03:00
|
|
|
App.Http.ServiceCategories.search(keyword, filterLimit).then((response) => {
|
2022-01-10 11:26:28 +03:00
|
|
|
filterResults = response;
|
2016-04-02 16:36:55 +03:00
|
|
|
|
2023-10-26 05:54:13 +03:00
|
|
|
$('#filter-service-categories .results').empty();
|
2020-05-06 20:15:11 +03:00
|
|
|
|
2023-10-26 05:54:13 +03:00
|
|
|
response.forEach((serviceCategory) => {
|
|
|
|
$('#filter-service-categories .results').append(getFilterHtml(serviceCategory)).append($('<hr/>'));
|
2022-01-17 08:40:48 +03:00
|
|
|
});
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
if (response.length === 0) {
|
2023-10-26 05:54:13 +03:00
|
|
|
$('#filter-service-categories .results').append(
|
2022-01-10 11:26:28 +03:00
|
|
|
$('<em/>', {
|
2022-01-18 10:22:25 +03:00
|
|
|
'text': lang('no_records_found')
|
2022-01-10 11:26:28 +03:00
|
|
|
})
|
2021-11-06 19:38:37 +03:00
|
|
|
);
|
2022-01-10 11:26:28 +03:00
|
|
|
} else if (response.length === filterLimit) {
|
|
|
|
$('<button/>', {
|
|
|
|
'type': 'button',
|
|
|
|
'class': 'btn btn-outline-secondary w-100 load-more text-center',
|
2022-01-18 10:22:25 +03:00
|
|
|
'text': lang('load_more'),
|
2022-01-17 08:40:48 +03:00
|
|
|
'click': () => {
|
2022-01-10 11:26:28 +03:00
|
|
|
filterLimit += 20;
|
|
|
|
filter(keyword, selectId, show);
|
2022-01-17 08:40:48 +03:00
|
|
|
}
|
2023-10-26 05:54:13 +03:00
|
|
|
}).appendTo('#filter-service-categories .results');
|
2022-01-10 11:26:28 +03:00
|
|
|
}
|
2020-05-06 20:15:11 +03:00
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
if (selectId) {
|
|
|
|
select(selectId, show);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
2023-10-26 05:54:13 +03:00
|
|
|
* Save a service-category record to the database (via AJAX post).
|
2016-04-02 16:36:55 +03:00
|
|
|
*
|
2023-10-26 05:54:13 +03:00
|
|
|
* @param {Object} serviceCategory Contains the service-category data.
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2023-10-26 05:54:13 +03:00
|
|
|
function save(serviceCategory) {
|
|
|
|
App.Http.ServiceCategories.save(serviceCategory).then((response) => {
|
|
|
|
App.Layouts.Backend.displayNotification(lang('service_category_saved'));
|
2022-01-10 11:26:28 +03:00
|
|
|
resetForm();
|
2022-01-17 08:40:48 +03:00
|
|
|
$filterCategories.find('.key').val('');
|
2022-01-10 11:26:28 +03:00
|
|
|
filter('', response.id, true);
|
|
|
|
});
|
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
2023-10-26 05:54:13 +03:00
|
|
|
* Delete service-category record.
|
2016-04-02 16:36:55 +03:00
|
|
|
*
|
2021-11-18 07:23:54 +03:00
|
|
|
* @param {Number} id Record ID to be deleted.
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
function remove(id) {
|
2023-10-26 05:54:13 +03:00
|
|
|
App.Http.ServiceCategories.destroy(id).then(() => {
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Layouts.Backend.displayNotification(lang('category_deleted'));
|
2022-01-10 11:26:28 +03:00
|
|
|
resetForm();
|
2023-10-26 05:54:13 +03:00
|
|
|
filter($('#filter-service-categories .key').val());
|
2022-01-10 11:26:28 +03:00
|
|
|
});
|
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
2023-10-26 05:54:13 +03:00
|
|
|
* Display a service-category record on the form.
|
2016-04-02 16:36:55 +03:00
|
|
|
*
|
2023-10-26 05:54:13 +03:00
|
|
|
* @param {Object} serviceCategory Contains the service-category data.
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2023-10-26 05:54:13 +03:00
|
|
|
function display(serviceCategory) {
|
|
|
|
$id.val(serviceCategory.id);
|
|
|
|
$name.val(serviceCategory.name);
|
|
|
|
$description.val(serviceCategory.description);
|
2022-01-10 11:26:28 +03:00
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
2023-10-26 05:54:13 +03:00
|
|
|
* Validate service-category data before save (insert or update).
|
2016-04-02 16:36:55 +03:00
|
|
|
*
|
2017-11-14 15:52:59 +03:00
|
|
|
* @return {Boolean} Returns the validation result.
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
function validate() {
|
2023-10-26 05:54:13 +03:00
|
|
|
$serviceCategories.find('.is-invalid').removeClass('is-invalid');
|
|
|
|
$serviceCategories.find('.form-message').removeClass('alert-danger').hide();
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
try {
|
2022-01-10 11:26:28 +03:00
|
|
|
let missingRequired = false;
|
2016-04-02 16:36:55 +03:00
|
|
|
|
2023-10-26 05:54:13 +03:00
|
|
|
$serviceCategories.find('.required').each((index, fieldEl) => {
|
2022-01-17 08:40:48 +03:00
|
|
|
if (!$(fieldEl).val()) {
|
|
|
|
$(fieldEl).addClass('is-invalid');
|
2016-04-02 16:36:55 +03:00
|
|
|
missingRequired = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missingRequired) {
|
2022-01-18 10:22:25 +03:00
|
|
|
throw new Error(lang('fields_are_required'));
|
2016-04-02 16:36:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-05-06 20:15:11 +03:00
|
|
|
} catch (error) {
|
2023-10-26 05:54:13 +03:00
|
|
|
$serviceCategories.find('.form-message').addClass('alert-danger').text(error.message).show();
|
2016-04-02 16:36:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-10 11:26:28 +03:00
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
2023-10-26 05:54:13 +03:00
|
|
|
* Bring the service-category form back to its initial state.
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
function resetForm() {
|
2022-01-17 08:40:48 +03:00
|
|
|
$filterCategories.find('.selected').removeClass('selected');
|
|
|
|
$filterCategories.find('button').prop('disabled', false);
|
|
|
|
$filterCategories.find('.results').css('color', '');
|
2020-09-08 09:57:07 +03:00
|
|
|
|
2023-10-26 05:54:13 +03:00
|
|
|
$serviceCategories.find('.add-edit-delete-group').show();
|
|
|
|
$serviceCategories.find('.save-cancel-group').hide();
|
|
|
|
$serviceCategories.find('.record-details').find('input, select, textarea').val('').prop('disabled', true);
|
|
|
|
$serviceCategories.find('.record-details .form-label span').prop('hidden', true);
|
|
|
|
$('#edit-service-category, #delete-service-category').prop('disabled', true);
|
2016-04-02 16:36:55 +03:00
|
|
|
|
2023-10-26 05:54:13 +03:00
|
|
|
$serviceCategories.find('.record-details .is-invalid').removeClass('is-invalid');
|
|
|
|
$serviceCategories.find('.record-details .form-message').hide();
|
2022-01-10 11:26:28 +03:00
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
2016-05-15 13:20:37 +03:00
|
|
|
* Get the filter results row HTML code.
|
2016-04-02 16:36:55 +03:00
|
|
|
*
|
2023-10-26 05:54:13 +03:00
|
|
|
* @param {Object} serviceCategory Contains the service-category data.
|
2016-05-15 13:20:37 +03:00
|
|
|
*
|
|
|
|
* @return {String} Returns the record HTML code.
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2023-10-26 05:54:13 +03:00
|
|
|
function getFilterHtml(serviceCategory) {
|
2020-05-07 19:47:14 +03:00
|
|
|
return $('<div/>', {
|
2023-10-26 05:54:13 +03:00
|
|
|
'class': 'service-category-row entry',
|
|
|
|
'data-id': serviceCategory.id,
|
2020-05-07 19:47:14 +03:00
|
|
|
'html': [
|
|
|
|
$('<strong/>', {
|
2023-10-26 05:54:13 +03:00
|
|
|
'text': serviceCategory.name
|
2020-05-07 19:47:14 +03:00
|
|
|
}),
|
2021-11-06 19:38:37 +03:00
|
|
|
$('<br/>')
|
2020-05-07 19:47:14 +03:00
|
|
|
]
|
|
|
|
});
|
2022-01-10 11:26:28 +03:00
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
2016-05-15 13:20:37 +03:00
|
|
|
* Select a specific record from the current filter results.
|
|
|
|
*
|
2023-10-26 05:54:13 +03:00
|
|
|
* If the service-category ID does not exist in the list then no record will be selected.
|
2016-04-02 16:36:55 +03:00
|
|
|
*
|
2016-05-15 13:20:37 +03:00
|
|
|
* @param {Number} id The record ID to be selected from the filter results.
|
2022-01-17 08:40:48 +03:00
|
|
|
* @param {Boolean} show Optional (false), if true then the method will display the record on the form.
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
function select(id, show = false) {
|
2022-01-17 08:40:48 +03:00
|
|
|
$filterCategories.find('.selected').removeClass('selected');
|
2016-04-02 16:36:55 +03:00
|
|
|
|
2023-10-26 05:54:13 +03:00
|
|
|
$filterCategories.find('.service-category-row[data-id="' + id + '"]').addClass('selected');
|
2016-04-02 16:36:55 +03:00
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
if (show) {
|
2023-10-26 05:54:13 +03:00
|
|
|
const serviceCategory = filterResults.find((serviceCategory) => Number(serviceCategory.id) === Number(id));
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2023-10-26 05:54:13 +03:00
|
|
|
display(serviceCategory);
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2023-10-26 05:54:13 +03:00
|
|
|
$('#edit-service-category, #delete-service-category').prop('disabled', false);
|
2016-04-02 16:36:55 +03:00
|
|
|
}
|
2022-01-10 11:26:28 +03:00
|
|
|
}
|
|
|
|
|
2022-01-14 11:26:44 +03:00
|
|
|
/**
|
|
|
|
* Initialize the module.
|
|
|
|
*/
|
2022-01-14 10:31:12 +03:00
|
|
|
function initialize() {
|
2022-01-10 11:26:28 +03:00
|
|
|
resetForm();
|
|
|
|
filter('');
|
2022-01-17 08:40:48 +03:00
|
|
|
addEventListeners();
|
2022-01-10 11:26:28 +03:00
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
2022-01-14 10:31:12 +03:00
|
|
|
document.addEventListener('DOMContentLoaded', initialize);
|
2022-01-10 11:26:28 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
filter,
|
|
|
|
save,
|
|
|
|
remove,
|
|
|
|
getFilterHtml,
|
|
|
|
resetForm,
|
|
|
|
select
|
|
|
|
};
|
2016-04-02 16:36:55 +03:00
|
|
|
})();
|