/
controls.cljs
346 lines (319 loc) · 16.3 KB
/
controls.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
(ns im-tables.views.table.head.controls
(:require [re-frame.core :refer [subscribe dispatch]]
[reagent.core :as reagent]
[im-tables.views.graphs.histogram :as histogram]
[im-tables.views.common :refer [no-value]]
[goog.i18n.NumberFormat.Format]
[imcljs.path :as path]
[clojure.string :as string]
[oops.core :refer [oget ocall ocall! oapply oget+]])
(:import
(goog.i18n NumberFormat)
(goog.i18n.NumberFormat Format)))
(def nff
(NumberFormat. Format/DECIMAL))
(defn- nf
[num]
(.format nff (str num)))
(defn filter-input []
(fn [loc view val]
[:div.inline-filter [:i.fa.fa-filter]
[:input.form-control
{:type "text"
:value val
:placeholder "Search for a value..."
:on-change (fn [e]
(dispatch [:select/set-text-filter
loc
view
(oget e :target :value)]))}]]))
(defn force-close
"Force a dropdown to close "
[component]
(-> (js/$ (reagent/dom-node component))
(ocall! "closest" ".dropdown-menu")
(ocall! "parent")
(ocall! "removeClass" "open")))
(defn has-text?
"Return true if a label contains a string"
[needle haystack]
(if needle
(if-let [text-to-search (:item haystack)]
(re-find (re-pattern (str "(?i)" needle)) (:item haystack))
false)
true))
(defn constraint-has-path? [view constraint]
(= view (:path constraint)))
(defn constraint-dropdown []
(fn [{:keys [value on-change]}]
[:select.form-control
{:value (if value value "=")
:on-change (fn [e] (on-change {:op (.. e -target -value)}))}
[:option {:value ">"} ">"]
[:option {:value ">="} ">="]
[:option {:value "<"} "<"]
[:option {:value "<="} "<="]
[:option {:value "="} "="]
[:option {:value "!="} "!="]
[:option {:value "LIKE"} "LIKE"]
[:option {:value "NOT LIKE"} "NOT LIKE"]
[:option {:value "CONTAINS"} "CONTAINS"]
[:option {:value "ONE OF"} "ONE OF"]
[:option {:value "NONE OF"} "NONE OF"]]))
(defn constraint-text []
(fn [{:keys [value on-change]}]
[:input.form-control {:type "text"
:on-change (fn [e] (on-change {:value (.. e -target -value)}))
:value value}]))
(defn blank-constraint [loc path state]
(fn [loc path]
(let [submit-constraint (fn [] (dispatch
[:filters/add-constraint loc @state]
(reset! state {:path path :op "=" :value nil})))]
[:div.imtable-constraint
[:div.constraint-operator
[constraint-dropdown
{:value (:op @state)
:on-change (fn [v] (swap! state assoc :op (:op v)))}]]
[:div.constraint-input [:input.form-control
{:type "text"
:value (:value @state)
:on-change (fn [e] (swap! state assoc :value (.. e -target -value)))
;:on-blur (fn [e] (when (not (clojure.string/blank? (.. e -target -value)))
; (submit-constraint)))
:on-key-press
(fn [e]
(let [keycode (.-charCode e)
input (.. e -target -value)]
;; submit when pressing enter & not blank.
(when (and (= keycode 13) (not (clojure.string/blank? input)))
(submit-constraint)
)))}]]
])))
(defn constraint []
(fn [loc {:keys [path op value values code] :as const}]
(letfn [(on-change [new-value] (dispatch [:filters/update-constraint loc (merge const new-value)]))]
[:div.imtable-constraint
[:div.constraint-operator
[constraint-dropdown {:value op
:on-change on-change}]]
[:div.constraint-input
[constraint-text {:value (or value values)
:on-change on-change}]]
[:button.btn.btn-danger
{:on-click (fn [] (dispatch [:filters/remove-constraint loc const]))
:type "button"} [:i.fa.fa-times]]])))
(defn filter-view [loc view blank-constraint-atom]
(let [response (subscribe [:selection/response loc view])
selections (subscribe [:selection/selections loc view])
query (subscribe [:main/temp-query loc view])]
(fn [loc view]
(let [active-filters (map (fn [c] [constraint loc c]) (filter (partial constraint-has-path? view) (:where @query)))
dropdown (reagent/current-component)]
[:form.form.filter-view {:style {:padding "5px"}
:on-submit (fn [e]
(ocall e "preventDefault")
(force-close dropdown)
(dispatch [:filters/save-changes loc]))}
[:div.alert.alert-success
(if (seq active-filters)
(into [:div [:h4 "Active filters:"]] active-filters)
[:h4 "No active filters"])]
[:div.alert.alert-default
[:h4 "Add a new filter:"]
; Note that we're storing the blank constraint's
[blank-constraint loc view blank-constraint-atom]]
[:div.btn-toolbar.pull-right
[:div.btn-group
[:button.btn.btn-default
{:on-click (fn [] (dispatch
[:filters/add-constraint loc @blank-constraint-atom]
(reset! blank-constraint-atom {:path view :op "=" :value nil})))
:type "button"} "Add More"]]
[:div.btn-group
[:input.btn.btn-primary.pull-right
{:type "submit"
:on-click (fn []
(when (not (clojure.string/blank? (:value @blank-constraint-atom)))
(dispatch
[:filters/add-constraint loc @blank-constraint-atom]
(reset! blank-constraint-atom {:path view :op "=" :value nil}))))
; don't put :data-toggle "dropdown" in here, it stops
; the form submitting.... silently. Nice.
:value "Apply"
}]]]]))))
(defn too-many-values []
(fn []
[:div.alert.alert-warning
[:div
{:style {:padding "10px"}}
[:h3 "Column summary not available"]
[:p "Summaries can only be made on columns with 1,000 values or less."]]]))
(def clj-min min)
(def clj-max max)
(defn numerical-column-summary []
(let []
(fn [loc view results trimmer]
(let [model (subscribe [:assets/model loc])
{:keys [min max average stdev]} (first results)
close-fn (partial force-close (reagent/current-component))
display-name (str (string/join " " (take-last 2 (string/split (path/friendly @model view) " > "))) "s")]
[:form.form.column-summary
[:h4 (str "Showing numerical distribution for " (count results) " " display-name)]
[histogram/numerical-histogram results @trimmer]
[:div.main-view
[:div.numerical-content-wrapper
[:table.table.table-condensed
[:thead
[:tr [:th "Min"] [:th "Max"] [:th "Average"] [:th "Std Deviation"]]]
[:tbody
[:tr [:td (nf min)] [:td (nf max)] [:td (nf average)] [:td (nf stdev)]]]]
[:div
[:label "Trim from " [:input {:type "text"
:value (or (:from @trimmer) min)
:on-change (fn [e]
(swap! trimmer assoc :from (oget e :target :value)))}]]
[:input {:type "range"
:min min
:value (or (:from @trimmer) min)
:max max
:on-change (fn [e] (swap! trimmer assoc :from
(let [new-value (js/parseInt (clj-min (or (:to @trimmer) max) (oget e :target :value)))]
(if (= new-value min) nil new-value))))}]
[:label "Trim to " [:input {:type "text"
:value (or (:to @trimmer) max)
:placeholder max
:on-change (fn [e]
(swap! trimmer assoc :to (oget e :target :value)))}]]
[:input {:type "range"
:min min
:value (or (:to @trimmer) max)
:max max
:on-change (fn [e] (swap! trimmer assoc :to
(let [new-value (js/parseInt (clj-max (or (:from @trimmer) min) (oget e :target :value)))]
(if (= new-value max) nil new-value))))}]]]
[:div.btn-toolbar.column-summary-toolbar
[:button.btn.btn-primary
{:type "button"
:on-click (fn []
(dispatch [:main/apply-numerical-filter loc view @trimmer])
(close-fn)
(reset! trimmer {}))}
[:i.fa.fa-filter]
(str " Filter")]]]]))))
(defn column-summary [loc view local-state]
(let [response (subscribe [:selection/response loc view])
selections (subscribe [:selection/selections loc view])
text-filter (subscribe [:selection/text-filter loc view])]
(reagent/create-class
{:component-will-mount
(fn [])
:component-will-update
(fn [])
:reagent-render
(fn [loc view]
(let [close-fn (partial force-close (reagent/current-component))]
(if (false? @response)
[too-many-values]
(if (contains? (first (:results @response)) :min)
[numerical-column-summary loc view (:results @response) local-state]
[:form.form.column-summary
[:div.main-view
[histogram/main (:results @response)]
[filter-input loc view @text-filter]
[:table.table.table-striped.table-condensed
[:thead [:tr [:th
(if (empty? @selections)
[:span {:title "Select all"
:on-click (fn [] (dispatch [:select/select-all loc view]))} [:i.fa.fa-check-square-o]]
[:span {:title "Deselect all"
:on-click (fn [] (dispatch [:select/clear-selection loc view]))} [:i.fa.fa-square-o]])
] [:th "Item"] [:th "Count"]]]
(into [:tbody]
(->> (filter (partial has-text? @text-filter) (:results @response))
(map (fn [{:keys [count item]}]
[:tr.hoverable
{:on-click (fn [e] (dispatch [:select/toggle-selection loc view item]))}
[:td
[:input
{:on-change (fn [])
:checked (contains? @selections item)
:type "checkbox"}]]
[:td (if item item [no-value])]
[:td
[:div count]]]))))]]
[:div.btn-toolbar.column-summary-toolbar
[:button.btn.btn-primary
{:type "button"
:on-click (fn []
(dispatch [:main/apply-summary-filter loc view])
(close-fn))}
[:i.fa.fa-filter]
(str " Filter")]]]))))})))
(defn filter-dropdown-menu [loc view idx col-count]
(let [query (subscribe [:main/query loc view])
blank-constraint-atom (reagent/atom {:path view :op "=" :value nil})]
(fn [loc view idx col-count right?]
(let [active-filters? (not-empty (filter (partial constraint-has-path? view) (:where @query)))]
[:span.dropdown
; Bootstrap and ReactJS don't always mix well. Components that make up dropdown menus are only
; mounted (reactjs) once and then their visibility is toggled (bootstrap). These means any local state
; in the dropdown menu does NOT get reset when the dropdown menu disappears because its never really unmounts.
; This was causing the new constraint textbox to retain its value if a user entered something but changed their
; mind and closed the dropdown. Reopening it would show what they previously entered. Making it look like it had been applied.;
; To fix this we've create the black constraint local atom all the way up here at the dropdown level so that we can reset
; it manually, and then we pass the atom down to the black constraint component. Lame.
{:on-click (fn []
; Reset the blank constraint atom when the dropdown is opened
(reset! blank-constraint-atom {:path view :op "=" :value nil}))
:ref (fn [e]
; *Try* to save the changes every time the dropdown is closed, even by just clicking off it.
; This means a user can remove a handful of filters without having to click Apply.
; The event will do a diff to make sure something has actually changed before rerunning the query
(some-> e js/$ (ocall :on "hide.bs.dropdown" (fn [] (dispatch [:filters/save-changes loc])))))}
[:i.fa.fa-filter.dropdown-toggle.filter-icon
{:on-click (fn [] (dispatch [:main/set-temp-query loc]))
:data-toggle "dropdown"
:class (cond active-filters? "active-filter")
:title (str "Filter " view " column")}]
; Crudely try to draw the dropdown near the middle of the page
[:div.dropdown-menu
{:class (when right? "dropdown-right")} [filter-view loc view blank-constraint-atom]]]))))
(defn obj->clj [obj]
(reduce (fn [total next-key]
(assoc total (keyword next-key) (oget+ obj next-key))) {} (js-keys obj)))
(defn align-right? [dom-node]
(let [{left :left} (obj->clj (ocall dom-node :getBoundingClientRect))
screen-width (oget js/window :innerWidth)]
(> left (/ screen-width 2))))
(defn toolbar []
(let [right? (reagent/atom false)]
(fn [loc view idx col-count]
(let [query (subscribe [:main/temp-query loc view])
active-filters? (seq (map (fn [c] [constraint loc c]) (filter (partial constraint-has-path? view) (:where @query))))
local-state (reagent/atom {})]
[:div.summary-toolbar
{:ref (fn [e]
(when e (reset! right? (align-right? e))))}
[:i.fa.fa-sort.sort-icon
{:on-click (fn [] (dispatch [:main/sort-by loc view]))
:title (str "Sort " view " column")}]
[:i.fa.fa-times.remove-icon
{:on-click (fn [] (dispatch [:main/remove-view loc view]))
:title (str "Remove " view " column")}]
[filter-dropdown-menu loc view idx col-count @right?]
[:span.dropdown
{:ref (fn [e]
; Bind an event to clear the selected items when the dropdown closes.
; Why don't we just avoid state all together and pick up the checkbox values
; when the user clicks "Filter"? Because we still want to know what's selected
; (for instance, highlighting the histogram).
; Use some-> because e isn't guaranteed to hold a value
(some-> e js/$ (ocall :on "hide.bs.dropdown" (fn []
(reset! local-state {})
(dispatch [:select/clear-selection loc view])))))}
[:i.fa.fa-bar-chart.dropdown-toggle {:data-toggle "dropdown"}]
[:div.dropdown-menu
{:title (str "Summarise " view " column")
:class (when @right? "dropdown-right")}
[column-summary loc view local-state]]]]))))