aboutsummaryrefslogtreecommitdiff
path: root/static/widgets/month-select/index.js
blob: 63caf703f836657255bbdc0a84c4228eac12752e (plain)
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
import icons from 'icons';
import * as sfw from 'sfw';
const { Div } = sfw.element.native;

const css = await sfw.css(import.meta.url, './index.css');

export default class MonthSelect extends sfw.element.Container {
	#container
	#month_container

	constructor() {
		super({ css });

		this.onscroll = (e) => e.stopPropagate();
		this.onmonth = () => {};

		this.body.append(
			this.#container = Div.new({
				id: 'container',
				children: [
					Div.new({ id: 'title', innerText: 'Select Month' }),
					Div.new({
						id: 'close-button',
						children: [ icons.close ],
						onclick: () => this.hide(),
					}),
					this.#month_container = Div.new({
						id: 'month-container',
					}),
				],
			}),
		);
	}

	show() {
		this.#container.classList.add('visible');
	}

	hide() {
		this.#container.classList.remove('visible');
	}

	set months(months) {
		months.sort((a, b) => a.is_before(b));

		this.#month_container.innerHTML = '';

		let last = null;

		for (const month of months) {
			if (last != null && !last.is_same_year(month)) {
				this.#month_container.append(
					Div.new({
						className: 'year-item',
						innerText: last.year,
					})
				);
			}

			this.#month_container.append(
				Div.new({
					className: 'month-item',
					innerText: month.name,
					onclick: () => {
						this.hide();
						this.onmonth(month);
					}
				})
			);

			last = month;
		}

		this.#month_container.append(
			Div.new({
				className: 'month-item none',
				innerText: 'No Date',
				onclick: () => {
					this.hide();
					this.onmonth(null);
				}
			})
		);
	}
}