Update page title handling, reduce reliance on JS

This commit is contained in:
Sky Johnson 2024-12-18 11:05:11 -06:00
parent faefbb37d4
commit 8084a84576
5 changed files with 21 additions and 30 deletions

View File

@ -40,7 +40,7 @@ main {
}
main > section {
padding: 4px;
padding: 0.75rem;
}
main > section > section {
@ -49,6 +49,7 @@ main > section > section {
main section#left {
width: 180px;
flex-shrink: 0;
border-right: solid 2px black;
}
@ -58,6 +59,7 @@ main section#middle {
main section#right {
width: 180px;
flex-shrink: 0;
border-left: solid 2px black;
}
@ -82,20 +84,6 @@ td {
vertical-align: top;
}
td.top {
border-bottom: solid 2px black;
}
td.left {
width: 180px;
border-right: solid 2px black;
}
td.right {
width: 180px;
border-left: solid 2px black;
}
a {
color: #663300;
text-decoration: none;
@ -126,6 +114,7 @@ a:hover {
padding: 5px;
font-size: 1.2rem;
font-family: serif;
margin-bottom: 0.5rem;
}
.copyright {

View File

@ -15,15 +15,13 @@ $r = new Router;
$r->get('/', function() {
if (user()->currentaction === "In Town") {
$page = Towns\town();
$title = "In Town";
} elseif (user()->currentaction === "Exploring") {
$page = doexplore();
$title = "Exploring";
$page = explore();
} elseif (user()->currentaction === "Fighting") {
redirect('/fight');
}
return is_htmx() ? $page : display($page, $title);
return is_htmx() ? $page : display($page, '');
});
$r->get('/ninja', function() {
@ -52,7 +50,8 @@ $l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
if (is_int($l)) exit("Error: $l");
$content = $l['handler'](...$l['params'] ?? []);
if (is_htmx() && $uri[0] !== 'babblebox') {
if (is_htmx()) {
$content .= '<title>'.page_title().'</title>';
$content .= Render\debug_db_info();
if ($GLOBALS['state']['user-state-changed'] ?? false) {
$content .= Render\right_nav();
@ -65,8 +64,9 @@ exit;
* Just spit out a blank exploring page. Exploring without a GET string is normally when they first log in, or when
* they've just finished fighting.
*/
function doexplore()
function explore()
{
page_title('Exploring');
return <<<HTML
<div class="title"><img src="/img/title_exploring.gif" alt="Exploring"></div>
You are exploring the map, and nothing has happened. Continue exploring using the direction buttons or the Travel To menus.

View File

@ -73,7 +73,7 @@ function town()
HTML;
}
if (is_htmx()) htmx_update_page_title($townrow['name']);
page_title($townrow['name']);
return render('towns', ['town' => $townrow]);
}
@ -86,7 +86,7 @@ function inn()
if ($town === false) { exit('Cheat attempt detected.<br><br>Get a life, loser.'); }
$htmx = is_htmx();
if ($htmx) htmx_update_page_title($town['name'] . ' Inn');
page_title($town['name'] . ' Inn');
if (user()->gold < $town['innprice']) {
$page = <<<HTML
@ -124,7 +124,7 @@ function buy()
if ($town === false) { exit('Cheat attempt detected.<br><br>Get a life, loser.'); }
$htmx = is_htmx();
if ($htmx) htmx_update_page_title($town['name'] . ' Shop');
page_title($town['name'] . ' Shop');
$page = <<<HTML
Buying weapons will increase your Attack Power. Buying armor and shields will increase your Defense Power.<br><br>

View File

@ -142,7 +142,6 @@ function display($content, $title, bool $topnav = true, bool $leftnav = true, bo
return render('layouts/primary', [
"dkgamename" => $controlrow["gamename"],
"title" => $title,
"content" => $content,
"game_skin" => $game_skin,
"leftnav" => $leftnav ? render('leftnav', ['town_list' => $town_list_html, 'current_town' => $current_town]) : '',
@ -598,8 +597,11 @@ function is_htmx(): bool
}
/**
* Update the page title using HTMX.
* Get the current page title per updates. Optionally set a new title.
*/
function htmx_update_page_title(string $new_title) {
header('HX-Trigger: ' . json_encode(['updateTitle' => ['title' => $new_title]]));
function page_title(string $new_title = ''): string
{
global $controlrow;
if ($new_title) return $GLOBALS['state']['new-page-title'] = $new_title;
return $GLOBALS['state']['new-page-title'] ?? $controlrow['gamename'];
}

View File

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $title ?></title>
<title><?= page_title() ?></title>
<link rel="stylesheet" href="/css/dk.css">
<script src="/js/htmx.js"></script>
@ -23,7 +23,7 @@
<body class="skin-<?= $game_skin ?>">
<div id="game-container">
<header>
<img id="logo" src="/img/logo.gif" alt="<?= $dkgamename ?>" title="<?= $dkgamename ?>">
<a href="/"><img id="logo" src="/img/logo.gif" alt="<?= $dkgamename ?>" title="<?= $dkgamename ?>"></a>
<nav><?= $topnav ?></nav>
</header>