Older/WebApplication/NavigationBar.cpp

82 lines
2.7 KiB
C++
Raw Permalink Normal View History

2024-12-18 23:35:26 +08:00
#include "NavigationBar.h"
2024-12-21 23:08:38 +08:00
#include "LoginPage.h"
#include <Wt/WApplication.h>
#include <Wt/WPushButton.h>
2024-12-21 13:35:12 +08:00
#include <format>
2024-12-18 23:35:26 +08:00
2024-12-21 13:35:12 +08:00
constexpr auto Burger = R"(
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
)";
2024-12-18 23:35:26 +08:00
2024-12-21 23:08:38 +08:00
constexpr auto Login = R"(
${register-button class="bulma-button bulma-is-primary"}
${login-button class="bulma-button bulma-is-light"}
)";
2024-12-21 13:35:12 +08:00
constexpr auto Menu = R"(
<div class="bulma-navbar-start">
<a class="bulma-navbar-item">Home</a>
</div>
<div class="bulma-navbar-end">
2024-12-21 23:08:38 +08:00
${login-item}
2024-12-21 13:35:12 +08:00
</div>
)";
constexpr auto Template = R"(
<div class="bulma-navbar-brand">
2024-12-22 13:56:15 +08:00
<a class="bulma-navbar-item" href="/">子非鱼</a>
2024-12-21 13:35:12 +08:00
${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');
}}
2024-12-18 23:35:26 +08:00
)";
NavigationBar::NavigationBar() : Wt::WTemplate(Template) {
2024-12-21 13:35:12 +08:00
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");
2024-12-21 23:08:38 +08:00
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);
2024-12-22 13:56:15 +08:00
registerButton->clicked().connect(this, [this](const Wt::WMouseEvent &e) { registerClicked.emit(); });
2024-12-18 23:35:26 +08:00
}