56 lines
2.1 KiB
Clojure
56 lines
2.1 KiB
Clojure
(ns auto-ap.views.components.paginator
|
|
(:require [re-frame.core :as re-frame]
|
|
[auto-ap.subs :as subs]
|
|
[auto-ap.views.utils :refer [date->str]]
|
|
[reagent.core :as reagent]
|
|
[clojure.string :as str]
|
|
[cljs-time.format :as format]))
|
|
|
|
|
|
(defn bound [x y z]
|
|
(cond
|
|
(< z x)
|
|
x
|
|
(< y x)
|
|
x
|
|
(> y z)
|
|
z
|
|
:else
|
|
y))
|
|
|
|
(defn paginator [{:keys [start end count total on-change]}]
|
|
(let [per-page 100
|
|
max-buttons 5
|
|
buttons-before (Math/floor (/ max-buttons 2))
|
|
total-pages (Math/ceil (/ total per-page))
|
|
current-page (Math/floor (/ start per-page))
|
|
first-page-button (bound 0 (- current-page buttons-before) (- total-pages max-buttons))
|
|
all-buttons (into [] (for [x (range total-pages)]
|
|
^{:key x}
|
|
[:li
|
|
[:a.pagination-link {:class (when (= current-page x)
|
|
"is-current")
|
|
:on-click (fn [e] (on-change {:start (* x per-page)}))}
|
|
(inc x)]]))
|
|
|
|
|
|
last-page-button (Math/min total-pages (+ max-buttons first-page-button))
|
|
|
|
extended-last-page-button (when (not= last-page-button total-pages)
|
|
(list
|
|
^ {:key -1} [:li [:span.pagination-ellipsis "…"]]
|
|
^ {:key -2} (last all-buttons)))
|
|
|
|
extended-first-page-button (when (not= first-page-button 0)
|
|
(list
|
|
^{:key -1} (first all-buttons)
|
|
^{:key -2} [:li [:span.pagination-ellipsis "…"]]))]
|
|
|
|
|
|
[:nav.pagination {:role "pagination"}
|
|
[:ul.pagination-list
|
|
extended-first-page-button
|
|
(apply list (subvec all-buttons first-page-button last-page-button))
|
|
extended-last-page-button
|
|
"Showing " (Math/min (inc start) total) "-" end "/" total]]))
|