From 8084a8457646be60d5f86e52e089d0a2a5da1cda Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Wed, 18 Dec 2024 11:05:11 -0600 Subject: [PATCH] Update page title handling, reduce reliance on JS --- public/css/dk.css | 19 ++++--------------- public/index.php | 12 ++++++------ src/actions/towns.php | 6 +++--- src/lib.php | 10 ++++++---- templates/layouts/primary.php | 4 ++-- 5 files changed, 21 insertions(+), 30 deletions(-) diff --git a/public/css/dk.css b/public/css/dk.css index b9a434c..3d101fb 100644 --- a/public/css/dk.css +++ b/public/css/dk.css @@ -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 { diff --git a/public/index.php b/public/index.php index 6fe5905..03fae53 100644 --- a/public/index.php +++ b/public/index.php @@ -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 .= ''.page_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 <<Exploring You are exploring the map, and nothing has happened. Continue exploring using the direction buttons or the Travel To menus. diff --git a/src/actions/towns.php b/src/actions/towns.php index 39e79a4..4b1f6b8 100644 --- a/src/actions/towns.php +++ b/src/actions/towns.php @@ -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.

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 = <<
Get a life, loser.'); } $htmx = is_htmx(); - if ($htmx) htmx_update_page_title($town['name'] . ' Shop'); + page_title($town['name'] . ' Shop'); $page = <<
diff --git a/src/lib.php b/src/lib.php index 5468dde..19031b3 100644 --- a/src/lib.php +++ b/src/lib.php @@ -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']; } diff --git a/templates/layouts/primary.php b/templates/layouts/primary.php index 6f82d9e..9778832 100644 --- a/templates/layouts/primary.php +++ b/templates/layouts/primary.php @@ -3,7 +3,7 @@ - <?= $title ?> + <?= page_title() ?> @@ -23,7 +23,7 @@
- +