2016-04-02 16:36:55 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @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
|
2016-04-02 16:36:55 +03:00
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
App.Pages.Categories = (function () {
|
|
|
|
const $categories = $('#categories');
|
|
|
|
let filterResults = {};
|
|
|
|
let filterLimit = 20;
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds the default event handlers of the categories tab.
|
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
function bindEventHandlers() {
|
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
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
$categories.on('submit', '#filter-categories form', function (event) {
|
2020-12-08 14:23:37 +03:00
|
|
|
event.preventDefault();
|
2022-01-10 11:26:28 +03:00
|
|
|
const key = $('#filter-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
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Filter Categories Row "Click"
|
|
|
|
*
|
|
|
|
* Displays the selected row data on the right side of the page.
|
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
$categories.on('click', '.category-row', function () {
|
2016-04-02 16:36:55 +03:00
|
|
|
if ($('#filter-categories .filter').prop('disabled')) {
|
|
|
|
$('#filter-categories .results').css('color', '#AAA');
|
|
|
|
return; // exit because we are on edit mode
|
|
|
|
}
|
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
const categoryId = $(this).attr('data-id');
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
const category = filterResults.find(function (filterResult) {
|
2020-05-12 21:52:32 +03:00
|
|
|
return Number(filterResult.id) === Number(categoryId);
|
2016-04-02 16:36:55 +03:00
|
|
|
});
|
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
display(category);
|
2016-07-15 22:09:38 +03:00
|
|
|
$('#filter-categories .selected').removeClass('selected');
|
|
|
|
$(this).addClass('selected');
|
2016-04-02 16:36:55 +03:00
|
|
|
$('#edit-category, #delete-category').prop('disabled', false);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Add Category Button "Click"
|
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
$categories.on('click', '#add-category', function () {
|
|
|
|
resetForm();
|
2016-04-02 16:36:55 +03:00
|
|
|
$('#categories .add-edit-delete-group').hide();
|
|
|
|
$('#categories .save-cancel-group').show();
|
2020-09-08 09:57:07 +03:00
|
|
|
$('#categories .record-details').find('input, select, textarea').prop('disabled', false);
|
2016-04-02 16:36:55 +03:00
|
|
|
$('#filter-categories button').prop('disabled', true);
|
|
|
|
$('#filter-categories .results').css('color', '#AAA');
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Edit Category Button "Click"
|
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
$categories.on('click', '#edit-category', function () {
|
2016-04-02 16:36:55 +03:00
|
|
|
$('#categories .add-edit-delete-group').hide();
|
|
|
|
$('#categories .save-cancel-group').show();
|
2020-11-12 15:15:52 +03:00
|
|
|
$('#categories .record-details').find('input, select, textarea').prop('disabled', false);
|
2016-04-02 16:36:55 +03:00
|
|
|
$('#filter-categories button').prop('disabled', true);
|
|
|
|
$('#filter-categories .results').css('color', '#AAA');
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Delete Category Button "Click"
|
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
$categories.on('click', '#delete-category', function () {
|
|
|
|
const categoryId = $('#category-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
|
|
|
{
|
2021-12-13 09:52:09 +03:00
|
|
|
text: App.Lang.cancel,
|
2018-01-23 12:08:37 +03:00
|
|
|
click: function () {
|
2020-09-07 11:08:18 +03:00
|
|
|
$('#message-box').dialog('close');
|
2017-06-16 09:51:05 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2021-12-13 09:52:09 +03:00
|
|
|
text: App.Lang.delete,
|
2018-01-23 12:08:37 +03:00
|
|
|
click: function () {
|
2022-01-10 11:26:28 +03:00
|
|
|
remove(categoryId);
|
2020-09-07 11:08:18 +03:00
|
|
|
$('#message-box').dialog('close');
|
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-10 11:26:28 +03:00
|
|
|
App.Utils.Message.show(App.Lang.delete_category, App.Lang.delete_record_prompt, buttons);
|
2016-04-02 16:36:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Categories Save Button "Click"
|
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
$categories.on('click', '#save-category', function () {
|
|
|
|
const category = {
|
2016-05-15 13:20:37 +03:00
|
|
|
name: $('#category-name').val(),
|
|
|
|
description: $('#category-description').val()
|
2016-04-02 16:36:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if ($('#category-id').val() !== '') {
|
|
|
|
category.id = $('#category-id').val();
|
|
|
|
}
|
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
if (!validate()) {
|
2016-04-02 16:36:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
save(category);
|
2016-04-02 16:36:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Cancel Category Button "Click"
|
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
$categories.on('click', '#cancel-category', function () {
|
|
|
|
const id = $('#category-id').val();
|
|
|
|
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
|
|
|
|
2020-12-08 14:23:37 +03:00
|
|
|
/**
|
|
|
|
* Remove the previously registered event handlers.
|
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
function unbindEventHandlers() {
|
|
|
|
$categories
|
2020-12-08 14:23:37 +03:00
|
|
|
.off('click', '#filter-categories .clear')
|
|
|
|
.off('submit', '#filter-categories form')
|
|
|
|
.off('click', '.category-row')
|
|
|
|
.off('click', '#add-category')
|
|
|
|
.off('click', '#edit-category')
|
|
|
|
.off('click', '#delete-category')
|
|
|
|
.off('click', '#save-category')
|
|
|
|
.off('click', '#cancel-category');
|
2022-01-10 11:26:28 +03:00
|
|
|
}
|
2020-12-08 14:23:37 +03:00
|
|
|
|
2016-04-02 16:36:55 +03:00
|
|
|
/**
|
|
|
|
* Filter service categories records.
|
|
|
|
*
|
2021-11-18 07:23:54 +03:00
|
|
|
* @param {String} keyword This key string is used to filter the 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) {
|
|
|
|
App.Http.Categories.search(keyword, filterLimit).then((response) => {
|
|
|
|
filterResults = response;
|
2016-04-02 16:36:55 +03:00
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
$('#filter-categories .results').empty();
|
2020-05-06 20:15:11 +03:00
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
response.forEach(
|
|
|
|
function (category) {
|
|
|
|
$('#filter-categories .results').append(getFilterHtml(category)).append($('<hr/>'));
|
|
|
|
}.bind(this)
|
|
|
|
);
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
if (response.length === 0) {
|
|
|
|
$('#filter-categories .results').append(
|
|
|
|
$('<em/>', {
|
|
|
|
'text': App.Lang.no_records_found
|
|
|
|
})
|
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',
|
|
|
|
'text': App.Lang.load_more,
|
|
|
|
'click': function () {
|
|
|
|
filterLimit += 20;
|
|
|
|
filter(keyword, selectId, show);
|
|
|
|
}.bind(this)
|
|
|
|
}).appendTo('#filter-categories .results');
|
|
|
|
}
|
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
|
|
|
|
|
|
|
/**
|
2016-05-15 13:20:37 +03:00
|
|
|
* Save a category record to the database (via AJAX post).
|
2016-04-02 16:36:55 +03:00
|
|
|
*
|
2016-05-15 13:20:37 +03:00
|
|
|
* @param {Object} category Contains the category data.
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
function save(category) {
|
|
|
|
App.Http.Categories.save(category).then((response) => {
|
|
|
|
Backend.displayNotification(App.Lang.category_saved);
|
|
|
|
resetForm();
|
|
|
|
$('#filter-categories .key').val('');
|
|
|
|
filter('', response.id, true);
|
|
|
|
});
|
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete category record.
|
|
|
|
*
|
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) {
|
|
|
|
App.Http.Categories.destroy(id).then(() => {
|
|
|
|
Backend.displayNotification(App.Lang.category_deleted);
|
|
|
|
resetForm();
|
|
|
|
filter($('#filter-categories .key').val());
|
|
|
|
});
|
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a category record on the form.
|
|
|
|
*
|
2016-05-15 13:20:37 +03:00
|
|
|
* @param {Object} category Contains the category data.
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
function display(category) {
|
2016-04-02 16:36:55 +03:00
|
|
|
$('#category-id').val(category.id);
|
|
|
|
$('#category-name').val(category.name);
|
|
|
|
$('#category-description').val(category.description);
|
2022-01-10 11:26:28 +03:00
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate category data before save (insert or update).
|
|
|
|
*
|
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() {
|
2021-11-23 12:10:09 +03:00
|
|
|
$('#categories .is-invalid').removeClass('is-invalid');
|
2021-11-06 19:38:37 +03:00
|
|
|
$('#categories .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
|
|
|
|
2020-05-12 21:52:32 +03:00
|
|
|
$('#categories .required').each(function (index, requiredField) {
|
|
|
|
if (!$(requiredField).val()) {
|
2021-11-23 12:10:09 +03:00
|
|
|
$(requiredField).addClass('is-invalid');
|
2016-04-02 16:36:55 +03:00
|
|
|
missingRequired = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missingRequired) {
|
2021-12-13 09:52:09 +03:00
|
|
|
throw new Error(App.Lang.fields_are_required);
|
2016-04-02 16:36:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-05-06 20:15:11 +03:00
|
|
|
} catch (error) {
|
2021-11-06 19:38:37 +03:00
|
|
|
$('#categories .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
|
|
|
|
|
|
|
/**
|
|
|
|
* Bring the category form back to its initial state.
|
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
function resetForm() {
|
2020-09-08 09:57:07 +03:00
|
|
|
$('#filter-categories .selected').removeClass('selected');
|
|
|
|
$('#filter-categories button').prop('disabled', false);
|
|
|
|
$('#filter-categories .results').css('color', '');
|
|
|
|
|
2016-04-02 16:36:55 +03:00
|
|
|
$('#categories .add-edit-delete-group').show();
|
|
|
|
$('#categories .save-cancel-group').hide();
|
2021-11-06 19:38:37 +03:00
|
|
|
$('#categories .record-details').find('input, select, textarea').val('').prop('disabled', true);
|
2016-04-02 16:36:55 +03:00
|
|
|
$('#edit-category, #delete-category').prop('disabled', true);
|
|
|
|
|
2021-11-23 12:10:09 +03:00
|
|
|
$('#categories .record-details .is-invalid').removeClass('is-invalid');
|
2020-09-08 09:57:07 +03:00
|
|
|
$('#categories .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
|
|
|
*
|
2016-05-15 13:20:37 +03:00
|
|
|
* @param {Object} category Contains the category data.
|
|
|
|
*
|
|
|
|
* @return {String} Returns the record HTML code.
|
2016-04-02 16:36:55 +03:00
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
function getFilterHtml(category) {
|
2020-05-07 19:47:14 +03:00
|
|
|
return $('<div/>', {
|
|
|
|
'class': 'category-row entry',
|
|
|
|
'data-id': category.id,
|
|
|
|
'html': [
|
|
|
|
$('<strong/>', {
|
|
|
|
'text': category.name
|
|
|
|
}),
|
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.
|
|
|
|
*
|
|
|
|
* If the 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-10 11:26:28 +03:00
|
|
|
* @param {Boolean} show Optional (false), if true then the method will display the record
|
2016-04-02 16:36:55 +03:00
|
|
|
* on the form.
|
|
|
|
*/
|
2022-01-10 11:26:28 +03:00
|
|
|
function select(id, show = false) {
|
2016-07-15 22:09:38 +03:00
|
|
|
$('#filter-categories .selected').removeClass('selected');
|
2016-04-02 16:36:55 +03:00
|
|
|
|
2020-05-12 21:52:32 +03:00
|
|
|
$('#filter-categories .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) {
|
|
|
|
const category = filterResults.find(
|
2021-11-06 19:38:37 +03:00
|
|
|
function (category) {
|
|
|
|
return Number(category.id) === Number(id);
|
|
|
|
}.bind(this)
|
|
|
|
);
|
2020-05-12 21:52:32 +03:00
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
display(category);
|
2020-05-12 21:52:32 +03:00
|
|
|
|
|
|
|
$('#edit-category, #delete-category').prop('disabled', false);
|
2016-04-02 16:36:55 +03:00
|
|
|
}
|
2022-01-10 11:26:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
resetForm();
|
|
|
|
filter('');
|
|
|
|
bindEventHandlers();
|
|
|
|
}
|
2016-04-02 16:36:55 +03:00
|
|
|
|
2022-01-10 11:26:28 +03:00
|
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
|
|
|
|
|
|
return {
|
|
|
|
filter,
|
|
|
|
save,
|
|
|
|
remove,
|
|
|
|
getFilterHtml,
|
|
|
|
resetForm,
|
|
|
|
select
|
|
|
|
};
|
2016-04-02 16:36:55 +03:00
|
|
|
})();
|