From d8cf0fbdc92f9f63e3e2b3ffa51e19557406ceb6 Mon Sep 17 00:00:00 2001 From: Lev Kokotov Date: Mon, 3 Jun 2024 12:48:10 -0700 Subject: [PATCH 1/2] Allow to inject arbitrary components into the --- .../src/components/layouts/head/mod.rs | 22 ++++++++++++++++++- .../src/components/layouts/head/template.html | 4 ++++ .../components/layouts/marketing/base/mod.rs | 11 +++++++++- pgml-dashboard/src/templates/mod.rs | 11 ++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/pgml-dashboard/src/components/layouts/head/mod.rs b/pgml-dashboard/src/components/layouts/head/mod.rs index 1111815ad..f64e907c7 100644 --- a/pgml-dashboard/src/components/layouts/head/mod.rs +++ b/pgml-dashboard/src/components/layouts/head/mod.rs @@ -1,4 +1,4 @@ -use pgml_components::component; +use pgml_components::{component, Component}; use sailfish::TemplateOnce; #[derive(TemplateOnce, Default, Clone)] @@ -10,6 +10,7 @@ pub struct Head { pub preloads: Vec, pub context: Option, pub canonical: Option, + pub additional_components: Vec, } impl Head { @@ -58,6 +59,25 @@ impl Head { self.context = context.to_owned(); self } + + /// Add a component to ``. + /// + /// This can be anything, e.g. a `")); + /// ``` + /// + pub fn add_component(mut self, component: Component) -> Self { + self.additional_components.push(component); + self + } } component!(Head); diff --git a/pgml-dashboard/src/components/layouts/head/template.html b/pgml-dashboard/src/components/layouts/head/template.html index 3ad5d44a9..823dbb80a 100644 --- a/pgml-dashboard/src/components/layouts/head/template.html +++ b/pgml-dashboard/src/components/layouts/head/template.html @@ -97,4 +97,8 @@ }; <% } %> + + <% for component in additional_components { %> + <%+ component %> + <% } %> diff --git a/pgml-dashboard/src/components/layouts/marketing/base/mod.rs b/pgml-dashboard/src/components/layouts/marketing/base/mod.rs index 5d1ee0d36..15d983756 100644 --- a/pgml-dashboard/src/components/layouts/marketing/base/mod.rs +++ b/pgml-dashboard/src/components/layouts/marketing/base/mod.rs @@ -3,7 +3,7 @@ use crate::components::notifications::marketing::AlertBanner; use crate::guards::Cluster; use crate::models::User; use crate::Notification; -use pgml_components::component; +use pgml_components::{component, Component}; use sailfish::TemplateOnce; use std::fmt; @@ -72,6 +72,15 @@ impl Base { rsp } + /// Add a component to the ``. + /// + /// See [`Head::add_component`](crate::components::layouts::Head::add_component) for more information. + /// + pub fn add_head_component(mut self, component: Component) -> Self { + self.head = self.head.add_component(component); + self + } + pub fn footer(mut self, footer: String) -> Self { self.footer = Some(footer); self diff --git a/pgml-dashboard/src/templates/mod.rs b/pgml-dashboard/src/templates/mod.rs index 39a614f85..0c62944db 100644 --- a/pgml-dashboard/src/templates/mod.rs +++ b/pgml-dashboard/src/templates/mod.rs @@ -163,6 +163,17 @@ impl<'a> WebAppBase<'a> { self } + /// Add a component to head (``). + /// + /// # Arguments + /// + /// * `component` - The component to add to the head. + /// + pub fn add_head_component(mut self, component: Component) -> Self { + self.head = self.head.add_component(component); + self + } + pub fn render(&mut self, template: T) -> String where T: sailfish::TemplateOnce, From fa1ec523c52f2b08f1f540f0c760d2cf1ee0339f Mon Sep 17 00:00:00 2001 From: Lev Kokotov Date: Mon, 3 Jun 2024 12:57:23 -0700 Subject: [PATCH 2/2] mut ref --- pgml-dashboard/src/templates/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pgml-dashboard/src/templates/mod.rs b/pgml-dashboard/src/templates/mod.rs index 0c62944db..d6328661e 100644 --- a/pgml-dashboard/src/templates/mod.rs +++ b/pgml-dashboard/src/templates/mod.rs @@ -169,8 +169,8 @@ impl<'a> WebAppBase<'a> { /// /// * `component` - The component to add to the head. /// - pub fn add_head_component(mut self, component: Component) -> Self { - self.head = self.head.add_component(component); + pub fn add_head_component(&mut self, component: Component) -> &mut Self { + self.head = self.head.clone().add_component(component); self }