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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use chrono::{prelude::*, Duration};
use config::PickerConfig;
use num_traits::FromPrimitive;
use seed::{prelude::*, *};
use style_names::{
    BODY, BUTTON, CLOSE, GRID_HEADER, HEADER, NEXT, OTHER_MONTH, PREVIOUS, SEED_DATEPICKER,
    SELECTABLE, SELECTED, TITLE, UNAVAILABLE,
};
use year_month::{year_group_end, year_group_range, year_group_start, YearMonth};

pub mod config;
mod style_names;
mod year_month;

#[macro_use]
extern crate derive_getters;

#[macro_use]
extern crate derive_builder;

/// Types of views for the datepicker.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum DialogViewType {
    /// YEARS_IN_YEAR_SELECTION Years, from a year which modulo `% 20 == 0`
    Years = 1,
    /// 1 full year with the selection of a month
    Months = 2,
    /// 1 full month with the selection of a day
    Days = 3,
}

impl Default for DialogViewType {
    fn default() -> Self {
        DialogViewType::Days
    }
}

/// `Model` describes the current datepicker state.
pub struct Model {
    /// value of the date that is selected
    selected_date: Option<NaiveDate>,

    /// whether the dialog is shown
    dialog_opened: bool,

    /// viewed time range
    year_month_info: YearMonth,

    /// dialog type
    dialog_view_type: DialogViewType,

    /// dialog position style, describing the position of the dialog
    dialog_position_style: Option<Style>,

    /// configuration of the picker, should be passed in during init and not modified later
    config: PickerConfig,
}

impl Model {
    /// selected value of the datepicker
    pub fn selected_date(&self) -> &Option<NaiveDate> {
        &self.selected_date
    }

    pub fn config(&self) -> &PickerConfig {
        &self.config
    }
}

/// `init` describes what should happen when your app started.
pub fn init<Ms: 'static>(
    _: Url,
    _: &mut impl Orders<Ms>,
    config: PickerConfig,
    _to_msg: impl FnOnce(Msg) -> Ms + Clone + 'static,
) -> Model {
    Model {
        selected_date: *config.initial_date(),
        dialog_opened: *config.initially_opened(),
        year_month_info: config.guess_allowed_year_month(),
        dialog_view_type: *config.initial_view_type(),
        dialog_position_style: None,
        config,
    }
}

/// `Msg` describes the different events you can modify state with.
pub enum Msg {
    DateSelected(NaiveDate),
    MonthSelected(Month),
    YearSelected(i32),
    /// open the dialog, optionally at the given (left, top) position
    OpenDialog(Option<(String, String)>),
    CloseDialog,
    PreviousButtonClicked,
    NextButtonClicked,

    /// clicks on the dialog title change the `DialogViewType`
    DialogTitleClicked,
}

/// `update` describes how to handle each `Msg`.
pub fn update<Ms: 'static>(
    msg: Msg,
    model: &mut Model,
    orders: &mut impl Orders<Ms>,
    on_change: Ms,
    to_msg: impl FnOnce(Msg) -> Ms + Clone + 'static,
) {
    match msg {
        Msg::DateSelected(new_date) => {
            model.selected_date = Some(new_date);
            model.year_month_info = new_date.into();
            orders.send_msg(to_msg(Msg::CloseDialog));
            orders.send_msg(on_change);
        }
        Msg::MonthSelected(new_month) => {
            if model.config.selection_type() == &DialogViewType::Months {
                let new_date = NaiveDate::from_ymd(
                    model.year_month_info.year,
                    new_month.number_from_month(),
                    1,
                );
                orders.send_msg(to_msg(Msg::DateSelected(new_date)));
            } else {
                model.dialog_view_type = DialogViewType::Days;
                model.year_month_info.month = new_month;
            }
        }
        Msg::YearSelected(new_year) => {
            if model.config.selection_type() == &DialogViewType::Years {
                let new_date = NaiveDate::from_ymd(new_year, 1, 1);
                orders.send_msg(to_msg(Msg::DateSelected(new_date)));
            } else {
                model.dialog_view_type = DialogViewType::Months;
                model.year_month_info.year = new_year;
            }
        }
        Msg::OpenDialog(position) => {
            model.dialog_opened = true;
            if let Some((left, top)) = position {
                model.dialog_position_style = Some(style! {
                    St::Left => left,
                    St::Top => top,
                });
            }
        }
        Msg::CloseDialog => model.dialog_opened = false,
        Msg::PreviousButtonClicked => {
            model.year_month_info = match model.dialog_view_type {
                DialogViewType::Days => model.year_month_info.previous_month(),
                DialogViewType::Months => model.year_month_info.previous_year(),
                DialogViewType::Years => model.year_month_info.previous_year_group(),
            };
        }
        Msg::NextButtonClicked => {
            model.year_month_info = match model.dialog_view_type {
                DialogViewType::Days => model.year_month_info.next_month(),
                DialogViewType::Months => model.year_month_info.next_year(),
                DialogViewType::Years => model.year_month_info.next_year_group(),
            };
        }
        Msg::DialogTitleClicked => {
            model.dialog_view_type = match model.dialog_view_type {
                DialogViewType::Days => DialogViewType::Months,
                DialogViewType::Months => DialogViewType::Years,
                DialogViewType::Years => DialogViewType::Years,
            }
        }
    };
}

