leptonic 0.5.0

The Leptos component library.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use leptos::*;
use time::macros::format_description;
use uuid::Uuid;

use crate::datetime::{
    is_in_range, start_of_next_month, start_of_previous_month, whole_days_in, Day, GuideMode,
    InMonth, Month, SaveReplaceYear, Week, Year,
};

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum Selection {
    Year,
    Month,
    Day,
}

#[component]
#[allow(clippy::too_many_lines)]
pub fn DateSelector(
    value: time::OffsetDateTime,
    #[prop(into)] on_change: Callback<time::OffsetDateTime>,
    #[prop(optional)] min: Option<time::OffsetDateTime>,
    #[prop(optional)] max: Option<time::OffsetDateTime>,
    #[prop(into, optional, default = GuideMode::CalendarFirst.into())] guide_mode: MaybeSignal<
        GuideMode,
    >,
) -> impl IntoView {
    let (staging, set_staging) = create_signal(value);

    let staging_year = Signal::derive(move || staging.get().year());
    let staging_month = Signal::derive(move || staging.get().month().to_string());

    let selected: Memo<time::OffsetDateTime> = create_memo(move |_| staging.get());
    create_effect(move |_| on_change.call(selected.get()));

    let (show, set_show) = create_signal(match guide_mode.get() {
        GuideMode::CalendarFirst => Selection::Day,
        GuideMode::YearFirst => Selection::Year,
    });

    let (years_starting_at, set_years_starting_at) = create_signal(None);
    let years = Signal::derive(move || {
        staging.with(|staging| {
            create_years(
                *staging,
                years_starting_at.get(),
                min.as_ref(),
                max.as_ref(),
            )
        })
    });
    let years_range = Signal::derive(move || {
        years.with(|years| {
            if years.is_empty() {
                "ERR: no years".to_owned()
            } else {
                format!("{} - {}", years[0].number, years[years.len() - 1].number)
            }
        })
    });
    let months = Signal::derive(move || {
        staging.with(|staging| create_months(*staging, min.as_ref(), max.as_ref()))
    });
    // TODO: Make static. Make i18n.
    let (short_weekday_names, _) = create_signal(create_week_day_names());
    let weeks = Signal::derive(move || create_weeks(&staging.get(), min.as_ref(), max.as_ref()));

    let select_previous_month =
        move |_| set_staging.update(|staging| *staging = start_of_previous_month(*staging));
    let select_next_month =
        move |_| set_staging.update(|staging| *staging = start_of_next_month(*staging));
    let select_previous_year = move |_| {
        set_staging
            .update(|staging| *staging = staging.save_replace_year(staging.year() - 1).unwrap());
        // TODO: Set set_years_starting_at to Some(self.staging.year() - 4),
    };
    let select_next_year = move |_| {
        set_staging
            .update(|staging| *staging = staging.save_replace_year(staging.year() + 1).unwrap());
        // TODO: Set set_years_starting_at to Some(self.staging.year() - 4),
    };
    let select_previous_years = move |_| {
        years.with(|years| {
            set_years_starting_at.update(|starting| {
                *starting = match years.len() {
                    0 => None,
                    _ => Some(years[0].number - (3 * 7)),
                }
            });
        });
    };
    let select_next_years = move |_| {
        years.with(|years| {
            set_years_starting_at.update(|starting| {
                *starting = match years.len() {
                    0 => None,
                    _ => Some(years[years.len() - 1].number + 1),
                }
            });
        });
    };
    let select_year = move |year: Year| {
        if !year.disabled {
            set_staging
                .update(|staging| *staging = staging.save_replace_year(year.number).unwrap());
            set_show.update(|show| *show = Selection::Month);
        }
    };
    let select_month = move |month: Month| {
        if !month.disabled {
            set_staging.update(|staging| {
                *staging = staging
                    .save_replace_month(time::Month::try_from(month.index).unwrap())
                    .unwrap();
            });
            set_show.update(|show| *show = Selection::Day);
        }
    };
    let select_day = move |day: Day| {
        if !day.disabled {
            set_staging.update(|staging| *staging = day.date_time);
        }
    };

    view! {
        <leptonic-datetime>
        <leptonic-date-selector>
            <leptonic-calender-month>
                <div class={"actions"}>
                    {move || match show.get() {
                        Selection::Year => view! {
                            <div on:click=select_previous_years
                                class="previous arrow-left">
                            </div>
                            <div on:click=move |_| set_show.update(|show| *show = Selection::Month)
                                class="current-date">
                                {years_range}
                            </div>
                            <div on:click=select_next_years
                                class="next arrow-right">
                            </div>
                        },
                        Selection::Month => view! {
                            <div on:click=select_previous_year
                                class="previous arrow-left">
                            </div>
                            <div on:click=move |_| set_show.update(|show| *show = Selection::Year)
                                class="current-date">
                                {staging_year}
                            </div>
                            <div on:click=select_next_year
                                class="next arrow-right">
                            </div>
                        },
                        Selection::Day => view! {
                            <div on:click=select_previous_month
                                class="previous arrow-left">
                            </div>
                            <div on:click=move |_| set_show.update(|show| *show = Selection::Year)
                                class="current-date">
                                {staging_month} " " {staging_year}
                            </div>
                            <div on:click=select_next_month
                                class="next arrow-right">
                            </div>
                        },
                    }}
                </div>

                <Show when=move || show.get() == Selection::Year fallback=|| ()>
                    <div class="years">
                        <For
                            each=move || years.get()
                            key=|year| year.number
                            children=move |year| {
                                view! {
                                    <div on:click=move |_e| select_year(year)
                                        class="year"
                                        class:is-staging=year.is_staging
                                        class:is-now=year.is_now
                                        class:disabled=year.disabled
                                    >
                                        {year.number}
                                    </div>
                                }
                            }
                        />
                    </div>
                </Show>

                <Show when=move || show.get() == Selection::Month fallback=|| ()>
                    <div class="months">
                        <For
                            each=move || months.get()
                            key=|month| month.index
                            children=move |month| {
                                let month_clone = month.clone();
                                view! {
                                    <div on:click=move |_e| select_month(month_clone.clone())
                                        class="month"
                                        class:is-staging=month.is_staging
                                        class:is-now=month.is_now
                                        class:disabled=month.disabled>
                                        {month.name}
                                    </div>
                                }
                            }
                        />
                    </div>
                </Show>

                <Show when=move || show.get() == Selection::Day fallback=|| ()>
                    <div class={"weekday-names"}>
                        // Not use For for this...?
                        <For
                            each=move || short_weekday_names.get()
                            key=|short_weekday_name| short_weekday_name.clone()
                            children=move |short_weekday_name| {
                                view! {
                                    <div class={"weekday-name"}>
                                        {short_weekday_name}
                                    </div>
                                }
                            }
                        />
                    </div>

                    <div class={"weeks"}>
                        <For
                            each=move || weeks.get()
                            key=|week| week.id
                            children=move |week| {
                                view! {
                                    <div class="week">
                                        <For
                                            each=move || week.days.clone()
                                            key=|day| day.id
                                            children=move |day| {
                                                view! {
                                                    <div
                                                        on:click=move |_e| select_day(day)
                                                        class="day"
                                                        class:is-staging=day.is_staging
                                                        class:is-now=day.is_now
                                                        class:not-in-month=day.in_month != InMonth::Current
                                                        class:disabled=day.disabled
                                                    >
                                                        <span class="text">
                                                            {day.index}
                                                        </span>
                                                    </div>
                                                }
                                            }
                                        />
                                    </div>
                                }
                            }
                        />
                    </div>
                </Show>

            </leptonic-calender-month>
        </leptonic-date-selector>
        </leptonic-datetime>
    }
}

