Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/router/templateFunctions.go

77 lignes
2,6 Kio
Go
Brut Vue normale Historique

package router
import (
"github.com/ewhal/nyaa/service/user/permission"
"github.com/nicksnyder/go-i18n/i18n"
"html/template"
"log"
"math"
"net/url"
"strconv"
)
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"
},
2017-05-05 17:14:10 +02:00
"genRouteWithQuery": func(name string, currentUrl *url.URL, params ...string) template.HTML {
url, err := Router.Get(name).URL(params...)
if err == nil {
return template.HTML(template.HTMLEscapeString(url.String() + "?" + currentUrl.RawQuery)) // TODO: Review application of character escaping
2017-05-05 17:14:10 +02:00
}
return "error"
},
2017-05-07 01:20:13 +02:00
"genNav": func(nav Navigation, currentUrl *url.URL, pagesSelectable int) template.HTML {
2017-05-10 16:43:50 +02:00
var ret = ""
if (nav.TotalItem > 0) {
maxPages := math.Ceil(float64(nav.TotalItem) / float64(nav.MaxItemPerPage))
if nav.CurrentPage-1 > 0 {
url, _ := Router.Get(nav.Route).URL("page", "1")
ret = ret + "<li><a id=\"page-prev\" href=\"" + url.String() + "?" + currentUrl.RawQuery + "\" aria-label=\"Previous\"><span aria-hidden=\"true\">&laquo;</span></a></li>"
}
startValue := 1
2017-05-10 03:15:29 +02:00
if nav.CurrentPage > pagesSelectable/2 {
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 + "<li"
if i == nav.CurrentPage {
ret = ret + " class=\"active\""
}
ret = ret + "><a href=\"" + url.String() + "?" + currentUrl.RawQuery + "\">" + strconv.Itoa(i) + "</a></li>"
}
if nav.CurrentPage < int(maxPages) {
url, _ := Router.Get(nav.Route).URL("page", strconv.Itoa(nav.CurrentPage+1))
ret = ret + "<li><a id=\"page-next\" href=\"" + url.String() + "?" + currentUrl.RawQuery + "\" aria-label=\"Next\"><span aria-hidden=\"true\">&raquo;</span></a></li>"
}
2017-05-10 16:43:50 +02:00
}
return template.HTML(ret)
},
"T": i18n.IdentityTfunc,
"getAvatar": func(hash string, size int) string {
return "https://www.gravatar.com/avatar/" + hash + "?s=" + strconv.Itoa(size)
},
"CurrentOrAdmin": userPermission.CurrentOrAdmin,
"CurrentUserIdentical": userPermission.CurrentUserIdentical,
"HasAdmin": userPermission.HasAdmin,
"GetRole": userPermission.GetRole,
"IsFollower": userPermission.IsFollower,
"NoEncode": func(str string) template.HTML {
return template.HTML(str)
},
}