-
Notifications
You must be signed in to change notification settings - Fork 0
/
leftnav.go
211 lines (198 loc) · 5.68 KB
/
leftnav.go
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package common
import (
"errors"
"fmt"
"github.com/keep94/appcommon/date_util"
"github.com/keep94/appcommon/http_util"
"github.com/keep94/finance/fin/categories"
"github.com/keep94/finance/fin/categories/categoriesdb"
"html/template"
"net/http"
"net/url"
"strconv"
"strings"
)
var (
kLeftNavTemplateSpec = `
<div class="leftnav">
<b>{{.UserName}}</b><br>
{{.LastLogin}}<br>
<br>
Accounts:
<ul>
{{with $top := .}}
{{range .ActiveAccountDetails}}
{{if $top.Account .Id}}
<li><span class="selected">{{.Name}}</span></li>
{{else}}
<li><a href="{{$top.AccountLink .Id}}">{{.Name}}</a></li>
{{end}}
{{end}}
{{end}}
</ul>
<br>
{{if .Reports}}
<span class="selected">Reports</span><br>
{{else}}
<a href="{{.ReportUrl}}">Reports</a><br>
{{end}}
{{if .Trends}}
<span class="selected">Trends</span><br>
{{else}}
<a href="{{.TrendUrl}}">Trends</a><br>
{{end}}
{{if .Totals}}
<span class="selected">Totals</span><br>
{{else}}
<a href="/fin/totals">Totals</a><br>
{{end}}
<br>
{{if .Search}}
<span class="selected">Search</span><br>
{{else}}
<a href="/fin/list">Search</a><br>
{{end}}
{{if .Unreviewed}}
<span class="selected">Review</span><br>
{{else}}
<a href="/fin/unreviewed">Review</a><br>
{{end}}
{{if .Manage}}
<span class="selected">Manage Categories</span><br>
{{else}}
<a href="/fin/catedit">Manage Categories</a><br>
{{end}}
{{if .Recurring}}
<span class="selected">Recurring</span><br>
{{else}}
<a href="/fin/recurringlist">Recurring</a><br>
{{end}}
{{if .Export}}
<span class="selected">Export</span><br>
{{else}}
<a href="/fin/export">Export</a><br>
{{end}}
<br>
{{if .Chpasswd}}
<span class="selected">Change Password</span><br>
{{else}}
<a href="/fin/chpasswd">Change Password</a><br>
{{end}}
<a href="/fin/logout">Sign out</a>
<br><br>
</div>`
)
var (
kLeftNavTemplate *template.Template
)
// Selecter indicates the item to be selected in the left navigation bar.
type Selecter struct {
cat int
id int64
}
func ParseSelecter(s string) (Selecter, error) {
ss := strings.SplitN(s, ":", 2)
if len(ss) < 2 {
return Selecter{}, errors.New("common: Missing colon.")
}
cat, err := strconv.ParseInt(ss[0], 10, 64)
if err != nil {
return Selecter{}, err
}
id, err := strconv.ParseInt(ss[1], 10, 64)
if err != nil {
return Selecter{}, err
}
return Selecter{cat: int(cat), id: id}, nil
}
func (s Selecter) String() string {
return fmt.Sprintf("%d:%d", s.cat, s.id)
}
const (
accounts = iota + 1
reports
trends
totals
search
unreviewed
manage
recurring
export
chpasswd
)
func SelectAccount(id int64) Selecter { return Selecter{cat: accounts, id: id} }
func SelectReports() Selecter { return Selecter{cat: reports} }
func SelectTrends() Selecter { return Selecter{cat: trends} }
func SelectTotals() Selecter { return Selecter{cat: totals} }
func SelectSearch() Selecter { return Selecter{cat: search} }
func SelectUnreviewed() Selecter { return Selecter{cat: unreviewed} }
func SelectManage() Selecter { return Selecter{cat: manage} }
func SelectRecurring() Selecter { return Selecter{cat: recurring} }
func SelectExport() Selecter { return Selecter{cat: export} }
func SelectChpasswd() Selecter { return Selecter{cat: chpasswd} }
func SelectNone() Selecter { return Selecter{} }
// LeftNav is for creating the left navigation bar.
type LeftNav struct {
Cdc categoriesdb.Getter
Clock date_util.Clock
}
// Generate generates the html for the left navigation bar including the div
// tags. sel indicates which item in the left navigation bar will be selected.
// If Generate can't generate the html, it returns the empty string, writes
// an error message to w, and writes the error to stderr.
func (l *LeftNav) Generate(
w http.ResponseWriter, r *http.Request, sel Selecter) template.HTML {
session := GetUserSession(r)
lastLoginStr := "--"
lastLogin, ok := session.LastLogin()
if ok {
lastLoginStr = lastLogin.Local().Format("Mon 01/02/2006 15:04")
}
cds, err := l.Cdc.Get(nil)
if err != nil {
http_util.ReportError(w, "Database error", err)
return ""
}
now := date_util.TimeToDate(l.Clock.Now())
// Include today!
now = now.AddDate(0, 0, 1)
oneMonthAgo := now.AddDate(0, -1, 0)
oneYearAgo := now.AddDate(-1, 0, 0)
var sb strings.Builder
http_util.WriteTemplate(&sb, kLeftNavTemplate, &view{
CatDetailStore: cds,
ReportUrl: http_util.NewUrl(
"/fin/report",
"sd", oneMonthAgo.Format(date_util.YMDFormat),
"ed", now.Format(date_util.YMDFormat)),
TrendUrl: http_util.NewUrl(
"/fin/trends",
"sd", oneYearAgo.Format(date_util.YMDFormat),
"ed", now.Format(date_util.YMDFormat)),
UserName: session.User.Name,
LastLogin: lastLoginStr,
sel: sel})
return template.HTML(sb.String())
}
type view struct {
AccountLinker
categories.CatDetailStore
ReportUrl *url.URL
TrendUrl *url.URL
UserName string
LastLogin string
sel Selecter
}
func (v *view) Account(id int64) bool { return v.sel == SelectAccount(id) }
func (v *view) Reports() bool { return v.sel == SelectReports() }
func (v *view) Trends() bool { return v.sel == SelectTrends() }
func (v *view) Totals() bool { return v.sel == SelectTotals() }
func (v *view) Search() bool { return v.sel == SelectSearch() }
func (v *view) Unreviewed() bool { return v.sel == SelectUnreviewed() }
func (v *view) Manage() bool { return v.sel == SelectManage() }
func (v *view) Recurring() bool { return v.sel == SelectRecurring() }
func (v *view) Export() bool { return v.sel == SelectExport() }
func (v *view) Chpasswd() bool { return v.sel == SelectChpasswd() }
func init() {
kLeftNavTemplate = NewTemplate("leftnav", kLeftNavTemplateSpec)
}