aboutsummaryrefslogtreecommitdiff
path: root/pkg/tpl/index.templ
blob: b405f3e4ee4988e2adb04e98be30afc599bc6c53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package tpl

import (
	"fmt"
	"sh.org.ru/pkg/model"
	"strconv"
)

templ Index(quotes []model.Quote, page, count int) {
	@Layout(HeaderParams{
		Title: "Цитатник Рунета",
		Description: "Новый цитатник Рунета",
		URL: "https://sh.org.ru/",
	}) {
		for _, q := range quotes {
			@Quote(&q)
		}
		<nav>
			<ul>
				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
}