diff --git a/pagetop-jquery/src/lib.rs b/pagetop-jquery/src/lib.rs index 216db02d..d3df0573 100644 --- a/pagetop-jquery/src/lib.rs +++ b/pagetop-jquery/src/lib.rs @@ -2,10 +2,11 @@ use pagetop::prelude::*; use_handle!(MODULE_JQUERY); +use_locale!(LOCALE_JQUERY); + use_static!(jquery); const JQUERY_PARAM: &str = "jquery.add"; -const JQUERY_SOURCE: &str = "/jquery/3.6.0/jquery.min.js"; pub struct JQuery; @@ -14,6 +15,18 @@ impl ModuleTrait for JQuery { MODULE_JQUERY } + fn actions(&self) -> Vec { + vec![action!(ActionBeforeRenderPage => before_render_page)] + } + + fn name(&self) -> L10n { + L10n::t("module_name", &LOCALE_JQUERY) + } + + fn description(&self) -> L10n { + L10n::t("module_description", &LOCALE_JQUERY) + } + fn configure_service(&self, cfg: &mut service::web::ServiceConfig) { serve_static_files!(cfg, "/jquery", jquery); } @@ -21,23 +34,20 @@ impl ModuleTrait for JQuery { impl JQuery { pub fn add_jquery(rcx: &mut RenderContext) { - match rcx.get_param::(JQUERY_PARAM) { - Some(true) => {} - _ => { - rcx.alter(ContextOp::AddJavaScript( - JavaScript::located(JQUERY_SOURCE) - .with_weight(isize::MIN) - .with_mode(ModeJS::Normal), - )); - rcx.set_param::(JQUERY_PARAM, true); - } - } + rcx.set_param::(JQUERY_PARAM, true); } pub fn remove_jquery(rcx: &mut RenderContext) { - if let Some(true) = rcx.get_param::(JQUERY_PARAM) { - rcx.alter(ContextOp::RemoveJavaScript(JQUERY_SOURCE)); - rcx.set_param::(JQUERY_PARAM, false); - } + rcx.set_param::(JQUERY_PARAM, false); + } +} + +fn before_render_page(page: &mut Page) { + if let Some(true) = page.context().get_param::(JQUERY_PARAM) { + page.context().alter(ContextOp::AddJavaScript( + JavaScript::located("/jquery/3.6.0/jquery.min.js") + .with_weight(isize::MIN) + .with_mode(ModeJS::Normal), + )); } } diff --git a/pagetop-jquery/src/locale/en-US/module.flt b/pagetop-jquery/src/locale/en-US/module.flt new file mode 100644 index 00000000..daa96fa3 --- /dev/null +++ b/pagetop-jquery/src/locale/en-US/module.flt @@ -0,0 +1,2 @@ +module_name = jQuery support +module_description = Integrate the jQuery library into web pages generated by other modules. diff --git a/pagetop-jquery/src/locale/es-ES/module.flt b/pagetop-jquery/src/locale/es-ES/module.flt new file mode 100644 index 00000000..df1c7b93 --- /dev/null +++ b/pagetop-jquery/src/locale/es-ES/module.flt @@ -0,0 +1,2 @@ +module_name = Soporte a jQuery +module_description = Incorpora la librería jQuery en páginas web generadas por otros módulos.