From 2b29d0df16f6f2cde5d0c0b0270a47b016b0bc7c Mon Sep 17 00:00:00 2001 From: bruno Date: Mon, 5 Feb 2024 21:21:04 +0100 Subject: [PATCH] ajax test --- assets/script.js | 69 ++++++++++++++++++++++++++++++++++++++- endpoints/global/page.php | 13 ++++++++ index.php | 2 +- lib/dynamic_style.php | 14 ++++++++ lib/navigation.php | 7 +++- lib/page.php | 38 ++++++++++++++++----- lib/script_data.php | 9 +++++ templates/skeleton.html | 9 +++-- 8 files changed, 147 insertions(+), 14 deletions(-) create mode 100644 endpoints/global/page.php create mode 100644 lib/dynamic_style.php create mode 100644 lib/script_data.php diff --git a/assets/script.js b/assets/script.js index 92a2871..823fd8d 100644 --- a/assets/script.js +++ b/assets/script.js @@ -20,6 +20,39 @@ function doAccountAction(requestData, successMessage, failureMessage, silent=fal }); } + + +function handlePageResponse(data){ + if(data.Navigation){ + document.getElementById("navbar").innerHTML = data.Navigation; + } + if(data.Page){ + document.getElementById("pagearea").innerHTML = data.Page; + if(data.PageLocation){ + history.pushState({}, "", data.PageLocation); + } + } +} + +function doPageAction(requestData){ + return fetch('/page', { + method: 'POST', + body: requestData, + }) + .then(response => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + return response.json(); + }) + .then(data => { + handlePageResponse(data); + }) + .catch((error) => { + console.error('Error:', error); + }); +} + function displayList(data, element_id, delete_function=null) { const tableContainer = document.getElementById(element_id); tableContainer.innerHTML = ""; // Clear previous content @@ -86,13 +119,47 @@ function handleResponse(data, SuccessMessage, failureMessage) { }, 3000); } +function navigateTo(site, page){ + const data = new URLSearchParams(); + data.append("action", "getPage"); + data.append("site", site); + data.append("page", page); + doPageAction(data); +} + +function refreshNavbar(){ + const data = new URLSearchParams(); + data.append("action", "getNavigation"); + doPageAction(data); +} + function logout() { const data = new URLSearchParams(); data.append("action", "logout"); doAccountAction(data, "Logout Successful!", "Logout failed.").then(() => { - location.reload(); + refreshNavbar(); + navigateTo("", pageData.defaultPage); // Expected output: "Success!" }); } +let links = document.querySelectorAll('.navsite_link, .navpage_link'); + +// Add click event listener to each link +links.forEach(function(link) { + link.addEventListener('click', function(e) { + e.preventDefault(); + + // Get page and site information + let page = this.closest('.navpage_item').dataset.page; + let site = this.closest('.navsite_item').dataset.site; + + if(site && page){ + navigateTo(site, page); + } + + // You can use this information to update the URL or perform other actions + }); +}); + diff --git a/endpoints/global/page.php b/endpoints/global/page.php new file mode 100644 index 0000000..f59be43 --- /dev/null +++ b/endpoints/global/page.php @@ -0,0 +1,13 @@ + getNavigationEndpoint(), + "getPage" => getPageEndpoint($endpoint_data["page"], $endpoint_data["site"]), + default => ["Status" => "Fail", "message" => "Invalid action"], + }; +} \ No newline at end of file diff --git a/index.php b/index.php index da8f996..bed7315 100644 --- a/index.php +++ b/index.php @@ -16,7 +16,7 @@ $canRender = initRouter(); if ($canRender) { /** @noinspection PhpArrayIsAlwaysEmptyInspection */ /** @noinspection PhpArrayIsAlwaysEmptyInspection */ - session_set_cookie_params(0, '/', "." . $routerRequest["domain"] . "." . $routerRequest["tld"], true, true); +; session_set_cookie_params(0, '/', "." . $routerRequest["domain"] . "." . $routerRequest["tld"], true, true); session_start(); if (!isLoggedIn()) { diff --git a/lib/dynamic_style.php b/lib/dynamic_style.php new file mode 100644 index 0000000..5fcd604 --- /dev/null +++ b/lib/dynamic_style.php @@ -0,0 +1,14 @@ +"; + $color = dechex($_SESSION["favorite_color"]); + $dynamic_style .= "--root{ --favorite-color: #$color;"; + $dynamic_style .= ""; + } + return $dynamic_style; + +} \ No newline at end of file diff --git a/lib/navigation.php b/lib/navigation.php index d6f9f20..e2d9274 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -94,4 +94,9 @@ function generateNavigation(): string return $nav_out; } - +function getNavigationEndpoint() :array{ + return [ + "Status" => "Success", + "Navigation" => generateNavigation(), + ]; +} \ No newline at end of file diff --git a/lib/page.php b/lib/page.php index aa363c6..21255d9 100644 --- a/lib/page.php +++ b/lib/page.php @@ -1,5 +1,7 @@ [], 'output' => $input]; } -function getPage($page_name = null): array|false|string +function getPage($page_name = null, $site_name = null): string { global $routerConfig; global $routerRequest; + if(!$site_name) { + $site_name = $routerRequest["subdomain"]; + } + if(!$page_name){ $page_name = $routerRequest["page_name"]; } - $dynamic_page_file = $routerConfig["page_dir"] . $routerRequest["subdomain"] . "/" . $page_name . ".php"; - $page_file = $routerConfig["page_dir"] . $routerRequest["subdomain"] . "/" . $page_name . ".html"; + $dynamic_page_file = $routerConfig["page_dir"] . $site_name . "/" . $page_name . ".php"; + $page_file = $routerConfig["page_dir"] . $site_name . "/" . $page_name . ".html"; $skeleton = file_get_contents($routerConfig["template_dir"] . "skeleton.html"); $nav = file_get_contents($routerConfig["template_dir"] . "nav.html"); @@ -113,13 +119,15 @@ function getPage($page_name = null): array|false|string $page_title = $page_name; } - $dynamic_style = ""; + + $dynamic_style = doDynamicStyling(); + $dynamic_script = generateScriptData([ + "currentPage" => $page_name, + "currentSite" => $site_name, + "currentTitle" => $page_title, + "defaultPage" => $routerConfig["default_page"], + ]); $navpages = generateNavigation(); $nav = str_replace("__NAV_PAGES__", $navpages, $nav); @@ -127,8 +135,20 @@ function getPage($page_name = null): array|false|string $out = $skeleton; $out = str_replace("__TEMPLATE__NAV__", $nav, $out); $out = str_replace("__TEMPLATE__PAGE__", $page, $out); + $out = str_replace("__TEMPLATE__DYNASCRIPT__", $dynamic_script, $out); $out = str_replace("__TEMPLATE__DYNASTYLE__", $dynamic_style, $out); $out = inlineLocalStylesFromHref($out); $out = inlineScriptFromSrc($out); return str_replace("__TEMPLATE_PAGE_TITLE__", $page_title, $out); +} + +function getPageEndpoint($page_name, $site_name) :array +{ + global $routerRequest, $routerConfig; + $page_location = $routerConfig["protocol"] . $site_name . $routerRequest["domain"] . "." . $routerRequest["tld"] . "/" . $page_name; + return [ + "Status" => "Success", + "Page" => getPage($page_name, $site_name), + "PageLocation" => $page_location, + ]; } \ No newline at end of file diff --git a/lib/script_data.php b/lib/script_data.php new file mode 100644 index 0000000..41d6cc0 --- /dev/null +++ b/lib/script_data.php @@ -0,0 +1,9 @@ +let pageData = JSON.parse('$jsonString');"; +} \ No newline at end of file diff --git a/templates/skeleton.html b/templates/skeleton.html index 70b637c..fdc262b 100644 --- a/templates/skeleton.html +++ b/templates/skeleton.html @@ -9,9 +9,14 @@ Adlerka __TEMPLATE_PAGE_TITLE__ __TEMPLATE__DYNASTYLE__ + __TEMPLATE__DYNASCRIPT__ - __TEMPLATE__NAV__ - __TEMPLATE__PAGE__ + +
+ __TEMPLATE__PAGE__ +
\ No newline at end of file