-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjquery-api.lisp
More file actions
300 lines (252 loc) · 8.81 KB
/
jquery-api.lisp
File metadata and controls
300 lines (252 loc) · 8.81 KB
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
(defpackage :javascript
(:use :cl :parenscript))
(in-package :javascript)
;;===== core
(defpsmacro define-site-variables ()
`(defvar *site-address* "www.golfholidays.tv"))
(defpsmacro relative-url (url)
`(+ "http://" *site-address* ,url))
;;===== domready
(defpsmacro domready (&body body)
`((@ ($ document) ready)
(lambda () ,@body)))
;;===== callback / event
(defpsmacro defcallback (selector event-type callback)
`((@ ,selector ,event-type) ,callback))
(defpsmacro defevent (selector event-type &body callback-body)
`((@ ,selector ,event-type) (lambda () ,@callback-body)))
;;===== dataform
(defpsmacro defform (selector callback)
`((@ ,selector ajax-form) ,callback))
(defpsmacro defsubmit (selector callback)
`((@ ,selector submit)
(lambda ()
((@ ($ this) ajax-submit)
,callback)
(return false))))
(defpsmacro dom-dataform (form-element action method body)
`(form (id ,form-element action ,action method ,method) append
,body))
(defpsmacro dom-upload-dataform (form-element action method body)
`(form (id ,form-element action ,action method ,method
enctype "multipart/form-data") append
,body))
(defpsmacro dom-select (id name selected lst)
`(select (id ,id name ,name) append
(dolist (el ,lst)
(if (= ,selected el)
(option (value el selected "selected") append el)
(option (value el) append el)))))
;; dataform elements
(defpsmacro dom-standard-control-elements/add-remove ()
`(div (id "datatable-standard-controls/add-remove") append
(submit (name "add" value "Add") append)
(submit (name "remove" value "Remove") append)))
(defpsmacro dom-standard-control-elements/remove ()
`(div (id "datatable-standard-controls") append
(submit (name "remove" value "Remove") append)))
(defpsmacro dom-datatable-control (tfoot-element control-elements)
`(tfoot (id ,tfoot-element) append
(tr () append
(td (id "datatable-npages") append)
(td (id "datatable-control" colspan "2") append
,control-elements))))
(defpsmacro dom-datatable (root-element new-root-element form-element table-element action
thead-element tbody-element tfoot)
`((@ ($ ,root-element) replace-with)
(div (id ,new-root-element) append
(form (id ,form-element action ,action method "POST") append
(div (id "datatable-select") append
"Select "
(a (id "select-all" href "#select-all") append "All") " "
(a (id "select-none" href "#select-none") append "None"))
(table (id ,table-element class "data-table") append
(thead (id ,thead-element) append)
(tbody (id ,tbody-element) append)
,tfoot)))))
(defpsmacro dom-standard-datatable/add-remove (root-element new-root-element form-element table-element action
thead-element tbody-element tfoot-element)
`(dom-datatable ,root-element ,new-root-element ,form-element ,table-element ,action ,thead-element ,tbody-element
(dom-datatable-control ,tfoot-element (dom-standard-control-elements/add-remove))))
(defpsmacro dom-standard-datatable/remove (root-element new-root-element form-element table-element action
thead-element tbody-element tfoot-element)
`(dom-datatable ,root-element ,new-root-element ,form-element ,table-element ,action ,thead-element ,tbody-element
(dom-datatable-control ,tfoot-element (dom-standard-control-elements/remove))))
;; create a new dom selector with the provided attributes
(defpsmacro make (selector &rest attributes)
`((@ ,selector attr) (create ,@attributes)))
(defmacro defpstag (tag html)
`(defpsmacro ,tag (property-list insert-body-function &body body)
(loop for content in body
with dom-tag = `(@ (make ($ ,,html) ,@property-list))
do (setf dom-tag `((@ ,dom-tag ,insert-body-function) ,content))
finally (return dom-tag))))
;;
(defpstag div "<div></div>")
(defpstag a "<a></a>")
(defpstag img "<img></img>")
(defpstag form "<form></form>")
(defpstag textbox "<input type=\"text\"></input>")
(defpstag fieldset "<fieldset></fieldset>")
(defpstag label "<label></label>")
(defpstag password "<input type=\"password\"></input>")
(defpstag hidden "<input type=\"hidden\"></input>")
(defpstag file "<input type=\"file\"></input>")
(defpstag textarea "<textarea></textarea>")
(defpstag script "<script></script>")
(defpstag slider "<div></div>")
(defpstag checkbox "<input type=\"checkbox\"></input>")
(defpstag submit "<input type=\"submit\"></input>")
(defpstag select "<select></select>")
(defpstag option "<option></option>")
;;
(defpstag ul "<ul></ul>")
(defpstag li "<li></li>")
;; table
(defpstag table "<table></table>")
(defpstag thead "<thead></thead>")
(defpstag tbody "<tbody></tbody>")
(defpstag tfoot "<tfoot></tfoot>")
(defpstag tr "<tr></tr>")
(defpstag td "<td></td>")
;; traversing
;; filtering
#|
(ps ((/. seq eq) index))
(ps ((/. seq filter) expr))
(ps ((/. seq is) expr))
(ps ((/. seq map) callback))
(ps ((/. seq not) expr))
(ps ((/. seq slice) start end))
|#
;; ajax
(defpsmacro get-json (url callback)
`($.ajax
(create url ,url
cache false
data-type "json"
success ,callback)))
(defpsmacro get-json/data (url data callback)
`($.ajax
(create type "post"
url ,url
data (+ "oid=" ,data)
data-type "json"
success ,callback)))
(defpsmacro simple-ajax-redirect (redirect-url query-string)
`($.ajax
(create type "get"
url (relative-url "")
data-type "json"
success
(lambda (data text-status)
(setf (@ window location href)
(+ ,redirect-url ,query-string)))
complete
(lambda (xml-http)
(setf (@ top location href) (+ ,redirect-url ,query-string))))))
(defpsmacro ajax-redirect (request-url redirect-url)
`($.ajax
(create type "post"
url ,request-url
data-type "json"
success
(lambda (data text-status)
(setf (@ window location href)
(+ ,redirect-url "?oid=" (@ data oid))))
complete
(lambda (xml-http)
(if (= (@ xml-http status) 301)
(setf (@ top location href) ,redirect-url))))))
(defpsmacro ajax-subredirect (request-url redirect-url)
`($.ajax
(create type "post"
url ,request-url
data-type "json"
success
(lambda (data text-status)
(setf (@ window location href)
(+ ,redirect-url "?oid=" ((@ $ get-url-var) "oid")
"&suboid=" (@ data oid))))
complete
(lambda (xml-http)
(if (= (@ xml-http status) 301)
(setf (@ top location href) ,redirect-url))))))
(defpsmacro ajax-subredirect2 (request-url redirect-url suboid)
`($.ajax
(create type "post"
url ,request-url
data-type "json"
success
(lambda (data text-status)
(setf (@ window location href)
(+ ,redirect-url "?oid=" ((@ $ get-url-var) "oid")
"&suboid=" ,suboid
"&suboid2=" (@ data oid))))
complete
(lambda (xml-http)
(if (= (@ xml-http status) 301)
(setf (@ top location href) ,redirect-url))))))
;; utilities
(defpsmacro each (items callback)
`($.each ,items ,callback))
;;
(defpsmacro json-bind (root json-url &body body)
`(get-json
,json-url
(lambda (data)
(if (not (null (@ data items)))
(each (@ data items)
(lambda (i item)
((@ ,@body
append-to)
,root)))
((@ (td (colspan "2") append "No items have been added yet.") append-to)
,root)))))
(defpsmacro json-bind/id (root json-url id &body body)
`(get-json/data
,json-url
,id
(lambda (data)
(if (not (null (@ data items)))
(each (@ data items)
(lambda (i item)
((@ ,@body
append-to)
,root)))
((@ (td (colspan "2") append "No items have been added yet.") append-to)
,root)))))
;; validation
(defpsmacro validate-required (value)
`(> (length ,value) 0))
(defpsmacro validate-integer (value)
`(let ((nr (new (*reg-exp "[0-9]+" "i"))))
((@ nr test) ,value)))
(defpsmacro validate-integer-range (value min max)
`(and (validate-integer ,value)
(>= ,value ,min)
(<= ,value ,max)))
(defpsmacro validate-postcode (value)
`(let ((r1 (new (*reg-exp "[a-z][a-z][0-9][0-9] [0-9][a-z][a-z]" "i")))
(r2 (new (*reg-exp "[a-z][a-z][0-9] [0-9][a-z][a-z]" "i")))
(r3 (new (*reg-exp "[a-z][a-z][0-9][0-9][0-9][a-z][a-z]" "i")))
(r4 (new (*reg-exp "[a-z][a-z][0-9][0-9][a-z][a-z]" "i"))))
(or ((@ r1 test) ,value)
((@ r2 test) ,value)
(= ,value ""))))
(defpsmacro validate-date (value)
`(let ((r1 (new (*reg-exp "[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9]"))))
((@ r1 test) ,value)))
(defpsmacro validate-e-mail (value)
`(let ((r1 (new (*reg-exp "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-].[a-zA-Z]"))))
((@ r1 test) ,value)))
(defpsmacro validate-day (value)
`(validate-integer-range ,value 1 31))
(defpsmacro validate-month (value)
`(validate-integer-range ,value 1 12))
(defpsmacro validate-year (value)
`(validate-integer-range ,value 2000 5000))
;; tinymce
(defpsmacro initialize-tinymce (&body body)
`((@ tiny-m-c-e init)
(create ,@body)))