-
✖
-
- {{ context.tr.t(Sentence::CalendarUnschedule) }}
-
-
+
✖{{ context.tr.t(Sentence::CalendarUnschedule) }}
diff --git a/frontend/src/calendar.rs b/frontend/src/calendar.rs
index 23f5583..59de545 100644
--- a/frontend/src/calendar.rs
+++ b/frontend/src/calendar.rs
@@ -167,22 +167,28 @@ pub fn setup(
EventListener::new(&days, "click", move |event| {
let target: Element = event.target().unwrap().dyn_into().unwrap();
- if target.class_name() == "number" && options.can_select_date {
- let first_day = first_grid_day(
- state_clone.get_displayed_date(),
- options.first_day_of_the_week,
- );
- let day_grid_id = target.parent_element().unwrap().id();
- let day_offset = day_grid_id[9..10].parse::
().unwrap() * 7
- + day_grid_id[10..11].parse::().unwrap();
- state_clone.set_selected_date(first_day + Days::new(day_offset));
- display_month(
- &calendar_clone,
- state_clone.clone(),
- options,
- recipe_scheduler,
- );
- } else if target.class_name().contains("remove-scheduled-recipe") {
+ // FIXME: Replace by an if let + condition when available in stable.
+ match target.selector_upwards::(".day") {
+ Some(element) if options.can_select_date => {
+ let first_day = first_grid_day(
+ state_clone.get_displayed_date(),
+ options.first_day_of_the_week,
+ );
+ let day_grid_id = element.id();
+ let day_offset = day_grid_id[9..10].parse::().unwrap() * 7
+ + day_grid_id[10..11].parse::().unwrap();
+ state_clone.set_selected_date(first_day + Days::new(day_offset));
+ display_month(
+ &calendar_clone,
+ state_clone.clone(),
+ options,
+ recipe_scheduler,
+ );
+ }
+ _ => (),
+ }
+
+ if target.class_name().contains("remove-scheduled-recipe") {
spawn_local(async move {
// Element id format example: "scheduled-recipe-1-2025-05-15".
let element_id = target.parent_element().unwrap().id();
@@ -275,27 +281,20 @@ fn display_month(
let day_content: Element = day_element.selector(".number");
day_content.set_inner_html(¤t.day().to_string());
- let mut class_name = String::new();
+ let mut classes = vec!["day".to_string()];
if current.month() == date.month() {
- if !class_name.is_empty() {
- class_name += " ";
- }
- class_name += "current-month";
+ classes.push("current-month".to_string());
}
if current == Local::now().date_naive() {
- if !class_name.is_empty() {
- class_name += " ";
- }
- class_name += "today";
+ classes.push("today".to_string());
}
+
if options.can_select_date && current == state.get_selected_date() {
- if !class_name.is_empty() {
- class_name += " ";
- }
- class_name += "selected-day"
+ classes.push("selected-day".to_string());
}
- day_element.set_class_name(&class_name);
+
+ day_element.set_class_name(&classes.join(" "));
current = current + Days::new(1);
}
diff --git a/frontend/src/utils.rs b/frontend/src/utils.rs
index ea68d51..e5929d4 100644
--- a/frontend/src/utils.rs
+++ b/frontend/src/utils.rs
@@ -14,6 +14,10 @@ pub trait SelectorExt {
where
T: JsCast;
+ fn selector_upwards(&self, selector: &str) -> Option
+ where
+ T: JsCast;
+
fn deep_clone(&self) -> Self;
}
@@ -41,6 +45,25 @@ impl SelectorExt for Element {
.collect()
}
+ fn selector_upwards(&self, selector: &str) -> Option
+ where
+ T: JsCast,
+ {
+ let mut current = self.clone();
+ // while let Some(parent) = current.parent_element() {
+ loop {
+ if let Ok(true) = current.matches(selector) {
+ return Some(current.dyn_into::().unwrap());
+ }
+
+ if let Some(parent) = current.parent_element() {
+ current = parent;
+ } else {
+ return None;
+ }
+ }
+ }
+
fn deep_clone(&self) -> Self {
self.clone_node_with_deep(true)
.unwrap()