Skip to main content

rumtk_web/components/
content_viewer.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::defaults::{DEFAULT_NO_TEXT, PARAMS_ID, PARAMS_TYPE};
22use crate::utils::defaults::{
23    DEFAULT_TEXT_ITEM, FORM_DATA_TYPE_HTML, FORM_DATA_TYPE_PDF, PARAMS_CONTENTS, PARAMS_CSS_CLASS,
24};
25use crate::utils::types::{SharedAppState, URLParams, URLPath};
26use crate::{rumtk_web_get_config, rumtk_web_get_text_item, ComponentResult, RUMWebTemplate};
27use rumtk_core::rumtk_generate_id;
28use rumtk_core::strings::RUMString;
29
30#[derive(RUMWebTemplate, Debug)]
31#[template(
32    source = "
33        {% if custom_css_enabled %}
34            <link href='/static/components/content_viewer.css' rel='stylesheet'>
35        {% endif %}
36        {% if typ == FORM_DATA_TYPE_PDF %}
37            <object data='{{contents}}' type='application/pdf' class='f18 contact-card-{{ css_class }}-container'>
38                <!-- Fallback content for browsers that cannot display the PDF inline -->
39                <p>It appears your browser doesn't have a PDF plugin. No problem, you can
40                <a href='{{contents}}'>click here to download the PDF file.</a></p>
41            </object>
42        {% else if typ == FORM_DATA_TYPE_HTML %}
43            <div id='div-{{id}}' class='f18 contact-card-{{ css_class }}-container'></div>
44            <script>
45                const contentDiv = document.getElementById('div-{{id}}');
46                 // Set the innerHTML to a string containing HTML markup
47                 contentDiv.innerHTML = '{{contents|safe}}';
48            </script>
49        {% else %}
50            <pre id='div-{{id}}' class='f18 contact-card-{{ css_class }}-container'>
51                {{contents}}
52            </pre>
53        {% endif %}
54    ",
55    ext = "html"
56)]
57pub struct ContentViewer<'a> {
58    id: RUMString,
59    typ: &'a str,
60    contents: &'a str,
61    css_class: RUMString,
62    custom_css_enabled: bool,
63}
64
65pub fn content_viewer<'a>(
66    _path_components: URLPath<'a, 'a>,
67    params: URLParams<'a>,
68    state: SharedAppState,
69) -> ComponentResult<ContentViewer<'a>> {
70    let default_id = rumtk_generate_id!();
71    let id = rumtk_web_get_text_item!(params, PARAMS_ID, default_id.as_str()).to_string();
72    let typ = rumtk_web_get_text_item!(params, PARAMS_TYPE, DEFAULT_TEXT_ITEM);
73    let contents = rumtk_web_get_text_item!(params, PARAMS_CONTENTS, DEFAULT_NO_TEXT);
74    let css_class = rumtk_web_get_text_item!(params, PARAMS_CSS_CLASS, DEFAULT_TEXT_ITEM).to_string();
75
76    let custom_css_enabled = rumtk_web_get_config!(state).flags.custom_css;
77
78    Ok(ContentViewer {
79        id,
80        typ,
81        contents,
82        css_class,
83        custom_css_enabled
84    })
85}