Compare commits
No commits in common. "4311e9f33504dc4aa0d53bd4b0165c8711cf08dd" and "c6c8c66a978b8329ae2208fba782788100487392" have entirely different histories.
4311e9f335
...
c6c8c66a97
11 changed files with 52 additions and 268 deletions
|
@ -2,6 +2,3 @@
|
||||||
|
|
||||||
mod html;
|
mod html;
|
||||||
pub use html::Html;
|
pub use html::Html;
|
||||||
|
|
||||||
mod poweredby;
|
|
||||||
pub use poweredby::PoweredBy;
|
|
||||||
|
|
|
@ -44,13 +44,11 @@ impl Component for Html {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
|
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
|
||||||
PrepareMarkup::With(self.html(cx))
|
PrepareMarkup::With((self.0)(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)
|
||||||
|
@ -68,24 +66,11 @@ 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`]).
|
||||||
#[builder_fn]
|
pub fn alter_html<F>(&mut self, f: F) -> &mut Self
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,69 +0,0 @@
|
||||||
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,7 +28,6 @@ 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 {
|
||||||
|
@ -59,8 +58,7 @@ 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" {
|
||||||
|
@ -87,10 +85,10 @@ async fn homepage(request: HttpRequest) -> ResultPage<Markup, ErrorPage> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}))
|
|
||||||
.with_component_in("footer", Html::with(move |cx| html! {
|
footer id="footer" {
|
||||||
section class="welcome-footer" {
|
section class="footer-inner" {
|
||||||
div class="welcome-footer__logo" {
|
div class="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"
|
||||||
|
@ -104,14 +102,15 @@ 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="welcome-footer__links" {
|
div class="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,11 +17,6 @@ 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,10 +94,11 @@ 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 region--" (region_name) }
|
class="region"
|
||||||
role="region"
|
role="region"
|
||||||
aria-label=[region_label.using(page)]
|
aria-label=[region_label.using(page)]
|
||||||
{
|
{
|
||||||
|
div class={ "region__" (region_name) } {
|
||||||
(output)
|
(output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,6 +106,7 @@ 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.
|
||||||
///
|
///
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
# PoweredBy component.
|
|
||||||
poweredby_pagetop = Powered by { $pagetop_link }
|
|
|
@ -1,2 +0,0 @@
|
||||||
# PoweredBy component.
|
|
||||||
poweredby_pagetop = Funciona con { $pagetop_link }
|
|
|
@ -1,11 +0,0 @@
|
||||||
/* Page layout */
|
|
||||||
|
|
||||||
.region--footer {
|
|
||||||
padding-bottom: 2rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* PoweredBy component */
|
|
||||||
|
|
||||||
.poweredby {
|
|
||||||
text-align: center;
|
|
||||||
}
|
|
|
@ -410,61 +410,51 @@ a:hover:visited {
|
||||||
transform: rotate(2deg);
|
transform: rotate(2deg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#footer {
|
||||||
* Region footer
|
width: 100%;
|
||||||
*/
|
|
||||||
|
|
||||||
.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%;
|
||||||
justify-content: center;
|
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
justify-content: center;
|
||||||
max-width: 80rem;
|
|
||||||
padding: 0 10.625rem 2rem;
|
|
||||||
/*
|
|
||||||
z-index: 10;
|
z-index: 10;
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
.welcome-footer a:visited {
|
#footer a:visited {
|
||||||
color: var(--color-gray);
|
color: var(--color-gray);
|
||||||
}
|
}
|
||||||
.welcome-footer__logo,
|
.footer-logo {
|
||||||
.welcome-footer__links {
|
max-height: 12.625rem;
|
||||||
|
}
|
||||||
|
.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%;
|
||||||
}
|
}
|
||||||
.welcome-footer__logo {
|
.footer-links {
|
||||||
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) {
|
||||||
.welcome-footer__logo {
|
.footer-logo {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@media (max-width: 64rem) {
|
@media (max-width: 64rem) {
|
||||||
.welcome-footer {
|
.footer-inner {
|
||||||
padding: 0 1rem 2rem;
|
padding: 0 1rem 2rem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* PoweredBy component */
|
|
||||||
|
|
||||||
.poweredby a:visited {
|
|
||||||
color: var(--color-gray);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,100 +0,0 @@
|
||||||
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