-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
toolbar.cljs
165 lines (161 loc) · 6.38 KB
/
toolbar.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
(ns swarmpit.component.toolbar
(:require [material.icon :as icon]
[material.components :as comp]
[sablono.core :refer-macros [html]]
[clojure.string :refer [join]]
[rum.core :as rum]))
(rum/defc menu-popper < rum/reactive [items-hash items anchor]
(comp/popper
{:open (some? @anchor)
:anchorEl @anchor
:placement "bottom-end"
:className "Swarmpit-popper"
:disablePortal true
:transition true}
(fn [props]
(let [{:keys [TransitionProps placement]} (js->clj props :keywordize-keys true)]
(comp/fade
(merge TransitionProps
{:timeout 450})
(comp/paper
(comp/click-away-listener
{:onClickAway #(reset! anchor nil)}
(comp/menu-list
(map
#(comp/menu-item
{:key (str "cmmi-" items-hash "-" (:name %))
:disabled (:disabled %)
:onClick (fn []
((:onClick %))
(reset! anchor nil))}
(comp/list-item-icon
{:key (str "cmmii-" items-hash "-" (:name %))
:className "Swarmpit-menu-icon"} (:icon %))
(comp/typography
{:variant "inherit"
:key (str "cmmit-" items-hash "-" (:name %))} (:name %))) items)))))))))
(rum/defcs menu < rum/static
(rum/local nil :menu/anchor)
[{anchor :menu/anchor} items]
(let [items-hash (join (map :name items))
main-action (first (filter #(= true (:main %)) items))
rest-actions (filter #(not= true (:main %)) items)]
(comp/box
{}
(comp/button-group
{:className "Swarmpit-form-toolbar-btn"
:variant "contained"
:color "primary"
:aria-label "split button"}
(comp/button
{:startIcon (:icon main-action)
:onClick (:onClick main-action)}
(:name main-action))
(comp/button
{:color "primary"
:size "small"
:aria-label "select action"
:aria-haspopup "menu"
:onClick #(reset! anchor (.-currentTarget %))}
(icon/arrow-dropdown {})))
(menu-popper items-hash rest-actions anchor))))
(rum/defc toolbar < rum/reactive [domain id actions]
(let [group-actions (filter #(= true (:group %)) actions)
single-actions (filter #(not= true (:group %)) actions)]
(comp/mui
(comp/toolbar
{:disableGutters true
:className "Swarmpit-ftoolbar"}
(comp/grid
{:container true
:spacing 3
:alignItems "flex-end"
:justify "space-between"}
(comp/grid
{:item true}
(comp/box
{:className "Swarmpit-ftoolbar-info"}
(comp/typography
{:variant "h6"
:className "Swarmpit-ftoolbar-title"
:noWrap false}
domain)
(comp/typography
{:variant "h6"
:className "Swarmpit-ftoolbar-subtitle"
:noWrap false}
id)))
(comp/grid
{:item true}
(comp/box
{:className "Swarmpit-ftoolbar-actions"}
(when (not-empty group-actions)
(menu group-actions))
(when (not-empty single-actions)
(map-indexed
(fn [index action]
(comp/button
(merge
{:color (or (:color action) "primary")
:variant (or (:variant action) "contained")
:key (str "toolbar-button-" index)
:startIcon (:icon action)
:onClick (:onClick action)}
(when (not= (dec (count single-actions)) index)
{:className "Swarmpit-form-toolbar-btn"}))
(:name action))) single-actions)))))))))
(rum/defc list-toobar < rum/reactive
[title items filtered-items actions]
(comp/mui
(comp/toolbar
{:disableGutters true
:className "Swarmpit-ftoolbar"}
(comp/grid
{:container true
:spacing 3
:alignItems "flex-end"
:justify "space-between"}
(comp/grid
{:item true}
(comp/box
{:className "Swarmpit-ftoolbar-info"}
(comp/typography
{:variant "subtitle1"
:className "Swarmpit-ftoolbar-title"
:noWrap false}
(if (= (count items)
(count filtered-items))
(str "Total (" (count items) ")")
(html [:span "Total (" [:b (count filtered-items)] "/" (count items) ")"])))))
(comp/grid
{:item true}
(comp/box
{:className "Swarmpit-ftoolbar-actions"}
(when (not-empty actions)
(map-indexed
(fn [index action]
(comp/box
{:key (str "toolbar-item-" index)}
(comp/box
{}
(comp/button
(merge
{:color (or (:color action) "primary")
:variant (or (:variant action) "contained")
:key (str "toolbar-button-" index)
:startIcon (:icon action)
:onClick (:onClick action)}
(when (not= (dec (count actions)) index)
{:className "Swarmpit-form-toolbar-btn"}))
(:name action)))
(comp/box
{:className "Swarmpit-section-mobile"}
;; Make FAB from first only (primary action)
(when (:primary action)
(comp/fab
{:className "Swarmpit-fab"
:color "primary"
:size "large"
:aria-label "add"
:onClick (:onClick action)}
(:icon-alt action)))))) actions))))))))