mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-14 03:52:21 +03:00
107 lines
2.9 KiB
JavaScript
107 lines
2.9 KiB
JavaScript
/* ----------------------------------------------------------------------------
|
|
* Easy!Appointments - Online Appointment Scheduler
|
|
*
|
|
* @package EasyAppointments
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
* @link https://easyappointments.org
|
|
* @since v1.5.0
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
* General settings page.
|
|
*
|
|
* This module implements the functionality of the general settings page.
|
|
*/
|
|
App.Pages.GeneralSettings = (function () {
|
|
const $saveSettings = $('#save-settings');
|
|
|
|
/**
|
|
* Check if the form has invalid values.
|
|
*
|
|
* @return {Boolean}
|
|
*/
|
|
function isInvalid() {
|
|
try {
|
|
$('#general-settings .is-invalid').removeClass('is-invalid');
|
|
|
|
// Validate required fields.
|
|
|
|
let missingRequiredFields = false;
|
|
|
|
$('#general-settings .required').each((index, requiredField) => {
|
|
const $requiredField = $(requiredField);
|
|
|
|
if (!$requiredField.val()) {
|
|
$requiredField.addClass('is-invalid');
|
|
missingRequiredFields = true;
|
|
}
|
|
});
|
|
|
|
if (missingRequiredFields) {
|
|
throw new Error(lang('fields_are_required'));
|
|
}
|
|
|
|
return false;
|
|
} catch (error) {
|
|
App.Layouts.Backend.displayNotification(error.message);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
function deserialize(generalSettings) {
|
|
generalSettings.forEach((generalSetting) => {
|
|
$('[data-field="' + generalSetting.name + '"]').val(generalSetting.value);
|
|
});
|
|
}
|
|
|
|
function serialize() {
|
|
const generalSettings = [];
|
|
|
|
$('[data-field]').each((index, field) => {
|
|
const $field = $(field);
|
|
|
|
generalSettings.push({
|
|
name: $field.data('field'),
|
|
value: $field.val()
|
|
});
|
|
});
|
|
|
|
return generalSettings;
|
|
}
|
|
|
|
/**
|
|
* Save the account information.
|
|
*/
|
|
function onSaveSettingsClick() {
|
|
if (isInvalid()) {
|
|
App.Layouts.Backend.displayNotification(lang('settings_are_invalid'));
|
|
|
|
return;
|
|
}
|
|
|
|
const generalSettings = serialize();
|
|
|
|
App.Http.GeneralSettings.save(generalSettings).done(() => {
|
|
App.Layouts.Backend.displayNotification(lang('settings_saved'));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Initialize the module.
|
|
*/
|
|
function initialize() {
|
|
const generalSettings = vars('general_settings');
|
|
|
|
deserialize(generalSettings);
|
|
|
|
$saveSettings.on('click', onSaveSettingsClick);
|
|
|
|
App.Layouts.Backend.placeFooterToBottom();
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', initialize);
|
|
|
|
return {};
|
|
})();
|