2017-05-05 16:39:15 +02:00
package router
2017-05-05 03:53:38 +02:00
2017-05-05 06:07:45 +02:00
import (
"html/template"
"log"
"math"
"net/url"
"strconv"
2017-05-07 02:32:32 +02:00
"github.com/nicksnyder/go-i18n/i18n"
2017-05-06 23:16:21 +02:00
"github.com/ewhal/nyaa/templates"
2017-05-05 03:53:38 +02:00
)
2017-05-05 16:39:15 +02:00
var FuncMap = template . FuncMap {
2017-05-05 06:07:45 +02:00
"min" : math . Min ,
"genRoute" : func ( name string , params ... string ) string {
2017-05-05 16:39:15 +02:00
url , err := Router . Get ( name ) . URL ( params ... )
2017-05-05 06:07:45 +02:00
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 {
2017-05-06 23:16:21 +02:00
return template . HTML ( url . String ( ) + "?" + currentUrl . RawQuery )
2017-05-05 17:14:10 +02:00
}
return "error"
} ,
2017-05-06 23:16:21 +02:00
"genNav" : func ( nav templates . Navigation , currentUrl * url . URL , pagesSelectable int ) template . HTML {
2017-05-05 06:07:45 +02:00
maxPages := math . Ceil ( float64 ( nav . TotalItem ) / float64 ( nav . MaxItemPerPage ) )
2017-05-05 03:53:38 +02:00
2017-05-05 06:07:45 +02:00
var ret = ""
if nav . CurrentPage - 1 > 0 {
2017-05-05 16:39:15 +02:00
url , _ := Router . Get ( nav . Route ) . URL ( "page" , "1" )
2017-05-05 06:07:45 +02:00
ret = ret + "<li><a id=\"page-prev\" href=\"" + url . String ( ) + "?" + currentUrl . RawQuery + "\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>"
}
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 )
2017-05-05 16:39:15 +02:00
url , _ := Router . Get ( nav . Route ) . URL ( "page" , pageNum )
2017-05-05 06:07:45 +02:00
ret = ret + "<li"
if i == nav . CurrentPage {
ret = ret + " class=\"active\""
}
2017-05-05 03:53:38 +02:00
2017-05-05 06:07:45 +02:00
ret = ret + "><a href=\"" + url . String ( ) + "?" + currentUrl . RawQuery + "\">" + strconv . Itoa ( i ) + "</a></li>"
}
if nav . CurrentPage < int ( maxPages ) {
2017-05-05 16:39:15 +02:00
url , _ := Router . Get ( nav . Route ) . URL ( "page" , strconv . Itoa ( nav . CurrentPage + 1 ) )
2017-05-05 06:07:45 +02:00
ret = ret + "<li><a id=\"page-next\" href=\"" + url . String ( ) + "?" + currentUrl . RawQuery + "\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>"
}
return template . HTML ( ret )
} ,
2017-05-06 23:16:21 +02:00
"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 ) )
} ,
2017-05-07 02:32:32 +02:00
"T" : i18n . IdentityTfunc ,
2017-05-05 06:07:45 +02:00
}