shorgru/pkg/tpl/list.templ

77 lines
2.2 KiB
Text
Raw Normal View History

2024-10-06 17:04:37 +03:00
package tpl
import (
"fmt"
"sh.org.ru/pkg/config"
2024-10-06 17:04:37 +03:00
"sh.org.ru/pkg/model"
"strconv"
"sh.org.ru/pkg/middleware"
2024-10-06 17:04:37 +03:00
)
templ List(quotes []model.Quote, page, count int) {
{{ host := ctx.Value(middleware.ContextKey("config")).(*config.Config).Host }}
2024-10-06 17:04:37 +03:00
@Layout(HeaderParams{
Title: "Цитатник Рунета",
Description: "Новый цитатник Рунета",
URL: host,
2024-10-06 17:04:37 +03:00
}) {
for _, q := range quotes {
@Quote(&q)
}
<span aria-busy="true" class="loader htmx-indicator">Загрузка...</span>
2024-10-06 17:04:37 +03:00
<nav>
<ul hx-boost="true" hx-indicator=".loader">
2024-10-06 17:04:37 +03:00
if page > 0 {
<li><a href={ templ.URL(fmt.Sprintf("/?page=%d", page-1)) }>&larr;</a></li>
}
for _, p := range generatePagination(page, count/20) {
if p == "..." {
<li>...</li>
} else if p == strconv.Itoa(page) {
<li>[{ p }]</li>
} else {
<li><a href={ templ.URL(fmt.Sprintf("/?page=%s", p)) }>{ p }</a></li>
}
}
if page < count/20 {
<li><a href={ templ.URL(fmt.Sprintf("/?page=%d", page+1)) }>&rarr;</a></li>
}
</ul>
</nav>
Всего { strconv.Itoa(count) } цитат.
}
}
func generatePagination(currentPage, totalPages int) []string {
pagination := make([]string, 0, 11)
if currentPage <= 3 {
for i := 0; i <= currentPage+3; i++ {
pagination = append(pagination, strconv.Itoa(i))
}
pagination = append(pagination, "...")
pagination = append(pagination, strconv.Itoa(totalPages-2))
pagination = append(pagination, strconv.Itoa(totalPages-1))
pagination = append(pagination, strconv.Itoa(totalPages))
} else if currentPage >= totalPages-3 {
pagination = append(pagination, "0")
pagination = append(pagination, "1")
pagination = append(pagination, "2")
pagination = append(pagination, "...")
for i := currentPage - 3; i <= totalPages; i++ {
pagination = append(pagination, strconv.Itoa(i))
}
} else {
pagination = append(pagination, "0")
pagination = append(pagination, "...")
for i := currentPage - 2; i <= currentPage+2; i++ {
pagination = append(pagination, strconv.Itoa(i))
}
pagination = append(pagination, "...")
pagination = append(pagination, strconv.Itoa(totalPages))
}
return pagination
}