pub fn create_week_day_names() -> Vec<String> {
    //let day_in_month = value.date().day(); // 1 based
    //let days_from_monday = value.date().weekday().num_days_from_monday(); // 0 based
    //let monday = value.date().with_day(day_in_month - days_from_monday).format("%a").to_string();

    vec![
        "Mon".to_owned(),
        "Tue".to_owned(),
        "Wed".to_owned(),
        "Thr".to_owned(),
        "Fri".to_owned(),
        "Sat".to_owned(),
        "Sun".to_owned(),
    ]
}

pub fn create_years(
    staging: time::OffsetDateTime,
    starting_year: Option<i32>,
    min: Option<&time::OffsetDateTime>,
    max: Option<&time::OffsetDateTime>,
) -> Vec<Year> {
    let amount = 3 * 5; // 5 rows of 3 year numbers each.
    let starting_year = starting_year.unwrap_or_else(|| staging.year() - 4);
    let mut years = Vec::<Year>::with_capacity(amount);
    let now = time::OffsetDateTime::now_utc();
    let this_year = now.year();
    let staging_year = staging.year();
    let min_year = min.map(|it| it.year()).unwrap_or(i32::MIN);
    let max_year = max.map(|it| it.year()).unwrap_or(i32::MAX);

    for i in 0..amount {
        let year_number = starting_year + i as i32;
        years.push(Year {
            number: year_number,
            is_staging: year_number == staging_year,
            is_now: year_number == this_year,
            disabled: year_number < min_year || year_number > max_year,
        });
    }
    years
}

