/* ---------------------------------------------------------------------------- * Easy!Appointments - Open Source Web Scheduler * * @package EasyAppointments * @author A.Tselegidis * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 * @link http://easyappointments.org * @since v1.0.0 * ---------------------------------------------------------------------------- */ (function () { 'use strict'; /** * CategoriesHelper Class * * This class contains the core method implementations that belong to the categories tab * of the backend services page. * * @class CategoriesHelper */ function CategoriesHelper() { this.filterResults = {}; this.filterLimit = 20; } /** * Binds the default event handlers of the categories tab. */ CategoriesHelper.prototype.bindEventHandlers = function () { var instance = this; /** * Event: Filter Categories Cancel Button "Click" */ $('#filter-categories .clear').click(function () { $('#filter-categories .key').val(''); instance.filter(''); instance.resetForm(); }); /** * Event: Filter Categories Form "Submit" */ $('#filter-categories form').submit(function () { var key = $('#filter-categories .key').val(); $('.selected').removeClass('selected'); instance.resetForm(); instance.filter(key); return false; }); /** * Event: Filter Categories Row "Click" * * Displays the selected row data on the right side of the page. */ $(document).on('click', '.category-row', function () { if ($('#filter-categories .filter').prop('disabled')) { $('#filter-categories .results').css('color', '#AAA'); return; // exit because we are on edit mode } var categoryId = $(this).attr('data-id'); var category = {}; $.each(instance.filterResults, function (index, item) { if (item.id === categoryId) { category = item; return false; } }); instance.display(category); $('#filter-categories .selected').removeClass('selected'); $(this).addClass('selected'); $('#edit-category, #delete-category').prop('disabled', false); }); /** * Event: Add Category Button "Click" */ $('#add-category').click(function () { instance.resetForm(); $('#categories .add-edit-delete-group').hide(); $('#categories .save-cancel-group').show(); $('#categories .record-details').find('input, textarea').prop('readonly', false); $('#filter-categories button').prop('disabled', true); $('#filter-categories .results').css('color', '#AAA'); }); /** * Event: Edit Category Button "Click" */ $('#edit-category').click(function () { $('#categories .add-edit-delete-group').hide(); $('#categories .save-cancel-group').show(); $('#categories .record-details').find('input, textarea').prop('readonly', false); $('#filter-categories button').prop('disabled', true); $('#filter-categories .results').css('color', '#AAA'); }); /** * Event: Delete Category Button "Click" */ $('#delete-category').click(function () { var categoryId = $('#category-id').val(); var buttons = [ { text: EALang.delete, click: function () { instance.delete(categoryId); $('#message_box').dialog('close'); } }, { text: EALang.cancel, click: function () { $('#message_box').dialog('close'); } } ]; GeneralFunctions.displayMessageBox(EALang.delete_category, EALang.delete_record_prompt, buttons); }); /** * Event: Categories Save Button "Click" */ $('#save-category').click(function () { var category = { name: $('#category-name').val(), description: $('#category-description').val() }; if ($('#category-id').val() !== '') { category.id = $('#category-id').val(); } if (!instance.validate()) { return; } instance.save(category); }); /** * Event: Cancel Category Button "Click" */ $('#cancel-category').click(function () { var id = $('#category-id').val(); instance.resetForm(); if (id !== '') { instance.select(id, true); } }); }; /** * Filter service categories records. * * @param {String} key 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} display Optional (false), if true then the selected record will be displayed on the form. */ CategoriesHelper.prototype.filter = function (key, selectId, display) { var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_service_categories'; var postData = { csrfToken: GlobalVariables.csrfToken, key: key, limit: this.filterLimit }; $.post(postUrl, postData, function (response) { if (!GeneralFunctions.handleAjaxExceptions(response)) { return; } this.filterResults = response; $('#filter-categories .results').html(''); $.each(response, function (index, category) { var html = this.getFilterHtml(category); $('#filter-categories .results').append(html); }.bind(this)); if (response.length === 0) { $('#filter-categories .results').html('' + EALang.no_records_found + ''); } else if (response.length === this.filterLimit) { $('