rumtk_web/components/buttons/
goto_button.rs1use 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}