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 "HomePage.h"
#include "LoginPage.h"
#include "NavigationBar.h"
#include "Restful.h"
#include "VisitorRecordsPage.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);
setTheme(std::make_shared<BulmaTheme>("bulma", !embedded));
if (!embedded) {
m_root = root();
m_navigationBar = root()->addNew<NavigationBar>();
m_root = root()->addNew<Wt::WContainerWidget>();
} else {
std::unique_ptr<Wt::WContainerWidget> topPtr = std::make_unique<Wt::WContainerWidget>();
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<WebToolkit::Server>::instance();
m_loginPage = std::make_unique<LoginPage>(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<LoginPage>(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<VisitorRecordsPage>(*m_session);

View File

@ -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<LoginPage> m_loginPage;
LoginPage *m_loginPageRef = nullptr;
};
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.";
break;
}
LOG(info) << "BulmaTheme::apply";
}
void BulmaTheme::applyValidationStyle(Wt::WWidget *widget, const Wt::WValidator::Result &validation,

View File

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

View File

@ -1,16 +1,14 @@
#include "LoginPage.h"
#include "Application.h"
#include <Wt/Auth/AuthService.h>
#include <Wt/Auth/AuthWidget.h>
#include <Wt/Auth/PasswordService.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 authWidget = addNew<Wt::Auth::AuthWidget>(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);");
}

View File

@ -1,11 +1,11 @@
#ifndef __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:
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__

View File

@ -1,23 +1,52 @@
#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"(
<nav class="bulma-navbar" role="navigation" aria-label="main navigation">
<div class="bulma-navbar-brand">
<a class="bulma-navbar-item" href="https://amass.fun">子非鱼</a>
<a role="button" class="bulma-navbar-burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
${navbar-burger role="button" class="bulma-navbar-burger" aria-label="menu" aria-expanded="false"}
</div>
${navbar-menu class="bulma-navbar-menu"}
)";
<div id="navbarBasicExample" class="bulma-navbar-menu">
<div></div>
<div></div>
</div>
</nav>
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");
auto navbarMenu = bindWidget("navbar-menu", std::make_unique<Wt::WTemplate>(Menu));
navbarBurger->clicked().connect(std::format(JS, navbarBurger->id(), navbarMenu->id()));
}