mirror of https://github.com/status-im/reagent.git
Tweak clock demo
This commit is contained in:
parent
af1d3e6d05
commit
b8ffbb8dd2
|
@ -34,7 +34,7 @@
|
||||||
(defn src-for [funmap defs]
|
(defn src-for [funmap defs]
|
||||||
[:pre (-> funmap (src-for-names defs) syntaxify)])
|
[:pre (-> funmap (src-for-names defs) syntaxify)])
|
||||||
|
|
||||||
(defn demo-component [{:keys [comp src complete]}]
|
(defn demo-component [{:keys [comp src complete no-heading]}]
|
||||||
(let [showing (atom true)]
|
(let [showing (atom true)]
|
||||||
(fn []
|
(fn []
|
||||||
[:div
|
[:div
|
||||||
|
@ -45,7 +45,8 @@
|
||||||
(swap! showing not)
|
(swap! showing not)
|
||||||
false)}
|
false)}
|
||||||
(if @showing "hide" "show")]
|
(if @showing "hide" "show")]
|
||||||
[:h3.demo-heading "Example "]
|
(when-not no-heading
|
||||||
|
[:h3.demo-heading "Example "])
|
||||||
(when @showing
|
(when @showing
|
||||||
(if-not complete
|
(if-not complete
|
||||||
[:div.simple-demo [comp]]
|
[:div.simple-demo [comp]]
|
||||||
|
@ -53,6 +54,7 @@
|
||||||
(if @showing
|
(if @showing
|
||||||
(if src
|
(if src
|
||||||
[:div.demo-source.clearfix
|
[:div.demo-source.clearfix
|
||||||
[:h3.demo-heading "Source"]
|
(when-not no-heading
|
||||||
|
[:h3.demo-heading "Source"])
|
||||||
src]
|
src]
|
||||||
[:div.clearfix]))])))
|
[:div.clearfix]))])))
|
||||||
|
|
|
@ -1,70 +1,50 @@
|
||||||
(ns reagentdemo.news.binaryclock
|
(ns reagentdemo.news.binaryclock
|
||||||
(:require [reagent.core :as r :refer [atom]]
|
(:require [reagent.core :as r :refer [atom]]))
|
||||||
[reagent.debug :refer-macros [dbg println]]))
|
|
||||||
|
|
||||||
(defn cell [n bit]
|
(defn cell [n bit]
|
||||||
[:div.clock.cell {:class (if (bit-test n bit)
|
[:div.clock-cell {:class (if (bit-test n bit)
|
||||||
'light
|
"light"
|
||||||
'dark)}])
|
"dark")}])
|
||||||
|
|
||||||
(defn column [n]
|
(defn column [n]
|
||||||
[:div.clock.col
|
[:div.clock-col
|
||||||
[cell n 3]
|
[cell n 3]
|
||||||
[cell n 2]
|
[cell n 2]
|
||||||
[cell n 1]
|
[cell n 1]
|
||||||
[cell n 0]
|
[cell n 0]
|
||||||
[:div.clock.cell n]])
|
[:div.clock-cell n]])
|
||||||
|
|
||||||
(defn column-pair [n]
|
(defn column-pair [n]
|
||||||
[:div.clock.colpair
|
[:div.clock-pair
|
||||||
[column (quot n 10)]
|
[column (quot n 10)]
|
||||||
[column (mod n 10)]])
|
[column (mod n 10)]])
|
||||||
|
|
||||||
(defn legend [& numbers]
|
(defn legend [& items]
|
||||||
(into [:div.clock.col.legend]
|
(into [:div.clock-col.clock-legend]
|
||||||
(map (partial vector :div.clock.cell) numbers)))
|
(map (partial vector :div.clock-cell)
|
||||||
|
items)))
|
||||||
|
|
||||||
(defn clock [time show-ms toggle-ms]
|
(defn clock [date show-100s toggle-100s]
|
||||||
[:div.clock.main {:on-click toggle-ms
|
[:div.clock-main {:on-click toggle-100s
|
||||||
:class (when show-ms 'wide)}
|
:class (when show-100s "wide")}
|
||||||
[legend 8 4 2 1]
|
[legend 8 4 2 1]
|
||||||
[column-pair (.getHours time)]
|
[column-pair (.getHours date)]
|
||||||
[column-pair (.getMinutes time)]
|
[column-pair (.getMinutes date)]
|
||||||
[column-pair (.getSeconds time)]
|
[column-pair (.getSeconds date)]
|
||||||
(if show-ms
|
(when show-100s
|
||||||
[column-pair (-> (.getMilliseconds time)
|
[column-pair (-> (.getMilliseconds date)
|
||||||
(quot 10))])])
|
(quot 10))])])
|
||||||
|
|
||||||
(def clock-state (atom {:show-ms false
|
(def clock-state (atom {:time (js/Date.)
|
||||||
:time (js/Date.)}))
|
:show-100s false}))
|
||||||
|
|
||||||
(defn update-time []
|
(defn update-time []
|
||||||
(swap! clock-state assoc :time (js/Date.)))
|
(swap! clock-state assoc :time (js/Date.)))
|
||||||
|
|
||||||
(defn now []
|
|
||||||
(.now js/Date))
|
|
||||||
|
|
||||||
(defn timing-wrapper [f]
|
|
||||||
(let [start-time (atom nil)
|
|
||||||
render-time (atom nil)
|
|
||||||
start #(reset! start-time (now))
|
|
||||||
stop #(reset! render-time (- (now) @start-time))
|
|
||||||
timed-f (with-meta f
|
|
||||||
{:component-will-mount start
|
|
||||||
:component-will-update start
|
|
||||||
:component-did-mount stop
|
|
||||||
:component-did-update stop})]
|
|
||||||
(fn [& args]
|
|
||||||
[:div
|
|
||||||
[:p [:em "render time: " @render-time "ms"]]
|
|
||||||
(into [timed-f] args)])))
|
|
||||||
|
|
||||||
(def clock-with-timing (timing-wrapper clock))
|
|
||||||
|
|
||||||
(defn main []
|
(defn main []
|
||||||
(let [{:keys [show-ms time]} @clock-state]
|
(let [{:keys [time show-100s]} @clock-state]
|
||||||
(if show-ms
|
(if show-100s
|
||||||
(r/next-tick update-time)
|
(r/next-tick update-time)
|
||||||
(js/setTimeout update-time 333))
|
(js/setTimeout update-time 333))
|
||||||
[clock-with-timing time show-ms
|
[clock time show-100s
|
||||||
#(swap! clock-state update-in [:show-ms] not)]))
|
#(swap! clock-state update-in [:show-100s] not)]))
|
||||||
|
|
|
@ -6,29 +6,71 @@
|
||||||
[reagentdemo.common :as common :refer [demo-component]]
|
[reagentdemo.common :as common :refer [demo-component]]
|
||||||
[reagentdemo.news.binaryclock :as binaryclock]))
|
[reagentdemo.news.binaryclock :as binaryclock]))
|
||||||
|
|
||||||
(def funmap (-> ::this get-source common/fun-map))
|
(def funmap (-> "reagentdemo/news/binaryclock.cljs"
|
||||||
|
get-source common/fun-map))
|
||||||
(def src-for (partial common/src-for funmap))
|
(def src-for (partial common/src-for funmap))
|
||||||
|
|
||||||
|
(defn fn-src [& parts]
|
||||||
|
[demo-component {:src (src-for (vec parts))
|
||||||
|
:no-heading true}])
|
||||||
|
|
||||||
(defn main [{:keys [summary]}]
|
(defn main [{:keys [summary]}]
|
||||||
(let [head "Binary clock"]
|
(let [head "Binary clock"]
|
||||||
|
|
||||||
[:div.reagent-demo
|
[:div.reagent-demo
|
||||||
[:h1 [link {:href main} head]]
|
[:h1 [link {:href main} head]]
|
||||||
[title (str "Reagent 0.4.0: " head)]
|
[title head]
|
||||||
[:div.demo-text
|
[:div.demo-text
|
||||||
|
|
||||||
[:h2 "Binary clock"]
|
(when-not summary
|
||||||
|
[:div
|
||||||
|
;; [demo-component {:comp binaryclock/main}]
|
||||||
|
[binaryclock/main]
|
||||||
|
[:p [:strong "Click to toggle 1/100th seconds."]]])
|
||||||
|
|
||||||
[:p "x"]
|
[:h2 "A simple binary clock in Reagent"]
|
||||||
|
|
||||||
|
[:p "some text"]
|
||||||
|
|
||||||
(if summary
|
(if summary
|
||||||
[link {:href main
|
[link {:href main
|
||||||
:class 'news-read-more} "Read more"]
|
:class 'news-read-more} "Read more"]
|
||||||
[:div.demo-text
|
[:div.demo-text
|
||||||
[:p "x"]
|
|
||||||
|
|
||||||
[demo-component {:comp binaryclock/main}]])]]))
|
[:p "We start with the basics: The clock is built out of
|
||||||
|
cells, that are light or dark if the bit they correspond to
|
||||||
|
is set."]
|
||||||
|
|
||||||
|
[fn-src :cell]
|
||||||
|
|
||||||
|
[:p "Cells are combined into columns of four bits, with a
|
||||||
|
decimal digit at the bottom."]
|
||||||
|
|
||||||
|
[fn-src :column]
|
||||||
|
|
||||||
|
[:p "Columns are in turn combined into pairs:"]
|
||||||
|
|
||||||
|
[fn-src :column-pair]
|
||||||
|
|
||||||
|
[:p "We'll also need the legend on the left side:"]
|
||||||
|
|
||||||
|
[fn-src :legend]
|
||||||
|
|
||||||
|
[:p "We combine these element into a component that shows the
|
||||||
|
legend, hours, minutes and seconds; and optionally 1/100
|
||||||
|
seconds. It also responds to clicks."]
|
||||||
|
|
||||||
|
[fn-src :clock]
|
||||||
|
|
||||||
|
[:p "We also need to keep track of the time, and of the
|
||||||
|
detail shown, in a Reagent atom. And a function to update the
|
||||||
|
time."]
|
||||||
|
|
||||||
|
[fn-src :clock-state :update-time]
|
||||||
|
|
||||||
|
[:p "And finally we use the clock component like this:"]
|
||||||
|
|
||||||
|
[fn-src :main]])]]))
|
||||||
|
|
||||||
(swap! page-map assoc
|
(swap! page-map assoc
|
||||||
"news/binary-clock.html" main)
|
"news/binary-clock.html" main)
|
||||||
|
|
|
@ -9,7 +9,8 @@
|
||||||
"filter" "vals" "count" "complement" "identity" "dotimes"
|
"filter" "vals" "count" "complement" "identity" "dotimes"
|
||||||
"update-in" "sorted-map" "inc" "dec" "false" "true" "not"
|
"update-in" "sorted-map" "inc" "dec" "false" "true" "not"
|
||||||
"=" "partial" "first" "second" "rest" "list" "conj"
|
"=" "partial" "first" "second" "rest" "list" "conj"
|
||||||
"drop" "when-let" "if-let" "add-watch"})
|
"drop" "when-let" "if-let" "add-watch" "mod" "quot"
|
||||||
|
"bit-test" "vector"})
|
||||||
|
|
||||||
(def styles {:comment {:style {:color "gray"
|
(def styles {:comment {:style {:color "gray"
|
||||||
:font-style "italic"}}
|
:font-style "italic"}}
|
||||||
|
|
|
@ -188,43 +188,44 @@ ul.nav > li.brand > a {
|
||||||
|
|
||||||
/* Binary clock */
|
/* Binary clock */
|
||||||
|
|
||||||
.clock.main {
|
.clock-main {
|
||||||
background: #333;
|
background: #333;
|
||||||
color: #cdcdcd;
|
color: #cdcdcd;
|
||||||
padding-top: 20px;
|
padding-top: 55px;
|
||||||
padding-left: 20px;
|
padding-left: 20px;
|
||||||
float: left;
|
float: left;
|
||||||
font-size: 28px;
|
font-size: 28px;
|
||||||
line-height: 34px;
|
line-height: 34px;
|
||||||
width: 620px;
|
width: 620px;
|
||||||
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
.clock.main.wide {
|
.clock-main.wide {
|
||||||
width: 800px;
|
width: 790px;
|
||||||
}
|
}
|
||||||
.clock.cell {
|
.clock-cell {
|
||||||
width: 55px;
|
width: 55px;
|
||||||
height: 55px;
|
height: 55px;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
margin: 0 20px 20px 0;
|
margin: 0 20px 20px 0;
|
||||||
}
|
}
|
||||||
.clock.cell.dark {
|
.clock-cell.dark {
|
||||||
background-color: #454545;
|
background-color: #454545;
|
||||||
}
|
}
|
||||||
.clock.cell.light {
|
.clock-cell.light {
|
||||||
background-color: #eee;
|
background-color: #eee;
|
||||||
}
|
}
|
||||||
.clock.col {
|
.clock-col {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
float: left;
|
float: left;
|
||||||
}
|
}
|
||||||
.clock.legend>.cell {
|
.clock-legend > .clock-cell {
|
||||||
margin-top: "10px"
|
margin-top: 10px;
|
||||||
}
|
}
|
||||||
.clock.colpair {
|
.clock-pair {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
float: left;
|
float: left;
|
||||||
}
|
}
|
||||||
.clock.colpair:not(:last-child) {
|
.clock-pair:not(:last-child) {
|
||||||
margin-right: 20px;
|
margin-right: 20px;
|
||||||
border-right 1px solid #454545;
|
/* border-right: 1px solid #454545; */
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue