package router import ( "html/template" "math" "net/url" "strconv" "time" "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/service/user/permission" "github.com/NyaaPantsu/nyaa/util" "github.com/NyaaPantsu/nyaa/util/categories" "github.com/NyaaPantsu/nyaa/util/languages" ) type captchaData struct { CaptchaID string T languages.TemplateTfunc } var FuncMap = template.FuncMap{ "inc": func(i int) int { return i + 1 }, "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.URL { url, err := Router.Get(name).URL(params...) if err == nil { return template.URL(url.String() + "?" + currentUrl.RawQuery) } return "error" }, "genViewTorrentRoute": func(torrent_id uint) string { // Helper for when you have an uint while genRoute("view_torrent", ...) takes a string // FIXME better solution? s := strconv.FormatUint(uint64(torrent_id), 10) url, err := Router.Get("view_torrent").URL("id", s) if err == nil { return url.String() } return "error" }, "genSearchWithOrdering": func(currentUrl url.URL, sortBy string) template.URL { values := currentUrl.Query() order := false //Default is DESC sort := "2" //Default is Date (Actually ID, but Date is the same thing) if _, ok := values["order"]; ok { order, _ = strconv.ParseBool(values["order"][0]) } if _, ok := values["sort"]; ok { sort = values["sort"][0] } if sort == sortBy { order = !order //Flip order by repeat-clicking } else { order = false //Default to descending when sorting by something new } values.Set("sort", sortBy) values.Set("order", strconv.FormatBool(order)) url, _ := Router.Get("search").URL() url.RawQuery = values.Encode() return template.URL(url.String()) }, "genSortArrows": func(currentUrl url.URL, sortBy string) template.HTML { values := currentUrl.Query() leftclass := "sortarrowdim" rightclass := "sortarrowdim" order := false sort := "2" if _, ok := values["order"]; ok { order, _ = strconv.ParseBool(values["order"][0]) } if _, ok := values["sort"]; ok { sort = values["sort"][0] } if sort == sortBy { if order { rightclass = "" } else { leftclass = "" } } arrows := "▼▲" return template.HTML(arrows) }, "genNav": func(nav Navigation, currentUrl *url.URL, pagesSelectable int) template.HTML { 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 + "