forked from ckirkendall/enfocus
/
core.cljs
432 lines (343 loc) · 12.9 KB
/
core.cljs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
(ns enfocus.core
(:require [goog.net.XhrIo :as xhr]
[goog.dom.query :as query]
[goog.style :as style]
[goog.events :as events]
[goog.dom :as dom]
[goog.events :as events]
[clojure.string :as string]))
(declare css-syms css-select create-sel-str)
;####################################################
; Utility functions
;####################################################
(defn node? [tst]
(dom/isNodeLike tst))
(defn nodelist? [tst]
(instance? js/NodeList tst))
(defn nodes->coll
"coverts a nodelist, node into a collection"
[nl]
(cond
(nil? nl) []
(node? nl) [nl]
(or (instance? js/Array nl) (coll? nl)) nl
(nodelist? nl) (for [x (range 0 (.length nl))]
(aget nl x))))
(defn- flatten-nodes-coll [values]
"takes a set of nodes and nodelists and flattens them"
(mapcat #(cond (string? %) [(dom/createTextNode %)]
:else (nodes->coll %)) values))
(defn- style-set
"Sets property name to a value on a javascript object
Returns the original object (js-set obj :attr value) "
([obj values]
(do (doseq [[attr value] (apply hash-map values)]
(style/setStyle obj (name attr) value))
obj)))
;####################################################
; The following functions are used to transform
; the dom structure
;####################################################
(def tpl-load-cnt
"this is incremented everytime a remote template is
loaded and decremented when it is added to the dom
cache"
(atom 0))
(def tpl-cache
"cache for the remote templates"
(atom {}))
(def hide-style (.strobj {"style" "display: none; width: 0px; height: 0px"}))
(defn create-hidden-dom
"Add a hidden div to hold the dom as we are transforming it this
has to be done because css selectors do not work unless we have
it in the main dom"
[child]
(let [div (dom/createDom "div" hide-style)]
(. div (appendChild child))
(dom/appendChild (.body (dom/getDocument)) div)
div))
(defn remove-node-return-child
"removes the hidden div and returns the children"
[div]
(let [child (.childNodes div)
frag (. js/document (createDocumentFragment))]
(dom/append frag child)
(dom/removeNode div)
frag))
(defn replace-ids
"replaces all the ids in a string html fragement/template
with a generated symbol appended on to a existing id
this is done to make sure we don't have id colisions
during the transformation process"
[text]
(let [re (js/RegExp. "(<.*?\\sid=['\"])(.*?)(['\"].*?>)" "g")
sym (str (name (gensym "id")) "_")]
[(str sym) (.replace text re (fn [a b c d] (str b sym c d)))]))
(defn reset-ids
"before adding the transformed dom back into the live dom we
reset the ids back to their original values"
[sym nod]
(let [id-nodes (css-select nod "*[id]")
nod-col (nodes->coll id-nodes)]
(doall (map #(let [id (. % (getAttribute "id"))
rid (. id (replace sym ""))]
(. % (setAttribute "id" rid))) nod-col))))
(defn load-remote-dom
"loads a remote file into the cache, before adding to the
cache we replace the ids to avoid collisions"
[uri]
(when (nil? (@tpl-cache uri))
(swap! tpl-load-cnt inc)
(let [req (new goog.net.XhrIo)
callback (fn [req]
(let [text (. req (getResponseText))
[sym txt] (replace-ids text)
data (dom/htmlToDocumentFragment txt)
body (first (nodes->coll (css-select data "body")))]
(if body
(let [frag (. js/document (createDocumentFragment))
childs (.childNodes frag)]
(dom/append frag childs)
(swap! tpl-cache assoc uri [sym frag]))
(swap! tpl-cache assoc uri [sym data] ))))]
(events/listen req goog.net.EventType/COMPLETE
#(do
(callback req)
(swap! tpl-load-cnt dec)))
(. req (send uri "GET")))))
(defn get-cached-dom
"returns and dom from the cache and symbol used to scope the ids"
[uri]
(let [nod (@tpl-cache uri)]
(when nod [(first nod) (. (second nod) (cloneNode true))])))
(defn get-cached-snippit
"returns the cached snippit or creates one and adds it to the
cache if needed"
[uri sel]
(let [sel-str (create-sel-str sel)
cache (@tpl-cache (str (uri sel-str)))]
(if cache [(first cache) (. (second cache) (cloneNode true))]
(let [[sym tdom] (get-cached-dom uri)
dom (create-hidden-dom tdom)
tsnip (css-select dom sel-str)
snip (if (instance? js/Array tsnip) (first tsnip) tsnip)]
(remove-node-return-child dom)
(assoc @tpl-cache (str uri sel-str) [sym snip])
[sym snip]))))
;####################################################
; The following functions are used to transform the
; dom structure. each function returns a function
; taking the a set of nodes from a selector
;####################################################
(defn multi-node-proc
"takes a function an returns a function that
applys a given function on all nodes returned
by a given selector"
[func]
(fn [pnodes]
(let [pnod-col (nodes->coll pnodes)]
(doall (map func pnod-col )))))
(defn content-based-trans
"HOF to remove the duplicate code in transformation that
handle creating a fragment and applying it in some way
to the selected node"
[values func]
(let [fnodes (flatten-nodes-coll values)]
(multi-node-proc
(fn [pnod]
(let [frag (. js/document (createDocumentFragment))]
(doall (map #(dom/appendChild frag (. % (cloneNode true))) fnodes))
(func pnod frag))))))
(defn en-content
"Replaces the content of the element. Values can be nodes or collection of nodes."
[& values]
(content-based-trans
values
(fn [pnod frag]
(dom/removeChildren pnod)
(dom/appendChild pnod frag))))
(defn en-html-content
"Replaces the content of the element with the dom structure
represented by the html string passed"
[txt]
(multi-node-proc
(fn [pnod]
(let [frag (dom/htmlToDocumentFragment txt)]
(dom/removeChildren pnod)
(dom/append pnod frag)))))
(defn en-set-attr
"Assocs attributes and values on the selected element."
[& values]
(let [at-lst (partition 2 values)]
(multi-node-proc
(fn[pnod]
(doall (map (fn [[a v]] (. pnod (setAttribute (name a) v))) at-lst))))))
(defn en-remove-attr
"Dissocs attributes on the selected element."
[& values]
(multi-node-proc
(fn[pnod]
(doall (map #(. pnod (removeAttribute (name %))) values)))))
(defn- has-class
"returns true if the element has a given class"
[el cls]
(let [regex (js/RegExp. (str "(\\s|^)" cls "(\\s|$)"))
cur-cls (.className el)]
(. cur-cls (match regex))))
(defn en-add-class
"Adds the specified classes to the selected element."
[ & values]
(multi-node-proc
(fn [pnod]
(let [cur-cls (.className pnod)]
(doall (map #(if (not (has-class pnod %))
(set! (.className pnod) (str cur-cls " " %)))
values))))))
(defn en-remove-class
"Removes the specified classes from the selected element."
[ & values]
(multi-node-proc
(fn [pnod]
(let [cur (.className pnod)]
(doall (map #(if (has-class pnod %)
(let [regex (js/RegExp. (str "(\\s|^)" % "(\\s|$)"))]
(set! (.className pnod) (. cur (replace regex " ")))))
values))))))
(defn en-do-> [ & forms]
"Chains (composes) several transformations. Applies functions from left to right."
(multi-node-proc
(fn [pnod]
(doall (map #(% pnod) forms)))))
(defn en-append
"Appends the content of the element. Values can be nodes or collection of nodes."
[& values]
(content-based-trans
values
(fn [pnod frag]
(dom/appendChild pnod frag))))
(defn en-prepend
"Prepends the content of the element. Values can be nodes or collection of nodes."
[& values]
(content-based-trans
values
(fn [pnod frag]
(let [nod (.firstChild pnod)]
(. pnod (insertBefore frag nod))))))
(defn en-before
"inserts the content before the selected node. Values can be nodes or collection of nodes"
[& values]
(content-based-trans
values
(fn [pnod frag]
(dom/insertSiblingBefore frag pnod))))
(defn en-after
"inserts the content after the selected node. Values can be nodes or collection of nodes"
[& values]
(content-based-trans
values
(fn [pnod frag]
(dom/insertSiblingAfter frag pnod))))
(defn en-substitute
"substitutes the content for the selected node. Values can be nodes or collection of nodes"
[& values]
(content-based-trans
values
(fn [pnod frag]
(dom/replaceNode frag pnod))))
(defn en-remove-node
"removes the selected nodes from the dom"
[& values]
(multi-node-proc
(fn [pnod]
(dom/removeNode pnod))))
(defn en-set-style
"set a list of style elements from the selected nodes"
[& values]
(multi-node-proc
(fn [pnod]
(style-set pnod values))))
(defn en-remove-style
"remove a list style elements from the selected nodes
note: you can only remove styles that are inline styles
set in css need to overridden through set-style"
[& values]
(multi-node-proc
(fn [pnod]
(doall
(map #(. (.style pnod) (removeProperty (name %))) values)))))
(defn en-add-event
"adding an event to the selected nodes"
[event func]
(multi-node-proc
(fn [pnod]
(events/listen pnod (name event) func))))
(defn en-remove-event
"adding an event to the selected nodes"
[& event-list]
(multi-node-proc
(fn [pnod]
(doall (map #(events/removeAll pnod (name %)) event-list)))))
;##################################################################
; functions involved in processing the selectors
;##################################################################
(defn- create-sel-str
"converts keywords, symbols and strings used in the enlive selector
syntax to a string representing a standard css selector. It also
takes a string to append to all ids so they do not conflict with
existing ids in the live dom"
([css-sel] (create-sel-str "" css-sel))
([id-scope-sym css-sel]
(apply str (map #(cond
(symbol? %) (css-syms %)
(keyword? %) (str " " (. (name %) (replace "#" (str "#" id-scope-sym))))
(vector? %) (create-sel-str %)
(string? %) (.replace % "#" (str "#" id-scope-sym)))
css-sel))))
(defn css-select
"takes either an enlive selector or a css3 selector and
returns a set of nodes that match the selector"
([dom-node css-sel] (css-select "" dom-node css-sel))
([id-scope-sym dom-node css-sel]
(let [sel (string/trim (string/replace (create-sel-str id-scope-sym css-sel) " :" ":"))
ret (dom/query sel dom-node)]
ret)))
;##################################################################
; The following functions are used to support the enlive selector
; syntax. They simply translate to the string representation to
; the standard css3 selector standard
;##################################################################
(def css-syms {'first-child " *:first-child"
'last-child " *:last-child"})
(defn attr?
"Matches any E element that contains att attribute:
css -> E[att][att2]..."
[& kys] (apply str (mapcat #(str "[" (name %) "]") kys)))
(defn attr=
"Matches any E element whose att attribute value equals 'val':
css -> E[att=val][att2=val2]..."
([] "")
([ky txt & forms]
(str "[" (name ky) "='" txt "']"
(apply attr= forms))))
(defn nth-child
"Matches any E element that is the n-th child of its parent:
css -> E:nth-child(x) or E:nth-child(xn+y)"
([x] (str ":nth-child(" x ")"))
([x y] (str ":nth-child(" x "n+" y ")")))
(defn nth-of-type
"Matches any E element that is the n-th sibling of its type:
css -> E:nth-of-type(x) or E:nth-of-type(xn+y)"
([x] (str ":nth-of-type(" x ")"))
([x y] (str ":nth-of-type(" x "n+" y ")")))
(defn nth-last-child
"Matches any E element that is the n-th child of its parent,
counting from the last child.
css -> E:nth-last-child(x) or E:nth-last-child(xn+y)"
([x] (str ":nth-last-child(" x ")"))
([x y] (str ":nth-last-child(" x "n+" y ")")))
(defn nth-last-of-type
"Matches any E element that is the n-th sibling of its type
counting from the last child:
css -> E:nth-last-of-type(x) or E:nth-last-of-type(xn+y)"
([x] (str ":nth-last-of-type(" x ")"))
([x y] (str ":nth-last-of-type(" x "n+" y ")")))