Older/WebApplication/NavigationBar.cpp
amass 6ab3e2b22b
All checks were successful
Deploy / Build (push) Successful in 7m25s
improve wt code.
2024-12-21 23:08:38 +08:00

82 lines
2.7 KiB
C++

#include "NavigationBar.h"
#include "LoginPage.h"
#include <Wt/WApplication.h>
#include <Wt/WPushButton.h>
#include <format>
constexpr auto Burger = R"(
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
)";
constexpr auto Login = R"(
${register-button class="bulma-button bulma-is-primary"}
${login-button class="bulma-button bulma-is-light"}
)";
constexpr auto Menu = R"(
<div class="bulma-navbar-start">
<a class="bulma-navbar-item">Home</a>
</div>
<div class="bulma-navbar-end">
${login-item}
</div>
)";
constexpr auto Template = R"(
<div class="bulma-navbar-brand">
<a class="bulma-navbar-item" href="https://amass.fun">子非鱼</a>
${navbar-burger role="button" class="bulma-navbar-burger" aria-label="menu" aria-expanded="false"}
</div>
${navbar-menu class="bulma-navbar-menu"}
)";
constexpr auto JS = R"(
function(o) {{
{}.classList.toggle('bulma-is-active');
{}.classList.toggle('bulma-is-active');
}}
)";
NavigationBar::NavigationBar() : Wt::WTemplate(Template) {
setHtmlTagName("nav");
setStyleClass("bulma-navbar");
setAttributeValue("role", "navigation");
setAttributeValue("aria-label", "main navigation");
auto navbarBurger = bindWidget("navbar-burger", std::make_unique<Wt::WTemplate>(Burger));
navbarBurger->setHtmlTagName("a");
m_menu = bindWidget("navbar-menu", std::make_unique<Wt::WTemplate>(Menu));
createLoginButtons();
navbarBurger->clicked().connect(std::format(JS, navbarBurger->id(), m_menu->id()));
}
NavigationBar::~NavigationBar() {
}
LoginPage *NavigationBar::addLoginItem(std::unique_ptr<LoginPage> item) {
auto ret = m_menu->bindWidget("login-item", std::move(item));
ret->addStyleClass("bulma-navbar-item");
return ret;
}
std::unique_ptr<LoginPage> NavigationBar::removeLoginItem() {
auto ret = m_menu->removeWidget("login-item");
createLoginButtons();
return std::unique_ptr<LoginPage>(dynamic_cast<LoginPage *>(ret.release()));
}
void NavigationBar::createLoginButtons() {
auto loginItem = m_menu->bindWidget("login-item", std::make_unique<Wt::WTemplate>(Login));
loginItem->addStyleClass("bulma-buttons");
auto loginButton = loginItem->bindWidget("login-button", std::make_unique<Wt::WPushButton>("登录"));
loginButton->clicked().connect([]() { Wt::WApplication::instance()->setInternalPath("/wt/login", true); });
auto registerButton = loginItem->bindWidget("register-button", std::make_unique<Wt::WPushButton>("<strong>注册</strong>"));
registerButton->setTextFormat(Wt::TextFormat::XHTML);
registerButton->clicked().connect([]() { Wt::WApplication::instance()->setInternalPath("/wt/register", true); });
}