improve wt code.
All checks were successful
Deploy / Build (push) Successful in 7m42s

This commit is contained in:
amass 2024-12-21 13:35:12 +08:00
parent fa6206aeaf
commit ab2fe4f492
7 changed files with 65 additions and 32 deletions

View File

@ -4,6 +4,7 @@
#include "Database/Session.h" #include "Database/Session.h"
#include "HomePage.h" #include "HomePage.h"
#include "LoginPage.h" #include "LoginPage.h"
#include "NavigationBar.h"
#include "Restful.h" #include "Restful.h"
#include "VisitorRecordsPage.h" #include "VisitorRecordsPage.h"
#include <Wt/Auth/AuthService.h> #include <Wt/Auth/AuthService.h>
@ -31,7 +32,8 @@ Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WAppl
m_session->login().changed().connect(this, &Application::authEvent); m_session->login().changed().connect(this, &Application::authEvent);
setTheme(std::make_shared<BulmaTheme>("bulma", !embedded)); setTheme(std::make_shared<BulmaTheme>("bulma", !embedded));
if (!embedded) { if (!embedded) {
m_root = root(); m_navigationBar = root()->addNew<NavigationBar>();
m_root = root()->addNew<Wt::WContainerWidget>();
} else { } else {
std::unique_ptr<Wt::WContainerWidget> topPtr = std::make_unique<Wt::WContainerWidget>(); std::unique_ptr<Wt::WContainerWidget> topPtr = std::make_unique<Wt::WContainerWidget>();
m_root = topPtr.get(); m_root = topPtr.get();
@ -59,6 +61,10 @@ Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WAppl
LOG(info) << "internal path: " << internalPath(); LOG(info) << "internal path: " << internalPath();
internalPathChanged().connect(this, &Application::handlePathChange); internalPathChanged().connect(this, &Application::handlePathChange);
handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath); handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath);
auto app = Amass::Singleton<WebToolkit::Server>::instance();
m_loginPage = std::make_unique<LoginPage>(app->authService(), m_session->users(), m_session->login());
m_loginPage->processEnvironment();
} }
Application::~Application() { Application::~Application() {
@ -78,7 +84,7 @@ void Application::handlePathChange(const std::string &path) {
LOG(info) << "handlePathChange: " << path; LOG(info) << "handlePathChange: " << path;
if (path.starts_with("/wt/login") || path.starts_with("/wt/register")) { if (path.starts_with("/wt/login") || path.starts_with("/wt/register")) {
m_root->clear(); m_root->clear();
m_root->addNew<LoginPage>(m_session->users(), m_session->login()); // m_loginPageRef = m_root->addWidget(std::move(m_loginPage));
} else if (path.starts_with("/wt/visitor/analysis")) { } else if (path.starts_with("/wt/visitor/analysis")) {
m_root->clear(); m_root->clear();
m_root->addNew<VisitorRecordsPage>(*m_session); m_root->addNew<VisitorRecordsPage>(*m_session);

View File

@ -18,6 +18,8 @@ class PasswordService;
}; // namespace Wt }; // namespace Wt
class Session; class Session;
class LoginPage;
class NavigationBar;
namespace WebToolkit { namespace WebToolkit {
@ -35,6 +37,9 @@ private:
std::string m_externalPath; std::string m_externalPath;
Wt::WContainerWidget *m_root = nullptr; Wt::WContainerWidget *m_root = nullptr;
NavigationBar *m_navigationBar = nullptr;
std::unique_ptr<LoginPage> m_loginPage;
LoginPage *m_loginPageRef = nullptr;
}; };
class Server { class Server {

View File

@ -126,8 +126,6 @@ void BulmaTheme::apply(Wt::WWidget *widget, Wt::DomElement &element, int element
LOG(warning) << "elemnet[" << element.type() << "] need style."; LOG(warning) << "elemnet[" << element.type() << "] need style.";
break; break;
} }
LOG(info) << "BulmaTheme::apply";
} }
void BulmaTheme::applyValidationStyle(Wt::WWidget *widget, const Wt::WValidator::Result &validation, void BulmaTheme::applyValidationStyle(Wt::WWidget *widget, const Wt::WValidator::Result &validation,

View File

@ -1,13 +1,10 @@
#include "HomePage.h" #include "HomePage.h"
#include "Dialog.h" #include "Dialog.h"
#include "NavigationBar.h"
#include <Wt/WApplication.h> #include <Wt/WApplication.h>
#include <Wt/WLineEdit.h> #include <Wt/WLineEdit.h>
#include <Wt/WPushButton.h> #include <Wt/WPushButton.h>
HomePage::HomePage() { HomePage::HomePage() {
auto navigation = addNew<NavigationBar>();
addStyleClass("bulma-content"); addStyleClass("bulma-content");
auto p = addWidget(std::make_unique<Wt::WText>("这是一个结合 C++ Web Toolkit 和 CSS Bulma 框架的前后端一体应用程序。")); auto p = addWidget(std::make_unique<Wt::WText>("这是一个结合 C++ Web Toolkit 和 CSS Bulma 框架的前后端一体应用程序。"));
p->setInline(false); p->setInline(false);

View File

@ -1,16 +1,14 @@
#include "LoginPage.h" #include "LoginPage.h"
#include "Application.h" #include "Application.h"
#include <Wt/Auth/AuthService.h> #include <Wt/Auth/AuthService.h>
#include <Wt/Auth/AuthWidget.h>
#include <Wt/Auth/PasswordService.h> #include <Wt/Auth/PasswordService.h>
#include <Wt/WVBoxLayout.h> #include <Wt/WVBoxLayout.h>
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<WebToolkit::Server>::instance(); auto app = Amass::Singleton<WebToolkit::Server>::instance();
auto authWidget = addNew<Wt::Auth::AuthWidget>(app->authService(), users, login); setInternalBasePath("/wt");
authWidget->setInternalBasePath("/wt"); model()->addPasswordAuth(&app->passwordService());
authWidget->model()->addPasswordAuth(&app->passwordService()); setRegistrationEnabled(true);
authWidget->setRegistrationEnabled(true);
authWidget->processEnvironment();
// setAttributeValue("style", "transform: translateY(-100px);"); // setAttributeValue("style", "transform: translateY(-100px);");
} }

View File

@ -1,11 +1,11 @@
#ifndef __LOGINWIDGET_H__ #ifndef __LOGINWIDGET_H__
#define __LOGINWIDGET_H__ #define __LOGINWIDGET_H__
#include <Wt/WContainerWidget.h> #include <Wt/Auth/AuthWidget.h>
class LoginPage : public Wt::WContainerWidget { class LoginPage : public Wt::Auth::AuthWidget {
public: 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__ #endif // __LOGINWIDGET_H__

View File

@ -1,23 +1,52 @@
#include "NavigationBar.h" #include "NavigationBar.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 Menu = R"(
<div class="bulma-navbar-start">
<a class="bulma-navbar-item">Home</a>
</div>
<div class="bulma-navbar-end">
<div class="bulma-navbar-item">
<div class="bulma-buttons">
<a class="bulma-button bulma-is-primary">
<strong></strong>
</a>
<a class="bulma-button bulma-is-light">登录</a>
</div>
</div>
</div>
)";
constexpr auto Template = R"( constexpr auto Template = R"(
<nav class="bulma-navbar" role="navigation" aria-label="main navigation"> <div class="bulma-navbar-brand">
<div class="bulma-navbar-brand"> <a class="bulma-navbar-item" href="https://amass.fun">子非鱼</a>
<a class="bulma-navbar-item" href="https://amass.fun">子非鱼</a> ${navbar-burger role="button" class="bulma-navbar-burger" aria-label="menu" aria-expanded="false"}
<a role="button" class="bulma-navbar-burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample"> </div>
<span aria-hidden="true"></span> ${navbar-menu class="bulma-navbar-menu"}
<span aria-hidden="true"></span> )";
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div id="navbarBasicExample" class="bulma-navbar-menu"> constexpr auto JS = R"(
<div></div> function(o) {{
<div></div> {}.classList.toggle('bulma-is-active');
</div> {}.classList.toggle('bulma-is-active');
</nav> }}
)"; )";
NavigationBar::NavigationBar() : Wt::WTemplate(Template) { 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");
auto navbarMenu = bindWidget("navbar-menu", std::make_unique<Wt::WTemplate>(Menu));
navbarBurger->clicked().connect(std::format(JS, navbarBurger->id(), navbarMenu->id()));
} }