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