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

View File

@ -15,15 +15,13 @@ $r = new Router;
$r->get('/', function() { $r->get('/', function() {
if (user()->currentaction === "In Town") { if (user()->currentaction === "In Town") {
$page = Towns\town(); $page = Towns\town();
$title = "In Town";
} elseif (user()->currentaction === "Exploring") { } elseif (user()->currentaction === "Exploring") {
$page = doexplore(); $page = explore();
$title = "Exploring";
} elseif (user()->currentaction === "Fighting") { } elseif (user()->currentaction === "Fighting") {
redirect('/fight'); redirect('/fight');
} }
return is_htmx() ? $page : display($page, $title); return is_htmx() ? $page : display($page, '');
}); });
$r->get('/ninja', function() { $r->get('/ninja', function() {
@ -52,7 +50,8 @@ $l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
if (is_int($l)) exit("Error: $l"); if (is_int($l)) exit("Error: $l");
$content = $l['handler'](...$l['params'] ?? []); $content = $l['handler'](...$l['params'] ?? []);
if (is_htmx() && $uri[0] !== 'babblebox') { if (is_htmx()) {
$content .= '<title>'.page_title().'</title>';
$content .= Render\debug_db_info(); $content .= Render\debug_db_info();
if ($GLOBALS['state']['user-state-changed'] ?? false) { if ($GLOBALS['state']['user-state-changed'] ?? false) {
$content .= Render\right_nav(); $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 * 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. * they've just finished fighting.
*/ */
function doexplore() function explore()
{ {
page_title('Exploring');
return <<<HTML return <<<HTML
<div class="title"><img src="/img/title_exploring.gif" alt="Exploring"></div> <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. 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; HTML;
} }
if (is_htmx()) htmx_update_page_title($townrow['name']); page_title($townrow['name']);
return render('towns', ['town' => $townrow]); return render('towns', ['town' => $townrow]);
} }
@ -86,7 +86,7 @@ function inn()
if ($town === false) { exit('Cheat attempt detected.<br><br>Get a life, loser.'); } if ($town === false) { exit('Cheat attempt detected.<br><br>Get a life, loser.'); }
$htmx = is_htmx(); $htmx = is_htmx();
if ($htmx) htmx_update_page_title($town['name'] . ' Inn'); page_title($town['name'] . ' Inn');
if (user()->gold < $town['innprice']) { if (user()->gold < $town['innprice']) {
$page = <<<HTML $page = <<<HTML
@ -124,7 +124,7 @@ function buy()
if ($town === false) { exit('Cheat attempt detected.<br><br>Get a life, loser.'); } if ($town === false) { exit('Cheat attempt detected.<br><br>Get a life, loser.'); }
$htmx = is_htmx(); $htmx = is_htmx();
if ($htmx) htmx_update_page_title($town['name'] . ' Shop'); page_title($town['name'] . ' Shop');
$page = <<<HTML $page = <<<HTML
Buying weapons will increase your Attack Power. Buying armor and shields will increase your Defense Power.<br><br> 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', [ return render('layouts/primary', [
"dkgamename" => $controlrow["gamename"], "dkgamename" => $controlrow["gamename"],
"title" => $title,
"content" => $content, "content" => $content,
"game_skin" => $game_skin, "game_skin" => $game_skin,
"leftnav" => $leftnav ? render('leftnav', ['town_list' => $town_list_html, 'current_town' => $current_town]) : '', "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) { function page_title(string $new_title = ''): string
header('HX-Trigger: ' . json_encode(['updateTitle' => ['title' => $new_title]])); {
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> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <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"> <link rel="stylesheet" href="/css/dk.css">
<script src="/js/htmx.js"></script> <script src="/js/htmx.js"></script>
@ -23,7 +23,7 @@
<body class="skin-<?= $game_skin ?>"> <body class="skin-<?= $game_skin ?>">
<div id="game-container"> <div id="game-container">
<header> <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> <nav><?= $topnav ?></nav>
</header> </header>