pub fn create_months(
    staging: time::OffsetDateTime,
    min: Option<&time::OffsetDateTime>,
    max: Option<&time::OffsetDateTime>,
) -> Vec<Month> {
    let now = time::OffsetDateTime::now_utc();
    let this_year = now.year();
    let this_month = now.month();
    let staging_year = staging.year();
    let staging_month = staging.month();
    let mut months = Vec::<Month>::with_capacity(12);
    for i in 1..=12u8 {
        let month: time::OffsetDateTime = staging
            .save_replace_month(time::Month::try_from(i).unwrap())
            .unwrap();
        let month_year = month.year();
        let month_month = month.month();
        months.push(Month {
            index: i,
            name: month.format(format_description!("[month]")).unwrap(), // TODO: format_description does not work!
            is_staging: staging_year == month_year && staging_month == month_month,
            is_now: this_year == month_year && this_month == month_month,
            disabled: !is_in_range(&month, max, min),
        });
    }
    assert_eq!(months.len(), 12);
    months
}

pub fn create_weeks(
    staging: &time::OffsetDateTime,
    min: Option<&time::OffsetDateTime>,
    max: Option<&time::OffsetDateTime>,
) -> Vec<Week> {
    const WEEKS_TO_DISPLAY: u8 = 6;
    const DAYS_PER_WEEK: u8 = 7;

    let now = time::OffsetDateTime::now_utc();
    // Calculate the index of the first day of the month (in current locale).

    let current_year = now.year();
    let current_month = now.month();
    let current_day = now.day();
    let staging_day = staging.day();

    let first_weekday_index = (*staging)
        .replace_day(1)
        .unwrap()
        .weekday()
        .number_days_from_monday(); // in range [0..6]
    let number_of_days_in_month = whole_days_in(staging.year(), staging.month());
    let index_of_last_day_in_month = first_weekday_index + number_of_days_in_month;

    let prev_month = start_of_previous_month(*staging);
    let this_month = staging;
    let next_month = start_of_next_month(*staging);

    let days_in_previous_month = whole_days_in(prev_month.year(), prev_month.month());

    let mut weeks = Vec::<Week>::with_capacity(WEEKS_TO_DISPLAY as usize);
    for w in 0..WEEKS_TO_DISPLAY {
        let mut week = Week {
            id: Uuid::new_v4(),
            days: Vec::with_capacity(DAYS_PER_WEEK as usize),
        };
        for d in 0..DAYS_PER_WEEK {
            let i = d + w * DAYS_PER_WEEK;

            let in_month = if i < first_weekday_index {
                InMonth::Previous
            } else if i >= first_weekday_index && i < index_of_last_day_in_month {
                InMonth::Current
            } else {
                InMonth::Next
            };

            // base 1 (!)
            let day_in_month = match in_month {
                InMonth::Previous => days_in_previous_month - first_weekday_index + i + 1,
                InMonth::Current => i - first_weekday_index + 1,
                InMonth::Next => i - index_of_last_day_in_month + 1,
            };
            let relevant_month = match in_month {
                InMonth::Previous => &prev_month,
                InMonth::Current => this_month,
                InMonth::Next => &next_month,
            };
            let date_time: time::OffsetDateTime = relevant_month.replace_day(day_in_month).unwrap();
            let disabled = !is_in_range(&date_time, max, min); // TODO: inversion of min/max correct?
            let selected = in_month == InMonth::Current && day_in_month == staging_day; // TODO: Can a day form prev not be selected?

            week.days.push(Day {
                id: Uuid::new_v4(),
                index: day_in_month,
                in_month,
                date_time,
                disabled,
                highlighted: false,
                is_staging: selected,
                // TODO: is year check necessary?
                is_now: current_month == relevant_month.month()
                    && current_year == relevant_month.year()
                    && current_day == day_in_month,
            });
        }
        weeks.push(week);
    }
    weeks
}