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