iflrandevu/assets/js/utils/lang.js

79 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-01-17 19:42:12 +03:00
/* ----------------------------------------------------------------------------
2022-01-18 15:05:42 +03:00
* Easy!Appointments - Online Appointment Scheduler
2022-01-17 19:42:12 +03:00
*
* @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
* ---------------------------------------------------------------------------- */
/**
* Lang utility.
*
2022-01-24 19:05:38 +03:00
* This module implements the functionality of translations.
2022-01-17 19:42:12 +03:00
*/
window.App.Utils.Lang = (function () {
/**
* Enable Language Selection
*
* Enables the language selection functionality. Must be called on every page has a language selection button.
* This method requires the global variable "vars('available_variables')" to be initialized before the execution.
2022-01-17 19:42:12 +03:00
*
* @param {Object} $target Selected element button for the language selection.
*/
function enableLanguageSelection($target) {
// Select Language
const $languageList = $('<ul/>', {
'id': 'language-list',
'html': vars('available_languages').map((availableLanguage) =>
2022-01-17 19:42:12 +03:00
$('<li/>', {
'class': 'language',
'data-language': availableLanguage,
'text': App.Utils.String.upperCaseFirstLetter(availableLanguage),
}),
),
2022-01-17 19:42:12 +03:00
});
$target.popover({
placement: 'top',
title: 'Select Language',
content: $languageList[0],
html: true,
container: 'body',
trigger: 'manual',
2022-01-17 19:42:12 +03:00
});
$target.on('click', function (event) {
event.stopPropagation();
const $target = $(event.target);
if ($('#language-list').length === 0) {
$target.popover('show');
} else {
$target.popover('hide');
}
$target.toggleClass('active');
});
$(document).on('click', 'li.language', (event) => {
// Change language with HTTP request and refresh page.
const language = $(event.target).data('language');
App.Http.Localization.changeLanguage(language).done(() => document.location.reload());
2022-01-17 19:42:12 +03:00
});
$(document).on('click', () => {
$target.popover('hide');
});
}
return {
enableLanguageSelection,
2022-01-17 19:42:12 +03:00
};
})();