plugins/caldav: add pagination to calendar view

This commit is contained in:
Simon Ser 2020-02-05 18:57:16 +01:00
parent a1b43cc5a8
commit 9fe20a604c
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
2 changed files with 28 additions and 6 deletions

View file

@ -7,6 +7,7 @@
</p> </p>
<h2>Calendar: {{.Calendar.Name}}</h2> <h2>Calendar: {{.Calendar.Name}}</h2>
<h3>{{.Time.Format "January 2006"}}</h3>
{{if .Events}} {{if .Events}}
<ul> <ul>
@ -22,4 +23,9 @@
<p>No events.</p> <p>No events.</p>
{{end}} {{end}}
<p>
<a href="/calendar?month={{.PrevPage}}">Previous</a>
· <a href="/calendar?month={{.NextPage}}">Next</a>
</p>
{{template "foot.html"}} {{template "foot.html"}}

View file

@ -12,8 +12,10 @@ import (
type CalendarRenderData struct { type CalendarRenderData struct {
koushin.BaseRenderData koushin.BaseRenderData
Calendar *caldav.Calendar Time time.Time
Events []caldav.CalendarObject Calendar *caldav.Calendar
Events []caldav.CalendarObject
PrevPage, NextPage string
} }
type EventRenderData struct { type EventRenderData struct {
@ -22,18 +24,29 @@ type EventRenderData struct {
Event *caldav.CalendarObject Event *caldav.CalendarObject
} }
var monthPageLayout = "2006-01"
func registerRoutes(p *koushin.GoPlugin, u *url.URL) { func registerRoutes(p *koushin.GoPlugin, u *url.URL) {
p.GET("/calendar", func(ctx *koushin.Context) error { p.GET("/calendar", func(ctx *koushin.Context) error {
var start time.Time
if s := ctx.QueryParam("month"); s != "" {
var err error
start, err = time.Parse(monthPageLayout, s)
if err != nil {
return fmt.Errorf("failed to parse month: %v", err)
}
} else {
now := time.Now()
start = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
}
end := start.AddDate(0, 1, 0)
// TODO: multi-calendar support // TODO: multi-calendar support
c, calendar, err := getCalendar(u, ctx.Session) c, calendar, err := getCalendar(u, ctx.Session)
if err != nil { if err != nil {
return err return err
} }
now := time.Now()
start := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
end := start.AddDate(0, 1, 0)
query := caldav.CalendarQuery{ query := caldav.CalendarQuery{
CompRequest: caldav.CalendarCompRequest{ CompRequest: caldav.CalendarCompRequest{
Name: "VCALENDAR", Name: "VCALENDAR",
@ -65,8 +78,11 @@ func registerRoutes(p *koushin.GoPlugin, u *url.URL) {
return ctx.Render(http.StatusOK, "calendar.html", &CalendarRenderData{ return ctx.Render(http.StatusOK, "calendar.html", &CalendarRenderData{
BaseRenderData: *koushin.NewBaseRenderData(ctx), BaseRenderData: *koushin.NewBaseRenderData(ctx),
Time: start,
Calendar: calendar, Calendar: calendar,
Events: events, Events: events,
PrevPage: start.AddDate(0, -1, 0).Format(monthPageLayout),
NextPage: start.AddDate(0, 1, 0).Format(monthPageLayout),
}) })
}) })