🚧 [jquery] Preparando el módulo para un tutorial

This commit is contained in:
Manuel Cillero 2023-06-26 21:23:48 +02:00
parent 7138b9c58b
commit e033630d2a
3 changed files with 30 additions and 16 deletions

View file

@ -2,10 +2,11 @@ use pagetop::prelude::*;
use_handle!(MODULE_JQUERY); use_handle!(MODULE_JQUERY);
use_locale!(LOCALE_JQUERY);
use_static!(jquery); use_static!(jquery);
const JQUERY_PARAM: &str = "jquery.add"; const JQUERY_PARAM: &str = "jquery.add";
const JQUERY_SOURCE: &str = "/jquery/3.6.0/jquery.min.js";
pub struct JQuery; pub struct JQuery;
@ -14,6 +15,18 @@ impl ModuleTrait for JQuery {
MODULE_JQUERY MODULE_JQUERY
} }
fn actions(&self) -> Vec<Action> {
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) { fn configure_service(&self, cfg: &mut service::web::ServiceConfig) {
serve_static_files!(cfg, "/jquery", jquery); serve_static_files!(cfg, "/jquery", jquery);
} }
@ -21,23 +34,20 @@ impl ModuleTrait for JQuery {
impl JQuery { impl JQuery {
pub fn add_jquery(rcx: &mut RenderContext) { pub fn add_jquery(rcx: &mut RenderContext) {
match rcx.get_param::<bool>(JQUERY_PARAM) { rcx.set_param::<bool>(JQUERY_PARAM, true);
Some(true) => {}
_ => {
rcx.alter(ContextOp::AddJavaScript(
JavaScript::located(JQUERY_SOURCE)
.with_weight(isize::MIN)
.with_mode(ModeJS::Normal),
));
rcx.set_param::<bool>(JQUERY_PARAM, true);
}
}
} }
pub fn remove_jquery(rcx: &mut RenderContext) { pub fn remove_jquery(rcx: &mut RenderContext) {
if let Some(true) = rcx.get_param::<bool>(JQUERY_PARAM) { rcx.set_param::<bool>(JQUERY_PARAM, false);
rcx.alter(ContextOp::RemoveJavaScript(JQUERY_SOURCE)); }
rcx.set_param::<bool>(JQUERY_PARAM, false); }
}
fn before_render_page(page: &mut Page) {
if let Some(true) = page.context().get_param::<bool>(JQUERY_PARAM) {
page.context().alter(ContextOp::AddJavaScript(
JavaScript::located("/jquery/3.6.0/jquery.min.js")
.with_weight(isize::MIN)
.with_mode(ModeJS::Normal),
));
} }
} }

View file

@ -0,0 +1,2 @@
module_name = jQuery support
module_description = Integrate the jQuery library into web pages generated by other modules.

View file

@ -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.