mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 13:11:10 +00:00
Adds a themes management page to the administrator menu
This commit is contained in:
1 parent
351368e7b5
commit
932fe2be28
25 files changed
+1824
-1
No files matched your search
@@ -0,0 +1,166 @@
|
||||
@*
|
||||
/admin/manage-themes/
|
||||
*@
|
||||
|
||||
@using Lumina.Presentation.Web.Common.DTO.Themes
|
||||
@using Lumina.Presentation.Web.Common.Services
|
||||
@using Microsoft.AspNetCore.Mvc.Localization
|
||||
|
||||
@{
|
||||
var themes = ViewData["themes"] as IReadOnlyList<ThemeInfoDto>;
|
||||
var currentThemeId = ViewData["currentThemeId"] as string;
|
||||
// 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();
|
||||
int bundledThemeCount = 0;
|
||||
if (themes is not null)
|
||||
foreach (ThemeInfoDto theme in themes)
|
||||
if (theme.IsBundled)
|
||||
bundledThemeCount++;
|
||||
}
|
||||
|
||||
@inject IViewLocalizer _localizer
|
||||
@inject IUrlService _urlService
|
||||
|
||||
@Html.AntiForgeryToken()
|
||||
|
||||
<div style="display: grid; grid-template-rows: auto 1fr;">
|
||||
<div style="min-height: 1rem;"></div>
|
||||
<div class="enlightenment-panel shadow-effect mx-auto panel w-75 mb-10" style="min-width: 320px;">
|
||||
<div class="shine-effect"></div>
|
||||
<div class="form-row">
|
||||
<h1 class="text-light-two w-100 text-center pt-0 pb-0 engraved-title">@_localizer["Themes"]</h1>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<button id="btn-upload-theme" type="button" class="confirm-button">@_localizer["Upload"]</button>
|
||||
<input id="theme-upload-input" type="file" accept=".zip" hidden />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
@if (themes is null || themes.Count == 0)
|
||||
{
|
||||
<p class="text-center">@_localizer["NoThemes"]</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<table id="themes-table">
|
||||
<thead class="text-light-two">
|
||||
<tr>
|
||||
<th class="w-30">@_localizer["Name"]</th>
|
||||
<th class="w-15">@_localizer["Version"]</th>
|
||||
<th class="w-25">@_localizer["Author"]</th>
|
||||
<th class="w-30">@_localizer["Actions"]</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (ThemeInfoDto theme in themes)
|
||||
{
|
||||
bool isCurrent = theme.Id == currentThemeId;
|
||||
bool isLastRemaining = themes.Count <= 1 || (theme.IsBundled && bundledThemeCount == 1);
|
||||
<tr data-theme-id="@theme.Id">
|
||||
<td>@theme.Name</td>
|
||||
<td>@theme.Version</td>
|
||||
<td>@theme.Author</td>
|
||||
<td class="form-row">
|
||||
<button type="button" class="confirm-button activate-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.SET_CURRENT_THEME)" @(isCurrent ? "disabled" : null)>@_localizer["Activate"]</button>
|
||||
<button type="button" class="confirm-button download-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.DOWNLOAD_THEME, new { themeId = theme.Id })">@_localizer["Download"]</button>
|
||||
<button type="button" class="abort-button delete-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.DELETE_THEME, new { themeId = theme.Id })" @(isLastRemaining ? "disabled" : null)>@_localizer["Delete"]</button>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@section Scripts {
|
||||
<script type="text/javascript" data-form="@scriptId" defer>
|
||||
// create cleanup registry for this view
|
||||
const cleanupFunctions = [];
|
||||
let isInitialized = false;
|
||||
|
||||
const uploadButton = document.getElementById('btn-upload-theme');
|
||||
const uploadInput = document.getElementById('theme-upload-input');
|
||||
const installUrl = '@_urlService.GetAbsoluteUrl(WebRoutes.Themes.INSTALL_THEME)';
|
||||
|
||||
// reloads the page after a theme mutation, so the new theme state and the themed shell are re-rendered
|
||||
function reloadPage() {
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
// activates the theme of the clicked row
|
||||
async function handleActivateClick(event) {
|
||||
const button = event.target.closest('.activate-theme-button');
|
||||
if (!button)
|
||||
return;
|
||||
await callApiPutAsync(button.dataset.url, { themeId: button.closest('tr').dataset.themeId });
|
||||
reloadPage();
|
||||
}
|
||||
|
||||
// deletes the theme of the clicked row after confirmation
|
||||
async function handleDeleteClick(event) {
|
||||
const button = event.target.closest('.delete-theme-button');
|
||||
if (!button)
|
||||
return;
|
||||
if (await dialogService.show('@_localizer["DeleteThemeConfirm"]', jsLocalizedDialogs.confirmation, DialogButtons.YES_NO, DialogIcon.QUESTION) === DialogButton.YES) {
|
||||
await callApiDeleteAsync(button.dataset.url);
|
||||
reloadPage();
|
||||
}
|
||||
}
|
||||
|
||||
// downloads the archive of the theme of the clicked row
|
||||
function handleDownloadClick(event) {
|
||||
const button = event.target.closest('.download-theme-button');
|
||||
if (button)
|
||||
window.location.href = button.dataset.url;
|
||||
}
|
||||
|
||||
// uploads the selected theme archive as a multipart request, keeping the antiforgery token
|
||||
async function handleUploadInputChange() {
|
||||
const file = uploadInput.files[0];
|
||||
if (!file)
|
||||
return;
|
||||
const formData = new FormData();
|
||||
formData.append('archive', file);
|
||||
formData.append('__RequestVerificationToken', document.querySelector('input[name="__RequestVerificationToken"]')?.value ?? '');
|
||||
const response = await fetch(installUrl, {
|
||||
method: 'POST',
|
||||
headers: { 'X-Requested-With': 'XMLHttpRequest' },
|
||||
body: formData
|
||||
});
|
||||
if (response.ok)
|
||||
reloadPage();
|
||||
else
|
||||
notificationService.show('@_localizer["UploadFailed"]', NotificationType.ERROR);
|
||||
}
|
||||
|
||||
// opens the file picker when the upload button is clicked
|
||||
function handleUploadButtonClick() {
|
||||
uploadInput.click();
|
||||
}
|
||||
|
||||
// add listeners
|
||||
uploadButton.addEventListener('click', handleUploadButtonClick);
|
||||
uploadInput.addEventListener('change', handleUploadInputChange);
|
||||
document.addEventListener('click', handleActivateClick);
|
||||
document.addEventListener('click', handleDownloadClick);
|
||||
document.addEventListener('click', handleDeleteClick);
|
||||
|
||||
// register cleanup for these listeners
|
||||
cleanupFunctions.push(() => {
|
||||
uploadButton.removeEventListener('click', handleUploadButtonClick);
|
||||
uploadInput.removeEventListener('change', handleUploadInputChange);
|
||||
document.removeEventListener('click', handleActivateClick);
|
||||
document.removeEventListener('click', handleDownloadClick);
|
||||
document.removeEventListener('click', handleDeleteClick);
|
||||
});
|
||||
|
||||
// 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>
|
||||
}
|
||||
@@ -52,6 +52,7 @@
|
||||
<li><a href="@_urlService.GetAbsoluteUrl(WebRoutes.Admin.MANAGE_ROLES)" class="nav-link"><span>@_localizer["ManageRoles"]</span></a></li>
|
||||
<li><a href="@_urlService.GetAbsoluteUrl(WebRoutes.Admin.MANAGE_PERMISSIONS)" class="nav-link"><span>@_localizer["ManagePermissions"]</span></a></li>
|
||||
<li><a href="@_urlService.GetAbsoluteUrl(WebRoutes.Plugins.INDEX)" class="nav-link"><span>@_localizer["Plugins"]</span></a></li>
|
||||
<li><a href="@_urlService.GetAbsoluteUrl(WebRoutes.Admin.MANAGE_THEMES)" class="nav-link"><span>@_localizer["Themes"]</span></a></li>
|
||||
</ul>
|
||||
}
|
||||
<div class="mobile-menu-heading">
|
||||
@@ -157,6 +158,7 @@
|
||||
<li><a href="@_urlService.GetAbsoluteUrl(WebRoutes.Admin.MANAGE_ROLES)" class="nav-link">@_localizer["ManageRoles"]</a></li>
|
||||
<li><a href="@_urlService.GetAbsoluteUrl(WebRoutes.Admin.MANAGE_PERMISSIONS)" class="nav-link">@_localizer["ManagePermissions"]</a></li>
|
||||
<li><a href="@_urlService.GetAbsoluteUrl(WebRoutes.Plugins.INDEX)" class="nav-link">@_localizer["Plugins"]</a></li>
|
||||
<li><a href="@_urlService.GetAbsoluteUrl(WebRoutes.Admin.MANAGE_THEMES)" class="nav-link">@_localizer["Themes"]</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user