Skip to main content

rumtk_web/components/app/
app_nav.rs

1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025  Ethan Dixon
6 * Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21use crate::components::app::logo::{logo, Logo};
22use crate::components::app::navlink::{navlink, NavLink};
23use crate::components::html::{header, Header};
24use crate::components::title::{title, Title};
25use crate::defaults::{PARAMS_TARGET, PARAMS_TITLE};
26use crate::utils::defaults::{DEFAULT_TEXT_ITEM, PARAMS_CSS_CLASS, PARAMS_SOURCE_URL};
27use crate::utils::types::{RUMString, SharedAppState, URLParams, URLPath};
28use crate::{rumtk_web_get_config, rumtk_web_get_text_item, rumtk_web_params_map, ComponentResult, PageConf, RUMWebData, RUMWebTemplate, RUMWebTemplateSafe};
29use rumtk_core::base::RUMResult;
30
31#[derive(RUMWebTemplate, Debug, Clone)]
32#[template(
33    source = "
34        {% if !disable_logo %}
35        <div class='header-{{ css_class }}-navlogo'>
36            <a class='undecorated no-select logo-{{css_class}}-container' href='./'>
37                {{logo|safe}}
38                {{company|safe}}
39            </a>
40        </div>
41        {% endif %}
42        {% if !disable_links %}
43        <div class='header-{{ css_class }}-navactions gap-10'>
44            {% for item in nav_links %}
45                {{item|safe}}
46            {% endfor %}
47        </div>
48        {% else %}
49        {{title|safe}}
50        {% endif %}
51        <div class='header-{{ css_class }}-misc gap-10'>
52        </div>
53    ",
54    ext = "html"
55)]
56pub struct Nav {
57    company: Title,
58    logo: Logo,
59    title: Title,
60    nav_links: Vec<NavLink>,
61    disable_links: bool,
62    disable_logo: bool,
63    css_class: RUMString,
64}
65
66impl RUMWebTemplateSafe for Nav {}
67
68fn get_nav_links(itms: &Vec<(RUMString, PageConf)>, app_state: SharedAppState) -> RUMResult<Vec<NavLink>> {
69    let mut nav_links = Vec::<NavLink>::with_capacity(itms.len());
70    for (k, itm) in itms {
71        nav_links.push(
72            navlink(
73                &[],
74                &RUMWebData::from([
75                    (PARAMS_TITLE.to_string(), k.to_string()),
76                    (PARAMS_TARGET.to_string(), itm.url.to_string()),
77                ]),
78                app_state.clone(),
79            )?,
80        );
81    }
82
83    Ok(nav_links)
84}
85
86#[derive(RUMWebTemplate, Debug, Clone)]
87#[template(
88    source = "
89        {{app_nav}}
90    ",
91    ext = "html"
92)]
93pub struct AppNav {
94    app_nav: Header<Nav>,
95}
96
97impl RUMWebTemplateSafe for AppNav {}
98
99pub fn app_nav<'a>(_path_components: URLPath<'a, 'a>, params: URLParams<'a>, state: SharedAppState) -> ComponentResult<AppNav> {
100    let css_class = rumtk_web_get_text_item!(params, PARAMS_CSS_CLASS, DEFAULT_TEXT_ITEM).to_string();
101
102    let company_params = rumtk_web_params_map!([(
103        PARAMS_TITLE,
104        rumtk_web_get_config!(state).company.as_str()
105    )]);
106    let company = title(
107        _path_components,
108        company_params.get_inner(),
109        state.clone()
110    )?;
111
112    let title_params = rumtk_web_params_map!([(
113                PARAMS_TITLE,
114                rumtk_web_get_config!(state).title.as_str()
115            )]);
116    let title = title(
117            _path_components,
118            title_params.get_inner(),
119            state.clone()
120        )?;
121
122    let links = match &rumtk_web_get_config!(state).router.pages {
123        Some(pages) => {
124            let mut links = Vec::<(RUMString, PageConf)>::with_capacity(pages.len());
125            for (k, v) in pages.iter() {
126                links.push((k.clone(), v.clone()));
127            }
128            links
129        },
130        None => vec![],
131    };
132    let disable_links = rumtk_web_get_config!(state).header_conf.disable_navlinks;
133    let nav_links = get_nav_links(&links, state.clone())?;
134
135    let disable_logo =
136        rumtk_web_get_config!(state).header_conf.disable_logo;
137    let logo_params = rumtk_web_params_map!(
138        [
139            (
140                PARAMS_SOURCE_URL,
141                rumtk_web_get_config!(state).header_conf.logo_source.clone().unwrap_or_default().as_str()
142            ),
143        ]
144    );
145    let logo = logo(_path_components, logo_params.get_inner(), state.clone())?;
146
147    let contents = Nav {
148        company,
149        logo,
150        title,
151        nav_links,
152        disable_logo,
153        disable_links,
154        css_class
155    };
156
157    let app_nav = header(
158        contents,
159        params,
160        state
161    )?;
162
163    Ok(AppNav {
164        app_nav
165    })
166}