elliotchance/gedcom

View on GitHub
html/publish_header.go

Summary

Maintainability
A
3 hrs
Test Coverage
package html

import (
    "io"

    "github.com/elliotchance/gedcom/v39"
    "github.com/elliotchance/gedcom/v39/html/core"
)

const (
    selectedIndividualsTab = "individuals"
    selectedPlacesTab      = "places"
    selectedFamiliesTab    = "families"
    selectedSurnamesTab    = "surnames"
    selectedSourcesTab     = "sources"
    selectedStatisticsTab  = "statistics"
    selectedExtraTab       = "extra"
)

// PublishHeader is the tabbed section at the top of each page. The PublishHeader will be the
// same on all pages except that some pages will use an extra tab for that page.
type PublishHeader struct {
    document     *gedcom.Document
    extraTab     string
    selectedTab  string
    options      *PublishShowOptions
    indexLetters []rune
    placesMap    map[string]*place
}

func NewPublishHeader(document *gedcom.Document, extraTab string, selectedTab string, options *PublishShowOptions, indexLetters []rune, placesMap map[string]*place) *PublishHeader {
    return &PublishHeader{
        document:     document,
        extraTab:     extraTab,
        selectedTab:  selectedTab,
        options:      options,
        indexLetters: indexLetters,
        placesMap:    placesMap,
    }
}

func (c *PublishHeader) WriteHTMLTo(w io.Writer) (int64, error) {
    items := []*core.NavItem{}

    if c.options.ShowIndividuals {
        badge := core.NewCountBadge(len(c.document.Individuals()))
        title := core.NewComponents(core.NewText("Individuals "), badge)
        item := core.NewNavItem(
            title,
            c.selectedTab == selectedIndividualsTab,
            PageIndividuals(c.indexLetters[0]),
        )
        items = append(items, item)
    }

    if c.options.ShowPlaces {
        badge := core.NewCountBadge(len(c.placesMap))
        item := core.NewNavItem(
            core.NewComponents(core.NewText("Places "), badge),
            c.selectedTab == selectedPlacesTab,
            PagePlaces(),
        )
        items = append(items, item)
    }

    if c.options.ShowFamilies {
        badge := core.NewCountBadge(len(c.document.Families()))
        item := core.NewNavItem(
            core.NewComponents(core.NewText("Families "), badge),
            c.selectedTab == selectedFamiliesTab,
            PageFamilies(),
        )
        items = append(items, item)
    }

    if c.options.ShowSurnames {
        badge := core.NewCountBadge(getSurnames(c.document).Len())
        item := core.NewNavItem(
            core.NewComponents(core.NewText("Surnames "), badge),
            c.selectedTab == selectedSurnamesTab,
            PageSurnames(),
        )
        items = append(items, item)
    }

    if c.options.ShowSources {
        badge := core.NewCountBadge(len(c.document.Sources()))
        item := core.NewNavItem(
            core.NewComponents(core.NewText("Sources "), badge),
            c.selectedTab == selectedSourcesTab,
            PageSources(),
        )
        items = append(items, item)
    }

    if c.options.ShowStatistics {
        item := core.NewNavItem(
            core.NewText("Statistics"),
            c.selectedTab == selectedStatisticsTab,
            PageStatistics(),
        )
        items = append(items, item)
    }

    if c.extraTab != "" {
        item := core.NewNavItem(
            core.NewText(c.extraTab),
            c.selectedTab == selectedExtraTab,
            "#",
        )
        items = append(items, item)
    }

    return core.NewComponents(
        core.NewSpace(),
        core.NewNavTabs(items),
        core.NewSpace(),
    ).WriteHTMLTo(w)
}

var surnames = gedcom.NewStringSet()

func getSurnames(document *gedcom.Document) *gedcom.StringSet {
    if surnames.Len() == 0 {
        for _, individual := range document.Individuals() {
            surname := individual.Name().Surname()
            if surname != "" {
                surnames.Add(surname)
            }
        }
    }

    return surnames
}