Add first day of the week feature to user settings and calendar functionality

This commit is contained in:
Greg Burri 2025-04-19 12:01:46 +02:00
parent 39f5b968b4
commit fdbf2e4f27
15 changed files with 191 additions and 42 deletions

View file

@ -66,6 +66,7 @@ impl CalendarState {
pub struct CalendarOptions {
pub can_select_date: bool,
pub with_link_and_remove: bool,
pub first_day_of_the_week: Weekday,
}
pub fn setup(
@ -118,7 +119,10 @@ pub fn setup(
// gloo::console::log!(event); // TODO: Remove.
if target.class_name() == "number" && options.can_select_date {
let first_day = first_grid_day(state_clone.get_displayed_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::<u64>().unwrap() * 7
+ day_grid_id[10..11].parse::<u64>().unwrap();
@ -212,7 +216,7 @@ fn display_month(
}
}
let first_day = first_grid_day(date);
let first_day = first_grid_day(date, options.first_day_of_the_week);
let mut current = first_day;
for i in 0..NB_CALENDAR_ROW {
@ -302,11 +306,11 @@ fn display_month(
});
}
fn first_grid_day(mut date: NaiveDate) -> NaiveDate {
fn first_grid_day(mut date: NaiveDate, first_day_of_the_week: Weekday) -> NaiveDate {
while (date - Days::new(1)).month() == date.month() {
date = date - Days::new(1);
}
while date.weekday() != Weekday::Mon {
while date.weekday() != first_day_of_the_week {
date = date - Days::new(1);
}
date