diff options
author | Drew DeVault <sir@cmpwn.com> | 2020-05-20 12:39:37 -0400 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2020-05-20 18:59:01 +0200 |
commit | 7142da950d468edd04f3fdb509634684fc28c38f (patch) | |
tree | c28278a6b0f70dc91334c30f6dffecce193998d1 /plugins/caldav | |
parent | 50cb8bef77cf2a6c2feed3a25af4871b2e6792ff (diff) | |
download | alps-7142da950d468edd04f3fdb509634684fc28c38f.tar.gz alps-7142da950d468edd04f3fdb509634684fc28c38f.zip |
alps theme: initial layout for calendar
Diffstat (limited to 'plugins/caldav')
-rw-r--r-- | plugins/caldav/routes.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/plugins/caldav/routes.go b/plugins/caldav/routes.go index cb8aa2a..d7c31f7 100644 --- a/plugins/caldav/routes.go +++ b/plugins/caldav/routes.go @@ -18,9 +18,16 @@ import ( type CalendarRenderData struct { alps.BaseRenderData Time time.Time + Now time.Time + Dates [7 * 6]time.Time Calendar *caldav.Calendar Events []CalendarObject PrevPage, NextPage string + PrevTime, NextTime time.Time + + EventsForDate func(time.Time) []CalendarObject + DaySuffix func(n int) string + Sub func(a, b int) int } type EventRenderData struct { @@ -96,13 +103,66 @@ func registerRoutes(p *alps.GoPlugin, u *url.URL) { return fmt.Errorf("failed to query calendar: %v", err) } + // TODO: Time zones are hard + var dates [7 * 6]time.Time + initialDate := start.UTC() + initialDate = initialDate.AddDate(0, 0, -int(initialDate.Weekday())) + for i := 0; i < len(dates); i += 1 { + dates[i] = initialDate + initialDate = initialDate.AddDate(0, 0, 1) + } + + eventMap := make(map[time.Time][]CalendarObject) + for _, ev := range events { + ev := ev // make a copy + // TODO: include event on each date for which it is active + co := ev.Data.Events()[0] + startTime, _ := co.DateTimeStart(nil) + startTime = startTime.UTC().Truncate(time.Hour * 24) + eventMap[startTime] = append(eventMap[startTime], CalendarObject{&ev}) + } + return ctx.Render(http.StatusOK, "calendar.html", &CalendarRenderData{ BaseRenderData: *alps.NewBaseRenderData(ctx), Time: start, + Now: time.Now(), // TODO: Use client time zone Calendar: calendar, + Dates: dates, Events: newCalendarObjectList(events), PrevPage: start.AddDate(0, -1, 0).Format(monthPageLayout), NextPage: start.AddDate(0, 1, 0).Format(monthPageLayout), + PrevTime: start.AddDate(0, -1, 0), + NextTime: start.AddDate(0, 1, 0), + + EventsForDate: func(when time.Time) []CalendarObject { + if events, ok := eventMap[when.Truncate(time.Hour * 24)]; ok { + return events + } + return nil + }, + + DaySuffix: func(n int) string { + if n % 100 >= 11 && n % 100 <= 13 { + return "th" + } + return map[int]string{ + 0: "th", + 1: "st", + 2: "nd", + 3: "rd", + 4: "th", + 5: "th", + 6: "th", + 7: "th", + 8: "th", + 9: "th", + }[n % 10] + }, + + Sub: func (a, b int) int { + // Why isn't this built-in, come on Go + return a - b + }, }) }) |