iflrandevu/assets/js/backend_services_helper.js

420 lines
14 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';
/**
* ServicesHelper
*
* This class contains the methods that will be used by the "Services" tab of the page.
*
* @class ServicesHelper
*/
function ServicesHelper() {
this.filterResults = {};
this.filterLimit = 20;
2017-01-21 22:02:38 +03:00
}
2018-01-23 12:08:37 +03:00
ServicesHelper.prototype.bindEventHandlers = function () {
var instance = this;
/**
* Event: Filter Services Form "Submit"
*
* @param {jQuery.Event} event
*/
2018-01-23 12:08:37 +03:00
$('#filter-services form').submit(function (event) {
var key = $('#filter-services .key').val();
$('#filter-services .selected').removeClass('selected');
instance.resetForm();
instance.filter(key);
return false;
});
/**
* Event: Filter Service Cancel Button "Click"
*/
2018-01-23 12:08:37 +03:00
$('#filter-services .clear').click(function () {
$('#filter-services .key').val('');
instance.filter('');
instance.resetForm();
});
/**
* Event: Filter Service Row "Click"
*
* Display the selected service data to the user.
*/
2018-01-23 12:08:37 +03:00
$(document).on('click', '.service-row', function () {
if ($('#filter-services .filter').prop('disabled')) {
$('#filter-services .results').css('color', '#AAA');
return; // exit because we are on edit mode
}
2016-07-15 21:52:21 +03:00
var serviceId = $(this).attr('data-id');
var service = instance.filterResults.find(function (filterResult) {
return Number(filterResult.id) === Number(serviceId);
});
// Add dedicated provider link.
var dedicatedUrl = GlobalVariables.baseUrl + '/index.php?service=' + encodeURIComponent(service.id);
var $link = $('<a/>', {
'href': dedicatedUrl,
'html': [
$('<span/>', {
'class': 'fas fa-link'
})
]
});
$('#services .record-details h3')
.find('a')
.remove()
.end()
.append($link);
instance.display(service);
$('#filter-services .selected').removeClass('selected');
$(this).addClass('selected');
$('#edit-service, #delete-service').prop('disabled', false);
});
/**
* Event: Add New Service Button "Click"
*/
2018-01-23 12:08:37 +03:00
$('#add-service').click(function () {
instance.resetForm();
$('#services .add-edit-delete-group').hide();
$('#services .save-cancel-group').show();
2020-05-05 20:35:33 +03:00
$('#services .record-details').find('input, textarea').prop('disabled', false);
$('#services .record-details').find('select').prop('disabled', false);
$('#filter-services button').prop('disabled', true);
$('#filter-services .results').css('color', '#AAA');
});
/**
* Event: Cancel Service Button "Click"
*
* Cancel add or edit of a service record.
*/
2018-01-23 12:08:37 +03:00
$('#cancel-service').click(function () {
var id = $('#service-id').val();
instance.resetForm();
if (id !== '') {
instance.select(id, true);
}
});
/**
* Event: Save Service Button "Click"
*/
2018-01-23 12:08:37 +03:00
$('#save-service').click(function () {
var service = {
name: $('#service-name').val(),
duration: $('#service-duration').val(),
price: $('#service-price').val(),
currency: $('#service-currency').val(),
description: $('#service-description').val(),
location: $('#service-location').val(),
availabilities_type: $('#service-availabilities-type').val(),
attendants_number: $('#service-attendants-number').val()
};
if ($('#service-category').val() !== 'null') {
service.id_service_categories = $('#service-category').val();
} else {
service.id_service_categories = null;
}
if ($('#service-id').val() !== '') {
service.id = $('#service-id').val();
}
if (!instance.validate()) {
return;
}
instance.save(service);
});
/**
* Event: Edit Service Button "Click"
*/
2018-01-23 12:08:37 +03:00
$('#edit-service').click(function () {
$('#services .add-edit-delete-group').hide();
$('#services .save-cancel-group').show();
2020-05-05 20:35:33 +03:00
$('#services .record-details').find('input, textarea').prop('disabled', false);
$('#services .record-details select').prop('disabled', false);
$('#filter-services button').prop('disabled', true);
$('#filter-services .results').css('color', '#AAA');
});
/**
* Event: Delete Service Button "Click"
*/
2018-01-23 12:08:37 +03:00
$('#delete-service').click(function () {
2016-07-15 21:52:21 +03:00
var serviceId = $('#service-id').val();
var buttons = [
{
text: EALang.delete,
2018-01-23 12:08:37 +03:00
click: function () {
instance.delete(serviceId);
$('#message_box').dialog('close');
}
},
{
text: EALang.cancel,
2018-01-23 12:08:37 +03:00
click: function () {
$('#message_box').dialog('close');
}
}
];
GeneralFunctions.displayMessageBox(EALang.delete_service,
2018-01-23 12:08:37 +03:00
EALang.delete_record_prompt, buttons);
});
};
/**
* Save service record to database.
*
* @param {Object} service Contains the service record data. If an 'id' value is provided
* then the update operation is going to be executed.
*/
2018-01-23 12:08:37 +03:00
ServicesHelper.prototype.save = function (service) {
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_service';
var data = {
2016-07-15 21:52:21 +03:00
csrfToken: GlobalVariables.csrfToken,
service: JSON.stringify(service)
};
$.post(url, data)
.done(function (response) {
Backend.displayNotification(EALang.service_saved);
this.resetForm();
$('#filter-services .key').val('');
this.filter('', response.id, true);
}.bind(this))
.fail(GeneralFunctions.ajaxFailureHandler);
};
/**
* Delete a service record from database.
*
* @param {Number} id Record ID to be deleted.
*/
2018-01-23 12:08:37 +03:00
ServicesHelper.prototype.delete = function (id) {
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_service';
var data = {
2016-07-15 21:52:21 +03:00
csrfToken: GlobalVariables.csrfToken,
service_id: id
};
$.post(url, data)
.done(function () {
Backend.displayNotification(EALang.service_deleted);
this.resetForm();
this.filter($('#filter-services .key').val());
}.bind(this))
.fail(GeneralFunctions.ajaxFailureHandler);
};
/**
* Validates a service record.
*
* @return {Boolean} Returns the validation result.
*/
2018-01-23 12:08:37 +03:00
ServicesHelper.prototype.validate = function () {
$('#services .has-error').removeClass('has-error');
try {
// validate required fields.
var missingRequired = false;
$('#services .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;
}
};
/**
* Resets the service tab form back to its initial state.
*/
2018-01-23 12:08:37 +03:00
ServicesHelper.prototype.resetForm = function () {
$('#services .record-details').find('input, textarea').val('');
$('#service-category').val('null');
$('#services .add-edit-delete-group').show();
$('#services .save-cancel-group').hide();
$('#edit-service, #delete-service').prop('disabled', true);
2020-05-05 20:35:33 +03:00
$('#services .record-details').find('input, textarea').prop('disabled', true);
$('#services .record-details').find('select').prop('disabled', true);
$('.record-details .has-error').removeClass('has-error');
$('#filter-services .selected').removeClass('selected');
$('#filter-services button').prop('disabled', false);
$('#filter-services .results').css('color', '');
};
/**
* Display a service record into the service form.
*
* @param {Object} service Contains the service record data.
*/
2018-01-23 12:08:37 +03:00
ServicesHelper.prototype.display = function (service) {
$('#service-id').val(service.id);
$('#service-name').val(service.name);
$('#service-duration').val(service.duration);
$('#service-price').val(service.price);
$('#service-currency').val(service.currency);
$('#service-description').val(service.description);
$('#service-location').val(service.location);
$('#service-availabilities-type').val(service.availabilities_type);
$('#service-attendants-number').val(service.attendants_number);
var categoryId = (service.id_service_categories !== null) ? service.id_service_categories : 'null';
$('#service-category').val(categoryId);
};
/**
* Filters service records depending a string key.
*
* @param {String} key This is used to filter the service records of the database.
* @param {Number} selectId Optional, if set then after the filter operation the record with this
* 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
ServicesHelper.prototype.filter = function (key, selectId, display) {
display = display || false;
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_services';
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-services .results').empty();
response.forEach(function (service, index) {
$('#filter-services .results')
.append(ServicesHelper.prototype.getFilterHtml(service))
.append( $('<hr/>'))
});
if (response.length === 0) {
$('#filter-services .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-services .results');
}
if (selectId) {
this.select(selectId, display);
}
}.bind(this))
.fail(GeneralFunctions.ajaxFailureHandler);
};
/**
* Get Filter HTML
*
* Get a service row HTML code that is going to be displayed on the filter results list.
*
* @param {Object} service Contains the service record data.
*
* @return {String} The HTML code that represents the record on the filter results list.
*/
2018-01-23 12:08:37 +03:00
ServicesHelper.prototype.getFilterHtml = function (service) {
var name = service.name;
var info = service.duration + ' min - ' + service.price + ' ' + service.currency;
return $('<div/>', {
'class': 'service-row entry',
'data-id': service.id,
'html': [
$('<strong/>', {
'text': name
}),
$('<br/>'),
$('<span/>', {
'text': info
}),
$('<br/>')
]
});
};
/**
* Select a specific record from the current filter results. If the service 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
ServicesHelper.prototype.select = function (id, display) {
display = display || false;
$('#filter-services .selected').removeClass('selected');
$('#filter-services .service-row[data-id="' + id + '"]').addClass('selected');
if (display) {
var service = this.filterResults.find(function (filterResult) {
return Number(filterResult.id) === Number(id);
});
this.display(service);
$('#edit-service, #delete-service').prop('disabled', false);
}
};
window.ServicesHelper = ServicesHelper;
})();