iflrandevu/assets/js/backend_categories_helper.js

368 lines
12 KiB
JavaScript
Raw Normal View History

/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.0.0
* ---------------------------------------------------------------------------- */
2018-01-23 12:08:37 +03:00
(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;
2017-01-21 22:02:38 +03:00
}
/**
* Binds the default event handlers of the categories tab.
*/
2018-01-23 12:08:37 +03:00
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"
*/
2018-01-23 12:08:37 +03:00
$('#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.
*/
2018-01-23 12:08:37 +03:00
$(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, textarea').prop('readonly', 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, textarea').prop('readonly', 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,
2018-01-23 12:08:37 +03:00
click: function () {
$('#message-box').dialog('close');
}
},
{
text: EALang.delete,
2018-01-23 12:08:37 +03:00
click: function () {
instance.delete(categoryId);
$('#message-box').dialog('close');
}
},
];
GeneralFunctions.displayMessageBox(EALang.delete_category,
2018-01-23 12:08:37 +03:00
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.
*/
2018-01-23 12:08:37 +03:00
CategoriesHelper.prototype.filter = function (key, selectId, display) {
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_service_categories';
var data = {
2016-07-15 21:52:21 +03:00
csrfToken: GlobalVariables.csrfToken,
key: key,
limit: this.filterLimit
2016-07-15 21:52:21 +03:00
};
$.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($('<hr/>'));
}.bind(this));
if (response.length === 0) {
$('#filter-categories .results').append(
$('<em/>', {
'text': EALang.no_records_found
})
);
} else if (response.length === this.filterLimit) {
$('<button/>', {
'type': 'button',
'class': 'btn btn-block btn-light load-more text-center',
'text': EALang.load_more,
'click': function () {
this.filterLimit += 20;
this.filter(key, selectId, display);
}.bind(this)
})
.appendTo('#filter-categories .results');
}
if (selectId) {
this.select(selectId, display);
}
}.bind(this))
.fail(GeneralFunctions.ajaxFailureHandler);
};
/**
* Save a category record to the database (via AJAX post).
*
* @param {Object} category Contains the category data.
*/
2018-01-23 12:08:37 +03:00
CategoriesHelper.prototype.save = function (category) {
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_service_category';
var data = {
2016-07-15 21:52:21 +03:00
csrfToken: GlobalVariables.csrfToken,
category: JSON.stringify(category)
};
$.post(url, data)
.done(function (response) {
Backend.displayNotification(EALang.service_category_saved);
this.resetForm();
$('#filter-categories .key').val('');
this.filter('', response.id, true);
BackendServices.updateAvailableCategories();
}.bind(this))
.fail(GeneralFunctions.ajaxFailureHandler);
};
/**
* Delete category record.
*
* @param Number} id Record ID to be deleted.
*/
2018-01-23 12:08:37 +03:00
CategoriesHelper.prototype.delete = function (id) {
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_service_category';
var data = {
2016-07-15 21:52:21 +03:00
csrfToken: GlobalVariables.csrfToken,
category_id: id
};
$.post(url, data)
.done(function () {
Backend.displayNotification(EALang.service_category_deleted);
this.resetForm();
this.filter($('#filter-categories .key').val());
BackendServices.updateAvailableCategories();
}.bind(this))
.fail(GeneralFunctions.ajaxFailureHandler);
};
/**
* Display a category record on the form.
*
* @param {Object} category Contains the category data.
*/
2018-01-23 12:08:37 +03:00
CategoriesHelper.prototype.display = function (category) {
$('#category-id').val(category.id);
$('#category-name').val(category.name);
$('#category-description').val(category.description);
};
/**
* Validate category data before save (insert or update).
*
* @return {Boolean} Returns the validation result.
*/
2018-01-23 12:08:37 +03:00
CategoriesHelper.prototype.validate = function () {
$('#categories .has-error').removeClass('has-error');
try {
var missingRequired = false;
$('#categories .required').each(function (index, requiredField) {
if (!$(requiredField).val()) {
$(requiredField).closest('.form-group').addClass('has-error');
missingRequired = true;
}
});
if (missingRequired) {
throw new Error(EALang.fields_are_required);
}
return true;
} catch (error) {
return false;
}
};
/**
* Bring the category form back to its initial state.
*/
2018-01-23 12:08:37 +03:00
CategoriesHelper.prototype.resetForm = function () {
$('#categories .add-edit-delete-group').show();
$('#categories .save-cancel-group').hide();
$('#categories .record-details').find('input, textarea').val('');
$('#categories .record-details').find('input, textarea').prop('readonly', true);
$('#edit-category, #delete-category').prop('disabled', true);
$('.record-details .has-error').removeClass('has-error');
$('#filter-categories .selected').removeClass('selected');
$('#filter-categories .results').css('color', '');
$('#filter-categories button').prop('disabled', false);
};
/**
* Get the filter results row HTML code.
*
* @param {Object} category Contains the category data.
*
* @return {String} Returns the record HTML code.
*/
2018-01-23 12:08:37 +03:00
CategoriesHelper.prototype.getFilterHtml = function (category) {
return $('<div/>', {
'class': 'category-row entry',
'data-id': category.id,
'html': [
$('<strong/>', {
'text': category.name
}),
$('<br/>'),
]
});
};
/**
* 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.
*
* @param {Number} id The record ID to be selected from the filter results.
* @param {Boolean} display Optional (false), if true then the method will display the record
* on the form.
*/
2018-01-23 12:08:37 +03:00
CategoriesHelper.prototype.select = function (id, display) {
display = display || false;
$('#filter-categories .selected').removeClass('selected');
$('#filter-categories .category-row[data-id="' + id + '"]').addClass('selected');
if (display) {
var category = this.filterResults.find(function (category) {
return Number(category.id) === Number(id);
}.bind(this));
this.display(category);
$('#edit-category, #delete-category').prop('disabled', false);
}
};
window.CategoriesHelper = CategoriesHelper;
})();