From 0b84b2b83b958806dc4f2632e06979dda32285f2 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Tue, 29 Oct 2024 13:10:07 +0200 Subject: [PATCH] Implements persistent audio player with ajax handled navigation --- .../BookLibrary/BooksController.cs | 20 +++ .../FileSystemBrowserViewComponent.cs | 2 +- .../BookLibrary/Books/Index.cshtml | 1 + .../BookLibrary/Books/Item.cshtml | 1 + .../Views/Shared/_Layout.cshtml | 10 ++ .../Views/Shared/_NavMenu.cshtml | 4 +- .../wwwroot/css/site.css | 39 +++++- .../wwwroot/js/site.js | 129 +++++++++++++++++- 8 files changed, 200 insertions(+), 6 deletions(-) create mode 100644 src/Lumina.Presentation.Web/Controllers/Library/WrittenContentLibrary/BookLibrary/BooksController.cs create mode 100644 src/Lumina.Presentation.Web/Views/Library/WrittenContentLibrary/BookLibrary/Books/Index.cshtml create mode 100644 src/Lumina.Presentation.Web/Views/Library/WrittenContentLibrary/BookLibrary/Books/Item.cshtml diff --git a/src/Lumina.Presentation.Web/Controllers/Library/WrittenContentLibrary/BookLibrary/BooksController.cs b/src/Lumina.Presentation.Web/Controllers/Library/WrittenContentLibrary/BookLibrary/BooksController.cs new file mode 100644 index 00000000..0cd52639 --- /dev/null +++ b/src/Lumina.Presentation.Web/Controllers/Library/WrittenContentLibrary/BookLibrary/BooksController.cs @@ -0,0 +1,20 @@ +using Microsoft.AspNetCore.Mvc; +using System; + +namespace Lumina.Presentation.Web.Controllers.Library.WrittenContentLibrary.BookLibrary; + +[Route("/library/written-content-library/books-library/books/")] +public class BooksController : Controller +{ + [HttpGet("")] + public IActionResult Index() + { + return View("/Views/Library/WrittenContentLibrary/BookLibrary/Books/Index.cshtml"); + } + + [HttpGet("{id}")] + public IActionResult EditBook(Guid id) + { + return View("/Views/Library/WrittenContentLibrary/BookLibrary/Books/Item.cshtml"); + } +} diff --git a/src/Lumina.Presentation.Web/ViewComponents/FileSystemBrowserViewComponent.cs b/src/Lumina.Presentation.Web/ViewComponents/FileSystemBrowserViewComponent.cs index 271c6d12..c7b98f62 100644 --- a/src/Lumina.Presentation.Web/ViewComponents/FileSystemBrowserViewComponent.cs +++ b/src/Lumina.Presentation.Web/ViewComponents/FileSystemBrowserViewComponent.cs @@ -35,7 +35,7 @@ public class FileSystemBrowserViewComponent : ViewComponent { return View(new { - Path = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? @"/" : @"C:\", + Path = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? @"/" : @"C:\Users\", ServerBasePath = $"{_serverConfigurationModel.BaseAddress}:{_serverConfigurationModel.Port}/api/v{_serverConfigurationModel.ApiVersion}/", ClientBasePath = "http://localhost:5012/", // TODO: take from appsettings or environment ViewMode = FileSystemViewMode.List, diff --git a/src/Lumina.Presentation.Web/Views/Library/WrittenContentLibrary/BookLibrary/Books/Index.cshtml b/src/Lumina.Presentation.Web/Views/Library/WrittenContentLibrary/BookLibrary/Books/Index.cshtml new file mode 100644 index 00000000..b706dcc1 --- /dev/null +++ b/src/Lumina.Presentation.Web/Views/Library/WrittenContentLibrary/BookLibrary/Books/Index.cshtml @@ -0,0 +1 @@ +

All books

one diff --git a/src/Lumina.Presentation.Web/Views/Library/WrittenContentLibrary/BookLibrary/Books/Item.cshtml b/src/Lumina.Presentation.Web/Views/Library/WrittenContentLibrary/BookLibrary/Books/Item.cshtml new file mode 100644 index 00000000..a25adbc1 --- /dev/null +++ b/src/Lumina.Presentation.Web/Views/Library/WrittenContentLibrary/BookLibrary/Books/Item.cshtml @@ -0,0 +1 @@ +

A book

diff --git a/src/Lumina.Presentation.Web/Views/Shared/_Layout.cshtml b/src/Lumina.Presentation.Web/Views/Shared/_Layout.cshtml index d954e569..ead54e48 100644 --- a/src/Lumina.Presentation.Web/Views/Shared/_Layout.cshtml +++ b/src/Lumina.Presentation.Web/Views/Shared/_Layout.cshtml @@ -25,6 +25,16 @@ @RenderBody() + +
+ + Toggle Full Height + Toggle Visibility +
diff --git a/src/Lumina.Presentation.Web/Views/Shared/_NavMenu.cshtml b/src/Lumina.Presentation.Web/Views/Shared/_NavMenu.cshtml index 362d39a6..4cca95af 100644 --- a/src/Lumina.Presentation.Web/Views/Shared/_NavMenu.cshtml +++ b/src/Lumina.Presentation.Web/Views/Shared/_NavMenu.cshtml @@ -31,7 +31,7 @@ Written Content
@@ -86,7 +86,7 @@
  • diff --git a/src/Lumina.Presentation.Web/wwwroot/css/site.css b/src/Lumina.Presentation.Web/wwwroot/css/site.css index 12899d10..eb5a490f 100644 --- a/src/Lumina.Presentation.Web/wwwroot/css/site.css +++ b/src/Lumina.Presentation.Web/wwwroot/css/site.css @@ -45,12 +45,45 @@ body { background-color: #222; min-width: 100%; min-height: 100%; + height: 100%; + position: relative; + display: flex; + flex-direction: column; } main { - padding-top: 20px; + position: fixed; + top: 20px; + bottom: 50px; + width: 100%; + overflow-y: auto; + transition: bottom 1s ease, height 1s ease; } + main.audio-player-not-shown { + bottom: 0; + } + + main.audio-player-full-height { + bottom: calc(100% - 20px); /* full height minus menu height */ + } + +.audio-player-container { + position: fixed; + bottom: 0; + width: 100%; + height: 50px; + transition: height 1s ease; +} + + .audio-player-container.not-shown { + height: 0px; + } + + .audio-player-container.full-height { + height: calc(100% - 20px); /* full height minus menu height */ + } + #modal-background { display: none; z-index: 998; @@ -101,6 +134,10 @@ main { padding: 10px; } +#audio-player-full-height-toggle, #audio-player-close { + cursor: pointer; +} + .text-light-one { color: #6DCBFE; } diff --git a/src/Lumina.Presentation.Web/wwwroot/js/site.js b/src/Lumina.Presentation.Web/wwwroot/js/site.js index 7ff86336..231c4fb6 100644 --- a/src/Lumina.Presentation.Web/wwwroot/js/site.js +++ b/src/Lumina.Presentation.Web/wwwroot/js/site.js @@ -4,8 +4,8 @@ const progressIndicator = document.getElementById('progress-indicator'); const progressIndicatorValue = document.getElementById('progress-indicator-value'); const progressIndicatorValueShadow = document.getElementById('progress-indicator-value-shadow'); const progressIndicatorValueText = document.getElementById('progress-indicator-value-text'); - - +const audioPlayerFullHeightToggle = document.getElementById('audio-player-full-height-toggle'); +const audioPlayerClose = document.getElementById('audio-player-close'); let enableConsoleDebugMessages = false; // whether to display debug messages at the developer console, or not @@ -38,6 +38,106 @@ async function callApiGetAsync(url) { } } +//+======================================================================================+ +//| Website navigation | +//+======================================================================================+ + +/** + * Initializes navigation by setting the initial history state, intercepting link clicks, + * and managing back/forward browser actions. + */ +function initializeNavigation() { + // store initial state + const initialState = { + url: window.location.href, + content: document.querySelector('main').innerHTML, + title: document.title + }; + history.replaceState(initialState, document.title, window.location.href); + // initial nav-link binding + bindNavigationLinks(); + + // handle browser back/forward + window.addEventListener('popstate', handlePopState); +} + + +/** + * Binds AJAX navigation behavior to links marked with .nav-link class. + */ +function bindNavigationLinks() { + document.querySelectorAll('.nav-link').forEach(link => { + // only bind if not already bound + if (!link.dataset.boundNavigation) { + link.addEventListener('click', handleNavigation); + link.dataset.boundNavigation = 'true'; + } + }); +} + +/** + * Handles link clicks to navigate dynamically without reloading the page. + * @param {Event} e - The click event for a navigation link. + */ +async function handleNavigation(e) { + e.preventDefault(); + const url = e.target.href; + await updateContent(url, true); +} + +/** + * Restores page content and title when navigating with browser back/forward buttons. + * @param {PopStateEvent} e - The popstate event with previous state data. + */ +async function handlePopState(e) { + if (e.state) { + // if we have cached content in state, use it immediately + if (e.state.content) { + document.querySelector('main').innerHTML = e.state.content; + document.title = e.state.title; + } + // still fetch fresh content to ensure it's up to date + await updateContent(e.state.url, false); + } +} + +/** + * Loads new page content and updates the URL and history if required. + * @param {string} url - URL to fetch new content from. + * @param {boolean} addToHistory - Whether to add the new state to history. + */ +async function updateContent(url, addToHistory) { + try { + const response = await fetch(url); + const html = await response.text(); + + // parse the new content + const parser = new DOMParser(); + const doc = parser.parseFromString(html, 'text/html'); + const content = doc.querySelector('main').innerHTML; + const title = doc.querySelector('title')?.textContent || document.title; + + // update the page + document.querySelector('main').innerHTML = content; + document.title = title; + + // bind navigation links in the new content + bindNavigationLinks(); + + // save to history if needed + if (addToHistory) { + const state = { + url: url, + content: content, + title: title + }; + history.pushState(state, title, url); + } + } catch (error) { + console.error('Navigation failed:', error); + } +} + //+======================================================================================+ //| Common Helper Functions | //+======================================================================================+ @@ -237,3 +337,28 @@ function getElementOffset(id) { height: 0 }; } + +/** + * Toggles the visibility of the audio player. + */ +function toggleAudioPlayerVisibility() { + const playerContainer = document.querySelector('.audio-player-container'); + const contentContainer = document.querySelector('main'); + playerContainer.classList.toggle('not-shown'); + contentContainer.classList.toggle('audio-player-not-shown'); +} + +/** + * Toggles the display of the audio player as collapsed player, or full height player. + */ +function toggleAudioPlayerFullHeight() { + const playerContainer = document.querySelector('.audio-player-container'); + const contentContainer = document.querySelector('main'); + playerContainer.classList.toggle('full-height'); + contentContainer.classList.toggle('audio-player-full-height'); +} + +audioPlayerFullHeightToggle.addEventListener('click', toggleAudioPlayerFullHeight); +audioPlayerClose.addEventListener('click', toggleAudioPlayerVisibility); + +document.addEventListener('DOMContentLoaded', initializeNavigation);