From 06bd63eef6f247b4a9d775ce3a21d32e18048c4c Mon Sep 17 00:00:00 2001 From: unwox Date: Tue, 5 Nov 2024 16:42:26 +0600 Subject: improve paginator --- bin/serve.fnl | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) (limited to 'bin/serve.fnl') 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 @@ "₽ за 1 гр.)")] "")]]) -(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]]]]]]]]) -- cgit v1.2.3