Merge pull request #33 from BinaryAlley/feature/implement-persistent-player-ajax-navigation

Implements persistent audio player with ajax handled navigation
This commit is contained in:
BinaryAlley authored and GitHub committed 2024-10-29 13:12:47 +02:00
commit cc9ee52040
8 files changed
+200 -6

No files matched your search

@@ -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");
}
}
@@ -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,
@@ -0,0 +1 @@
<p>All books</p><a href="books/E6B599D6-3819-4FAB-B988-8F027072CF7B" class="nav-link">one</a>
@@ -25,6 +25,16 @@
@RenderBody()
</article>
</main>
<!--Audio player-->
<div class="audio-player-container">
<video controls
preload="auto"
height="50" width="600">
<source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4">
</video>
<a id="audio-player-full-height-toggle">Toggle Full Height</a>
<a id="audio-player-close">Toggle Visibility</a>
</div>
</div>
<!--Modal grid background-->
<div id="modal-background"></div>
@@ -31,7 +31,7 @@
Written Content
</div>
<ul>
<li><a href="library/written-content/books"><span>Books</span></a></li>
<li><a href="library/written-content-library/books-library/books" class="nav-link"><span>Books</span></a></li>
<li><a href="library/written-content/magazines"><span>Magazines</span></a></li>
</ul>
<div class="mobile-menu-heading">
@@ -86,7 +86,7 @@
<li class="has-submenu" tabindex="0">
<label>Written Content</label>
<ul>
<li><a href="library/written-content/books">Books</a></li>
<li><a href="library/written-content-library/books-library/books" class="nav-link">Books</a></li>
<li><a href="library/written-content/magazines">Magazines</a></li>
</ul>
</li>
@@ -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;
}
+127 -2
View File
@@ -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);