/
calendar.cljc
154 lines (138 loc) · 3.68 KB
/
calendar.cljc
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
(ns dgknght.app-lib.calendar
(:require #?(:clj [clj-time.core :as t]
:cljs [cljs-time.core :as t])
#?(:clj [clj-time.coerce :refer [to-local-date]]
:cljs [cljs-time.coerce :refer [to-local-date]])
#?(:clj [clj-time.periodic :refer [periodic-seq]]
:cljs [cljs-time.periodic :refer [periodic-seq]])))
(def month-names
["January"
"February"
"March"
"April"
"May"
"June"
"July"
"August"
"September"
"October"
"November"
"December"])
(def day-data
[{:abbreviation "Mon"
:name "Monday"}
{:abbreviation "Tue"
:name "Tuesday"}
{:abbreviation "Wed"
:name "Wednesday"}
{:abbreviation "Thu"
:name "Thursday"}
{:abbreviation "Fri"
:name "Friday"}
{:abbreviation "Sat"
:name "Saturday"}
{:abbreviation "Sun"
:name "Sunday"}])
(def ^:private days-of-the-week
(->> [:monday
:tuesday
:wednesday
:thursday
:friday
:saturday
:sunday]
(map-indexed #(vector %2 (+ 1 %1)))
(into {})))
; Library day-of-week values
; 1 - Monday
; 2 - Tuesday
; 3 - Wednesday
; 4 - Thursday
; 5 - Friday
; 6 - Saturday
; 7 - Sunday
(def ^:private offsets
{:monday 0
:sunday 1})
(defn- start-offset
[first-dom {:keys [first-day-of-week]}]
{:pre [(contains? offsets first-day-of-week)]}
(if (= (t/day-of-week first-dom)
(days-of-the-week first-day-of-week))
0
(+ (- (t/day-of-week first-dom)
1)
(get-in offsets [first-day-of-week]))))
(defn- day-info
[{:keys [first-day-of-week]}]
(->> day-data
cycle
(drop (- 7 (get-in offsets [first-day-of-week])))
(take 7)))
(defn- date->map
[date month opts]
{:date date
:today? (t/equal? date (t/today))
:selected? (and (:selected opts)
(t/equal? date (:selected opts)))
:in-month? (= month (t/month date))})
(defn- ensure-year-month
[{:keys [selected year month] :as opts}]
(cond
(and year month)
opts
selected
(assoc opts
:year (t/year selected)
:month (t/month selected))
:else
(let [today (t/today)]
(assoc opts
:year (t/year today)
:month (t/month today)))))
(def ^:private defaults
{:first-day-of-week :sunday})
(defn- prepare-opts
[opts]
(-> (merge defaults opts)
ensure-year-month))
(defn init
([]
(let [today (t/today)]
(init {:year (t/year today)
:month (t/month today)})))
([options]
(let [{:keys [year month] :as opts} (prepare-opts options)
first-dom (t/first-day-of-the-month year month)
offset (start-offset first-dom opts)
start (t/minus first-dom (t/days offset))
days (periodic-seq start (t/days 1))]
(assoc opts
:title (str (nth month-names (- month 1)) " " year)
:week-days (day-info opts)
:weeks (->> days
(map (comp #(date->map % month opts)
to-local-date))
(partition 7)
(take-while #(some :in-month? %))
(map vec)
(into []))))))
(defn next-month
[{:keys [year month options]}]
(let [[y m] (if (= month 12)
[(inc year) 1]
[year (inc month)])]
(init (assoc options
:year y
:month m))))
(defn prev-month
[{:keys [year month options]}]
(let [[y m] (if (= month 1)
[(dec year) 12]
[year (dec month)])]
(init (assoc options
:year y
:month m))))
(defn select
[calendar date]
(init (assoc calendar :selected date)))