/// `view` describes what to display.
pub fn view<Ms: 'static>(
    model: &Model,
    to_msg: impl FnOnce(Msg) -> Ms + Clone + 'static,
) -> Node<Ms> {
    IF!(model.dialog_opened => div![
        C![SEED_DATEPICKER],
        model.dialog_position_style.as_ref(),
        view_dialog_header(model, to_msg.clone()),
        view_dialog_body(model, to_msg),
    ])
    .unwrap_or(empty![])
}

fn view_dialog_header<Ms: 'static>(
    model: &Model,
    to_msg: impl FnOnce(Msg) -> Ms + Clone + 'static,
) -> Node<Ms> {
    div![
        C![HEADER],
        button![
            C![BUTTON, PREVIOUS],
            style! {
                St::Visibility => if should_display_previous_button(model) { "visible" } else {"hidden"},
            },
            "«",
            ev(Ev::Click, {
                let to_msg = to_msg.clone();
                |_| to_msg(Msg::PreviousButtonClicked)
            }),
        ],
        span![
            C![TITLE],
            attrs! {
                At::from("role") => "heading",
            },
            create_dialog_title_text(model),
            ev(Ev::Click, {
                let to_msg = to_msg.clone();
                |_| to_msg(Msg::DialogTitleClicked)
            }),
        ],
        button![
            C![BUTTON, NEXT],
            style! {
                St::Visibility => if should_display_next_button(model) { "visible" } else { "hidden" },
            },
            "»",
            ev(Ev::Click, {
                let to_msg = to_msg.clone();
                |_| to_msg(Msg::NextButtonClicked)
            }),
        ],
        button![
            C![BUTTON, CLOSE],
            "x",
            ev(Ev::Click, |_| to_msg(Msg::CloseDialog)),
        ],
    ]
}

fn create_dialog_title_text(model: &Model) -> String {
    match model.dialog_view_type {
        DialogViewType::Days => model
            .year_month_info
            .first_day_of_month()
            .format(model.config.month_title_format())
            .to_string(),
        DialogViewType::Months => model
            .year_month_info
            .first_day_of_month()
            .format("%Y")
            .to_string(),
        DialogViewType::Years => format!(
            "{} - {}",
            year_group_start(model.year_month_info.year),
            year_group_end(model.year_month_info.year)
        ),
    }
}

fn should_display_previous_button(model: &Model) -> bool {
    match model.dialog_view_type {
        DialogViewType::Days => !model
            .config
            .is_month_forbidden(&model.year_month_info.previous_month()),
        DialogViewType::Months => !model
            .config
            .is_year_forbidden(model.year_month_info.year - 1),
        DialogViewType::Years => !model
            .config
            .is_year_group_forbidden(year_group_start(model.year_month_info.year) - 1),
    }
}

fn should_display_next_button(model: &Model) -> bool {
    match model.dialog_view_type {
        DialogViewType::Days => !model
            .config
            .is_month_forbidden(&model.year_month_info.next_month()),
        DialogViewType::Months => !model
            .config
            .is_year_forbidden(model.year_month_info.year + 1),
        DialogViewType::Years => !model
            .config
            .is_year_group_forbidden(year_group_end(model.year_month_info.year) + 1),
    }
}

fn view_dialog_body<Ms: 'static>(
    model: &Model,
    to_msg: impl FnOnce(Msg) -> Ms + Clone + 'static,
) -> Node<Ms> {
    match model.dialog_view_type {
        DialogViewType::Days => view_dialog_days(model, to_msg),
        DialogViewType::Months => view_dialog_months(model, to_msg),
        DialogViewType::Years => view_dialog_years(model, to_msg),
    }
}

fn view_dialog_years<Ms: 'static>(
    model: &Model,
    to_msg: impl FnOnce(Msg) -> Ms + Clone + 'static,
) -> Node<Ms> {
    let years: Vec<Node<Ms>> = year_group_range(model.year_month_info.year)
        .map(|year| view_year_cell(year, model, to_msg.clone()))
        .collect();

    div![
        C![BODY],
        style! {
            St::GridTemplateColumns => "1fr ".repeat(4),
        },
        years,
    ]
}

