-
Notifications
You must be signed in to change notification settings - Fork 20
/
place_list_page.go
81 lines (66 loc) 路 2.06 KB
/
place_list_page.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
package html
import (
"github.com/elliotchance/gedcom"
"github.com/elliotchance/gedcom/html/core"
"io"
"sort"
)
// PlaceListPage lists all places.
type PlaceListPage struct {
document *gedcom.Document
googleAnalyticsID string
options PublishShowOptions
}
func NewPlaceListPage(document *gedcom.Document, googleAnalyticsID string, options PublishShowOptions) *PlaceListPage {
return &PlaceListPage{
document: document,
googleAnalyticsID: googleAnalyticsID,
options: options,
}
}
func (c *PlaceListPage) WriteHTMLTo(w io.Writer) (int64, error) {
table := []core.Component{}
places := GetPlaces(c.document)
// Get all countries
countries := gedcom.NewStringSet()
for _, place := range places {
countries.Add(place.country)
}
sortedPlaces := []*place{}
for _, placeName := range places {
sortedPlaces = append(sortedPlaces, placeName)
}
sort.SliceStable(sortedPlaces, func(i, j int) bool {
if sortedPlaces[i].country != sortedPlaces[j].country {
return sortedPlaces[i].country < sortedPlaces[j].country
}
return sortedPlaces[i].PrettyName < sortedPlaces[j].PrettyName
})
lastCountry := ""
for _, place := range sortedPlaces {
if lastCountry != place.country {
anchor := core.NewAnchor(place.country)
heading := core.NewHeading(2, "", core.NewText(place.country))
components := core.NewComponents(anchor, heading)
cell := core.NewTableCell(components)
countryTitle := core.NewTableRow(cell)
table = append(table, countryTitle)
}
placeEntry := NewPlaceInList(c.document, place)
table = append(table, placeEntry)
lastCountry = place.country
}
// Render
pills := []core.Component{}
for _, country := range countries.Strings() {
pills = append(pills, core.NewNavLink(country, "#"+country, false))
}
return core.NewPage("Places", core.NewComponents(
NewPublishHeader(c.document, "", selectedPlacesTab, c.options),
core.NewNavPillsRow(pills),
core.NewSpace(),
core.NewRow(
core.NewColumn(core.EntireRow, core.NewTable("", table...)),
),
), c.googleAnalyticsID).WriteHTMLTo(w)
}