361 lines
13 KiB
Rust
361 lines
13 KiB
Rust
/*!
|
|
<div align="center">
|
|
|
|
<h1>PageTop Bootsier</h1>
|
|
|
|
<p>Tema de <strong>PageTop</strong> basado en Bootstrap para aplicar su catálogo de estilos y componentes flexibles.</p>
|
|
|
|
[](https://docs.rs/pagetop-bootsier)
|
|
[](https://crates.io/crates/pagetop-bootsier)
|
|
[](https://crates.io/crates/pagetop-bootsier)
|
|
[](https://git.cillero.es/manuelcillero/pagetop/src/branch/main/extensions/pagetop-bootsier#licencia)
|
|
|
|
</div>
|
|
|
|
## Sobre PageTop
|
|
|
|
[PageTop](https://docs.rs/pagetop) es un entorno de desarrollo que reivindica la esencia de la web
|
|
clásica para crear soluciones web SSR (*renderizadas en el servidor*) modulares, extensibles y
|
|
configurables, basadas en HTML, CSS y JavaScript.
|
|
|
|
## Guía rápida
|
|
|
|
Igual que con otras extensiones, **añade la dependencia** a tu `Cargo.toml`:
|
|
|
|
```toml
|
|
[dependencies]
|
|
pagetop-bootsier = { ... }
|
|
```
|
|
|
|
**Declara la extensión** en tu aplicación (o extensión que la requiera). Recuerda que el orden en
|
|
`dependencies()` determina la prioridad relativa frente a las otras extensiones:
|
|
|
|
```rust,no_run
|
|
use pagetop::prelude::*;
|
|
|
|
struct MyApp;
|
|
|
|
#[async_trait]
|
|
impl Extension for MyApp {
|
|
fn dependencies(&self) -> Vec<ExtensionRef> {
|
|
vec![
|
|
// ...
|
|
&pagetop_bootsier::Bootsier,
|
|
// ...
|
|
]
|
|
}
|
|
}
|
|
```
|
|
|
|
Y **selecciona el tema en la configuración** de la aplicación:
|
|
|
|
```toml
|
|
[app]
|
|
theme = "Bootsier"
|
|
```
|
|
|
|
o **fuerza el tema por código** en una página concreta:
|
|
|
|
```rust,no_run
|
|
use pagetop::prelude::*;
|
|
use pagetop_bootsier::Bootsier;
|
|
|
|
async fn homepage(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
|
Page::new(request)
|
|
.with_theme(&Bootsier)
|
|
.with_child(
|
|
Block::new()
|
|
.with_title(Lc::l("sample_title"))
|
|
.with_child(Html::with(|cx| html! {
|
|
p { (Lc::l("sample_content").using(cx)) }
|
|
})),
|
|
)
|
|
.render().await
|
|
}
|
|
```
|
|
|
|
## Plantillas
|
|
|
|
Bootsier maqueta las dos plantillas de PageTop (`CoreTemplates`) con las mismas regiones (cabecera,
|
|
lateral, contenido y pie): `Standard`, la plantilla por defecto de cualquier página; y `Admin`, que
|
|
se activa creando la página con `Page::admin()` en lugar de `Page::new()` y añade el panel de
|
|
administración (ver más abajo).
|
|
|
|
```rust,no_run
|
|
use pagetop::prelude::*;
|
|
|
|
async fn about(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
|
Page::new(request)
|
|
.with_child(Html::with(|_| html! {
|
|
h1 { "Sobre nosotros" }
|
|
p { "Texto de presentación." }
|
|
}))
|
|
.render().await
|
|
}
|
|
```
|
|
|
|
## Panel de administración
|
|
|
|
En la plantilla `Admin`, Bootsier sigue la composición del núcleo: tras la cabecera pinta la
|
|
navegación del panel (`AdminMenu`) y, en lugar del contenido, el marco de la página (`AdminFrame`:
|
|
migas, título, botones y pestañas). Los dos se construyen con componentes del núcleo
|
|
(`Navbar`/`Nav`/`Dropdown`, `Breadcrumb`), que Bootsier ya pinta con su aspecto.
|
|
|
|
## Selector de modo de color
|
|
|
|
`bs::theme_toggle()` crea un ítem de menú con el selector de modo de color (claro / oscuro /
|
|
automático), pensado para añadirse a cualquier `Nav` de la aplicación, no sólo a las páginas de
|
|
administración:
|
|
|
|
```rust,no_run
|
|
use pagetop::prelude::*;
|
|
use pagetop_bootsier::theme::*;
|
|
|
|
let navbar = bs::Navbar::simple().with_item(bs::navbar::Item::nav(
|
|
bs::Nav::new()
|
|
.with_item(bs::nav::Item::link(Lc::n("Home"), "/"))
|
|
.with_item(bs::theme_toggle()),
|
|
));
|
|
```
|
|
|
|
## Temas hijo
|
|
|
|
Un tema hijo hereda todo de Bootsier (plantillas, componentes, estilos, *scripts* y fuentes) y
|
|
cambia su aspecto con una hoja de estilos propia que se carga detrás de la de Bootsier. Esa hoja
|
|
sólo contiene lo que cambia: Bootstrap define casi todo su aspecto con variables CSS (`--bs-*`), y
|
|
basta con redefinirlas:
|
|
|
|
```css
|
|
:root {
|
|
--bs-border-radius: .25rem;
|
|
--bs-link-color: #6f42c1;
|
|
--bs-link-color-rgb: 111, 66, 193;
|
|
--bs-link-hover-color: #59359a;
|
|
--bs-link-hover-color-rgb: 89, 53, 154;
|
|
}
|
|
```
|
|
|
|
Las variantes de color de los componentes (`.btn-primary`, `.alert-primary`...) llevan sus propios
|
|
valores, así que se cambian redefiniendo las variables de cada componente:
|
|
|
|
```css
|
|
.btn-primary {
|
|
--bs-btn-bg: #6f42c1;
|
|
--bs-btn-border-color: #6f42c1;
|
|
--bs-btn-hover-bg: #59359a;
|
|
--bs-btn-hover-border-color: #59359a;
|
|
--bs-btn-active-bg: #4b2d83;
|
|
--bs-btn-active-border-color: #4b2d83;
|
|
}
|
|
```
|
|
|
|
Lo redefinido en `:root` vale también en el modo oscuro, porque gana a los valores oscuros de
|
|
Bootstrap; lo que deba ser distinto en él va dentro de `[data-bs-theme=dark] { ... }`.
|
|
|
|
El `build.rs` del tema hijo incluye la hoja en el binario con `pagetop-build`:
|
|
|
|
```rust,ignore
|
|
use pagetop_build::StaticFilesBundle;
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
StaticFilesBundle::from_dir("./assets/css", None)
|
|
.with_name("my_theme_css")
|
|
.build()
|
|
}
|
|
```
|
|
|
|
El tema la sirve con `serve_static_files!(files, [my_theme_css] => "/my-theme/css")` en
|
|
`Extension::configure_static_files()`, declara a Bootsier como padre y añade su hoja tras la de
|
|
Bootsier:
|
|
|
|
```rust,no_run
|
|
use pagetop::prelude::*;
|
|
use pagetop_bootsier::Bootsier;
|
|
|
|
pub struct MyTheme;
|
|
|
|
#[async_trait]
|
|
impl Extension for MyTheme {
|
|
fn theme(&self) -> Option<ThemeRef> {
|
|
Some(&Self)
|
|
}
|
|
|
|
fn dependencies(&self) -> Vec<ExtensionRef> {
|
|
vec![&Bootsier]
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl Theme for MyTheme {
|
|
fn parent(&self) -> Option<ThemeRef> {
|
|
Some(&Bootsier)
|
|
}
|
|
|
|
fn before_render_page_body(&self, page: &mut Page) {
|
|
Bootsier.before_render_page_body(page);
|
|
// Justo detrás de la hoja de Bootsier (peso -99).
|
|
page.alter_assets(StyleSheet::from("/my-theme/css/my-theme.css").with_weight(-98));
|
|
}
|
|
}
|
|
```
|
|
*/
|
|
|
|
#![doc(
|
|
html_favicon_url = "https://git.cillero.es/manuelcillero/pagetop/raw/branch/main/assets/favicon.ico"
|
|
)]
|
|
|
|
use pagetop::prelude::*;
|
|
|
|
include_locales!(LOCALES_BOOTSIER);
|
|
|
|
const BOOTSTRAP_VERSION: &str = "5.3.8";
|
|
|
|
pub mod config;
|
|
|
|
pub mod theme;
|
|
|
|
/// Implementa el tema.
|
|
pub struct Bootsier;
|
|
|
|
#[async_trait]
|
|
impl Extension for Bootsier {
|
|
fn name(&self) -> Lc {
|
|
Lc::t("extension_name", &LOCALES_BOOTSIER)
|
|
}
|
|
|
|
fn description(&self) -> Lc {
|
|
Lc::t("extension_description", &LOCALES_BOOTSIER)
|
|
}
|
|
|
|
fn theme(&self) -> Option<ThemeRef> {
|
|
Some(&Self)
|
|
}
|
|
|
|
fn settings(&self) -> Option<&'static dyn LoadSettings> {
|
|
Some(&config::SETTINGS)
|
|
}
|
|
|
|
fn configure_static_files(&self, files: StaticFiles) -> StaticFiles {
|
|
let base = &config::SETTINGS.static_dir;
|
|
let subdir = |s: &str| {
|
|
if base.is_empty() {
|
|
String::new()
|
|
} else {
|
|
format!("{base}/{s}")
|
|
}
|
|
};
|
|
serve_static_files!(files, [subdir("css"), bootsier_css] => "/bootsier/css");
|
|
serve_static_files!(files, [subdir("js"), bootsier_js] => "/bootsier/js");
|
|
serve_static_files!(files, [subdir("fonts"), bootsier_fonts] => "/bootsier/fonts");
|
|
files
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl Theme for Bootsier {
|
|
#[rustfmt::skip]
|
|
fn breakpoint_entry(&self, bp: Breakpoint) -> BreakpointEntry {
|
|
use Breakpoint::*;
|
|
|
|
// Anchos mínimos de la rejilla de Bootstrap (`$grid-breakpoints`). Bootstrap no tiene un
|
|
// punto de corte por encima de `xxl`, así que `Xxxl` se resuelve como `xxl`.
|
|
match bp {
|
|
Xs => BreakpointEntry { breakpoint: Xs, name: "xs", min_width: "" },
|
|
Sm => BreakpointEntry { breakpoint: Sm, name: "sm", min_width: "576px" },
|
|
Md => BreakpointEntry { breakpoint: Md, name: "md", min_width: "768px" },
|
|
Lg => BreakpointEntry { breakpoint: Lg, name: "lg", min_width: "992px" },
|
|
Xl => BreakpointEntry { breakpoint: Xl, name: "xl", min_width: "1200px" },
|
|
Xxl => BreakpointEntry { breakpoint: Xxl, name: "xxl", min_width: "1400px" },
|
|
Xxxl => BreakpointEntry { breakpoint: Xxxl, name: "xxl", min_width: "1400px" },
|
|
}
|
|
}
|
|
|
|
fn intent_color(&self, intent: Intent) -> &'static str {
|
|
theme::BootsierColors::from(intent).as_str()
|
|
}
|
|
|
|
fn setup_component(&self, component: &mut dyn Component, _cx: &mut Context) {
|
|
setup_component!(component, {
|
|
Badge => |c| theme::bs::badge::setup(c),
|
|
Brand => |c| theme::bs::brand::setup(c),
|
|
Button => |c| theme::bs::button::setup(c),
|
|
Container => |c| theme::bs::container::setup(c),
|
|
Dialog => |c| theme::bs::dialog::setup(c),
|
|
Dropdown => |c| theme::bs::dropdown::setup(c),
|
|
Image => |c| theme::bs::image::setup(c),
|
|
Nav => |c| theme::bs::nav::setup(c),
|
|
Navbar => |c| theme::bs::navbar::setup(c),
|
|
form::input::Field => |c| theme::bs::form::input::setup(c),
|
|
form::select::Field => |c| theme::bs::form::select::setup(c),
|
|
form::Textarea => |c| theme::bs::form::textarea::setup(c),
|
|
});
|
|
}
|
|
|
|
async fn render_component(
|
|
&self,
|
|
component: &dyn Component,
|
|
cx: &mut Context,
|
|
) -> Option<Result<Markup, ComponentError>> {
|
|
render_component!(component, {
|
|
layout::Template => |c| theme::bs::layout::template::render(c, cx).await?,
|
|
Dialog => |c| theme::bs::dialog::render(c, cx).await,
|
|
Dropdown => |c| theme::bs::dropdown::render(c, cx).await,
|
|
nav::Item => |c| theme::bs::nav::item_render(c, cx).await,
|
|
Navbar => |c| theme::bs::navbar::render(c, cx).await,
|
|
navbar::Item => |c| theme::bs::navbar::item_render(c, cx).await,
|
|
form::input::Field => |c| theme::bs::form::input::render(c, cx),
|
|
form::select::Field => |c| theme::bs::form::select::render(c, cx),
|
|
form::Textarea => |c| theme::bs::form::textarea::render(c, cx),
|
|
})
|
|
}
|
|
|
|
fn before_render_page_body(&self, page: &mut Page) {
|
|
// Etiquetas de los botones del modal de confirmación que sustituye al `confirm()` nativo
|
|
// de htmx (ver `bootsier.confirm.js`); se leen aquí porque este es el único punto con
|
|
// acceso al `Context` para resolverlas antes de escribirlas en el `<body>`.
|
|
let confirm_ok = Lc::t("confirm_ok", &LOCALES_BOOTSIER)
|
|
.lookup(page)
|
|
.unwrap_or_default();
|
|
let confirm_cancel = Lc::t("confirm_cancel", &LOCALES_BOOTSIER)
|
|
.lookup(page)
|
|
.unwrap_or_default();
|
|
|
|
// Las URLs de las fuentes deben coincidir exactamente con las declaradas en @font-face de
|
|
// _bootsier-custom.scss; cualquier discrepancia hace que el navegador descargue dos veces.
|
|
page.alter_assets(Preload::font("/bootsier/fonts/bootsier.font.woff2").with_weight(-99))
|
|
.alter_assets(
|
|
Preload::font("/bootsier/fonts/bootsier.font.italic.woff2").with_weight(-99),
|
|
)
|
|
.alter_assets(
|
|
StyleSheet::from("/bootsier/css/bootsier.min.css")
|
|
.with_version(BOOTSTRAP_VERSION)
|
|
.with_weight(-99),
|
|
)
|
|
.alter_assets(
|
|
JavaScript::defer("/bootsier/js/bootsier.bundle.min.js")
|
|
.with_version(BOOTSTRAP_VERSION)
|
|
.with_weight(-99),
|
|
)
|
|
.alter_assets(
|
|
JavaScript::defer("/bootsier/js/bootsier.theme.min.js")
|
|
.with_version(BOOTSTRAP_VERSION)
|
|
.with_weight(-99),
|
|
)
|
|
.alter_assets(
|
|
JavaScript::defer("/bootsier/js/bootsier.dialog.min.js")
|
|
.with_version(BOOTSTRAP_VERSION)
|
|
.with_weight(-99),
|
|
)
|
|
.alter_assets(
|
|
JavaScript::defer("/bootsier/js/bootsier.confirm.min.js")
|
|
.with_version(BOOTSTRAP_VERSION)
|
|
.with_weight(-99),
|
|
)
|
|
.alter_body_props(PropsOp::set("data-confirm-ok", confirm_ok))
|
|
.alter_body_props(PropsOp::set("data-confirm-cancel", confirm_cancel))
|
|
.alter_child_in(
|
|
&CoreRegions::Footer,
|
|
ChildOp::AddIfEmpty(PoweredBy::new().into()),
|
|
);
|
|
}
|
|
}
|