-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.cljs
192 lines (172 loc) · 5.04 KB
/
todo.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
(ns recurrent-examples.examples.todo
(:require
recurrent.drivers.dom
[clojure.string :as string]
[recurrent.core :as recurrent :include-macros true]
[recurrent.drivers.state :as state]
[ulmus.signal :as ulmus]))
(def initial-state
{:todos [{:text "Make a todo app"
:completed? true}
{:text "????"
:completed? false}
{:text "Profit"
:completed? false}]})
(recurrent/defcomponent TodoInput
[]
(let [return-$
(ulmus/filter
(fn [e] (= (.-keyCode e) 13))
($ :recurrent/dom-$ "input" "keypress"))
value-$
(ulmus/start-with! ""
(ulmus/merge
(ulmus/map
(fn [e]
(.-value (.-target e)))
($ :recurrent/dom-$ "input" "input"))
(ulmus/map
(constantly "")
return-$)))
submit-$
(ulmus/sample-on
(ulmus/map
first
(ulmus/slice 2 value-$))
return-$)]
{:clear-all-$
($ :recurrent/dom-$ ".clear-all" "click")
:submit-$
submit-$
:recurrent/dom-$
(ulmus/map
(fn [value]
[:div {:id "what-needs-done"}
[:div {:class "clear-all"} "<"]
[:input {:type "text"
:placeholder "What needs to be done?"
:value value}]])
value-$)}))
(recurrent/defcomponent Todo
[mode-$]
{:close-$
(ulmus/sample-on
($ :recurrent/state-$)
(ulmus/map
(fn [e] (.stopPropagation e))
($ :recurrent/dom-$ ".close" "click")))
:recurrent/dom-$
(ulmus/map
(fn [[todo mode]]
[:div (merge
{:class
(str "todo "
(if (:completed? todo) "completed"))}
(if (or (and (= mode :outstanding)
(:completed? todo))
(and (= mode :completed)
(not (:completed? todo))))
{:style {:display "none"}}))
(:text todo)
[:div {:class "close"} "X"]])
(ulmus/zip
($ :recurrent/state-$)
mode-$))
:recurrent/state-$
(ulmus/map
(fn []
#(assoc % :completed? (not (:completed? %))))
($ :recurrent/dom-$ :root "click"))})
(recurrent/defcomponent ViewBar
[]
(let [mode-$
(ulmus/start-with! :all
(ulmus/map
(fn [e]
(keyword (.getAttribute (.-target e) "data-mode")))
($ :recurrent/dom-$ ".mode" "click")))
mode-fn
(fn [selected-mode mode]
[:div {:data-mode mode
:class
(str "mode "
(if (= selected-mode (keyword mode))
"selected"))}
(string/capitalize mode)])]
{:mode-$
mode-$
:recurrent/dom-$
(ulmus/map
(fn [mode]
(let [make-mode (partial mode-fn mode)]
[:div {:class "view-bar"}
[:h4 "View"]
(make-mode "all")
(make-mode "outstanding")
(make-mode "completed")]))
mode-$)}))
(recurrent/defcomponent TodoFRP
[]
(let [todo-input (! TodoInput)
view-bar (! ViewBar)
todos (! (state/isolate
[:todos]
(state/collection Todo))
(:mode-$ view-bar))]
{:recurrent/dom-$
(ulmus/map
(fn [[todo-input todos view-bar]]
[:div {:id "todo-frp" :class "example"}
[:h1 "Todo FRP"]
[:a {:href "https://github.com/jeremykross/recurrent-examples/blob/master/src/recurrent_examples/examples/todo.cljs"
:target "_blank"
:rel "noopener noreferrer"} "Source"]
[:div {:class "content"}
todo-input
todos
view-bar]])
(ulmus/zip
(:recurrent/dom-$ todo-input)
(:recurrent/dom-$ todos)
(:recurrent/dom-$ view-bar)))
:recurrent/state-$
(ulmus/merge
(:recurrent/state-$ todos)
(ulmus/map
(fn [new-todo]
(fn [state]
(update
state :todos
conj
{:text new-todo
:completed? false})))
(:submit-$ todo-input))
(ulmus/map
(fn [remove-todo]
(fn [state]
(update
state :todos
(fn [todos]
(into
[]
(remove #(= % remove-todo) todos))))))
(ulmus/pickmerge :close-$ (:elements-$ todos)))
(ulmus/map
(fn []
(fn [state]
(update
state :todos
(fn [todos]
(mapv (fn [todo] (assoc
todo
:completed? true))
todos)))))
(:clear-all-$ todo-input)))}))
(defn start!
[]
(recurrent/start!
TodoFRP
{:recurrent/dom-$
(recurrent.drivers.dom/render-into! "app")
:recurrent/state-$
(recurrent.drivers.state/create-store! initial-state)}))