MaketRandevu/assets/js/pages/legal_settings.js
2022-01-14 08:31:12 +01:00

157 lines
4.8 KiB
JavaScript

/* ----------------------------------------------------------------------------
* 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
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Legal Settings
*
* Contains the functionality of the legal settings page.
*/
App.Pages.LegalSettings = (function () {
const $saveSettings = $('#save-settings');
const $displayCookieNotice = $('#display-cookie-notice');
const $cookieNoticeContent = $('#cookie-notice-content');
const $displayTermsAndConditions = $('#display-terms-and-conditions');
const $termsAndConditionsContent = $('#terms-and-conditions-content');
const $displayPrivacyPolicy = $('#display-privacy-policy');
const $privacyPolicyContent = $('#privacy-policy-content');
/**
* Check if the form has invalid values.
*
* @return {Boolean}
*/
function isInvalid() {
try {
$('#legal-settings .is-invalid').removeClass('is-invalid');
// Validate required fields.
let missingRequiredFields = false;
$('#legal-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(legalSettings) {
legalSettings.forEach((legalSetting) => {
if (legalSetting.name === 'display_cookie_notice') {
$displayCookieNotice.prop('checked', Boolean(Number(legalSetting.value)));
}
if (legalSetting.name === 'cookie_notice_content') {
$cookieNoticeContent.trumbowyg('html', legalSetting.value);
}
if (legalSetting.name === 'display_terms_and_conditions') {
$displayTermsAndConditions.prop('checked', Boolean(Number(legalSetting.value)));
}
if (legalSetting.name === 'terms_and_conditions_content') {
$termsAndConditionsContent.trumbowyg('html', legalSetting.value);
}
if (legalSetting.name === 'display_privacy_policy') {
$displayPrivacyPolicy.prop('checked', Boolean(Number(legalSetting.value)));
}
if (legalSetting.name === 'privacy_policy_content') {
$privacyPolicyContent.trumbowyg('html', legalSetting.value);
}
});
}
function serialize() {
const legalSettings = [];
legalSettings.push({
name: 'display_cookie_notice',
value: $displayCookieNotice.prop('checked') ? '1' : '0'
});
legalSettings.push({
name: 'cookie_notice_content',
value: $cookieNoticeContent.trumbowyg('html')
});
legalSettings.push({
name: 'display_terms_and_conditions',
value: $displayTermsAndConditions.prop('checked') ? '1' : '0'
});
legalSettings.push({
name: 'terms_and_conditions_content',
value: $termsAndConditionsContent.trumbowyg('html')
});
legalSettings.push({
name: 'display_privacy_policy',
value: $displayPrivacyPolicy.prop('checked') ? '1' : '0'
});
legalSettings.push({
name: 'privacy_policy_content',
value: $privacyPolicyContent.trumbowyg('html')
});
return legalSettings;
}
/**
* Save the account information.
*/
function onSaveSettingsClick() {
if (isInvalid()) {
Backend.displayNotification(App.Lang.settings_are_invalid);
return;
}
const legalSettings = serialize();
App.Http.LegalSettings.save(legalSettings).done(() => {
Backend.displayNotification(App.Lang.settings_saved);
});
}
function initialize() {
$cookieNoticeContent.trumbowyg();
$termsAndConditionsContent.trumbowyg();
$privacyPolicyContent.trumbowyg();
const legalSettings = App.Vars.legal_settings;
deserialize(legalSettings);
$saveSettings.on('click', onSaveSettingsClick);
Backend.placeFooterToBottom();
}
document.addEventListener('DOMContentLoaded', initialize);
return {};
})();