fn view_year_cell<Ms: 'static>(
    year: i32,
    model: &Model,
    to_msg: impl FnOnce(Msg) -> Ms + Clone + 'static,
) -> Node<Ms> {
    let is_year_forbidden = model.config.is_year_forbidden(year);
    let is_year_selected = model
        .selected_date
        .map_or(false, |optval| optval.year() == year);

    span![
        year.to_string(),
        C![
            if is_year_forbidden {
                UNAVAILABLE
            } else {
                SELECTABLE
            },
            IF!(is_year_selected => SELECTED),
        ],
        attrs! {
            At::from("role") => "gridcell",
            At::AriaSelected => is_year_selected.as_at_value(),
        },
        IF!(!is_year_forbidden => ev(Ev::Click, move |_| to_msg(Msg::YearSelected(year)))),
    ]
}

fn view_dialog_months<Ms: 'static>(
    model: &Model,
    to_msg: impl FnOnce(Msg) -> Ms + Clone + 'static,
) -> Node<Ms> {
    let months: Vec<Node<Ms>> = (Month::January.number_from_month()
        ..=Month::December.number_from_month())
        .map(|month| {
            view_month_cell(
                YearMonth {
                    year: model.year_month_info.year,
                    month: Month::from_u32(month).unwrap(),
                },
                model,
                to_msg.clone(),
            )
        })
        .collect();

    div![
        C![BODY],
        style! {
            St::GridTemplateColumns => "1fr ".repeat(3),
        },
        months
    ]
}

fn view_month_cell<Ms: 'static>(
    year_month_info: YearMonth,
    model: &Model,
    to_msg: impl FnOnce(Msg) -> Ms + Clone + 'static,
) -> Node<Ms> {
    let is_month_forbidden = model.config.is_month_forbidden(&year_month_info);
    let is_month_selected = model
        .selected_date
        .map_or(false, |optval| year_month_info.contains(&optval));

    span![
        year_month_info.month.name(),
        C![
            if is_month_forbidden {
                UNAVAILABLE
            } else {
                SELECTABLE
            },
            IF!(is_month_selected => SELECTED),
        ],
        attrs! {
            At::from("role") => "gridcell",
            At::AriaSelected => is_month_selected.as_at_value(),
        },
        IF!(!is_month_forbidden => ev(Ev::Click, move |_| to_msg(Msg::MonthSelected(year_month_info.month)))),
    ]
}

fn view_dialog_days<Ms: 'static>(
    model: &Model,
    to_msg: impl FnOnce(Msg) -> Ms + Clone + 'static,
) -> Node<Ms> {
    let first_day_of_month = model.year_month_info.first_day_of_month();
    let first_day_of_calendar = first_day_of_month
        - Duration::days(first_day_of_month.weekday().num_days_from_monday().into());

    let day_nodes: Vec<Node<Ms>> = first_day_of_calendar
        .iter_days()
        .take(7 * 6)
        .map(|day| view_day_cell(day, model, to_msg.clone()))
        .collect();

    div![
        C!["body"],
        style! {
            St::GridTemplateColumns => "1fr ".repeat(7),
        },
        view_weekday_name(Weekday::Mon),
        view_weekday_name(Weekday::Tue),
        view_weekday_name(Weekday::Wed),
        view_weekday_name(Weekday::Thu),
        view_weekday_name(Weekday::Fri),
        view_weekday_name(Weekday::Sat),
        view_weekday_name(Weekday::Sun),
        day_nodes,
    ]
}

fn view_weekday_name<Ms: 'static>(day: Weekday) -> Node<Ms> {
    span![
        day.to_string(),
        C![GRID_HEADER],
        attrs! {
            At::from("role") => "columnheader",
        },
    ]
}

fn view_day_cell<Ms: 'static>(
    date: NaiveDate,
    model: &Model,
    to_msg: impl FnOnce(Msg) -> Ms + Clone + 'static,
) -> Node<Ms> {
    let is_day_forbidden = model.config.is_day_forbidden(&date);
    let is_date_selected = model.selected_date.map_or(false, |optval| optval == date);

    span![
        date.day().to_string(),
        C![
            if is_day_forbidden {
                UNAVAILABLE
            } else {
                SELECTABLE
            },
            IF!(date.month() != model.year_month_info.month.number_from_month() => OTHER_MONTH),
            IF!(is_date_selected => SELECTED),
        ],
        attrs! {
            At::from("role") => "gridcell",
            At::AriaSelected => is_date_selected.as_at_value(),
        },
        IF!(!is_day_forbidden => ev(Ev::Click, move |_| to_msg(Msg::DateSelected(date)))),
    ]
}