2019-03-07 08:43:28 +05:00
|
|
|
package pagination
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-16 06:22:33 +05:00
|
|
|
"go.dev.pztrn.name/fastpastebin/assets"
|
2019-03-07 08:43:28 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateHTML creates pagination HTML based on passed parameters.
|
|
|
|
func CreateHTML(currentPage int, pages int, linksBase string) string {
|
|
|
|
// Load templates.
|
2021-06-16 06:22:33 +05:00
|
|
|
paginationHTMLRaw, err := assets.Data.ReadFile("pagination.html")
|
2019-03-07 08:43:28 +05:00
|
|
|
if err != nil {
|
|
|
|
return "Missing pagination.html"
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:22:33 +05:00
|
|
|
paginationLinkRaw, err1 := assets.Data.ReadFile("pagination_link.html")
|
2019-03-07 08:43:28 +05:00
|
|
|
if err1 != nil {
|
|
|
|
return "Missing pagination_link.html"
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:22:33 +05:00
|
|
|
paginationLinkCurrentRaw, err2 := assets.Data.ReadFile("pagination_link_current.html")
|
2019-03-07 08:43:28 +05:00
|
|
|
if err2 != nil {
|
|
|
|
return "Missing pagination_link_current.html"
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:22:33 +05:00
|
|
|
paginationEllipsisRaw, err3 := assets.Data.ReadFile("pagination_ellipsis.html")
|
2019-03-07 08:43:28 +05:00
|
|
|
if err3 != nil {
|
|
|
|
return "Missing pagination_ellipsis.html"
|
|
|
|
}
|
|
|
|
|
|
|
|
// First page should always be visible.
|
2019-10-13 16:26:52 +05:00
|
|
|
var paginationString string
|
2019-03-07 08:43:28 +05:00
|
|
|
if currentPage == 1 {
|
|
|
|
paginationString = strings.Replace(string(paginationLinkCurrentRaw), "{pageNum}", strconv.Itoa(currentPage), -1)
|
|
|
|
} else {
|
|
|
|
paginationString = strings.Replace(string(paginationLinkRaw), "{pageNum}", "1", -1)
|
2019-12-22 01:17:18 +05:00
|
|
|
paginationString = strings.Replace(paginationString, "{paginationLink}", linksBase+"1", -1)
|
2019-03-07 08:43:28 +05:00
|
|
|
}
|
|
|
|
|
2019-12-22 01:17:18 +05:00
|
|
|
var (
|
|
|
|
ellipsisStartAdded = false
|
|
|
|
ellipsisEndAdded = false
|
|
|
|
i = 2
|
|
|
|
)
|
|
|
|
|
2019-03-07 08:43:28 +05:00
|
|
|
for i <= pages {
|
2021-06-14 23:48:34 +05:00
|
|
|
// ToDo: fix it!
|
|
|
|
// nolint:nestif
|
2019-03-07 08:43:28 +05:00
|
|
|
if pages > 5 {
|
|
|
|
if currentPage-3 < i && currentPage+3 > i || i == pages {
|
2021-06-14 23:48:34 +05:00
|
|
|
paginationItemRaw := string(paginationLinkRaw)
|
2019-03-07 08:43:28 +05:00
|
|
|
if i == currentPage {
|
|
|
|
paginationItemRaw = string(paginationLinkCurrentRaw)
|
|
|
|
}
|
2020-02-29 22:41:06 +05:00
|
|
|
|
2019-03-07 08:43:28 +05:00
|
|
|
paginationItem := strings.Replace(paginationItemRaw, "{pageNum}", strconv.Itoa(i), -1)
|
|
|
|
paginationItem = strings.Replace(paginationItem, "{paginationLink}", linksBase+strconv.Itoa(i), 1)
|
|
|
|
paginationString += paginationItem
|
|
|
|
} else {
|
|
|
|
if currentPage-3 < i && !ellipsisStartAdded {
|
|
|
|
paginationString += string(paginationEllipsisRaw)
|
|
|
|
ellipsisStartAdded = true
|
|
|
|
} else if currentPage+3 > i && !ellipsisEndAdded {
|
|
|
|
paginationString += string(paginationEllipsisRaw)
|
|
|
|
ellipsisEndAdded = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-14 23:48:34 +05:00
|
|
|
paginationItemRaw := string(paginationLinkRaw)
|
2019-03-07 08:43:28 +05:00
|
|
|
if i == currentPage {
|
|
|
|
paginationItemRaw = string(paginationLinkCurrentRaw)
|
|
|
|
}
|
|
|
|
paginationItem := strings.Replace(paginationItemRaw, "{pageNum}", strconv.Itoa(i), -1)
|
|
|
|
paginationItem = strings.Replace(paginationItem, "{paginationLink}", linksBase+strconv.Itoa(i), 1)
|
|
|
|
paginationString += paginationItem
|
|
|
|
}
|
|
|
|
|
2020-02-29 22:41:06 +05:00
|
|
|
i++
|
2019-03-07 08:43:28 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
pagination := strings.Replace(string(paginationHTMLRaw), "{paginationLinks}", paginationString, 1)
|
|
|
|
if currentPage+1 <= pages {
|
|
|
|
pagination = strings.Replace(pagination, "{nextPageLink}", linksBase+strconv.Itoa(currentPage+1), 1)
|
|
|
|
} else {
|
|
|
|
pagination = strings.Replace(pagination, "{nextPageLink}", linksBase+strconv.Itoa(pages), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentPage-1 > 1 {
|
|
|
|
pagination = strings.Replace(pagination, "{previousPageLink}", linksBase+strconv.Itoa(currentPage-1), 1)
|
|
|
|
} else {
|
|
|
|
pagination = strings.Replace(pagination, "{previousPageLink}", linksBase, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pagination
|
|
|
|
}
|