Add calendar
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2021-02-11 21:20:38 +01:00
parent 67d658d1ba
commit d1858ad849
4 changed files with 186 additions and 11 deletions

View File

@ -219,19 +219,74 @@ mark {
.grid { .grid {
display: grid; display: grid;
grid-template-columns: 300px auto; grid-template-columns: 300px 1000px auto;
.sidebar { .sidebar {
padding: 0 12px; padding: 0 12px;
width: 100%; width: 100%;
.wiki-link { .wiki-link {
&::before, &::after { &::before, &::after {
content: ''; content: '';
} }
} }
}
.sidebar-right {
width: 100%;
padding: 0 12px;
}
}
.calendar-grid {
display: grid;
width: calc(8*48px);
grid-template-columns: repeat(8, auto);
border-right: 1px solid #444;
border-bottom: 1px solid #444;
.day {
width: 48px;
height: 48px;
border-left: 1px solid #444;
border-top: 1px solid #444;
text-align: center;
vertical-align: middle;
padding: 3px 0 0 0;
position: relative;
}
.week {
background: #ebebff;
}
.week a {
color: black;
}
.day a {
position: absolute;
top: 1px;
right: 3px;
font-size: 9pt;
color: black;
}
.day span {
position: absolute;
width: 100%;
height: 100%;
font-size: 20pt;
color: black;
top: 8px;
left: 0;
right: 0;
bottom: 0;
}
.today {
font-weight: bold;
}
.current {
background: #f0f0f0;
} }
} }

111
main.go
View File

@ -42,6 +42,13 @@ var (
authToken = os.Getenv("API_TOKEN") authToken = os.Getenv("API_TOKEN")
) )
type Day struct {
Class string
Text string
URL string
Count string
}
type Backref struct { type Backref struct {
Name string Name string
Title string Title string
@ -99,6 +106,8 @@ type PagesRepository interface {
type pageBaseInfo struct { type pageBaseInfo struct {
BaseURL string BaseURL string
RedirectURI string RedirectURI string
Days []Day
Month string
} }
type indexPage struct { type indexPage struct {
@ -168,7 +177,7 @@ type recentPage struct {
Recent []Change Recent []Change
} }
var baseTemplate = []string{"templates/layout.html", "templates/sidebar.html"} var baseTemplate = []string{"templates/layout.html", "templates/sidebar.html", "templates/sidebar-right.html"}
type indexHandler struct{} type indexHandler struct{}
type graphHandler struct{} type graphHandler struct{}
@ -336,7 +345,7 @@ func (h *historyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
pageBase := getPageBase() pageBase := getPageBase(time.Now())
pageData := historyPage{ pageData := historyPage{
pageBaseInfo: pageBase, pageBaseInfo: pageBase,
Session: sess, Session: sess,
@ -352,12 +361,90 @@ func (h *historyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
func getPageBase() pageBaseInfo { func daysInMonth(year int, month time.Month) int {
return time.Date(year, month, 1, 0, 0, 0, 0, time.UTC).AddDate(0, 1, -1).Day()
}
func prepareDays(t time.Time) []Day {
today := time.Now()
var days []Day
curDate := t.AddDate(0, 0, -t.Day()+1)
preDays := int(curDate.Weekday()) - 1
maxDays := daysInMonth(curDate.Year(), curDate.Month())
endOfMonth := false
for i := 0; i < 6; i++ {
_, week := curDate.ISOWeek()
days = append(days, Day{
Class: "week day",
Text: fmt.Sprintf("%02d", week),
URL: "",
Count: "",
})
for d := 0; d < 7; d++ {
day := -preDays + (i * 7) + 1 + d
fday := fmt.Sprintf("%d", day)
if day <= 0 {
fday = ""
}
if day > maxDays {
fday = ""
endOfMonth = true
}
class := "day"
if timeEqual(curDate, today) {
class += " today"
}
if timeEqual(curDate, t) {
class += " current"
}
days = append(days, Day{
Class: class,
Text: fday,
URL: fmt.Sprintf("/edit/%s", formatDatePageName(curDate)),
Count: "",
})
if fday != "" {
curDate = curDate.AddDate(0, 0, 1)
}
if day == maxDays {
endOfMonth = true
}
}
if endOfMonth {
break
}
}
return days
}
func timeEqual(date time.Time, today time.Time) bool {
if date.Year() != today.Year() {
return false
}
if date.Month() != today.Month() {
return false
}
if date.Day() != today.Day() {
return false
}
return true
}
func getPageBase(t time.Time) pageBaseInfo {
clientID := *baseurl clientID := *baseurl
pageBase := pageBaseInfo{ pageBase := pageBaseInfo{
BaseURL: clientID, BaseURL: clientID,
RedirectURI: redirectURI, RedirectURI: redirectURI,
Days: prepareDays(t),
Month: t.Month().String(),
} }
return pageBase return pageBase
} }
@ -374,7 +461,6 @@ func (h *saveHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer func() { defer func() {
if err := sess.Flush(); err != nil { if err := sess.Flush(); err != nil {
log.Println(err) log.Println(err)
log.Println(err)
} }
}() }()
@ -461,7 +547,11 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
`, html.EscapeString(pageText))) `, html.EscapeString(pageText)))
} }
pageBase := getPageBase() curDate, err := ParseDatePageName(page)
if err != nil {
curDate = time.Now()
}
pageBase := getPageBase(curDate)
title := cleanTitle(mpPage.Title) title := cleanTitle(mpPage.Title)
if newTitle, err := PageTitle(pageText); err == nil { if newTitle, err := PageTitle(pageText); err == nil {
title = newTitle title = newTitle
@ -571,7 +661,7 @@ func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
pageBase := getPageBase() pageBase := getPageBase(time.Now())
data := graphPage{ data := graphPage{
pageBaseInfo: pageBase, pageBaseInfo: pageBase,
Session: sess, Session: sess,
@ -736,7 +826,11 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pageText = renderLinks(pageText, false) pageText = renderLinks(pageText, false)
pageText = renderMarkdown2(pageText) pageText = renderMarkdown2(pageText)
pageBase := getPageBase() curDate, err := ParseDatePageName(page)
if err != nil {
curDate = time.Now()
}
pageBase := getPageBase(curDate)
data := indexPage{ data := indexPage{
pageBaseInfo: pageBase, pageBaseInfo: pageBase,
Session: sess, Session: sess,
@ -850,7 +944,7 @@ func (h *recentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
pageBase := getPageBase() pageBase := getPageBase(time.Now())
err = t.Execute(w, recentPage{ err = t.Execute(w, recentPage{
pageBaseInfo: pageBase, pageBaseInfo: pageBase,
Session: sess, Session: sess,
@ -914,7 +1008,6 @@ func main() {
return return
} }
format := r.URL.Query().Get("format") format := r.URL.Query().Get("format")
if format == "" { if format == "" {
format = "json" format = "json"

View File

@ -57,6 +57,10 @@
<section class="section"> <section class="section">
{{ template "content" . }} {{ template "content" . }}
</section> </section>
{{ block "sidebar-right" . }}
<div class="sidebar-right"></div>
{{end}}
</div> </div>

View File

@ -0,0 +1,23 @@
{{ define "sidebar-right" }}
<div class="sidebar-right">
<div class="calendar">
<h3>{{ .Month }}</h3>
<div class="calendar-grid">
<div class="day"></div>
<div class="day">m</div>
<div class="day">d</div>
<div class="day">w</div>
<div class="day">d</div>
<div class="day">v</div>
<div class="day">z</div>
<div class="day">z</div>
{{ range .Days }}
<div class="{{ .Class }}">
<a href="{{ .URL }}">{{ .Text }}</a>
<span>{{ .Count }}</span>
</div>
{{ end }}
</div>
</div>
</div>
{{ end }}