mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 18:21:10 +00:00
Adds theme previews to the themes list with a reusable image viewer
This commit is contained in:
1 parent
74e5efdf74
commit
ebafd743b5
16 files changed
+416
-7
No files matched your search
@@ -153,4 +153,7 @@
|
|||||||
<data name="DeleteThemeConfirm" xml:space="preserve">
|
<data name="DeleteThemeConfirm" xml:space="preserve">
|
||||||
<value>Möchten Sie das angegebene Thema wirklich löschen?</value>
|
<value>Möchten Sie das angegebene Thema wirklich löschen?</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Preview" xml:space="preserve">
|
||||||
|
<value>Vorschau</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
@@ -153,4 +153,7 @@
|
|||||||
<data name="DeleteThemeConfirm" xml:space="preserve">
|
<data name="DeleteThemeConfirm" xml:space="preserve">
|
||||||
<value>Are you sure you want to delete the specified theme?</value>
|
<value>Are you sure you want to delete the specified theme?</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Preview" xml:space="preserve">
|
||||||
|
<value>Preview</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
@@ -153,4 +153,7 @@
|
|||||||
<data name="DeleteThemeConfirm" xml:space="preserve">
|
<data name="DeleteThemeConfirm" xml:space="preserve">
|
||||||
<value>¿Está seguro de que desea eliminar el tema especificado?</value>
|
<value>¿Está seguro de que desea eliminar el tema especificado?</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Preview" xml:space="preserve">
|
||||||
|
<value>Vista previa</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
@@ -153,4 +153,7 @@
|
|||||||
<data name="DeleteThemeConfirm" xml:space="preserve">
|
<data name="DeleteThemeConfirm" xml:space="preserve">
|
||||||
<value>Voulez-vous vraiment supprimer le thème spécifié ?</value>
|
<value>Voulez-vous vraiment supprimer le thème spécifié ?</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Preview" xml:space="preserve">
|
||||||
|
<value>Aperçu</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
@@ -153,4 +153,7 @@
|
|||||||
<data name="DeleteThemeConfirm" xml:space="preserve">
|
<data name="DeleteThemeConfirm" xml:space="preserve">
|
||||||
<value>Sei sicuro di voler eliminare il tema specificato?</value>
|
<value>Sei sicuro di voler eliminare il tema specificato?</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Preview" xml:space="preserve">
|
||||||
|
<value>Anteprima</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
@@ -153,4 +153,7 @@
|
|||||||
<data name="DeleteThemeConfirm" xml:space="preserve">
|
<data name="DeleteThemeConfirm" xml:space="preserve">
|
||||||
<value>指定されたテーマを削除しますか?</value>
|
<value>指定されたテーマを削除しますか?</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Preview" xml:space="preserve">
|
||||||
|
<value>プレビュー</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
@@ -153,4 +153,7 @@
|
|||||||
<data name="DeleteThemeConfirm" xml:space="preserve">
|
<data name="DeleteThemeConfirm" xml:space="preserve">
|
||||||
<value>Are you sure you want to delete the specified theme?</value>
|
<value>Are you sure you want to delete the specified theme?</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Preview" xml:space="preserve">
|
||||||
|
<value>Preview</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
@@ -153,4 +153,7 @@
|
|||||||
<data name="DeleteThemeConfirm" xml:space="preserve">
|
<data name="DeleteThemeConfirm" xml:space="preserve">
|
||||||
<value>Sigur doriți să ștergeți tema specificată?</value>
|
<value>Sigur doriți să ștergeți tema specificată?</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Preview" xml:space="preserve">
|
||||||
|
<value>Previzualizare</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
@@ -153,4 +153,7 @@
|
|||||||
<data name="DeleteThemeConfirm" xml:space="preserve">
|
<data name="DeleteThemeConfirm" xml:space="preserve">
|
||||||
<value>Вы уверены, что хотите удалить указанную тему?</value>
|
<value>Вы уверены, что хотите удалить указанную тему?</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Preview" xml:space="preserve">
|
||||||
|
<value>Предпросмотр</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
@@ -153,4 +153,7 @@
|
|||||||
<data name="DeleteThemeConfirm" xml:space="preserve">
|
<data name="DeleteThemeConfirm" xml:space="preserve">
|
||||||
<value>确定要删除指定的主题吗?</value>
|
<value>确定要删除指定的主题吗?</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="Preview" xml:space="preserve">
|
||||||
|
<value>预览</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
@@ -23,6 +23,32 @@
|
|||||||
|
|
||||||
@Html.AntiForgeryToken()
|
@Html.AntiForgeryToken()
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.theme-preview-button {
|
||||||
|
border: 1px solid #505050;
|
||||||
|
border-radius: 3px;
|
||||||
|
background-color: #000;
|
||||||
|
padding: 2px;
|
||||||
|
line-height: 0;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-preview-button:hover {
|
||||||
|
border-color: var(--text-foreground);
|
||||||
|
box-shadow: 0 0 4px var(--shadow-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-preview-thumb {
|
||||||
|
width: 64px;
|
||||||
|
height: 40px;
|
||||||
|
object-fit: cover;
|
||||||
|
}
|
||||||
|
|
||||||
|
#themes-table td {
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
<div style="display: grid; grid-template-rows: auto 1fr;">
|
<div style="display: grid; grid-template-rows: auto 1fr;">
|
||||||
<div style="min-height: 1rem;"></div>
|
<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="enlightenment-panel shadow-effect mx-auto panel w-75 mb-10" style="min-width: 320px;">
|
||||||
@@ -30,7 +56,7 @@
|
|||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<h1 class="text-light-two w-100 text-center pt-0 pb-0 engraved-title">@_localizer["Themes"]</h1>
|
<h1 class="text-light-two w-100 text-center pt-0 pb-0 engraved-title">@_localizer["Themes"]</h1>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row" style="margin-bottom: 0.75rem;">
|
||||||
<button id="btn-upload-theme" type="button" class="confirm-button">@_localizer["Upload"]</button>
|
<button id="btn-upload-theme" type="button" class="confirm-button">@_localizer["Upload"]</button>
|
||||||
<input id="theme-upload-input" type="file" accept=".zip" hidden />
|
<input id="theme-upload-input" type="file" accept=".zip" hidden />
|
||||||
</div>
|
</div>
|
||||||
@@ -44,10 +70,11 @@
|
|||||||
<table id="themes-table">
|
<table id="themes-table">
|
||||||
<thead class="text-light-two">
|
<thead class="text-light-two">
|
||||||
<tr>
|
<tr>
|
||||||
<th class="w-30">@_localizer["Name"]</th>
|
<th class="w-10">@_localizer["Preview"]</th>
|
||||||
|
<th class="w-25">@_localizer["Name"]</th>
|
||||||
<th class="w-15">@_localizer["Version"]</th>
|
<th class="w-15">@_localizer["Version"]</th>
|
||||||
<th class="w-25">@_localizer["Author"]</th>
|
<th class="w-25">@_localizer["Author"]</th>
|
||||||
<th class="w-30">@_localizer["Actions"]</th>
|
<th class="w-25">@_localizer["Actions"]</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -56,13 +83,20 @@
|
|||||||
bool isCurrent = theme.Id == currentThemeId;
|
bool isCurrent = theme.Id == currentThemeId;
|
||||||
bool isLastRemaining = themes.Count <= 1 || (theme.IsBundled && bundledThemeCount == 1);
|
bool isLastRemaining = themes.Count <= 1 || (theme.IsBundled && bundledThemeCount == 1);
|
||||||
<tr data-theme-id="@theme.Id">
|
<tr data-theme-id="@theme.Id">
|
||||||
|
<td class="text-center">
|
||||||
|
<button type="button" class="theme-preview-button" data-preview-url="@theme.PreviewUrl" data-preview-title="@theme.Name" title="@_localizer["Preview"]">
|
||||||
|
<img class="theme-preview-thumb" src="@theme.PreviewUrl" alt="@theme.Name" loading="lazy" />
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
<td>@theme.Name</td>
|
<td>@theme.Name</td>
|
||||||
<td>@theme.Version</td>
|
<td>@theme.Version</td>
|
||||||
<td>@theme.Author</td>
|
<td>@theme.Author</td>
|
||||||
<td class="form-row">
|
<td class="text-end">
|
||||||
<button type="button" class="confirm-button activate-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.SET_CURRENT_THEME)" @(isCurrent ? "disabled" : null)>@_localizer["Activate"]</button>
|
<div class="form-row">
|
||||||
<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="confirm-button activate-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.SET_CURRENT_THEME)" @(isCurrent ? "disabled" : null)>@_localizer["Activate"]</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>
|
<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>
|
||||||
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
}
|
}
|
||||||
@@ -115,6 +149,25 @@
|
|||||||
window.location.href = button.dataset.url;
|
window.location.href = button.dataset.url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// opens the image viewer with the preview of the clicked theme
|
||||||
|
function handlePreviewClick(event) {
|
||||||
|
const button = event.target.closest('.theme-preview-button');
|
||||||
|
if (button)
|
||||||
|
imageViewer.open([{ src: button.dataset.previewUrl, title: button.dataset.previewTitle }]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// falls back to the placeholder image when the preview of a theme is missing or fails to load
|
||||||
|
function attachPreviewErrorFallbacks() {
|
||||||
|
const placeholderUrl = '@Url.Content("~/admin/theme-placeholder.svg")';
|
||||||
|
document.querySelectorAll('.theme-preview-thumb').forEach(img => {
|
||||||
|
img.addEventListener('error', function handlePreviewError() {
|
||||||
|
img.removeEventListener('error', handlePreviewError);
|
||||||
|
img.src = placeholderUrl;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
attachPreviewErrorFallbacks();
|
||||||
|
|
||||||
// uploads the selected theme archive as a multipart request, keeping the antiforgery token
|
// uploads the selected theme archive as a multipart request, keeping the antiforgery token
|
||||||
async function handleUploadInputChange() {
|
async function handleUploadInputChange() {
|
||||||
const file = uploadInput.files[0];
|
const file = uploadInput.files[0];
|
||||||
@@ -145,6 +198,7 @@
|
|||||||
document.addEventListener('click', handleActivateClick);
|
document.addEventListener('click', handleActivateClick);
|
||||||
document.addEventListener('click', handleDownloadClick);
|
document.addEventListener('click', handleDownloadClick);
|
||||||
document.addEventListener('click', handleDeleteClick);
|
document.addEventListener('click', handleDeleteClick);
|
||||||
|
document.addEventListener('click', handlePreviewClick);
|
||||||
|
|
||||||
// register cleanup for these listeners
|
// register cleanup for these listeners
|
||||||
cleanupFunctions.push(() => {
|
cleanupFunctions.push(() => {
|
||||||
@@ -153,6 +207,7 @@
|
|||||||
document.removeEventListener('click', handleActivateClick);
|
document.removeEventListener('click', handleActivateClick);
|
||||||
document.removeEventListener('click', handleDownloadClick);
|
document.removeEventListener('click', handleDownloadClick);
|
||||||
document.removeEventListener('click', handleDeleteClick);
|
document.removeEventListener('click', handleDeleteClick);
|
||||||
|
document.removeEventListener('click', handlePreviewClick);
|
||||||
});
|
});
|
||||||
|
|
||||||
// store all cleanups for this view on window object using the scriptId
|
// store all cleanups for this view on window object using the scriptId
|
||||||
|
|||||||
@@ -7,5 +7,6 @@
|
|||||||
<link rel="stylesheet" href="~/css/navigation-menu.css" asp-append-version="true" />
|
<link rel="stylesheet" href="~/css/navigation-menu.css" asp-append-version="true" />
|
||||||
<link rel="stylesheet" href="~/css/notification.css" asp-append-version="true" />
|
<link rel="stylesheet" href="~/css/notification.css" asp-append-version="true" />
|
||||||
<link rel="stylesheet" href="~/css/dialog.css" asp-append-version="true" />
|
<link rel="stylesheet" href="~/css/dialog.css" asp-append-version="true" />
|
||||||
|
<link rel="stylesheet" href="~/css/image-viewer.css" asp-append-version="true" />
|
||||||
<link rel="stylesheet" href="~/css/textbox.css" asp-append-version="true" />
|
<link rel="stylesheet" href="~/css/textbox.css" asp-append-version="true" />
|
||||||
<link rel="stylesheet" href="~/Lumina.Presentation.Web.styles.css" asp-append-version="true" />
|
<link rel="stylesheet" href="~/Lumina.Presentation.Web.styles.css" asp-append-version="true" />
|
||||||
@@ -34,4 +34,5 @@
|
|||||||
<script src="~/libs/microsoft-signalr/signalr.min.js" asp-append-version="true"></script>
|
<script src="~/libs/microsoft-signalr/signalr.min.js" asp-append-version="true"></script>
|
||||||
<script defer src="~/js/notifications.js" asp-append-version="true"></script>
|
<script defer src="~/js/notifications.js" asp-append-version="true"></script>
|
||||||
<script defer src="~/js/dialogs.js" asp-append-version="true"></script>
|
<script defer src="~/js/dialogs.js" asp-append-version="true"></script>
|
||||||
|
<script defer src="~/js/image-viewer.js" asp-append-version="true"></script>
|
||||||
<script defer src="~/js/site.js" asp-append-version="true"></script>
|
<script defer src="~/js/site.js" asp-append-version="true"></script>
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="120" viewBox="0 0 200 120">
|
||||||
|
<rect width="200" height="120" fill="#232323"/>
|
||||||
|
<rect x="0" y="0" width="200" height="3" fill="#404040"/>
|
||||||
|
<rect x="70" y="30" width="60" height="46" rx="4" fill="none" stroke="#404040" stroke-width="4"/>
|
||||||
|
<circle cx="90" cy="44" r="5" fill="#404040"/>
|
||||||
|
<polygon points="70,76 100,50 116,66 130,54 140,76" fill="#404040"/>
|
||||||
|
<text x="100" y="104" font-family="monospace" font-size="10" fill="#6DCBFE" text-anchor="middle">No preview</text>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 542 B |
@@ -0,0 +1,146 @@
|
|||||||
|
.image-viewer-overlay {
|
||||||
|
display: none;
|
||||||
|
position: fixed;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
z-index: 1001;
|
||||||
|
background-color: rgba(0, 0, 0, 0.85);
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-overlay.open {
|
||||||
|
display: flex;
|
||||||
|
animation: fadeIn 0.3s ease-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-stage {
|
||||||
|
position: relative;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
max-width: 92%;
|
||||||
|
max-height: 92%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-image {
|
||||||
|
max-width: 100%;
|
||||||
|
max-height: 88vh;
|
||||||
|
object-fit: contain;
|
||||||
|
opacity: 0;
|
||||||
|
border: 1px solid #505050;
|
||||||
|
background-color: #000;
|
||||||
|
box-shadow: 0 0 20px rgba(0, 0, 0, 0.6);
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-image.loaded {
|
||||||
|
opacity: 1;
|
||||||
|
transition: opacity 0.2s ease-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-caption {
|
||||||
|
margin-top: 12px;
|
||||||
|
color: var(--text-foreground);
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
text-align: center;
|
||||||
|
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-spinner {
|
||||||
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
margin: -20px 0 0 -20px;
|
||||||
|
border: 4px solid rgba(255, 255, 255, 0.25);
|
||||||
|
border-top-color: #fff;
|
||||||
|
border-radius: 50%;
|
||||||
|
animation: image-viewer-spin 0.8s linear infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes image-viewer-spin {
|
||||||
|
to {
|
||||||
|
transform: rotate(360deg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-button {
|
||||||
|
position: absolute;
|
||||||
|
width: 44px;
|
||||||
|
height: 44px;
|
||||||
|
border: 1px solid #505050;
|
||||||
|
border-radius: 50%;
|
||||||
|
background-color: rgba(0, 0, 0, 0.5);
|
||||||
|
cursor: pointer;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-button:hover {
|
||||||
|
border-color: var(--text-foreground);
|
||||||
|
background-color: rgba(0, 0, 0, 0.75);
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-button:disabled {
|
||||||
|
opacity: 0.35;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-close {
|
||||||
|
top: 18px;
|
||||||
|
right: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-close::before,
|
||||||
|
.image-viewer-close::after {
|
||||||
|
content: '';
|
||||||
|
position: absolute;
|
||||||
|
width: 20px;
|
||||||
|
height: 2px;
|
||||||
|
background-color: var(--text-foreground);
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-close::before {
|
||||||
|
transform: rotate(45deg);
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-close::after {
|
||||||
|
transform: rotate(-45deg);
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-prev {
|
||||||
|
left: 18px;
|
||||||
|
top: 50%;
|
||||||
|
transform: translateY(-50%);
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-prev::before {
|
||||||
|
content: '';
|
||||||
|
width: 12px;
|
||||||
|
height: 12px;
|
||||||
|
margin-left: 5px;
|
||||||
|
border-left: 3px solid var(--text-foreground);
|
||||||
|
border-bottom: 3px solid var(--text-foreground);
|
||||||
|
transform: rotate(45deg);
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-next {
|
||||||
|
right: 18px;
|
||||||
|
top: 50%;
|
||||||
|
transform: translateY(-50%);
|
||||||
|
}
|
||||||
|
|
||||||
|
.image-viewer-next::before {
|
||||||
|
content: '';
|
||||||
|
width: 12px;
|
||||||
|
height: 12px;
|
||||||
|
margin-right: 5px;
|
||||||
|
border-right: 3px solid var(--text-foreground);
|
||||||
|
border-top: 3px solid var(--text-foreground);
|
||||||
|
transform: rotate(45deg);
|
||||||
|
}
|
||||||
@@ -0,0 +1,168 @@
|
|||||||
|
/**
|
||||||
|
* Manages the full-screen image viewer overlay, used to display a single image or a small photo gallery.
|
||||||
|
*/
|
||||||
|
class ImageViewerService {
|
||||||
|
constructor() {
|
||||||
|
this.overlay = null;
|
||||||
|
this.imageElement = null;
|
||||||
|
this.captionElement = null;
|
||||||
|
this.spinnerElement = null;
|
||||||
|
this.prevButton = null;
|
||||||
|
this.nextButton = null;
|
||||||
|
this.items = [];
|
||||||
|
this.currentIndex = 0;
|
||||||
|
this.keydownHandler = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens the viewer with the given images.
|
||||||
|
* @param {Array<{src: string, title?: string}>|{src: string, title?: string}} items - The image(s) to display.
|
||||||
|
*/
|
||||||
|
open(items) {
|
||||||
|
this.items = Array.isArray(items) ? items : [items];
|
||||||
|
if (this.items.length === 0)
|
||||||
|
return;
|
||||||
|
this.ensureOverlay();
|
||||||
|
this.currentIndex = 0;
|
||||||
|
this.show();
|
||||||
|
this.showItem(this.currentIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the viewer and releases the currently displayed image.
|
||||||
|
*/
|
||||||
|
close() {
|
||||||
|
if (!this.overlay)
|
||||||
|
return;
|
||||||
|
this.overlay.classList.remove('open');
|
||||||
|
if (this.keydownHandler) {
|
||||||
|
document.removeEventListener('keydown', this.keydownHandler);
|
||||||
|
this.keydownHandler = null;
|
||||||
|
}
|
||||||
|
// stop the browser from finishing the load of the previous image in the background
|
||||||
|
this.imageElement.removeAttribute('src');
|
||||||
|
this.items = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays the previous image of the gallery, when there is one.
|
||||||
|
*/
|
||||||
|
previous() {
|
||||||
|
if (this.currentIndex > 0)
|
||||||
|
this.showItem(this.currentIndex - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays the next image of the gallery, when there is one.
|
||||||
|
*/
|
||||||
|
next() {
|
||||||
|
if (this.currentIndex < this.items.length - 1)
|
||||||
|
this.showItem(this.currentIndex + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds the overlay and appends it to the document body the first time it is used.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ensureOverlay() {
|
||||||
|
if (this.overlay)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.overlay = document.createElement('div');
|
||||||
|
this.overlay.className = 'image-viewer-overlay';
|
||||||
|
this.overlay.innerHTML = `
|
||||||
|
<button type="button" class="image-viewer-button image-viewer-close" aria-label="Close"></button>
|
||||||
|
<button type="button" class="image-viewer-button image-viewer-prev" aria-label="Previous"></button>
|
||||||
|
<div class="image-viewer-stage">
|
||||||
|
<div class="image-viewer-spinner"></div>
|
||||||
|
<img class="image-viewer-image" alt="" />
|
||||||
|
<div class="image-viewer-caption"></div>
|
||||||
|
</div>
|
||||||
|
<button type="button" class="image-viewer-button image-viewer-next" aria-label="Next"></button>
|
||||||
|
`;
|
||||||
|
|
||||||
|
this.imageElement = this.overlay.querySelector('.image-viewer-image');
|
||||||
|
this.captionElement = this.overlay.querySelector('.image-viewer-caption');
|
||||||
|
this.spinnerElement = this.overlay.querySelector('.image-viewer-spinner');
|
||||||
|
this.prevButton = this.overlay.querySelector('.image-viewer-prev');
|
||||||
|
this.nextButton = this.overlay.querySelector('.image-viewer-next');
|
||||||
|
|
||||||
|
// clicking the dimmed background closes the viewer, but not the image or the controls
|
||||||
|
this.overlay.addEventListener('click', (e) => {
|
||||||
|
if (e.target === this.overlay)
|
||||||
|
this.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
// reveal the image only after it finished loading, so the spinner is visible meanwhile
|
||||||
|
const handleImageLoaded = () => {
|
||||||
|
this.spinnerElement.style.display = 'none';
|
||||||
|
this.imageElement.classList.add('loaded');
|
||||||
|
};
|
||||||
|
this.imageElement.addEventListener('load', handleImageLoaded);
|
||||||
|
this.imageElement.addEventListener('error', handleImageLoaded);
|
||||||
|
|
||||||
|
this.overlay.querySelector('.image-viewer-close').addEventListener('click', () => this.close());
|
||||||
|
this.prevButton.addEventListener('click', (e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
this.previous();
|
||||||
|
});
|
||||||
|
this.nextButton.addEventListener('click', (e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
this.next();
|
||||||
|
});
|
||||||
|
|
||||||
|
document.body.appendChild(this.overlay);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shows the overlay and registers the keyboard navigation.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
show() {
|
||||||
|
this.overlay.classList.add('open');
|
||||||
|
this.keydownHandler = (e) => {
|
||||||
|
if (e.key === 'Escape')
|
||||||
|
this.close();
|
||||||
|
else if (e.key === 'ArrowLeft')
|
||||||
|
this.previous();
|
||||||
|
else if (e.key === 'ArrowRight')
|
||||||
|
this.next();
|
||||||
|
};
|
||||||
|
document.addEventListener('keydown', this.keydownHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays the image at the given index and updates the gallery controls.
|
||||||
|
* @private
|
||||||
|
* @param {number} index - The index of the image to display.
|
||||||
|
*/
|
||||||
|
showItem(index) {
|
||||||
|
if (index < 0 || index >= this.items.length)
|
||||||
|
return;
|
||||||
|
this.currentIndex = index;
|
||||||
|
const item = this.items[index];
|
||||||
|
|
||||||
|
this.spinnerElement.style.display = 'block';
|
||||||
|
this.imageElement.classList.remove('loaded');
|
||||||
|
this.imageElement.alt = item.title || '';
|
||||||
|
this.imageElement.src = item.src;
|
||||||
|
|
||||||
|
if (item.title) {
|
||||||
|
this.captionElement.textContent = item.title;
|
||||||
|
this.captionElement.style.display = 'block';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.captionElement.textContent = '';
|
||||||
|
this.captionElement.style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasMultipleItems = this.items.length > 1;
|
||||||
|
this.prevButton.style.display = hasMultipleItems ? 'flex' : 'none';
|
||||||
|
this.nextButton.style.display = hasMultipleItems ? 'flex' : 'none';
|
||||||
|
this.prevButton.disabled = index === 0;
|
||||||
|
this.nextButton.disabled = index === this.items.length - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a global instance of the ImageViewerService
|
||||||
|
const imageViewer = new ImageViewerService();
|
||||||
Reference in new issue
Block a user