forked from tastejs/todomvc
/
app.cljs
192 lines (161 loc) · 6.19 KB
/
app.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
(ns todomvc.app
(:require-macros [cljs.core.async.macros :refer [go]]
[secretary.macros :refer [defroute]])
(:require [goog.events :as events]
[cljs.core.async :refer [put! <! chan]]
[om.core :as om]
[om.dom :as dom]
[secretary.core :as secretary]
[todomvc.utils :refer [pluralize now guid store hidden]]
[clojure.string :as string]
[todomvc.item :as item])
(:import [goog History]
[goog.history EventType]))
(enable-console-print!)
(def ENTER_KEY 13)
(def app-state (atom {:showing :all :todos []}))
;; =============================================================================
;; Routing
(defroute "/" [] (swap! app-state assoc :showing :all))
(defroute "/:filter" [filter] (swap! app-state assoc :showing (keyword filter)))
(def history (History.))
(events/listen history EventType.NAVIGATE
(fn [e] (secretary/dispatch! (.-token e))))
(.setEnabled history true)
;; =============================================================================
;; Main and Footer Components
(declare toggle-all)
(defn visible? [todo filter]
(case filter
:all true
:active (not (:completed todo))
:completed (:completed todo)))
(defn main [{:keys [todos showing editing] :as app} comm]
(dom/section #js {:id "main" :style (hidden (empty? todos))}
(dom/input
#js {:id "toggle-all" :type "checkbox"
:onChange #(toggle-all % app)
:checked (every? :completed todos)})
(apply dom/ul #js {:id "todo-list"}
(om/build-all item/todo-item todos
{:init-state {:comm comm}
:key :id
:fn (fn [todo]
(cond-> todo
(= (:id todo) editing) (assoc :editing true)
(not (visible? todo showing)) (assoc :hidden true)))}))))
(defn make-clear-button [completed comm]
(when (pos? completed)
(dom/button
#js {:id "clear-completed"
:onClick #(put! comm [:clear (now)])}
(str "Clear completed (" completed ")"))))
(defn footer [app count completed comm]
(let [clear-button (make-clear-button completed comm)
sel (-> (zipmap [:all :active :completed] (repeat ""))
(assoc (:showing app) "selected"))]
(dom/footer #js {:id "footer" :style (hidden (empty? (:todos app)))}
(dom/span #js {:id "todo-count"}
(dom/strong nil count)
(str " " (pluralize count "item") " left"))
(dom/ul #js {:id "filters"}
(dom/li nil (dom/a #js {:href "#/" :className (sel :all)} "All"))
(dom/li nil (dom/a #js {:href "#/active" :className (sel :active)} "Active"))
(dom/li nil (dom/a #js {:href "#/completed" :className (sel :completed)} "Completed")))
clear-button)))
;; =============================================================================
;; Todos
(defn toggle-all [e app]
(let [checked (.. e -target -checked)]
(om/transact! app :todos
(fn [todos] (vec (map #(assoc % :completed checked) todos))))))
(defn handle-new-todo-keydown [e app owner]
(when (== (.-which e) ENTER_KEY)
(let [new-field (om/get-node owner "newField")]
(when-not (string/blank? (.. new-field -value trim))
(let [new-todo {:id (guid)
:title (.-value new-field)
:completed false}]
(om/transact! app :todos
#(conj % new-todo)
[:create new-todo]))
(set! (.-value new-field) "")))
false))
(defn destroy-todo [app {:keys [id]}]
(om/transact! app :todos
(fn [todos] (into [] (remove #(= (:id %) id) todos)))
[:delete id]))
(defn edit-todo [app {:keys [id]}] (om/update! app :editing id))
(defn save-todos [app] (om/update! app :editing nil))
(defn cancel-action [app] (om/update! app :editing nil))
(defn clear-completed [app]
(om/transact! app :todos
(fn [todos] (into [] (remove :completed todos)))))
(defn handle-event [type app val]
(case type
:destroy (destroy-todo app val)
:edit (edit-todo app val)
:save (save-todos app)
:clear (clear-completed app)
:cancel (cancel-action app)
nil))
(def render-start nil)
(defn todo-app [{:keys [todos] :as app} owner]
(reify
om/IWillMount
(will-mount [_]
(let [comm (chan)]
(om/set-state! owner :comm comm)
(go (while true
(let [[type value] (<! comm)]
(handle-event type app value))))))
om/IWillUpdate
(will-update [_ _ _] (set! render-start (now)))
om/IDidUpdate
(did-update [_ _ _]
(store "todos" todos)
(let [ms (- (.valueOf (now)) (.valueOf render-start))]
(set! (.-innerHTML (js/document.getElementById "message")) (str ms "ms"))))
om/IRenderState
(render-state [_ {:keys [comm]}]
(let [active (count (remove :completed todos))
completed (- (count todos) active)]
(dom/div nil
(dom/header #js {:id "header"}
(dom/h1 nil "todos")
(dom/input
#js {:ref "newField" :id "new-todo"
:placeholder "What needs to be done?"
:onKeyDown #(handle-new-todo-keydown % app owner)})
(main app comm)
(footer app active completed comm)))))))
(om/root todo-app app-state
{:target (.getElementById js/document "todoapp")})
(dom/render
(dom/div nil
(dom/p nil "Double-click to edit a todo")
(dom/p nil
(dom/a #js {:href "http://github.com/swannodette"}))
(dom/p nil
#js ["Part of"
(dom/a #js {:href "http://todomvc.com"} "TodoMVC")]))
(.getElementById js/document "info"))
;; =============================================================================
;; Benchmark Stuff
(aset js/window "benchmark1"
(fn [e]
(dotimes [_ 200]
(swap! app-state update-in [:todos] conj
{:id (guid) :title "foo" :completed false}))))
(aset js/window "benchmark2"
(fn [e]
(dotimes [_ 200]
(swap! app-state update-in [:todos] conj
{:id (guid) :title "foo" :completed false}))
(dotimes [_ 5]
(swap! app-state update-in [:todos]
(fn [todos]
(map #(assoc-in % [:completed] not) todos))))
(swap! app-state update-in [:todos]
(fn [todos]
(into [] (remove :completed todos))))))