package router
import (
"html/template"
"log"
"math"
"net/url"
"strconv"
"github.com/nicksnyder/go-i18n/i18n"
"github.com/ewhal/nyaa/templates"
)
var FuncMap = template.FuncMap{
"min": math.Min,
"genRoute": func(name string, params ...string) string {
url, err := Router.Get(name).URL(params...)
if err == nil {
return url.String()
}
return "error"
},
"genRouteWithQuery": func(name string, currentUrl *url.URL, params ...string) template.HTML {
url, err := Router.Get(name).URL(params...)
if err == nil {
return template.HTML(url.String() + "?" + currentUrl.RawQuery)
}
return "error"
},
"genNav": func(nav templates.Navigation, currentUrl *url.URL, pagesSelectable int) template.HTML {
maxPages := math.Ceil(float64(nav.TotalItem) / float64(nav.MaxItemPerPage))
var ret = ""
if nav.CurrentPage-1 > 0 {
url, _ := Router.Get(nav.Route).URL("page", "1")
ret = ret + "
«"
}
startValue := 1
if nav.CurrentPage > pagesSelectable {
startValue = (int(math.Min((float64(nav.CurrentPage)+math.Floor(float64(pagesSelectable)/2)), maxPages)) - pagesSelectable + 1)
}
endValue := (startValue + pagesSelectable - 1)
if endValue > int(maxPages) {
endValue = int(maxPages)
}
log.Println(nav.TotalItem)
for i := startValue; i <= endValue; i++ {
pageNum := strconv.Itoa(i)
url, _ := Router.Get(nav.Route).URL("page", pageNum)
ret = ret + "" + strconv.Itoa(i) + ""
}
if nav.CurrentPage < int(maxPages) {
url, _ := Router.Get(nav.Route).URL("page", strconv.Itoa(nav.CurrentPage+1))
ret = ret + "»"
}
return template.HTML(ret)
},
"SearchCommon": func(s templates.SearchForm) template.HTML {
return template.HTML(templates.SearchCommon(s))
},
"SearchButton": func(s templates.SearchForm) template.HTML {
return template.HTML(templates.SearchButton(s))
},
"SearchAdvanced": func(nav templates.Navigation, s templates.SearchForm) template.HTML {
return template.HTML(templates.SearchAdvanced(nav, s))
},
"T": i18n.IdentityTfunc,
}