easyappointments/assets/js/pages/general_settings.js

108 lines
2.8 KiB
JavaScript
Raw Normal View History

/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web 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
2021-12-18 23:04:15 +03:00
* @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(App.Lang.fields_are_required);
}
return false;
} catch (error) {
Backend.displayNotification(error.message);
return true;
}
}
function deserialize(generalSettings) {
generalSettings.forEach((generalSetting) => {
2021-12-18 23:04:15 +03:00
$('[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()) {
Backend.displayNotification(App.Lang.settings_are_invalid);
return;
}
const generalSettings = serialize();
App.Http.GeneralSettings.save(generalSettings).done(() => {
Backend.displayNotification(App.Lang.settings_saved);
});
}
/**
* Initialize the module.
*/
2022-01-14 10:31:12 +03:00
function initialize() {
const generalSettings = App.Vars.general_settings;
deserialize(generalSettings);
$saveSettings.on('click', onSaveSettingsClick);
Backend.placeFooterToBottom();
}
2022-01-14 10:31:12 +03:00
document.addEventListener('DOMContentLoaded', initialize);
return {};
})();