Skip to main content

rumtk_web/components/buttons/
goto_button.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::html::{button, script, Button, Script};
22use crate::utils::defaults::{DEFAULT_TEXT_ITEM, PARAMS_CSS_CLASS, PARAMS_FUNCTION,
23};
24use crate::utils::types::{SharedAppState, URLParams, URLPath};
25use crate::{rumtk_web_get_config, rumtk_web_get_text_item, ComponentResult, RUMWebTemplate, RUMWebTemplateSafe, DEFAULT_GOTO_FUNCTION, DEFAULT_SCRIPT_IMPORT, PARAMS_ID, PARAMS_TARGET, PARAMS_TITLE};
26use rumtk_core::strings::{rumtk_format, RUMString};
27
28#[derive(RUMWebTemplate, Debug, Clone)]
29#[template(
30    source = "
31        {% if custom_css_enabled %}
32            <link href='/static/components/goto_button.css' rel='stylesheet'>
33        {% endif %}
34        {{script|safe}}
35        <div id={{id}} class='goto-{{ css_class }}-button-container'>
36            {{button|safe}}
37        </div>
38    ",
39    ext = "html"
40)]
41pub struct GotoButton {
42    id: RUMString,
43    button: Button,
44    script: Script,
45    css_class: RUMString,
46    custom_css_enabled: bool,
47}
48
49impl RUMWebTemplateSafe for GotoButton {}
50
51pub fn goto_button<'a>(
52    _path_components: URLPath<'a, 'a>,
53    params: URLParams<'a>,
54    state: SharedAppState,
55) -> ComponentResult<GotoButton> {
56    let id = rumtk_web_get_text_item!(params, PARAMS_ID, DEFAULT_TEXT_ITEM).to_string();
57    let title = rumtk_web_get_text_item!(params, PARAMS_TITLE, DEFAULT_TEXT_ITEM);
58    let function = rumtk_web_get_text_item!(params, PARAMS_FUNCTION, DEFAULT_GOTO_FUNCTION);
59    let url = rumtk_web_get_text_item!(params, PARAMS_TARGET, DEFAULT_TEXT_ITEM);
60    let css_class = rumtk_web_get_text_item!(params, PARAMS_CSS_CLASS, DEFAULT_TEXT_ITEM).to_string();
61
62    let custom_css_enabled = rumtk_web_get_config!(state).flags.custom_css;
63
64    let button = button(title, function, &rumtk_format!("'{url}'"), state.clone())?;
65    let script = script(DEFAULT_SCRIPT_IMPORT, "goto", true)?;
66
67    Ok(GotoButton {
68        id,
69        button,
70        script,
71        css_class,
72        custom_css_enabled
73    })
74}