Have arrowkey nav use HTML next/prev links

This commit is contained in:
Shish 2020-02-04 01:59:08 +00:00
parent 015a597027
commit 13f4de8c14
4 changed files with 20 additions and 114 deletions

View file

@ -84,15 +84,15 @@ class BaseThemelet
$body = $this->build_paginator($page_number, $total_pages, $base, $query, $show_random);
$page->add_block(new Block(null, $body, "main", 90, "paginator"));
$page->add_html_header("<link rel='first' href='".make_link($base.'/1', $query)."'>");
$page->add_html_header("<link rel='first' href='".make_http(make_link($base.'/1', $query))."'>");
if ($page_number < $total_pages) {
$page->add_html_header("<link rel='prefetch' href='".make_link($base.'/'.($page_number+1), $query)."'>");
$page->add_html_header("<link rel='next' href='".make_link($base.'/'.($page_number+1), $query)."'>");
$page->add_html_header("<link rel='prefetch' href='".make_http(make_link($base.'/'.($page_number+1), $query))."'>");
$page->add_html_header("<link rel='next' href='".make_http(make_link($base.'/'.($page_number+1), $query))."'>");
}
if ($page_number > 1) {
$page->add_html_header("<link rel='previous' href='".make_link($base.'/'.($page_number-1), $query)."'>");
$page->add_html_header("<link rel='previous' href='".make_http(make_link($base.'/'.($page_number-1), $query))."'>");
}
$page->add_html_header("<link rel='last' href='".make_link($base.'/'.$total_pages, $query)."'>");
$page->add_html_header("<link rel='last' href='".make_http(make_link($base.'/'.$total_pages, $query))."'>");
}
private function gen_page_link(string $base_url, ?string $query, int $page, string $name): string

View file

@ -1,15 +0,0 @@
<?php declare(strict_types=1);
class ArrowkeyNavigationInfo extends ExtensionInfo
{
public const KEY = "arrowkey_navigation";
public $key = self::KEY;
public $name = "Arrow Key Navigation";
public $url = "http://www.drudexsoftware.com/";
public $authors = ["Drudex Software"=>"support@drudexsoftware.com"];
public $license = self::LICENSE_GPLV2;
public $description = "Allows viewers no navigate between images using the left & right arrow keys.";
public $documentation =
"Simply enable this extension in the extension manager to enable arrow key navigation.";
}

View file

@ -1,93 +0,0 @@
<?php declare(strict_types=1);
class ArrowkeyNavigation extends Extension
{
/**
* Adds functionality for post/view on images.
*/
public function onDisplayingImage(DisplayingImageEvent $event)
{
$prev_url = make_http(make_link("post/prev/".$event->image->id));
$next_url = make_http(make_link("post/next/".$event->image->id));
$this->add_arrowkeys_code($prev_url, $next_url);
}
/**
* Adds functionality for post/list.
*/
public function onPageRequest(PageRequestEvent $event)
{
if ($event->page_matches("post/list")) {
$pageinfo = $this->get_list_pageinfo($event);
$prev_url = make_http(make_link("post/list/".$pageinfo["prev"]));
$next_url = make_http(make_link("post/list/".$pageinfo["next"]));
$this->add_arrowkeys_code($prev_url, $next_url);
}
}
/**
* Adds the javascript to the page with the given urls.
*/
private function add_arrowkeys_code(string $prev_url, string $next_url)
{
global $page;
$page->add_html_header("<script type=\"text/javascript\">
(function($){
$(document).keyup(function(e) {
if($(e.target).is('input', 'textarea')){ return; }
if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey) { return; }
if (e.keyCode == 37) { window.location.href = '{$prev_url}'; }
else if (e.keyCode == 39) { window.location.href = '{$next_url}'; }
});
})(jQuery);
</script>", 60);
}
/**
* Returns info about the current page number.
*/
private function get_list_pageinfo(PageRequestEvent $event): array
{
global $config, $database;
// get the amount of images per page
$images_per_page = $config->get_int(IndexConfig::IMAGES);
if ($event->count_args() > 1) {
// if there are tags, use pages with tags
$prefix = url_escape($event->get_arg(0)) . "/";
$page_number = $event->try_page_num(1);
$total_pages = ceil($database->get_one(
"SELECT count FROM tags WHERE tag=:tag",
["tag"=>$event->get_arg(0)]
) / $images_per_page);
} else {
// if there are no tags, use default
$prefix = "";
$page_number = $event->try_page_num(0);
$total_pages = ceil($database->get_one(
"SELECT COUNT(*) FROM images"
) / $images_per_page);
}
// creates previous & next values
// When previous first page, go to last page
if ($page_number <= 1) {
$prev = $total_pages;
} else {
$prev = $page_number-1;
}
if ($page_number >= $total_pages) {
$next = 1;
} else {
$next = $page_number+1;
}
// Create return array
return [
"prev" => $prefix.$prev,
"next" => $prefix.$next,
];
}
}

View file

@ -32,6 +32,7 @@ $(document).ready(function() {
/** Setup tablesorter **/
$("table.sortable").tablesorter();
/** Setup sidebar toggle **/
try {
var sidebar_hidden = (Cookies.get("ui-sidebar-hidden") || "").split("|");
for(var i in sidebar_hidden) {
@ -53,7 +54,7 @@ $(document).ready(function() {
}
else {
for (var i in sidebar_hidden) {
if (sidebar_hidden[i] === tid) {
if (sidebar_hidden[i] === tid) {
sidebar_hidden.splice(i, 1);
}
}
@ -62,6 +63,7 @@ $(document).ready(function() {
});
});
/** setup unlocker buttons **/
$(".shm-unlocker").each(function(idx, elm) {
var tid = $(elm).data("unlock-sel");
var tob = $(tid);
@ -70,4 +72,16 @@ $(document).ready(function() {
tob.attr("disabled", false);
});
});
/** setup arrow key bindings **/
$(document).keyup(function(e) {
if($(e.target).is('input', 'textarea')){ return; }
if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey) { return; }
if (e.keyCode == 37 && $("[rel='prev']").length) {
window.location.href = $("[rel='pref']").attr("href");
}
else if (e.keyCode == 39 && $("[rel='next']").length) {
window.location.href = $("[rel='next']").attr("href");
}
});
});