diff --git a/WebApplication/Application.cpp b/WebApplication/Application.cpp index dc12bf3..27ed0c7 100644 --- a/WebApplication/Application.cpp +++ b/WebApplication/Application.cpp @@ -4,6 +4,7 @@ #include "Database/Session.h" #include "HomePage.h" #include "LoginPage.h" +#include "NavigationBar.h" #include "Restful.h" #include "VisitorRecordsPage.h" #include @@ -31,7 +32,8 @@ Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WAppl m_session->login().changed().connect(this, &Application::authEvent); setTheme(std::make_shared("bulma", !embedded)); if (!embedded) { - m_root = root(); + m_navigationBar = root()->addNew(); + m_root = root()->addNew(); } else { std::unique_ptr topPtr = std::make_unique(); m_root = topPtr.get(); @@ -59,6 +61,10 @@ Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WAppl LOG(info) << "internal path: " << internalPath(); internalPathChanged().connect(this, &Application::handlePathChange); handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath); + + auto app = Amass::Singleton::instance(); + m_loginPage = std::make_unique(app->authService(), m_session->users(), m_session->login()); + m_loginPage->processEnvironment(); } Application::~Application() { @@ -78,7 +84,7 @@ void Application::handlePathChange(const std::string &path) { LOG(info) << "handlePathChange: " << path; if (path.starts_with("/wt/login") || path.starts_with("/wt/register")) { m_root->clear(); - m_root->addNew(m_session->users(), m_session->login()); + // m_loginPageRef = m_root->addWidget(std::move(m_loginPage)); } else if (path.starts_with("/wt/visitor/analysis")) { m_root->clear(); m_root->addNew(*m_session); diff --git a/WebApplication/Application.h b/WebApplication/Application.h index 5f30793..62e52c7 100644 --- a/WebApplication/Application.h +++ b/WebApplication/Application.h @@ -18,6 +18,8 @@ class PasswordService; }; // namespace Wt class Session; +class LoginPage; +class NavigationBar; namespace WebToolkit { @@ -35,6 +37,9 @@ private: std::string m_externalPath; Wt::WContainerWidget *m_root = nullptr; + NavigationBar *m_navigationBar = nullptr; + std::unique_ptr m_loginPage; + LoginPage *m_loginPageRef = nullptr; }; class Server { diff --git a/WebApplication/BulmaTheme.cpp b/WebApplication/BulmaTheme.cpp index b068dee..aa6b6c3 100644 --- a/WebApplication/BulmaTheme.cpp +++ b/WebApplication/BulmaTheme.cpp @@ -126,8 +126,6 @@ void BulmaTheme::apply(Wt::WWidget *widget, Wt::DomElement &element, int element LOG(warning) << "elemnet[" << element.type() << "] need style."; break; } - - LOG(info) << "BulmaTheme::apply"; } void BulmaTheme::applyValidationStyle(Wt::WWidget *widget, const Wt::WValidator::Result &validation, diff --git a/WebApplication/HomePage.cpp b/WebApplication/HomePage.cpp index 5c8b947..b26fd99 100644 --- a/WebApplication/HomePage.cpp +++ b/WebApplication/HomePage.cpp @@ -1,13 +1,10 @@ #include "HomePage.h" #include "Dialog.h" -#include "NavigationBar.h" #include #include #include HomePage::HomePage() { - auto navigation = addNew(); - addStyleClass("bulma-content"); auto p = addWidget(std::make_unique("这是一个结合 C++ Web Toolkit 和 CSS Bulma 框架的前后端一体应用程序。")); p->setInline(false); diff --git a/WebApplication/LoginPage.cpp b/WebApplication/LoginPage.cpp index cbe3e63..9f0390b 100644 --- a/WebApplication/LoginPage.cpp +++ b/WebApplication/LoginPage.cpp @@ -1,16 +1,14 @@ #include "LoginPage.h" #include "Application.h" #include -#include #include #include -LoginPage::LoginPage(Wt::Auth::AbstractUserDatabase &users, Wt::Auth::Login &login) { +LoginPage::LoginPage(const Wt::Auth::AuthService &baseAuth, Wt::Auth::AbstractUserDatabase &users, Wt::Auth::Login &login) + : Wt::Auth::AuthWidget(baseAuth, users, login) { auto app = Amass::Singleton::instance(); - auto authWidget = addNew(app->authService(), users, login); - authWidget->setInternalBasePath("/wt"); - authWidget->model()->addPasswordAuth(&app->passwordService()); - authWidget->setRegistrationEnabled(true); - authWidget->processEnvironment(); + setInternalBasePath("/wt"); + model()->addPasswordAuth(&app->passwordService()); + setRegistrationEnabled(true); // setAttributeValue("style", "transform: translateY(-100px);"); } diff --git a/WebApplication/LoginPage.h b/WebApplication/LoginPage.h index 4a68680..9b4d9da 100644 --- a/WebApplication/LoginPage.h +++ b/WebApplication/LoginPage.h @@ -1,11 +1,11 @@ #ifndef __LOGINWIDGET_H__ #define __LOGINWIDGET_H__ -#include +#include -class LoginPage : public Wt::WContainerWidget { +class LoginPage : public Wt::Auth::AuthWidget { public: - LoginPage(Wt::Auth::AbstractUserDatabase &users, Wt::Auth::Login &login); + LoginPage(const Wt::Auth::AuthService &baseAuth, Wt::Auth::AbstractUserDatabase &users, Wt::Auth::Login &login); }; #endif // __LOGINWIDGET_H__ \ No newline at end of file diff --git a/WebApplication/NavigationBar.cpp b/WebApplication/NavigationBar.cpp index fd60fd6..d1d574f 100644 --- a/WebApplication/NavigationBar.cpp +++ b/WebApplication/NavigationBar.cpp @@ -1,23 +1,52 @@ #include "NavigationBar.h" +#include + +constexpr auto Burger = R"( + + + + +)"; + +constexpr auto Menu = R"( +
+ Home +
+
+ +
+)"; constexpr auto Template = R"( - +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"); + auto navbarMenu = bindWidget("navbar-menu", std::make_unique(Menu)); + + navbarBurger->clicked().connect(std::format(JS, navbarBurger->id(), navbarMenu->id())); }