use crate::defaults::{DEFAULT_SCRIPT, DEFAULT_SCRIPT_MODULE};
use crate::js::rumtk_web_js_get_item;
use crate::utils::types::RUMString;
use crate::{ComponentResult, RUMWebTemplate};
#[derive(RUMWebTemplate, Debug, Clone)]
#[template(
source = "
{% if typ == DEFAULT_SCRIPT %}
<script type='text/javascript'>{{script|safe}}</script>
{% else if typ == DEFAULT_SCRIPT_MODULE %}
<script type='module'>{{script|safe}}</script>
{% else %}
<script type='module' src='{{script}}' async defer></script>
{% endif %}
",
ext = "html"
)]
pub struct Script {
typ: RUMString,
script: RUMString,
}
pub fn script<'a>(typ: &str, contents: &str, global: bool) -> ComponentResult<Script> {
if !typ.is_empty() || typ != DEFAULT_SCRIPT_MODULE {
Ok(Script {
typ: typ.to_string(),
script: match global {
true => rumtk_web_js_get_item(contents),
false => Ok(contents.to_string()),
}?,
})
} else {
Ok(Script {
typ: typ.to_string(),
script: contents.to_string(),
})
}
}