summaryrefslogtreecommitdiff
path: root/bin/serve.fnl
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-11-05 16:42:26 +0600
committerunwox <me@unwox.com>2024-11-05 16:55:34 +0600
commit06bd63eef6f247b4a9d775ce3a21d32e18048c4c (patch)
tree9369c343be923359dda5233e662eea8ce19d6284 /bin/serve.fnl
parentad9691cf051c205c992064ab9d6c46c2aba79dd6 (diff)
improve paginator
Diffstat (limited to 'bin/serve.fnl')
-rw-r--r--bin/serve.fnl43
1 files changed, 29 insertions, 14 deletions
diff --git a/bin/serve.fnl b/bin/serve.fnl
index 06ecd22..dde1138 100644
--- a/bin/serve.fnl
+++ b/bin/serve.fnl
@@ -310,15 +310,11 @@
"₽ за&nbsp;1&nbsp;гр.)")]
"")]])
-(fn paginator-template [form page limit total]
+(fn paginator-template [form page limit total max-numbers]
(local last-page (math.ceil (/ total limit)))
-
- (if (< limit total)
- [:div {:class "paginator"}
- [:div {:class "paginator-numbers"}
- (if (< 1 page)
- [:a {:href (form->path (- page 1) form)} "←"]
- "")
+ (local radius (math.floor (/ (- max-numbers 4) 2)))
+ (local number-items
+ (if (<= last-page max-numbers)
(faccumulate [res [:span {}] i 1 last-page]
(do
(table.insert
@@ -326,9 +322,29 @@
:class (if (= page i) "paginator-active" "")}
(tostring i)])
res))
- (if (< page last-page)
- [:a {:href (form->path (+ page 1) form)} "→"]
- "")]
+ [:span {}
+ (table.unpack
+ (array.concat
+ (if (< (+ 1 radius) page)
+ [[:a {:href (form->path 1 form)} "1"]
+ (if (< (+ 2 radius) page) "…" "")]
+ [])
+ (faccumulate [res [] i (math.max 1 (- page radius))
+ (math.min last-page (+ page radius))]
+ (do
+ (table.insert
+ res [:a {:href (form->path i form)
+ :class (if (= page i) "paginator-active" "")}
+ (tostring i)])
+ res))
+ (if (< radius (- last-page page))
+ [(if (< (+ 1 radius) (- last-page page)) "…" "")
+ [:a {:href (form->path last-page form)} (tostring last-page)]]
+ [])))]))
+
+ (if (< limit total)
+ [:div {:class "paginator"}
+ [:div {:class "paginator-numbers"} number-items]
[:div {} "Всего: " [:strong {} (string.format "%d" total)]]]
""))
@@ -406,7 +422,6 @@
paginator]])
(fn base-template [form path page total items text]
- (local paginator (paginator-template form page 48 total))
(local menu-path
(if (and form.tags (< 0 (# form.tags)))
(category-menu-path (. form.tags 1))
@@ -434,7 +449,7 @@
[:body {}
[:div {:class "container"}
[:div {:class "content"}
- (aside-template form path paginator)
+ (aside-template form path (paginator-template form page 48 total 7))
[:main {}
(menu-template path)
[:article {:class "description"}
@@ -457,7 +472,7 @@
spellfix-suggestion
spellfix-suggestion)]
[:NO-ESCAPE texts.no-results])])
- [:div {} paginator]
+ [:div {} (paginator-template form page 48 total 17)]
[:footer {}
[:small {:class "text"} [:NO-ESCAPE texts.footer-text]]]]]]]])