rumtk_web/components/form/
form.rs1use crate::components::form::form_node::FormNode;
22use crate::components::title::{title, Title};
23use crate::defaults::{DEFAULT_HTMX_SWAP_MODE, DEFAULT_NO_TEXT, DEFAULT_PROGRESS_MODE, DEFAULT_TEXT_ITEM, PARAMS_CSS_CLASS, PARAMS_ENDPOINT, PARAMS_MODULE, PARAMS_PROGRESS_MODE, PARAMS_SWAP_MODE, PARAMS_TARGET, PARAMS_TITLE, PARAMS_TYPE, SECTION_ENDPOINTS, SECTION_MODULES};
24use crate::utils::types::{RUMString, SharedAppState, URLParams, URLPath};
25use crate::{rumtk_web_get_config, rumtk_web_get_config_section, rumtk_web_get_form, rumtk_web_get_text_item, rumtk_web_params_map, ComponentResult, RUMWebTemplate, RUMWebTemplateSafe};
26
27#[derive(RUMWebTemplate, Debug)]
28#[template(
29 source = "
30 <div id='form-{{htmx_target}}-box'>
31 {% if custom_css_enabled %}
32 <link href='/static/components/form/form.css' rel='stylesheet'>
33 {% endif %}
34 {% if !module.is_empty() %}
35 <script type='module' id='form-script' src='/static/js/forms/form_{{typ}}.js' defer>
36 </script>
37 {% endif %}
38 {{title|safe}}
39 <form id='form-{{htmx_target}}' class='f18 centered form-default-contents gap-10 form-{{css_class}}-contents' role='form' hx-encoding='multipart/form-data' hx-post='{{endpoint}}' aria-label='{{typ}} form' hx-swap='{{htmx_swap_mode}}' hx-target='#form-{{htmx_target}}'>
40 {% for element in elements %}
41 {{ element|safe }}
42 {% endfor %}
43 </form>
44 <script>
45 htmx.on('#form-{{typ}}', 'htmx:xhr:progress', function(evt) {
46 let progressValue = evt.detail.loaded/evt.detail.total * 100;
47 let progressElement = htmx.find('#progress');
48
49 {% if auto_hide_progress %}
50 progressElement.hidden = false;
51 if (progressValue >= 100) {
52 progressElement.hidden = true;
53 }
54 {% endif %}
55
56 progressElement.setAttribute('value', progressValue);
57 });
58 </script>
59 </div>
60 ",
61 ext = "html"
62)]
63pub struct Form {
64 typ: RUMString,
65 title: Title,
66 module: RUMString,
67 endpoint: RUMString,
68 htmx_target: RUMString,
69 htmx_swap_mode: RUMString,
70 elements: Vec<FormNode>,
71 css_class: RUMString,
72 custom_css_enabled: bool,
73 auto_hide_progress: bool,
74}
75
76impl RUMWebTemplateSafe for Form {}
77
78pub fn form<'a>(_path_components: URLPath<'a, 'a>, params: URLParams<'a>, state: SharedAppState) -> ComponentResult<Form> {
79 let typ = rumtk_web_get_text_item!(params, PARAMS_TYPE, DEFAULT_TEXT_ITEM).to_string();
80 let title_str = rumtk_web_get_text_item!(params, PARAMS_TITLE, DEFAULT_NO_TEXT);
81 let module = rumtk_web_get_text_item!(params, PARAMS_MODULE, &typ);
82 let endpoint = rumtk_web_get_text_item!(params, PARAMS_ENDPOINT, &typ);
83 let auto_hide_progress = rumtk_web_get_text_item!(params, PARAMS_PROGRESS_MODE, DEFAULT_PROGRESS_MODE);
84 let htmx_swap_mode = rumtk_web_get_text_item!(params, PARAMS_SWAP_MODE, DEFAULT_HTMX_SWAP_MODE).to_string();
85 let htmx_target = rumtk_web_get_text_item!(params, PARAMS_TARGET, DEFAULT_TEXT_ITEM).to_string();
86 let css_class = rumtk_web_get_text_item!(params, PARAMS_CSS_CLASS, DEFAULT_TEXT_ITEM).to_string();
87
88 let title_params = rumtk_web_params_map!(
89 [(PARAMS_TITLE, title_str)]
90 );
91 let title_elem = title(_path_components, title_params.get_inner(), state.clone())?;
92
93 let module_store = rumtk_web_get_config_section!(state, SECTION_MODULES);
94 let module_name = rumtk_web_get_text_item!(&module_store, module, DEFAULT_NO_TEXT).to_string();
95
96 let endpoint_store = rumtk_web_get_config_section!(state, SECTION_ENDPOINTS);
97 let endpoint_url = rumtk_web_get_text_item!(&endpoint_store, endpoint, endpoint).to_string();
98
99 let custom_css_enabled = rumtk_web_get_config!(state).flags.custom_css;
100
101 let element_results = rumtk_web_get_form!(&typ)?;
102 let mut elements = Vec::<FormNode>::with_capacity(element_results.len());
103
104 for result in element_results {
105 elements.push(result?);
106 }
107
108 Ok(Form {
109 typ,
110 title: title_elem,
111 module: module_name,
112 endpoint: endpoint_url,
113 htmx_target,
114 htmx_swap_mode,
115 elements,
116 css_class,
117 custom_css_enabled,
118 auto_hide_progress: auto_hide_progress == DEFAULT_PROGRESS_MODE,
119 })
120}