pagination and sorting for reminders.
This commit is contained in:
54
src/cljs/auto_ap/views/components/paginator.cljs
Normal file
54
src/cljs/auto_ap/views/components/paginator.cljs
Normal file
@@ -0,0 +1,54 @@
|
||||
(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 20
|
||||
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
|
||||
[:li [:span.pagination-ellipsis "…"]]
|
||||
(last all-buttons)))
|
||||
|
||||
extended-first-page-button (when (not= first-page-button 0)
|
||||
(list
|
||||
(first all-buttons)
|
||||
[: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]]))
|
||||
Reference in New Issue
Block a user