-
Notifications
You must be signed in to change notification settings - Fork 65
/
core.clj
231 lines (188 loc) · 8.54 KB
/
core.clj
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
;; Copyright (c) Alan Dipert and Micha Niskin. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
(ns hoplon.core
(:require [clojure.spec.alpha :as spec]
[javelin.core :as j]
[hoplon.spec]))
;; Hoplon Interpolation ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- ^{:from 'org.clojure/core.incubator} silent-read
"Attempts to clojure.core/read a single form from the provided String, returning
a vector containing the read form and a String containing the unread remainder
of the provided String. Returns nil if no valid form can be read from the
head of the String."
[s]
(try
(let [r (-> s java.io.StringReader. java.io.PushbackReader.)]
[(read r) (slurp r)])
(catch Exception _))) ; this indicates an invalid form -- the head of s is just string data
(defn- ^{:from 'org.clojure/core.incubator} terpol8*
"Yields a seq of Strings and read forms."
([s atom?]
(lazy-seq
(if-let [[form rest] (silent-read (subs s (if atom? 2 1)))]
(cons form (terpol8* (if atom? (subs rest 1) rest)))
(cons (subs s 0 2) (terpol8* (subs s 2))))))
([^String s]
(if-let [start (->> ["~{" "~("]
(map #(.indexOf s ^String %))
(remove #(== -1 %))
sort
first)]
(lazy-seq (cons
(subs s 0 start)
(terpol8* (subs s start) (= \{ (.charAt s (inc start))))))
[s])))
(defn terpol8 [s]
(let [parts (remove #(= "" %) (terpol8* s))]
(if (every? string? parts) s `(str ~@parts))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Defining Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro elem
"Create an anonymous custom element."
[bind & body]
(let [[prepost & body] (if (map? (first body)) body (conj body nil))]
`(fn [& args#] ~(or prepost {}) (let [~bind (parse-args args#)] ~@body))))
(spec/fdef elem :args :hoplon.spec/elem :ret any?)
(defmacro defelem
"Defines an element function.
An element function creates a DOM Element (parent) given two arguments:
* `attrs` - a number of key-value pairs for attributes and their values
* `kids` - a sequence of DOM Elements to be appended/used inside
The returned DOM Element is itself a function which can accept more
attributes and child elements."
[name & forms]
(let [[_ name [_ & [fdecl]]] (macroexpand-1 `(defn ~name ~@forms))
[docstr & [bind & body]] (if (string? (first fdecl)) fdecl (conj fdecl nil))]
`(def ^{:doc ~docstr} ~name (elem ~bind ~@body))))
(spec/fdef defelem :args :hoplon.spec/defelem :ret any?)
(defmacro defattr
"Defines an attribute function.
An element attribute is a function given three arguments:
* `elem` - the target DOM Element containing the attribute
* `key` - the attribute keyword or symbol
* `value` - the attribute value
The attribute function is called whenever the value argument changes."
[name & forms]
`(defmethod hoplon.core/do! ~name ~@forms))
(spec/fdef defattr :args :hoplon.spec/defattr :ret any?)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Caching DOM Manipulation Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro ^:private safe-deref [expr] `(deref (or ~expr (atom nil))))
(defn- parse-e [[tag & [head & tail :as args]]]
(let [kw1? (comp keyword? first)
mkkw #(->> (partition 2 %) (take-while kw1?) (map vec))
drkw #(->> (partition 2 2 [] %) (drop-while kw1?) (mapcat identity))]
(cond (map? head) [tag head tail]
(keyword? head) [tag (into {} (mkkw args)) (drkw args)]
:else [tag nil args])))
(defmacro loop-tpl
"Template. Works identically to `for-tpl`, only expects a `:bindings`
attribute to accomodate the HTML HLisp representation:
(loop-tpl :bindings [x xs] ...)
"
[& args]
(let [[_ {[bindings items] :bindings} [body]] (parse-e (cons '_ args))]
`(loop-tpl* ~items
(fn [item#] (j/cell-let [~bindings item#] ~body)))))
(spec/fdef loop-tpl :args :hoplon.spec/loop-tpl :ret any?)
(defmacro for-tpl
"Template. Accepts a cell-binding and returns a cell containing a sequence of
elements:
(for-tpl [x xs] (span x))
"
[[bindings items] body]
`(loop-tpl* ~items (fn [item#] (j/cell-let [~bindings item#] ~body))))
(spec/fdef for-tpl :args :hoplon.spec/for-tpl :ret any?)
(defmacro if-tpl
"Template. Accepts a `predicate` cell and returns a cell containing either
the element produced by `consequent` or `alternative`, depending on the value
of the predicate:
(if-tpl predicate (span \"True\") (span \"False\"))
"
[predicate consequent & [alternative]]
`(let [con# (delay ~consequent)
alt# (delay ~alternative)
tpl# (fn [p#] (safe-deref (if p# con# alt#)))]
((j/formula tpl#) ~predicate)))
(spec/fdef if-tpl :args :hoplon.spec/if-tpl :ret any?)
(defmacro when-tpl
"Template. Accepts a `predicate` cell and returns a cell containing either
the element produced by `consequent` or nothing, depending on the value of
the predicate:
(when-tpl predicate (span \"Value\"))
"
[predicate & body]
`(if-tpl ~predicate (do ~@body)))
(spec/fdef when-tpl :args :hoplon.spec/when-tpl :ret any?)
(defmacro cond-tpl
"Template. Accepts a number of `clauses` cell-template pairs and returns a
cell with the value produced by the matching clause:
(cond-tpl
clause-a (span \"A\")
clause-b (span \"B\")
:else (span \"Default\"))
"
[& clauses]
(assert (even? (count clauses)))
(let [[conds tpls] (apply map vector (partition 2 clauses))
syms1 (repeatedly (count conds) gensym)
syms2 (repeatedly (count conds) gensym)]
`(let [~@(interleave syms1 (map (fn [x] `(delay ~x)) tpls))
tpl# (fn [~@syms2] (safe-deref (cond ~@(interleave syms2 syms1))))]
((j/formula tpl#) ~@conds))))
(spec/fdef cond-tpl :args :hoplon.spec/cond-tpl :ret any?)
(defmacro case-tpl
"Template. Accepts an `expr` cell and a number of `clauses` and returns a
cell with the value produced by the matching clause:
(case-tpl expr
:a (span \"A\")
:b (span \"B\")
(span \"Default\"))
"
[expr & clauses]
(let [[cases tpls] (apply map vector (partition 2 clauses))
default (when (odd? (count clauses)) (last clauses))
syms (repeatedly (inc (count cases)) gensym)]
`(let [~@(interleave syms (map (fn [x] `(delay ~x)) (conj tpls default)))
tpl# (fn [expr#] (safe-deref (case expr# ~@(interleave cases syms) ~(last syms))))]
((j/formula tpl#) ~expr))))
(spec/fdef case-tpl :args :hoplon.spec/case-tpl :ret any?)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DOM Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro with-dom
"Evaluates the body after elem has been inserted into the DOM."
[elem & body]
`(when-dom ~elem (fn [] ~@body)))
(defmacro with-timeout
"Evaluates the body after msec milliseconds, asynchronously. Returns the
timeout ID which can be used to cancel the operation (see js/clearTimeout)."
[msec & body]
`(js/setTimeout (fn [] ~@body) ~msec))
(defmacro with-interval
"Evaluates the body every msec milliseconds, asynchronously. Returns the
interval ID which can be used to cancel the operation (see js/clearInterval)."
[msec & body]
`(js/setInterval (fn [] ~@body) ~msec))
(defmacro with-animation-frame
"Evaluates the body before the next browser repaint as requestAnimationFrame."
[& body]
`(.requestAnimationFrame js/window (fn [] ~@body)))
(defmacro with-init!
"Evaluates the body after Hoplon has completed constructing the page."
[& body]
`(add-initfn! (fn [] ~@body)))
(defmacro text
"Creates a DOM Text node and binds its text content to a formula created via
string interpolation, so the Text node updates with the formula."
[form]
(let [i (if-not (string? form) form (terpol8 form))]
(if (string? i)
`(.createTextNode js/document ~i)
`(j/with-let [t# (.createTextNode js/document "")]
(j/cell= (set! (.-nodeValue t#) ~i))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;