-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.html
162 lines (144 loc) · 6.8 KB
/
demo.html
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
<!DOCTYPE html>
<html>
<head>
<title>domino-clj</title>
<meta charset="utf-8">
<!-- KLIPSE ASSETS -->
<link rel="stylesheet" type="text/css" href="codemirror.css">
<script>
window.klipse_settings = {
selector: '.lang-eval-clojure', // css selector for the html elements you want to klipsify
};
</script>
<!-- END KLIPSE ASSETS -->
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" type="text/css" href="highlight.pack.css">
<link rel="apple-touch-icon" sizes="180x180" href="logo/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="logo/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="logo/favicon-16x16.png">
<link rel="manifest" href="logo/site.webmanifest">
<link rel="mask-icon" href="logo/safari-pinned-tab.svg" color="#74a995">
<meta name="msapplication-TileColor" content="#74a995">
<meta name="theme-color" content="#ffffff">
</head>
<body>
<h1>Example of Domino with re-frame</h1><p><pre><code class="language-clojure lang-eval-clojure" data-external-libs="https://raw.githubusercontent.com/domino-clj/domino/master/src,https://raw.githubusercontent.com/day8/re-frame/tree/master/src"> (require '[domino.core :as domino]) </code></pre></p><pre><code class="clojure lang-eval-clojure">
(require '[reagent.core :as reagent])
(require '[reagent.dom :as reagent-dom])
(require '[re-frame.core :as rf])
(require '[goog.string :as string])
(require '[goog.string.format])
(require '[cljs.pprint :refer [pprint]])
</code></pre><pre><code class="clojure lang-eval-clojure">;;initialize the Domino context using the supplied schema
(rf/reg-event-db
:init
(fn [_ [_ schema]]
(domino/initialize schema)))
;;trigger effects with the provided ids
(rf/reg-event-db
:trigger-effects
(fn [ctx [_ effect-ids]]
(domino/trigger-effects ctx effect-ids)))
;;dispatch an input event
(rf/reg-event-db
:event
(fn [ctx [_ id value]]
(domino/transact ctx [[(get-in (::domino/model ctx) [:id->path id]) value]])))
;;subscribe to the value at the given id
(rf/reg-sub
:id
(fn [ctx [_ id]]
(get-in (::domino/db ctx) (get-in (::domino/model ctx) [:id->path id]))))
;;returns Domino db
(rf/reg-sub
:db
(fn [ctx _]
(::domino/db ctx)))
(defn parse-float [s]
(let [value (js/parseFloat s)]
(when-not (js/isNaN value) value)))
(defn format-number [n]
(when n (string/format "%.2f" n)))
(defn text-input [label id]
[:div
[:label label]
[:input
{:type :text
:value @(rf/subscribe [:id id])
:on-change #(rf/dispatch [:event id (-> % .-target .-value)])}]])
(defn numeric-input [label id]
(reagent/with-let [value (reagent/atom nil)]
[:div
[:label label]
[:input
{:type :text
:value @value
:on-focus #(reset! value @(rf/subscribe [:id id]))
:on-blur #(rf/dispatch [:event id (parse-float @value)])
:on-change #(reset! value (-> % .-target .-value))}]]))
;;initialize Domino
(rf/dispatch-sync
[:init
{:model
[[:demographics
[:first-name {:id :first-name}]
[:last-name {:id :last-name}]
[:full-name {:id :full-name}]]
[:vitals
[:height {:id :height}]
[:weight {:id :weight}]
[:bmi {:id :bmi}]]
[:counter {:id :counter}]]
:effects
[{:id :increment
:outputs [:counter]
:handler (fn [_ state]
(update state :counter (fnil inc 0)))}
{:inputs [:full-name]
:handler (fn [_ {:keys [full-name]}]
(when (= "Bobberton, Bob" full-name)
(js/alert "Hi Bob!")))}]
:events
[{:inputs [:first-name :last-name]
:outputs [:full-name]
:handler (fn [_ {:keys [first-name last-name]} _]
{:full-name (or (when (and first-name last-name)
(str last-name ", " first-name))
first-name
last-name)})}
{:inputs [:height :weight]
:outputs [:bmi]
:handler (fn [_ {:keys [height weight]} {:keys [bmi]}]
{:bmi (if (and height weight)
(/ weight (* height height))
bmi)})}]}])
;;render the UI
(defn home-page []
[:div
[:h3 "Patient demographics"]
[text-input "First name" :first-name]
[text-input "Last name" :last-name]
[numeric-input "Height (M)" :height (fnil js/parseFloat 0)]
[numeric-input "Weight (KG)" :weight (fnil js/parseFloat 0)]
[:button
{:on-click #(rf/dispatch [:trigger-effects [:increment]])}
"increment count"]
[:p>label "Full name " @(rf/subscribe [:id :full-name])]
[:p>label "BMI " (format-number @(rf/subscribe [:id :bmi]))]
[:p>label "Counter " @(rf/subscribe [:id :counter])]
[:hr]
[:h4 "DB state"]
[:pre (with-out-str (pprint @(rf/subscribe [:db])))]])
(reagent-dom/render [home-page] js/klipse-container)
</code></pre>
<script src="highlight.pack.js"></script>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
document.querySelectorAll('pre code.clojure').forEach((block) => {
hljs.highlightBlock(block);
});
});
</script>
<script src="https://storage.googleapis.com/app.klipse.tech/plugin/js/klipse_plugin.js"></script>
</body>
</html>