Skip to main content

rumtk_web/components/form/
form.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::form::form_node::FormNode;
22use 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_TITLE, PARAMS_TYPE, SECTION_ENDPOINTS, SECTION_MODULES};
23use crate::utils::types::{RUMString, SharedAppState, URLParams, URLPath};
24use crate::{rumtk_web_get_config, rumtk_web_get_config_section, rumtk_web_get_form, rumtk_web_get_text_item, ComponentResult, RUMWebTemplate, RUMWebTemplateSafe};
25use rumtk_core::rumtk_generate_id;
26
27#[derive(RUMWebTemplate, Debug)]
28#[template(
29    source = "
30        <div class='form-{{css_class}}-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' async defer>
36                </script>
37            {% endif %}
38            <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}}'>
39                <h1>{{ title }}</h1>
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: RUMString,
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_generate_id!().to_string();
86    let css_class = rumtk_web_get_text_item!(params, PARAMS_CSS_CLASS, DEFAULT_TEXT_ITEM).to_string();
87
88    let module_store = rumtk_web_get_config_section!(state, SECTION_MODULES);
89    let module_name = rumtk_web_get_text_item!(&module_store, module, DEFAULT_NO_TEXT).to_string();
90
91    let endpoint_store = rumtk_web_get_config_section!(state, SECTION_ENDPOINTS);
92    let endpoint_url = rumtk_web_get_text_item!(&endpoint_store, endpoint, endpoint).to_string();
93
94    let custom_css_enabled = rumtk_web_get_config!(state).flags.custom_css;
95
96    let element_results = rumtk_web_get_form!(&typ)?;
97    let mut elements = Vec::<FormNode>::with_capacity(element_results.len());
98
99    for result in element_results {
100        elements.push(result?);
101    }
102
103    Ok(Form {
104        typ,
105        title: title_str.to_string(),
106        module: module_name,
107        endpoint: endpoint_url,
108        htmx_target,
109        htmx_swap_mode,
110        elements,
111        css_class,
112        custom_css_enabled,
113        auto_hide_progress: auto_hide_progress == DEFAULT_PROGRESS_MODE,
114    })
115}