Compare commits
2 commits
c6c8c66a97
...
4311e9f335
Author | SHA1 | Date | |
---|---|---|---|
4311e9f335 | |||
0c1b12aacd |
11 changed files with 268 additions and 52 deletions
|
@ -2,3 +2,6 @@
|
||||||
|
|
||||||
mod html;
|
mod html;
|
||||||
pub use html::Html;
|
pub use html::Html;
|
||||||
|
|
||||||
|
mod poweredby;
|
||||||
|
pub use poweredby::PoweredBy;
|
||||||
|
|
|
@ -44,11 +44,13 @@ impl Component for Html {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
|
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
|
||||||
PrepareMarkup::With((self.0)(cx))
|
PrepareMarkup::With(self.html(cx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Html {
|
impl Html {
|
||||||
|
// Html BUILDER ********************************************************************************
|
||||||
|
|
||||||
/// Crea una instancia que generará el `Markup`, con acceso opcional al contexto.
|
/// Crea una instancia que generará el `Markup`, con acceso opcional al contexto.
|
||||||
///
|
///
|
||||||
/// El método [`prepare_component()`](crate::core::component::Component::prepare_component)
|
/// El método [`prepare_component()`](crate::core::component::Component::prepare_component)
|
||||||
|
@ -66,11 +68,24 @@ impl Html {
|
||||||
/// Permite a otras extensiones modificar la función de renderizado que se ejecutará cuando
|
/// Permite a otras extensiones modificar la función de renderizado que se ejecutará cuando
|
||||||
/// [`prepare_component()`](crate::core::component::Component::prepare_component) invoque esta
|
/// [`prepare_component()`](crate::core::component::Component::prepare_component) invoque esta
|
||||||
/// instancia. La nueva función también recibe una referencia al contexto ([`Context`]).
|
/// instancia. La nueva función también recibe una referencia al contexto ([`Context`]).
|
||||||
pub fn alter_html<F>(&mut self, f: F) -> &mut Self
|
#[builder_fn]
|
||||||
|
pub fn with_fn<F>(mut self, f: F) -> Self
|
||||||
where
|
where
|
||||||
F: Fn(&mut Context) -> Markup + Send + Sync + 'static,
|
F: Fn(&mut Context) -> Markup + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
self.0 = Box::new(f);
|
self.0 = Box::new(f);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Html GETTERS ********************************************************************************
|
||||||
|
|
||||||
|
/// Aplica la función interna de renderizado con el [`Context`] proporcionado.
|
||||||
|
///
|
||||||
|
/// Normalmente no se invoca manualmente, ya que el proceso de renderizado de los componentes lo
|
||||||
|
/// invoca automáticamente durante la construcción de la página. Puede usarse, no obstante, para
|
||||||
|
/// sobrescribir [`prepare_component()`](crate::core::component::Component::prepare_component)
|
||||||
|
/// y alterar el comportamiento del componente.
|
||||||
|
pub fn html(&self, cx: &mut Context) -> Markup {
|
||||||
|
(self.0)(cx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
69
src/base/component/poweredby.rs
Normal file
69
src/base/component/poweredby.rs
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
/// Muestra un texto con información de copyright, típica en un pie de página.
|
||||||
|
///
|
||||||
|
/// Por defecto, usando [`default()`](Self::default) sólo se muestra un
|
||||||
|
/// reconocimiento a PageTop. Sin embargo, se puede usar [`new()`](Self::new)
|
||||||
|
/// para crear una instancia con un texto de copyright predeterminado.
|
||||||
|
#[derive(AutoDefault)]
|
||||||
|
pub struct PoweredBy {
|
||||||
|
copyright: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Component for PoweredBy {
|
||||||
|
/// Crea una nueva instancia de `PoweredBy`.
|
||||||
|
///
|
||||||
|
/// El copyright se genera automáticamente con el año actual y el nombre de
|
||||||
|
/// la aplicación configurada en [`global::SETTINGS`].
|
||||||
|
fn new() -> Self {
|
||||||
|
let year = Utc::now().format("%Y").to_string();
|
||||||
|
let c = join!(year, " © ", global::SETTINGS.app.name);
|
||||||
|
PoweredBy { copyright: Some(c) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
|
||||||
|
let poweredby_pagetop = L10n::l("poweredby_pagetop")
|
||||||
|
.with_arg(
|
||||||
|
"pagetop_link",
|
||||||
|
"<a href=\"https://crates.io/crates/pagetop\">PageTop</a>",
|
||||||
|
)
|
||||||
|
.to_markup(cx);
|
||||||
|
|
||||||
|
PrepareMarkup::With(html! {
|
||||||
|
div id=[self.id()] class="poweredby" {
|
||||||
|
@if let Some(c) = self.copyright() {
|
||||||
|
span class="poweredby__copyright" { (c) "." } " "
|
||||||
|
}
|
||||||
|
span class="poweredby__pagetop" { (poweredby_pagetop) }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PoweredBy {
|
||||||
|
// PoweredBy BUILDER ***************************************************************************
|
||||||
|
|
||||||
|
/// Establece el texto de copyright que mostrará el componente.
|
||||||
|
///
|
||||||
|
/// Al pasar `Some(valor)` se sobrescribe el texto de copyright por defecto. Al pasar `None` se
|
||||||
|
/// eliminará, pero en este caso es necesario especificar el tipo explícitamente:
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use pagetop::prelude::*;
|
||||||
|
///
|
||||||
|
/// let p1 = PoweredBy::default().with_copyright(Some("2001 © Foo Inc."));
|
||||||
|
/// let p2 = PoweredBy::new().with_copyright(None::<String>);
|
||||||
|
/// ```
|
||||||
|
#[builder_fn]
|
||||||
|
pub fn with_copyright(mut self, copyright: Option<impl Into<String>>) -> Self {
|
||||||
|
self.copyright = copyright.map(Into::into);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
// PoweredBy GETTERS ***************************************************************************
|
||||||
|
|
||||||
|
/// Devuelve el texto de copyright actual, si existe.
|
||||||
|
pub fn copyright(&self) -> Option<&str> {
|
||||||
|
self.copyright.as_deref()
|
||||||
|
}
|
||||||
|
}
|
|
@ -28,6 +28,7 @@ async fn homepage(request: HttpRequest) -> ResultPage<Markup, ErrorPage> {
|
||||||
.with_title(L10n::l("welcome_page"))
|
.with_title(L10n::l("welcome_page"))
|
||||||
.with_theme("Basic")
|
.with_theme("Basic")
|
||||||
.with_assets(AssetsOp::AddStyleSheet(StyleSheet::from("/css/welcome.css")))
|
.with_assets(AssetsOp::AddStyleSheet(StyleSheet::from("/css/welcome.css")))
|
||||||
|
.with_body_classes(ClassesOp::Add, "welcome")
|
||||||
.with_component(Html::with(move |cx| html! {
|
.with_component(Html::with(move |cx| html! {
|
||||||
div id="main-header" {
|
div id="main-header" {
|
||||||
header {
|
header {
|
||||||
|
@ -58,7 +59,8 @@ async fn homepage(request: HttpRequest) -> ResultPage<Markup, ErrorPage> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}))
|
||||||
|
.with_component(Html::with(move |cx| html! {
|
||||||
main id="main-content" {
|
main id="main-content" {
|
||||||
section class="content-body" {
|
section class="content-body" {
|
||||||
div id="poweredby-button" {
|
div id="poweredby-button" {
|
||||||
|
@ -85,10 +87,10 @@ async fn homepage(request: HttpRequest) -> ResultPage<Markup, ErrorPage> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}))
|
||||||
footer id="footer" {
|
.with_component_in("footer", Html::with(move |cx| html! {
|
||||||
section class="footer-inner" {
|
section class="welcome-footer" {
|
||||||
div class="footer-logo" {
|
div class="welcome-footer__logo" {
|
||||||
svg
|
svg
|
||||||
viewBox="0 0 1614 1614"
|
viewBox="0 0 1614 1614"
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
@ -102,15 +104,14 @@ async fn homepage(request: HttpRequest) -> ResultPage<Markup, ErrorPage> {
|
||||||
path fill="rgb(255,255,255)" d="M 1071,648 C 998,648 939,707 939,780 939,853 998,912 1071,912 1144,912 1203,853 1203,780 1203,707 1144,648 1071,648 L 1071,648 1071,648 Z M 1071,859 C 1027,859 992,824 992,780 992,736 1027,701 1071,701 1115,701 1150,736 1150,780 1150,824 1115,859 1071,859 L 1071,859 1071,859 Z" {}
|
path fill="rgb(255,255,255)" d="M 1071,648 C 998,648 939,707 939,780 939,853 998,912 1071,912 1144,912 1203,853 1203,780 1203,707 1144,648 1071,648 L 1071,648 1071,648 Z M 1071,859 C 1027,859 992,824 992,780 992,736 1027,701 1071,701 1115,701 1150,736 1150,780 1150,824 1115,859 1071,859 L 1071,859 1071,859 Z" {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
div class="footer-links" {
|
div class="welcome-footer__links" {
|
||||||
a href="https://crates.io/crates/pagetop" target="_blank" rel="noreferrer" { ("Crates.io") }
|
a href="https://crates.io/crates/pagetop" target="_blank" rel="noreferrer" { ("Crates.io") }
|
||||||
a href="https://docs.rs/pagetop" target="_blank" rel="noreferrer" { ("Docs.rs") }
|
a href="https://docs.rs/pagetop" target="_blank" rel="noreferrer" { ("Docs.rs") }
|
||||||
a href="https://git.cillero.es/manuelcillero/pagetop" target="_blank" rel="noreferrer" { (L10n::l("welcome_code").to_markup(cx)) }
|
a href="https://git.cillero.es/manuelcillero/pagetop" target="_blank" rel="noreferrer" { (L10n::l("welcome_code").to_markup(cx)) }
|
||||||
em { (L10n::l("welcome_have_fun").to_markup(cx)) }
|
em { (L10n::l("welcome_have_fun").to_markup(cx)) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}))
|
}))
|
||||||
|
.with_component_in("footer", PoweredBy::new())
|
||||||
.render()
|
.render()
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,11 @@ impl Theme for Basic {
|
||||||
StyleSheet::from("/css/normalize.css")
|
StyleSheet::from("/css/normalize.css")
|
||||||
.with_version("8.0.1")
|
.with_version("8.0.1")
|
||||||
.with_weight(-99),
|
.with_weight(-99),
|
||||||
|
))
|
||||||
|
.alter_assets(AssetsOp::AddStyleSheet(
|
||||||
|
StyleSheet::from("/css/basic.css")
|
||||||
|
.with_version(env!("CARGO_PKG_VERSION"))
|
||||||
|
.with_weight(-99),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,11 +94,10 @@ pub trait Theme: Extension + Send + Sync {
|
||||||
@let region_name = region.name();
|
@let region_name = region.name();
|
||||||
div
|
div
|
||||||
id=(region_name)
|
id=(region_name)
|
||||||
class="region"
|
class={ "region region--" (region_name) }
|
||||||
role="region"
|
role="region"
|
||||||
aria-label=[region_label.using(page)]
|
aria-label=[region_label.using(page)]
|
||||||
{
|
{
|
||||||
div class={ "region__" (region_name) } {
|
|
||||||
(output)
|
(output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,7 +105,6 @@ pub trait Theme: Extension + Send + Sync {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Acciones específicas del tema después de renderizar el `<body>` de la página.
|
/// Acciones específicas del tema después de renderizar el `<body>` de la página.
|
||||||
///
|
///
|
||||||
|
|
2
src/locale/en-US/base.ftl
Normal file
2
src/locale/en-US/base.ftl
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
# PoweredBy component.
|
||||||
|
poweredby_pagetop = Powered by { $pagetop_link }
|
2
src/locale/es-ES/base.ftl
Normal file
2
src/locale/es-ES/base.ftl
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
# PoweredBy component.
|
||||||
|
poweredby_pagetop = Funciona con { $pagetop_link }
|
11
static/css/basic.css
Normal file
11
static/css/basic.css
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
/* Page layout */
|
||||||
|
|
||||||
|
.region--footer {
|
||||||
|
padding-bottom: 2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* PoweredBy component */
|
||||||
|
|
||||||
|
.poweredby {
|
||||||
|
text-align: center;
|
||||||
|
}
|
|
@ -410,51 +410,61 @@ a:hover:visited {
|
||||||
transform: rotate(2deg);
|
transform: rotate(2deg);
|
||||||
}
|
}
|
||||||
|
|
||||||
#footer {
|
/*
|
||||||
width: 100%;
|
* Region footer
|
||||||
|
*/
|
||||||
|
|
||||||
|
.region--footer {
|
||||||
background-color: black;
|
background-color: black;
|
||||||
color: var(--color-gray);
|
color: var(--color-gray);
|
||||||
|
}
|
||||||
|
|
||||||
|
.welcome-footer {
|
||||||
font-size: 1.15rem;
|
font-size: 1.15rem;
|
||||||
font-weight: 300;
|
font-weight: 300;
|
||||||
line-height: 100%;
|
line-height: 100%;
|
||||||
display: flex;
|
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
max-width: 80rem;
|
||||||
|
padding: 0 10.625rem 2rem;
|
||||||
|
/*
|
||||||
z-index: 10;
|
z-index: 10;
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
#footer a:visited {
|
.welcome-footer a:visited {
|
||||||
color: var(--color-gray);
|
color: var(--color-gray);
|
||||||
}
|
}
|
||||||
.footer-logo {
|
.welcome-footer__logo,
|
||||||
max-height: 12.625rem;
|
.welcome-footer__links {
|
||||||
}
|
|
||||||
.footer-logo svg {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
.footer-logo,
|
|
||||||
.footer-links,
|
|
||||||
.footer-inner {
|
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
.footer-links {
|
.welcome-footer__logo {
|
||||||
|
max-height: 12.625rem;
|
||||||
|
}
|
||||||
|
.welcome-footer__logo svg {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
.welcome-footer__links {
|
||||||
gap: 1.875rem;
|
gap: 1.875rem;
|
||||||
flex-wrap: wrap;
|
flex-wrap: wrap;
|
||||||
margin-top: 2rem;
|
margin-top: 2rem;
|
||||||
}
|
}
|
||||||
.footer-inner {
|
|
||||||
max-width: 80rem;
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
padding: 0 10.625rem 2rem;
|
|
||||||
}
|
|
||||||
@media (max-width: 48rem) {
|
@media (max-width: 48rem) {
|
||||||
.footer-logo {
|
.welcome-footer__logo {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@media (max-width: 64rem) {
|
@media (max-width: 64rem) {
|
||||||
.footer-inner {
|
.welcome-footer {
|
||||||
padding: 0 1rem 2rem;
|
padding: 0 1rem 2rem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* PoweredBy component */
|
||||||
|
|
||||||
|
.poweredby a:visited {
|
||||||
|
color: var(--color-gray);
|
||||||
|
}
|
||||||
|
|
100
tests/component_poweredby.rs
Normal file
100
tests/component_poweredby.rs
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
use pagetop::prelude::*;
|
||||||
|
|
||||||
|
#[pagetop::test]
|
||||||
|
async fn poweredby_default_shows_only_pagetop_recognition() {
|
||||||
|
let _app = service::test::init_service(Application::new().test()).await;
|
||||||
|
|
||||||
|
let p = PoweredBy::default();
|
||||||
|
let html = render_component(&p);
|
||||||
|
|
||||||
|
// Debe mostrar el bloque de reconocimiento a PageTop.
|
||||||
|
assert!(html.contains("poweredby__pagetop"));
|
||||||
|
|
||||||
|
// Y NO debe mostrar el bloque de copyright.
|
||||||
|
assert!(!html.contains("poweredby__copyright"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pagetop::test]
|
||||||
|
async fn poweredby_new_includes_current_year_and_app_name() {
|
||||||
|
let _app = service::test::init_service(Application::new().test()).await;
|
||||||
|
|
||||||
|
let p = PoweredBy::new();
|
||||||
|
let html = render_component(&p);
|
||||||
|
|
||||||
|
let year = Utc::now().format("%Y").to_string();
|
||||||
|
assert!(html.contains(&year), "HTML should include the current year");
|
||||||
|
|
||||||
|
// El nombre de la app proviene de `global::SETTINGS.app.name`.
|
||||||
|
let app_name = &global::SETTINGS.app.name;
|
||||||
|
assert!(
|
||||||
|
html.contains(app_name),
|
||||||
|
"HTML should include the application name"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Debe existir el span de copyright.
|
||||||
|
assert!(html.contains("poweredby__copyright"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pagetop::test]
|
||||||
|
async fn poweredby_with_copyright_overrides_text() {
|
||||||
|
let _app = service::test::init_service(Application::new().test()).await;
|
||||||
|
|
||||||
|
let custom = "2001 © FooBar Inc.";
|
||||||
|
let p = PoweredBy::default().with_copyright(Some(custom));
|
||||||
|
let html = render_component(&p);
|
||||||
|
|
||||||
|
assert!(html.contains(custom));
|
||||||
|
assert!(html.contains("poweredby__copyright"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pagetop::test]
|
||||||
|
async fn poweredby_with_copyright_none_hides_text() {
|
||||||
|
let _app = service::test::init_service(Application::new().test()).await;
|
||||||
|
|
||||||
|
let p = PoweredBy::new().with_copyright(None::<String>);
|
||||||
|
let html = render_component(&p);
|
||||||
|
|
||||||
|
assert!(!html.contains("poweredby__copyright"));
|
||||||
|
// El reconocimiento a PageTop siempre debe aparecer.
|
||||||
|
assert!(html.contains("poweredby__pagetop"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pagetop::test]
|
||||||
|
async fn poweredby_link_points_to_crates_io() {
|
||||||
|
let _app = service::test::init_service(Application::new().test()).await;
|
||||||
|
|
||||||
|
let p = PoweredBy::default();
|
||||||
|
let html = render_component(&p);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
html.contains("https://crates.io/crates/pagetop"),
|
||||||
|
"Link should point to crates.io/pagetop"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pagetop::test]
|
||||||
|
async fn poweredby_getter_reflects_internal_state() {
|
||||||
|
let _app = service::test::init_service(Application::new().test()).await;
|
||||||
|
|
||||||
|
// Por defecto no hay copyright.
|
||||||
|
let p0 = PoweredBy::default();
|
||||||
|
assert_eq!(p0.copyright(), None);
|
||||||
|
|
||||||
|
// Y `new()` lo inicializa con año + nombre de app.
|
||||||
|
let p1 = PoweredBy::new();
|
||||||
|
let c1 = p1.copyright().expect("Expected copyright to exis");
|
||||||
|
assert!(c1.contains(&Utc::now().format("%Y").to_string()));
|
||||||
|
assert!(c1.contains(&global::SETTINGS.app.name));
|
||||||
|
}
|
||||||
|
|
||||||
|
// HELPERS *****************************************************************************************
|
||||||
|
|
||||||
|
fn render(x: &impl Render) -> String {
|
||||||
|
x.render().into_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_component<C: Component>(c: &C) -> String {
|
||||||
|
let mut cx = Context::default();
|
||||||
|
let pm = c.prepare_component(&mut cx);
|
||||||
|
render(&pm)
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue