From e033630d2ac1900a4ab00920c3bf39d44cff9682 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Mon, 26 Jun 2023 21:23:48 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20[jquery]=20Preparando=20el=20m?= =?UTF-8?q?=C3=B3dulo=20para=20un=20tutorial?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop-jquery/src/lib.rs | 42 +++++++++++++--------- pagetop-jquery/src/locale/en-US/module.flt | 2 ++ pagetop-jquery/src/locale/es-ES/module.flt | 2 ++ 3 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 pagetop-jquery/src/locale/en-US/module.flt create mode 100644 pagetop-jquery/src/locale/es-ES/module.flt 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.