reagent/demo/reagentdemo/news/binaryclock.cljs

51 lines
1.3 KiB
Plaintext
Raw Normal View History

2014-02-22 11:36:34 +00:00
(ns reagentdemo.news.binaryclock
2014-02-22 16:24:22 +00:00
(:require [reagent.core :as r :refer [atom]]))
2014-02-22 11:36:34 +00:00
(defn cell [n bit]
2014-02-22 16:24:22 +00:00
[:div.clock-cell {:class (if (bit-test n bit)
"light"
"dark")}])
2014-02-22 11:36:34 +00:00
(defn column [n]
2014-02-22 16:24:22 +00:00
[:div.clock-col
2014-02-22 11:36:34 +00:00
[cell n 3]
[cell n 2]
[cell n 1]
[cell n 0]
2014-02-22 16:24:22 +00:00
[:div.clock-cell n]])
2014-02-22 11:36:34 +00:00
(defn column-pair [n]
2014-02-22 16:24:22 +00:00
[:div.clock-pair
2014-02-22 11:36:34 +00:00
[column (quot n 10)]
[column (mod n 10)]])
2014-02-22 16:24:22 +00:00
(defn legend [& items]
(into [:div.clock-col.clock-legend]
(map (partial vector :div.clock-cell)
items)))
2014-02-22 11:36:34 +00:00
2014-02-22 16:24:22 +00:00
(defn clock [date show-100s toggle-100s]
[:div.clock-main {:on-click toggle-100s
:class (when show-100s "wide")}
2014-02-22 11:36:34 +00:00
[legend 8 4 2 1]
2014-02-22 16:24:22 +00:00
[column-pair (.getHours date)]
[column-pair (.getMinutes date)]
[column-pair (.getSeconds date)]
(when show-100s
[column-pair (-> (.getMilliseconds date)
2014-02-22 11:36:34 +00:00
(quot 10))])])
2014-02-22 16:24:22 +00:00
(def clock-state (atom {:time (js/Date.)
:show-100s false}))
2014-02-22 11:36:34 +00:00
(defn update-time []
(swap! clock-state assoc :time (js/Date.)))
(defn main []
2014-02-22 16:24:22 +00:00
(let [{:keys [time show-100s]} @clock-state]
(if show-100s
2014-02-22 11:36:34 +00:00
(r/next-tick update-time)
2014-02-26 12:17:18 +00:00
(js/setTimeout update-time 1000))
2014-02-22 16:24:22 +00:00
[clock time show-100s
#(swap! clock-state update-in [:show-100s] not)]))