Skip to content

Commit

Permalink
Support mouse wheel in browsing
Browse files Browse the repository at this point in the history
  • Loading branch information
electroly committed Nov 2, 2024
1 parent ff98495 commit c8951fc
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 12 deletions.
35 changes: 34 additions & 1 deletion src/J.Server/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace J.Server;

public readonly record struct Page(List<Page.Block> Blocks, string Title)
public readonly record struct Page(List<Page.Block> Blocks, string Title, string PreviousPageUrl, string NextPageUrl)
{
public readonly record struct Block(
MovieId MovieId, // movie to get the clip from
Expand Down Expand Up @@ -131,9 +131,42 @@ function openTag(id) {
location.href = '/tag.html?tagId=' + encodeURIComponent(id) + '&pageIndex=0';
}
// Disable context menu
document.addEventListener('contextmenu', function(event) {
event.preventDefault();
});
// Handle mouse wheel
(function() {
// Handles mouse wheel navigation on a page with no scrolling.
// - Prevents default wheel behavior (scrolling)
// - First downward wheel triggers next page
// - First upward wheel triggers previous page
// - Each direction works exactly once
document.addEventListener('wheel', (e) => { e.preventDefault(); }, { passive: false });
let hasHandledUp = false;
let hasHandledDown = false;
const previousPageUrl = '{{PreviousPageUrl}}';
const nextPageUrl = '{{NextPageUrl}}';
document.addEventListener('wheel', (event) => {
const isDownScroll = Math.sign(event.deltaY) > 0;
if (isDownScroll && !hasHandledDown) {
hasHandledDown = true;
if (nextPageUrl !== '') {
location.href = nextPageUrl;
}
} else if (!isDownScroll && !hasHandledUp) {
hasHandledUp = true;
if (previousPageUrl !== '') {
location.href = previousPageUrl;
}
}
});
})();
</script>
</head>
<body>
Expand Down
60 changes: 49 additions & 11 deletions src/J.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,28 @@ void RefreshLibrary()
{
Dictionary<ListPageKey, Lazy<List<Page>>> dict = [];
dict[new ListPageKey(ListPageType.Movies, null)] = new(
() => SplitMoviesIntoPages(movies, "Movies", optionShuffle)
() =>
SplitMoviesIntoPages(
movies,
"Movies",
optionShuffle,
n => $"/list.html?type={ListPageType.Movies}&pageIndex={n - 1}"
)
);

foreach (var tagType in libraryProvider.GetTagTypes())
{
ListPageKey key = new(ListPageType.TagType, tagType.Id);
dict[key] = new(() => GetTagListPage(libraryProvider, tagType, optionShuffle));
dict[key] = new(
() =>
GetTagListPage(
libraryProvider,
tagType,
optionShuffle,
n =>
$"/list.html?type={ListPageType.TagType}&tagTypeId={tagType.Id.Value}&pageIndex={n - 1}"
)
);
}

listPages = dict;
Expand All @@ -66,7 +81,8 @@ void RefreshLibrary()
SplitMoviesIntoPages(
libraryProvider.GetMoviesWithTag(movieIds, tag.Id),
tag.Name,
optionShuffle
optionShuffle,
n => $"/tag.html?tagId={tag.Id.Value}&pageIndex={n - 1}"
)
);
tagPages = dict;
Expand Down Expand Up @@ -141,7 +157,12 @@ void Add(bool x)
}
}

static List<Page> GetTagListPage(LibraryProvider libraryProvider, TagType tagType, bool shuffle)
static List<Page> GetTagListPage(
LibraryProvider libraryProvider,
TagType tagType,
bool shuffle,
Func<int, string> getUrlForPageNumber
)
{
var tags = libraryProvider.GetTags(tagType.Id);
var dict = libraryProvider.GetRandomMoviePerTag(tagType);
Expand All @@ -154,19 +175,30 @@ static List<Page> GetTagListPage(LibraryProvider libraryProvider, TagType tagTyp
Page.Block block = new(movieId, tag.Id, tag.Name);
blocks.Add(block);
}
return SplitBlocksIntoPages(blocks, tagType.PluralName, shuffle);
return SplitBlocksIntoPages(blocks, tagType.PluralName, shuffle, getUrlForPageNumber);
}

static List<Page> SplitMoviesIntoPages(List<Movie> movies, string title, bool shuffle)
static List<Page> SplitMoviesIntoPages(
List<Movie> movies,
string title,
bool shuffle,
Func<int, string> getUrlForPageNumber
)
{
return SplitBlocksIntoPages(
(from x in movies select new Page.Block(x.Id, null, x.Filename)).ToList(),
title,
shuffle
shuffle,
getUrlForPageNumber
);
}

static List<Page> SplitBlocksIntoPages(List<Page.Block> blocks, string title, bool shuffle)
static List<Page> SplitBlocksIntoPages(
List<Page.Block> blocks,
string title,
bool shuffle,
Func<int, string> getUrlForPageNumber
)
{
if (shuffle)
Shuffle(blocks);
Expand All @@ -177,11 +209,17 @@ static List<Page> SplitBlocksIntoPages(List<Page.Block> blocks, string title, bo
var pageNumber = 1;
foreach (var chunk in blocks.Chunk(BLOCKS_PER_PAGE))
{
Page page = new([.. chunk], "");
var previousPageUrl = pageNumber > 1 ? getUrlForPageNumber(pageNumber - 1) : "";
var nextPageUrl = getUrlForPageNumber(pageNumber + 1);
Page page = new([.. chunk], "", previousPageUrl, nextPageUrl);
pages.Add(page);
pageNumber++;
}

// Blank out the NextPageUrl for the last page.
if (pages.Count > 0)
pages[^1] = pages[^1] with { NextPageUrl = "" };

for (var i = 0; i < pages.Count; i++)
{
pages[i] = pages[i] with { Title = $"{title} ({i + 1}/{pages.Count})" };
Expand Down Expand Up @@ -275,7 +313,7 @@ HttpResponse response
ListPageKey key = new(listPageType, tagTypeId is null ? null : new(tagTypeId));
var lazy = listPages[key];
if (pageIndex < 0 || pageIndex >= lazy.Value.Count)
page = new([], "Blank");
page = new([], "Blank", "", "");
else
page = lazy.Value[pageIndex];

Expand All @@ -292,7 +330,7 @@ HttpResponse response

var lazy = tagPages[new(tagId)];
if (pageIndex < 0 || pageIndex >= lazy.Value.Count)
page = new([], "");
page = new([], "", "", "");
else
page = lazy.Value[pageIndex];

Expand Down

0 comments on commit c8951fc

Please sign in to comment.