#include "NavigationBar.h" #include "LoginPage.h" #include #include #include constexpr auto Burger = R"( )"; constexpr auto Login = R"( ${register-button class="bulma-button bulma-is-primary"} ${login-button class="bulma-button bulma-is-light"} )"; constexpr auto Menu = R"(
${login-item}
)"; constexpr auto Template = R"(
子非鱼 ${navbar-burger role="button" class="bulma-navbar-burger" aria-label="menu" aria-expanded="false"}
${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(Burger)); navbarBurger->setHtmlTagName("a"); m_menu = bindWidget("navbar-menu", std::make_unique(Menu)); createLoginButtons(); navbarBurger->clicked().connect(std::format(JS, navbarBurger->id(), m_menu->id())); } NavigationBar::~NavigationBar() { } LoginPage *NavigationBar::addLoginItem(std::unique_ptr item) { auto ret = m_menu->bindWidget("login-item", std::move(item)); ret->addStyleClass("bulma-navbar-item"); return ret; } std::unique_ptr NavigationBar::removeLoginItem() { auto ret = m_menu->removeWidget("login-item"); createLoginButtons(); return std::unique_ptr(dynamic_cast(ret.release())); } void NavigationBar::createLoginButtons() { auto loginItem = m_menu->bindWidget("login-item", std::make_unique(Login)); loginItem->addStyleClass("bulma-buttons"); auto loginButton = loginItem->bindWidget("login-button", std::make_unique("登录")); loginButton->clicked().connect([]() { Wt::WApplication::instance()->setInternalPath("/wt/login", true); }); auto registerButton = loginItem->bindWidget("register-button", std::make_unique("注册")); registerButton->setTextFormat(Wt::TextFormat::XHTML); registerButton->clicked().connect(this, [this](const Wt::WMouseEvent &e) { registerClicked.emit(); }); }