/* ---------------------------------------------------------------------------- * 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').on('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 = instance.filterResults.find(function (filterResult) { return Number(filterResult.id) === Number(categoryId); }); 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').on('click', function () { instance.resetForm(); $('#categories .add-edit-delete-group').hide(); $('#categories .save-cancel-group').show(); $('#categories .record-details').find('input, select, textarea').prop('disabled', false); $('#filter-categories button').prop('disabled', true); $('#filter-categories .results').css('color', '#AAA'); }); /** * Event: Edit Category Button "Click" */ $('#edit-category').on('click', function () { $('#categories .add-edit-delete-group').hide(); $('#categories .save-cancel-group').show(); $('#categories .record-details').find('input, select, textarea').prop('disbaled', false); $('#filter-categories button').prop('disabled', true); $('#filter-categories .results').css('color', '#AAA'); }); /** * Event: Delete Category Button "Click" */ $('#delete-category').on('click', function () { var categoryId = $('#category-id').val(); var buttons = [ { text: EALang.cancel, click: function () { $('#message-box').dialog('close'); } }, { text: EALang.delete, click: function () { instance.delete(categoryId); $('#message-box').dialog('close'); } }, ]; GeneralFunctions.displayMessageBox(EALang.delete_category, EALang.delete_record_prompt, buttons); }); /** * Event: Categories Save Button "Click" */ $('#save-category').on('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').on('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 url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_service_categories'; var data = { csrfToken: GlobalVariables.csrfToken, key: key, limit: this.filterLimit }; $.post(url, data) .done(function (response) { this.filterResults = response; $('#filter-categories .results').empty(); response.forEach(function(category) { $('#filter-categories .results') .append(this.getFilterHtml(category)) .append($('
')); }.bind(this)); if (response.length === 0) { $('#filter-categories .results').append( $('', { 'text': EALang.no_records_found }) ); } else if (response.length === this.filterLimit) { $('