-
Notifications
You must be signed in to change notification settings - Fork 0
/
libs.cljs
82 lines (71 loc) · 2.68 KB
/
libs.cljs
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
73
74
75
76
77
78
79
80
81
82
(ns libs)
(require '[clojure.edn :as edn]
'[reagent.core :as r]
'[ajax.core :refer [GET]]
'[clojure.string :as str])
;; (def data (slurp "https://scicloj.github.io/docs/resources/model.edn"))
(def state (r/atom {
:clicks 0
:raw-response nil
}))
(defn set-raw-response! [response]
(swap! state assoc :raw-response response)
(.DataTable (js/$ "#table2")))
(defn view [data]
[:pre (pr-str data)])
(defn view-table
[{:keys [rows header viewers derived-rows id]}]
(let [prep-row (fn [row]
(reduce (fn [row [row-id create-derived]]
(assoc row row-id (create-derived row)))
row
derived-rows))]
[:table {:id id}
[:thead
(for [h header]
[:th h])]
[:tbody
(for [r rows]
(let [r (prep-row r)]
[:tr
(for [h header]
(let [view (or (h viewers) identity)]
[:td (view (h r))]))]))]]))
(defn scicloj-libs-model->table [model]
{:header [:library :star :description #_#_ :lib/name :lib/url #_:lib/category :tags]
:rows (apply concat (:libs model))
:viewers {:star (fn [item]
(when (= item :star)
"⭐"))
:lib/url (fn [url]
(when url
(let [shortname (str/replace url #"https://github.com/" "")]
[:a {:href url} shortname])))}
:derived-rows {:library (fn [row]
[:a {:href (:lib/url row)} (:lib/name row)])}})
(defn table []
[:div
#_#_
[:p "Clacks: " (:clicks @state)]
[:p [:button {:on-click #(swap! state update :clicks inc)}
"Click me!"]]
[:h2 "Table of contents"]
[:ol
[:li [:a {:href "#plain"} "Plain table"]]
[:li [:a {:href "#fancy"} "Fancy table"]]]
(when-let [response (:raw-response @state)]
(let [model (edn/read-string response)]
[:div
[:h2 "What data do we have?"]
[view (keys (ffirst (:libs model)))]
[:p "Slight problem: description contains markdown."
#_" So we should really try to render that properly."]
[:h2 {:id "plain"} "Plain HTML table"]
[view-table (merge {:id "table1"}
(scicloj-libs-model->table model))]
[:h2 {:id "fancy"} "Fancy table"]
[:p "Try using a " [:a {:href "https://datatables.net/"} "JQuery data table"] "."]
[view-table (merge {:id "table2"}
(scicloj-libs-model->table model))]
]))])
(GET "https://scicloj.github.io/docs/resources/model.edn" {:handler set-raw-response!})