-
Notifications
You must be signed in to change notification settings - Fork 21
/
individual_index_header.go
74 lines (60 loc) · 1.76 KB
/
individual_index_header.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
package html
import (
"io"
"strings"
"github.com/elliotchance/gedcom/v39"
"github.com/elliotchance/gedcom/v39/html/core"
)
type IndividualIndexHeader struct {
document *gedcom.Document
selectedLetter rune
livingVisibility LivingVisibility
indexLetters []rune
}
func NewIndividualIndexHeader(document *gedcom.Document, selectedLetter rune, livingVisibility LivingVisibility, indexLetters []rune) *IndividualIndexHeader {
return &IndividualIndexHeader{
document: document,
selectedLetter: selectedLetter,
livingVisibility: livingVisibility,
indexLetters: indexLetters,
}
}
func GetIndexLetters(document *gedcom.Document, livingVisibility LivingVisibility) []rune {
letterMap := map[rune]bool{}
for _, individual := range document.Individuals() {
switch livingVisibility {
case LivingVisibilityShow, LivingVisibilityPlaceholder:
letterMap[getIndexLetter(individual)] = true
case LivingVisibilityHide:
// nothing
}
}
indexLetters := []rune{}
if _, ok := letterMap[symbolLetter]; ok {
indexLetters = []rune{symbolLetter}
}
for i := rune('a'); i <= rune('z'); i++ {
if _, ok := letterMap[i]; ok {
indexLetters = append(indexLetters, i)
}
}
return indexLetters
}
func getIndexLetter(individual *gedcom.IndividualNode) rune {
name := strings.ToLower(individual.Name().Surname())
switch {
case name == "", name[0] < 'a', name[0] > 'z':
return symbolLetter
}
return rune(name[0])
}
func (c *IndividualIndexHeader) WriteHTMLTo(w io.Writer) (int64, error) {
pills := []core.Component{}
for _, letter := range c.indexLetters {
pills = append(pills,
NewIndividualIndexLetter(letter, letter == c.selectedLetter))
}
return core.NewRow(
core.NewColumn(core.EntireRow, core.NewNavPills(pills)),
).WriteHTMLTo(w)
}