-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippet.clj
96 lines (89 loc) · 3.68 KB
/
snippet.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
(ns closerve.snippet
(:require [hickory.core :as h]
[clojure.zip :as zip]
[clojure.set :as set]
[clojure.core.async :refer [go close! >!! <! >! sliding-buffer chan]])
(:use [clojure.xml]
[clojure.walk]
[clojure.contrib.core]
[hickory zip render]
[ring.util.time :only (format-date)]
[ring.util.codec :as codec]
[closerve state util html wscmd liftutil]))
;;;the call back func of form.ajax will see {name val} dict as cmd
(register-lift-snippet
"form.ajax"
[node req page-id lift-instr]
(let [sub-nodes-transed (process-snippets node req page-id)
{:keys [new-node uuid->name form-id]} (randomize-node-names sub-nodes-transed)]
(register-call-back-fn page-id form-id
(lift-instr "callback")
{:session-id (-> req :session :session-id)
:req req
:page-id page-id
:form-id form-id
:name->uuid (set/map-invert uuid->name)}
:pre-proc-fn (fn [cm]
(let [data (:data cm)
params (codec/form-decode data "UTF-8")
back-to-orig-name
(into {} (for [[k v] params]
(if-let [orig-name (uuid->name k)]
[orig-name v] [k v])))]
back-to-orig-name)))
;tell the page to bind form-id to send form values back to server
(send-cmd-to-page page-id {:act :submit :form-id form-id})
new-node)
)
(register-lift-snippet
"LazyLoad"
[node req page-id lift-instr]
(let [uuid (make-random-uuid)
fastret (assoc-in node [:attrs :id] uuid)
fastret (assoc fastret :content ["Loading..."])]
(go (let [slowret (process-snippets node req page-id)]
(send-cmd-to-page page-id {:act :replaceWith
:selector (str "#" uuid)
:html (hickory-to-html slowret)})))
fastret)
)
(register-lift-snippet
"comet"
[node req page-id lift-instr]
(let [uuid (make-random-uuid)
actor-fn (@name->comet (lift-instr "type"))
comet-ch (chan)
fastret (assoc node [:attrs :id] uuid)]
(swap! page->comet-ch assoc-into-key-values page-id comet-ch)
(if actor-fn (actor-fn comet-ch req page-id uuid))
fastret))
(register-lift-snippet
;let browser run a js query, and send the result back to server
"query.ajax"
[node req page-id lift-instr]
(let [uuid (make-random-uuid)]
(register-call-back-fn page-id uuid
(lift-instr "callback")
{:session-id (-> req :session :session-id)
:req req
:page-id page-id
:uuid uuid}
:pre-proc-fn (fn [cm]
(let [data (:data cm)]
data)))
(send-cmd-to-page page-id {:act :queryEval :code (lift-instr "code") :uuid uuid})
node)
)
(register-lift-snippet
"embed"
[node req page-id lift-instr]
(if-let [to-embed (lift-instr "what")]
(try
(let [efilename (str @root-dir "/templates-hidden/" to-embed ".html")
eforms (map h/as-hickory (h/parse-fragment (slurp efilename)))]
(assoc node :content (apply vector eforms) ))
(catch Exception e
(prn "Error to process lift embed" lift-instr)
node))
node)
)