Skip to content

Commit 935c24b

Browse files
committed
improve style for calendar
1 parent e3e033c commit 935c24b

File tree

2 files changed

+152
-39
lines changed

2 files changed

+152
-39
lines changed

extensions/date/handlers.go

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ func dateHandler(r Request) Output {
3737
}
3838

3939
func calendarHandler(r Request) Output {
40-
type pair struct {
41-
Time time.Time
42-
Page Page
43-
}
44-
4540
calendar := []pair{}
4641

4742
EachPage(r.Context(), func(p Page) {
@@ -55,12 +50,92 @@ func calendarHandler(r Request) Output {
5550
}
5651
})
5752

58-
slices.SortFunc(calendar, func(a, b pair) int {
59-
return int(b.Time.Unix()) - int(a.Time.Unix())
53+
cal := organizeCalendar(calendar)
54+
55+
slices.SortFunc(cal, func(a, b Year) int {
56+
return int(b.Year) - int(a.Year)
6057
})
6158

6259
return Render("calendar", Locals{
6360
"page": DynamicPage{NameVal: "Calendar"},
64-
"calendar": calendar,
61+
"calendar": cal,
6562
})
6663
}
64+
65+
type pair struct {
66+
Time time.Time
67+
Page Page
68+
}
69+
70+
type Day struct {
71+
Date time.Time
72+
Pages []Page
73+
}
74+
75+
type Month struct {
76+
Name string
77+
Days [6][7]*Day // 6 weeks, 7 days per week
78+
}
79+
80+
type Year struct {
81+
Year int
82+
Months []Month
83+
}
84+
85+
func organizeCalendar(pairs []pair) []Year {
86+
grouped := make(map[int]map[time.Month][]pair)
87+
88+
for _, p := range pairs {
89+
year, month := p.Time.Year(), p.Time.Month()
90+
if grouped[year] == nil {
91+
grouped[year] = make(map[time.Month][]pair)
92+
}
93+
grouped[year][month] = append(grouped[year][month], p)
94+
}
95+
96+
var years []Year
97+
for year, months := range grouped {
98+
var yearData Year
99+
yearData.Year = year
100+
101+
for month, pairs := range months {
102+
var monthData Month
103+
monthData.Name = month.String()
104+
105+
firstDay := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
106+
startOffset := int(firstDay.Weekday())
107+
108+
var days [6][7]*Day
109+
for i := range days {
110+
for j := range days[i] {
111+
days[i][j] = nil
112+
}
113+
}
114+
115+
currentDay := firstDay
116+
for day := 1; day <= 31; day++ {
117+
if currentDay.Month() != month {
118+
break
119+
}
120+
dayOffset := (day + startOffset - 1) / 7
121+
weekday := (day + startOffset - 1) % 7
122+
if days[dayOffset][weekday] == nil {
123+
days[dayOffset][weekday] = &Day{Date: currentDay}
124+
}
125+
for _, p := range pairs {
126+
if p.Time.Day() == currentDay.Day() {
127+
days[dayOffset][weekday].Pages = append(days[dayOffset][weekday].Pages, p.Page)
128+
}
129+
}
130+
currentDay = currentDay.AddDate(0, 0, 1)
131+
}
132+
133+
monthData.Days = days
134+
yearData.Months = append(yearData.Months, monthData)
135+
}
136+
137+
years = append(years, yearData)
138+
}
139+
140+
return years
141+
}
Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,77 @@
11
{{ template "header" . }}
2-
{{ $year := 0 }}
3-
{{ $month := 0 }}
4-
{{ $day := 0 }}
2+
<style>
3+
.calendar{
4+
td,th {
5+
width: calc(100%/7);
6+
}
7+
td{
8+
height: 8em;
9+
overflow: hidden;
10+
padding: 0 !important;
11+
margin: 0 !important;
12+
}
13+
a .page{
14+
font-size: 0.7em;
15+
overflow: hidden;
16+
text-overflow: ellipsis;
17+
display: -webkit-box;
18+
-webkit-line-clamp: 1;
19+
line-clamp: 1;
20+
-webkit-box-orient: vertical;
21+
}
22+
}
23+
</style>
524

6-
<aside class="menu" dir="auto">
7-
<div class="menu-list" dir="auto">
8-
{{ range .calendar }}
9-
{{ if ne $year .Time.Year }}
10-
<h1>{{ .Time.Year }}</h1>
11-
{{ $year = .Time.Year }}
12-
{{ $month = 0 }}
13-
{{ $day = 0 }}
14-
{{ end }}
25+
{{range .calendar}}
26+
<h1>{{.Year}}</h1>
27+
{{range .Months}}
28+
<h2>{{.Name}}</h2>
29+
<table class="calendar table is-bordered is-fullwidth is-hoverable">
30+
<thead>
31+
<tr>
32+
<th>Sun</th>
33+
<th>Mon</th>
34+
<th>Tue</th>
35+
<th>Wed</th>
36+
<th>Thu</th>
37+
<th>Fri</th>
38+
<th>Sat</th>
39+
</tr>
40+
</thead>
41+
<tbody>
42+
{{range .Days}}
43+
<tr>
44+
{{range .}}
45+
<td>
46+
{{if .}}
47+
<span class="tag">{{.Date.Day}}</span>
1548

16-
{{ if ne $month .Time.Month }}
17-
{{ if ne $day .Time.Day }}
18-
<h2>{{ .Time.Day }} {{ .Time.Month }}</h2>
19-
{{ $month = .Time.Month }}
20-
{{ $day = .Time.Day }}
21-
{{ end }}
22-
{{ end }}
49+
{{ if .Pages }}
50+
<aside class="menu" dir="auto">
51+
<div class="menu-list" dir="auto">
2352

24-
{{ with .Page }}
53+
{{range .Pages}}
54+
<a href="/{{.Name}}" dir="auto">
55+
<span class="page">
56+
{{ emoji . }}
57+
{{ $props := properties . }}
58+
{{ with $props.title }} {{ .Value }} {{ else }} {{ .Name }} {{ end }}
59+
</span>
60+
</a>
61+
{{end}}
2562

26-
<a href="/{{.Name}}" dir="auto" class="is-clearfix">
27-
<span class="is-block">
28-
{{ emoji . }}
29-
{{ $props := properties . }}
30-
{{ with $props.title }} {{ .Value }} {{ else }} {{ .Name }} {{ end }}
31-
</span>
32-
</a>
63+
</div>
64+
</aside>
65+
{{end}}
3366

34-
{{ end }}
67+
{{end}}
68+
</td>
69+
{{end}}
70+
</tr>
71+
{{end}}
72+
</tbody>
73+
</table>
74+
{{end}}
75+
{{end}}
3576

36-
{{ end }}
37-
</div>
38-
</aside>
3977
{{ template "footer" . }}

0 commit comments

Comments
 (0)