Files
Nemera/src/Lumina.Presentation.Web/Core/Views/Tools/Settings.cshtml
T

169 lines
8.1 KiB
Plaintext

@*
/tools/settings
*@
@using Lumina.Presentation.Web.Common.DTO.UsersManagement
@using Lumina.Presentation.Web.Common.Services
@using Microsoft.AspNetCore.Mvc.Localization
@{
// generate a unique id for the scripts section of this view, so it can be dynamically loaded/unloaded by the global AJAX navigator handler
var scriptId = ScriptIdentifierHelper.GenerateScriptId();
}
@inject IUrlService _urlService
@inject IViewLocalizer _localizer
@model UserSettingsDto
<div style="display: grid; grid-template-rows: auto 1fr;"><!--container with two rows, one fixed, one filling remaining space-->
<div style="min-height: 1rem;"></div><!--the first row is a dummy row for "margin", because setting a margin on the next row would offset the parent's position-->
<div class="enlightenment-panel shadow-effect mx-auto panel w-50 mb-10" style="min-width: 320px;">
<div class="shine-effect"></div>
<form method="post" id="user-settings-form" class="inline">
@Html.AntiForgeryToken()
<div class="form-row">
<h1 class="text-light-two w-100 text-center pt-0 pb-0 engraved-title">
@_localizer["Settings"]
</h1>
</div>
<div class="form-container">
<!-- Pagination -->
<div class="form-row">
<div class="form-label">
<label asp-for="IsPaginationEnabled">@_localizer["EnablePagination"]</label>:
</div>
<div class="form-field">
<label class="enlightenment-checkbox-label">
<input type="checkbox" asp-for="IsPaginationEnabled" class="enlightenment-checkbox-hidden" />
<span class="enlightenment-checkbox"></span>
</label>
</div>
<small class="form-label text-disabled form-description">@_localizer["DescriptionEnablePagination"]</small>
</div>
<!-- Items per page -->
<div class="form-row">
<div class="form-label">
<label asp-for="ItemsPerPage">@_localizer["ItemsPerPage"]</label>
<span class="mandatory-field">*</span>:
</div>
<div class="form-field">
<input asp-for="ItemsPerPage" min="1" step="1" class="enlightenment-input" />
</div>
<small class="form-label text-disabled form-description">@_localizer["DescriptionItemsPerPage"]</small>
</div>
<!-- Alpha picker -->
<div class="form-row">
<div class="form-label">
<label asp-for="IgnoreThePrefixForAlphaPicker">@_localizer["IgnoreThePrefixForAlphaPicker"]</label>:
</div>
<div class="form-field">
<label class="enlightenment-checkbox-label">
<input type="checkbox" asp-for="IgnoreThePrefixForAlphaPicker" class="enlightenment-checkbox-hidden" />
<span class="enlightenment-checkbox"></span>
</label>
</div>
<small class="form-label text-disabled form-description">@_localizer["DescriptionIgnoreThePrefixForAlphaPicker"]</small>
</div>
<!-- Theme caching -->
<div class="form-row">
<div class="form-label">
<label asp-for="IsThemeCachingEnabled">@_localizer["EnableThemeCaching"]</label>:
</div>
<div class="form-field">
<label class="enlightenment-checkbox-label">
<input type="checkbox" asp-for="IsThemeCachingEnabled" class="enlightenment-checkbox-hidden" />
<span class="enlightenment-checkbox"></span>
</label>
</div>
<small class="form-label text-disabled form-description">@_localizer["DescriptionEnableThemeCaching"]</small>
</div>
<div class="form-row" style="justify-content: center;">
<button type="submit" class="confirm-button h-22px">@_localizer["Save"]</button>
</div>
</div>
</form>
</div>
</div>
@section Scripts {
<script type="text/javascript" data-form="@scriptId" defer>
// create cleanup registry for this view
const cleanupFunctions = [];
let isInitialized = false;
const form = document.getElementById('user-settings-form');
const checkboxIsPaginationEnabled = document.getElementById('IsPaginationEnabled');
const inputItemsPerPage = document.getElementById('ItemsPerPage');
const checkboxIgnoreThePrefixForAlphaPicker = document.getElementById('IgnoreThePrefixForAlphaPicker');
const checkboxIsThemeCachingEnabled = document.getElementById('IsThemeCachingEnabled');
// toggle the enabled state of the items per page input based on the pagination checkbox
function updateItemsPerPageEnabledState() {
inputItemsPerPage.disabled = !checkboxIsPaginationEnabled.checked;
}
// load the settings of the current user
async function loadUserSettingsAsync() {
const data = await callApiGetAsync('@_urlService.GetAbsoluteUrl(WebRoutes.Settings.GET_USER_SETTINGS)');
if (data) {
checkboxIsPaginationEnabled.checked = data.isPaginationEnabled === true;
inputItemsPerPage.value = data.itemsPerPage;
checkboxIgnoreThePrefixForAlphaPicker.checked = data.ignoreThePrefixForAlphaPicker === true;
checkboxIsThemeCachingEnabled.checked = data.isThemeCachingEnabled === true;
updateItemsPerPageEnabledState();
}
}
// save the settings of the current user
async function handleSaveUserSettingsAsync() {
const formData = {
IsPaginationEnabled: checkboxIsPaginationEnabled.checked,
ItemsPerPage: Number(inputItemsPerPage.value),
IgnoreThePrefixForAlphaPicker: checkboxIgnoreThePrefixForAlphaPicker.checked,
IsThemeCachingEnabled: checkboxIsThemeCachingEnabled.checked
};
const data = await callApiPostAsync('@_urlService.GetAbsoluteUrl(WebRoutes.Settings.UPDATE_USER_SETTINGS)', formData);
if (data && data.isUpdated === true)
notificationService.show('@_localizer["SettingsSaved"]', NotificationType.SUCCESS);
}
// on form submit event
const handleSubmit = async function(e) {
e.preventDefault();
await handleSaveUserSettingsAsync();
};
// on pagination checkbox change, update the enabled state of the items per page input
const handlePaginationCheckboxChange = function() {
updateItemsPerPageEnabledState();
};
// add listeners
form.addEventListener('submit', handleSubmit);
checkboxIsPaginationEnabled.addEventListener('change', handlePaginationCheckboxChange);
document.addEventListener('DOMContentLoaded', InitializeAsync);
// register cleanup for these listeners
cleanupFunctions.push(() => {
form.removeEventListener('submit', handleSubmit);
checkboxIsPaginationEnabled.removeEventListener('change', handlePaginationCheckboxChange);
document.removeEventListener('DOMContentLoaded', InitializeAsync);
});
async function InitializeAsync() {
if (isInitialized)
return;
isInitialized = true;
await loadUserSettingsAsync();
}
// store all cleanups for this view on window object using the scriptId
window.viewCleanups = window.viewCleanups || {};
window.viewCleanups['@scriptId'] = () => {
cleanupFunctions.forEach(cleanup => cleanup());
console.log(`Unloaded @scriptId`);
};
console.log(`Loaded @scriptId`);
</script>
}