2015-02-25 01:31:11 +00:00
|
|
|
(ns simpleexample.core
|
2015-07-24 14:34:45 +00:00
|
|
|
(:require-macros [reagent.ratom :refer [reaction]])
|
2015-07-10 06:31:19 +00:00
|
|
|
(:require [reagent.core :as reagent]
|
2015-03-06 00:09:40 +00:00
|
|
|
[re-frame.core :refer [register-handler
|
|
|
|
path
|
2015-04-17 12:22:43 +00:00
|
|
|
register-sub
|
|
|
|
dispatch
|
|
|
|
dispatch-sync
|
2015-02-25 22:38:22 +00:00
|
|
|
subscribe]]))
|
2015-02-25 01:31:11 +00:00
|
|
|
|
2015-04-17 12:22:43 +00:00
|
|
|
;; trigger a dispatch every second
|
2015-02-25 01:31:11 +00:00
|
|
|
(defonce time-updater (js/setInterval
|
|
|
|
#(dispatch [:timer (js/Date.)]) 1000))
|
|
|
|
|
2015-04-17 12:22:43 +00:00
|
|
|
(def initial-state
|
2015-02-25 01:31:11 +00:00
|
|
|
{:timer (js/Date.)
|
|
|
|
:time-color "#f34"})
|
|
|
|
|
|
|
|
|
2015-04-17 12:22:43 +00:00
|
|
|
;; -- Event Handlers ----------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
(register-handler ;; setup initial state
|
|
|
|
:initialize ;; usage: (submit [:initialize])
|
2015-07-24 14:34:45 +00:00
|
|
|
(fn
|
2015-02-25 01:31:11 +00:00
|
|
|
[db _]
|
2015-04-17 12:22:43 +00:00
|
|
|
(merge db initial-state))) ;; what it returns becomes the new state
|
|
|
|
|
2015-02-25 01:31:11 +00:00
|
|
|
|
2015-02-25 22:38:22 +00:00
|
|
|
(register-handler
|
2015-04-17 12:22:43 +00:00
|
|
|
:time-color ;; usage: (submit [:time-color 34562])
|
|
|
|
(path [:time-color]) ;; this is middleware
|
2015-02-25 01:31:11 +00:00
|
|
|
(fn
|
2015-04-17 12:22:43 +00:00
|
|
|
[time-color [_ value]] ;; path middleware adjusts the first parameter
|
2015-03-06 00:09:40 +00:00
|
|
|
value))
|
2015-02-25 01:31:11 +00:00
|
|
|
|
2015-04-17 12:22:43 +00:00
|
|
|
|
2015-02-25 22:38:22 +00:00
|
|
|
(register-handler
|
2015-02-25 01:31:11 +00:00
|
|
|
:timer
|
|
|
|
(fn
|
2015-07-24 14:34:45 +00:00
|
|
|
;; the first item in the second argument is :timer the second is the
|
2015-02-25 01:31:11 +00:00
|
|
|
;; new value
|
|
|
|
[db [_ value]]
|
2015-04-17 12:22:43 +00:00
|
|
|
(assoc db :timer value))) ;; return the new version of db
|
|
|
|
|
|
|
|
|
|
|
|
;; -- Subscription Handlers ---------------------------------------------------
|
|
|
|
|
2015-02-25 01:31:11 +00:00
|
|
|
|
2015-02-26 03:23:03 +00:00
|
|
|
(register-sub
|
2015-02-25 22:38:22 +00:00
|
|
|
:timer
|
2015-07-24 14:34:45 +00:00
|
|
|
(fn
|
2015-04-17 12:22:43 +00:00
|
|
|
[db _] ;; db is the app-db atom
|
2015-07-08 17:28:42 +00:00
|
|
|
(reaction (:timer @db)))) ;; wrap the computation in a reaction
|
2015-04-17 12:22:43 +00:00
|
|
|
|
2015-02-25 22:38:22 +00:00
|
|
|
|
2015-02-26 03:23:03 +00:00
|
|
|
(register-sub
|
2015-02-25 22:38:22 +00:00
|
|
|
:time-color
|
2015-07-24 14:34:45 +00:00
|
|
|
(fn
|
2015-02-25 22:38:22 +00:00
|
|
|
[db _]
|
|
|
|
(reaction (:time-color @db))))
|
|
|
|
|
2015-02-25 01:31:11 +00:00
|
|
|
|
2015-04-17 12:22:43 +00:00
|
|
|
;; -- View Components ---------------------------------------------------------
|
2015-02-25 01:31:11 +00:00
|
|
|
|
2015-04-17 12:22:43 +00:00
|
|
|
(defn greeting
|
|
|
|
[message]
|
2015-02-25 01:31:11 +00:00
|
|
|
[:h1 message])
|
|
|
|
|
2015-04-17 12:22:43 +00:00
|
|
|
|
|
|
|
(defn clock
|
|
|
|
[]
|
2015-02-25 22:38:22 +00:00
|
|
|
(let [time-color (subscribe [:time-color])
|
|
|
|
timer (subscribe [:timer])]
|
2015-04-17 12:22:43 +00:00
|
|
|
|
|
|
|
(fn clock-render
|
|
|
|
[]
|
|
|
|
(let [time-str (-> @timer
|
|
|
|
.toTimeString
|
|
|
|
(clojure.string/split " ")
|
|
|
|
first)
|
|
|
|
style {:style {:color @time-color}}]
|
|
|
|
[:div.example-clock style time-str]))))
|
|
|
|
|
|
|
|
|
|
|
|
(defn color-input
|
|
|
|
[]
|
2015-02-25 22:38:22 +00:00
|
|
|
(let [time-color (subscribe [:time-color])]
|
2015-04-17 12:22:43 +00:00
|
|
|
|
|
|
|
(fn color-input-render
|
|
|
|
[]
|
|
|
|
[:div.color-input
|
|
|
|
"Time color: "
|
|
|
|
[:input {:type "text"
|
|
|
|
:value @time-color
|
2015-07-09 22:03:54 +00:00
|
|
|
:on-change #(dispatch-sync
|
2015-04-17 12:22:43 +00:00
|
|
|
[:time-color (-> % .-target .-value)])}]])))
|
|
|
|
|
|
|
|
(defn simple-example
|
|
|
|
[]
|
2015-02-25 01:31:11 +00:00
|
|
|
[:div
|
|
|
|
[greeting "Hello world, it is now"]
|
|
|
|
[clock]
|
|
|
|
[color-input]])
|
|
|
|
|
2015-04-17 12:22:43 +00:00
|
|
|
|
|
|
|
;; -- Entry Point -------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
(defn ^:export run
|
|
|
|
[]
|
|
|
|
(dispatch-sync [:initialize])
|
2015-02-25 01:31:11 +00:00
|
|
|
(reagent/render [simple-example]
|
2015-04-17 12:22:43 +00:00
|
|
|
(js/document.getElementById "app")))
|