oficina / calendar Public

Clone
git clone https://amadan.net/oficina/calendar

Plain git — no account needed to clone.

Download

Download this file

1package calendar
2
3import (
4 "fmt"
5 "html/template"
6 "strconv"
7)
8
9// appFuncs are this app's own template helpers, on top of rastrillo's.
10//
11// Each one exists because the alternative is arithmetic or branching
12// inside a template, and a template is the one place in this app that
13// has no tests and no compiler. Anything with a decision in it is
14// computed in Go and printed here.
15var appFuncs = template.FuncMap{
16 "if_eq": ifEq,
17 "repeatOptions": repeatOptions,
18 "calendarOptions": calendarOptions,
19 "eventCancelHref": eventCancelHref,
20 "zoneOptions": zoneOptions,
21 "weekStartOptions": weekStartOptions,
22 "minutesLabel": minutesLabel,
23 "channelLabel": channelLabel,
24 "duration": humanDuration,
25 "feedURL": feedURL,
26 "deleteAllLabel": deleteAllLabel,
27 "canWrite": CanWrite,
28}
29
30// channelLabel names how a reminder rule reaches its person, at the head
31// of its line on the event page: "Email 10 minutes before", as every rule
32// read before there was a choice, or "In Calendar 10 minutes before".
33func channelLabel(channel string) string {
34 switch channel {
35 case ChannelInApp:
36 return "In Calendar"
37 case ChannelBoth:
38 return "Email and in Calendar"
39 }
40 return "Email"
41}
42
43// deleteAllLabel names the expensive option with its own count, so the
44// warning is in the label rather than in a sentence beside it.
45//
46// "All 52 of them" is a different decision from "All". A series with no
47// end has no count to give, and says that instead of inventing one.
48func deleteAllLabel(count int, forever bool) string {
49 if forever || count <= 0 {
50 return "Every occurrence, including all the future ones"
51 }
52 if count == 1 {
53 return "The only occurrence"
54 }
55 return fmt.Sprintf("All %d of them", count)
56}
57
58// ifEq returns value when field matches want, and "" otherwise. It is
59// how a shared error string is shown against exactly the field it
60// belongs to, without the form data carrying one error slot per field.
61func ifEq(field, want, value string) string {
62 if field == want {
63 return value
64 }
65 return ""
66}
67
68// repeatOptions builds the repeat picker, labelled against this event's
69// own start date so it says "Every Wednesday" rather than "Weekly".
70func repeatOptions(startLocal, tz string, allDay bool, selected string) []map[string]any {
71 if selected == "" {
72 selected = RepeatNone
73 }
74 var out []map[string]any
75 for _, c := range RepeatChoicesFor(startLocal, tz, allDay) {
76 out = append(out, map[string]any{
77 "Value": c.Value,
78 "Label": c.Label,
79 "Selected": c.Value == selected,
80 })
81 }
82 return out
83}
84
85func calendarOptions(cals []CalendarView, selected int64) []map[string]any {
86 var out []map[string]any
87 for _, c := range cals {
88 out = append(out, map[string]any{
89 "Value": strconv.FormatInt(c.ID, 10),
90 "Label": c.Name,
91 "Selected": c.ID == selected,
92 })
93 }
94 return out
95}
96
97// eventCancelHref sends Cancel back where it came from: the event's own
98// page when editing, and the calendar when creating, because a new
99// event has no page to return to.
100func eventCancelHref(eventID int64) string {
101 if eventID == 0 {
102 return "/today"
103 }
104 return "/events/" + strconv.FormatInt(eventID, 10)
105}
106
107func zoneOptions(selected string) []map[string]any {
108 var out []map[string]any
109 found := false
110 for _, z := range CommonZones {
111 if z == selected {
112 found = true
113 }
114 out = append(out, map[string]any{"Value": z, "Label": z, "Selected": z == selected})
115 }
116 // Somebody in a zone the short list omits must not have it silently
117 // replaced by the first option the moment they save anything else.
118 if !found && selected != "" {
119 out = append(out, map[string]any{"Value": selected, "Label": selected, "Selected": true})
120 }
121 return out
122}
123
124func weekStartOptions(selected int) []map[string]any {
125 return []map[string]any{
126 {"Value": strconv.Itoa(WeekStartMonday), "Label": "Monday", "Selected": selected == WeekStartMonday},
127 {"Value": strconv.Itoa(WeekStartSaturday), "Label": "Saturday", "Selected": selected == WeekStartSaturday},
128 {"Value": strconv.Itoa(WeekStartSunday), "Label": "Sunday", "Selected": selected == WeekStartSunday},
129 }
130}
131
132// minutesLabel writes a reminder offset the way somebody would say it.
133func minutesLabel(min int) string {
134 switch {
135 case min == 0:
136 return "at the time it starts"
137 case min < 0:
138 return fmt.Sprintf("%s afterwards", humanDuration(-min))
139 case min%(24*60) == 0:
140 d := min / (24 * 60)
141 if d == 1 {
142 return "one day before"
143 }
144 return fmt.Sprintf("%d days before", d)
145 default:
146 return humanDuration(min) + " before"
147 }
148}
149
150// feedURL builds the absolute subscription URL. Absolute because a
151// calendar client is given this by copy and paste and has no page to
152// resolve a relative path against.
153func feedURL(origin, token string) string {
154 return origin + "/feed/" + token + ".ics"
155}
156