forked from kennytilton/qooxlisp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qx-utils.lisp
458 lines (372 loc) · 14.2 KB
/
qx-utils.lisp
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
;; -*- mode: Lisp; Syntax: Common-Lisp; Package: qooxlisp; -*-
#|
qx-utils
(See package.lisp for license and copyright notigification)
|#
(in-package :qxl)
(eval-now!
(defun qx-dummy ())
(set-macro-character #\® #'qx-dummy))
(defconstant +qx-alt-key-mask+ 4)
(defconstant +qx-shift-key-mask+ 1)
(defconstant +qx-control-key-mask+ 2)
(defconstant +qx-meta-key-mask+ 8)
(defparameter +red+ "red")
(defparameter +blue+ "blue")
(defparameter +white+ "white")
(defparameter +gray+ "gray")
(defparameter +black+ "black")
(defparameter +yellow+ "yellow")
(defparameter +green+ "green")
(export! +red+ +blue+ +white+ +gray+ +black+ +yellow+ +green+)
(defparameter *session-ct* 0)
(defvar *web-session*)
(defparameter *qx-sessions* (make-hash-table))
(defun qx-alt-key-p (x)
(logtest +qx-alt-key-mask+ (if (stringp x) (parse-integer x) x)))
(defun qx-control-key-p (x)
(logtest +qx-control-key-mask+ (if (stringp x) (parse-integer x) x)))
(defun qx-shift-key-p (x)
(logtest +qx-shift-key-mask+ (if (stringp x) (parse-integer x) x)))
(export! oid-to-object oid$-to-object)
(defun oid-to-object (oid &optional caller)
(or (gethash oid (dictionary *web-session*))
(error "no item for oid ~a, caller ~a" oid caller)))
(defun oid$-to-object (oid$ &optional caller)
(b-if oid (parse-integer oid$ :junk-allowed t)
(oid-to-object oid caller)
(error "oid NAN ~a, caller ~a" oid$ caller)))
(export! qx-alt-key-p qx-control-key-p qx-shift-key-p)
(defmacro cfg (f &optional (qxl-alias f))
;; alias needed because, eg, Cells uses value for the model associated with any widget
;; while the qooxdoo 'value' is a labels displayed string, the qxl 'text$'
(let ((x (gensym)))
`(b-when ,x (,qxl-alias self)
;(list (cons ,(intern f :keyword) ,x)) ;; Does not work on sbcl: f is not a string
(list (cons ,(intern (symbol-name f) :keyword) ,x))
)))
(defun k-word (s)
(when s (if (consp s) (mapcar 'k-word s)
(intern s :keyword))))
(let ((case (if (string= "x" (symbol-name 'x)) :modern :ansi)))
(defun qxl-sym (s)
(intern (ecase case (:modern s)(:ansi (string-upcase s))) :qxl)))
#+nil
(defmacro whtml (&body body)
`(catch 'excl::printer-error
(net.html.generator:html ,@body)))
#+nil
(defun req-val (req tag)
(net.aserve:request-query-value tag req))
(defun req-val (req tag)
(backend-request-value *backend* req tag))
#+nil ;; henrik: seems unused, if not could easily be move to backend
(defmacro with-plain-text-response ((req ent) &body body)
`(prog1 nil
(net.aserve:with-http-response (,req ,ent :content-type "text/plain")
(net.aserve:with-http-body (,req ,ent)
(let* ((ws (net.aserve:websession-from-req ,req)))
(declare (ignorable ws ns))
,@body)))))
#+nil ;; henrik: seems unused, if not could easily be move to backend
(defmacro with-html-response ((req ent) &body body)
`(prog1 nil
(net.aserve:with-http-response (,req ,ent :content-type "text/html")
(net.aserve:with-http-body (,req ,ent)
(let ((ws (net.aserve:websession-from-req ,req)))
(declare (ignorable ws))
,@body)))))
(defparameter *js-response* nil)
(defparameter *ekojs* nil)
#+nil
(defmacro with-js-response ((req ent) &body body)
`(prog1 nil
(backend-js-response *backend* ,req ,ent
(lambda ()
(setf *js-response* nil)
,@body ;; this populates *js-response*
(when *ekojs*
(format t "ekojs:~%~s" *js-response*)
;;(trcx :ekojsrq (rq-raw ,req))
)
;; (format t "~&js response size ~a~%> " (length *js-response*))
;;(push *js-response* (responses session))
(format nil "(function () {~a})();" (or *js-response* "null;"))))))
(defmacro with-js-response ((req ent) &body body)
`(backend-js-response *backend* ,req ,ent
(lambda ()
(setf *js-response* nil)
,@body ;; this populates *js-response*
(when *ekojs*
(format t "ekojs:~%~s" *js-response*)
;;(trcx :ekojsrq (rq-raw ,req))
)
;; (format t "~&js response size ~a~%> " (length *js-response*))
;;(push *js-response* (responses session))
(format nil "(function () {~a})();" (or *js-response* "null;")))))
#+nil
(defmacro with-js-response ((req ent) &body body)
`(prog1 nil
(net.aserve:with-http-response (,req ,ent :content-type "text/javascript")
(net.aserve:with-http-body (,req ,ent)
(setf *js-response* nil)
,@body ;; this populates *js-response*
(when *ekojs*
(format t "ekojs:~%~s" *js-response*)
;;(trcx :ekojsrq (rq-raw ,req))
)
;; (format t "~&js response size ~a~%> " (length *js-response*))
;;(push *js-response* (responses session))
(qxl:whtml (:princ (format nil "(function () {~a})();" (or *js-response* "null;"))))))))
#+bbbbb
(princ *js-response*)
(export! rq-raw *ekojs*)
(defun rq-raw (r) (request-raw-request r))
#+check
(print *js-response*)
#+nil
(defmacro with-json-response ((req ent) &body body)
`(prog1 nil
(net.aserve:with-http-response (,req ,ent :content-type "application/json")
(net.aserve:with-http-body (,req ,ent)
(let ((ws nil #+nahhh (net.aserve:websession-from-req ,req)))
(declare (ignorable ws))
,@body)))))
#+nil
(defmacro with-json-response ((req ent) &body body)
`(prog1 nil
(backend-json-response *backend* ,req ,ent
(lambda ()
(let ((ws nil #+nahhh (net.aserve:websession-from-req ,req)))
(declare (ignorable ws))
,@body)))))
(defmacro with-json-response ((req ent) &body body)
`(backend-json-response *backend* ,req ,ent
(lambda ()
(let ((ws nil #+nahhh (net.aserve:websession-from-req ,req)))
(declare (ignorable ws))
,@body))))
(defun qxfmt (fs &rest fa)
(when (eq :deferred-to-ufb-1 fs)
(error "fshasit ~a" fs))
(let ((n (apply 'format nil (conc$ "~&" fs "~%") fa)))
(when (search "deferred-to-ufb" n :test 'string-equal)
(print `(:response-at-error ,*js-response*))
(error "JS got deferred ~a" (list* fs fa)))
(setf *js-response*
(conc$ *js-response* n)))
(values))
#+save
(defun qxfmt (fs &rest fa)
(setf *js-response*
(conc$ *js-response* (apply 'format nil (conc$ "~&" fs "~%") fa))))
(search "deferred-to-ufb" "abc" :test 'string-equal)
(defun qxfmtd (fs &rest fa)
(let ((x (apply 'format nil (conc$ "~&" fs "~%") fa)))
(trcx :qxfmtd-adds x)
(setf *js-response*
(conc$ *js-response* x))))
#+nil ;;Henrik: seems unused, if not should be moved to backend
(defmacro ml$ (&rest x)
(let ((s (gensym)))
`(with-output-to-string (,s)
(net.html.generator:html-stream ,s
,@x))))
(defun js-prep (&rest lists)
(format nil "(~{~a~})"
(loop for list in lists
collecting (format nil "(~{~(~a~)~^ {~a}~})" list))))
(defun json$ (x) (json:encode-json-to-string x))
(defun jsk$ (&rest plist)
(json$ (loop for (a b) on plist by #'cddr
collecting (cons a b))))
(defun cvtjs (x)
(cond
((string-equal x "null") nil)
((string-equal x "true") t)
((string-equal x "false") nil)
(t x)))
(defstruct (qx-keypress (:conc-name kpr-))
mods key)
(defstruct (qx-keyinput (:conc-name kin-))
mods char code)
(export! qx-keypress qx-keyinput make-qx-keyinput make-qx-keypress kpr-key kpr-mods kin-mods kin-char kin-code)
(defmacro mk-layout (model class &rest initargs) ;; >>> use make-layout
`(make-instance ,class
:oid (get-next-oid (session ,model))
,@initargs))
(defun make-layout (model class initargs)
(apply 'make-instance class
:oid (get-next-oid (session model))
initargs))
#+xxxx
(jsk$ :left 2 :top 3)
#+test
(json$ (list (cons 'aa-bb t)))
(defmacro groupbox ((&rest layo-iargs)(&rest iargs) &rest kids)
`(make-kid 'qx-group-box
,@iargs
:layout (c? (mk-layout self 'qx-vbox ,@layo-iargs))
:kids (c? (the-kids ,@kids))))
(defmacro scroller ((&rest iargs) &rest kids)
`(make-kid 'qx-scroll
,@iargs
:kids (c? (the-kids ,@kids))))
(defmacro stack ((&rest iargs) &rest kids)
`(make-kid 'qx-stack
,@iargs
:kids (c? (the-kids ,@kids))))
(defmacro stackn ((&rest iargs) &rest kids)
`(make-kid 'qx-stack
,@iargs
:kids (c?n (the-kids ,@kids)))) ;; c-in would not set self correctly for kids' parent
(export! tabview qx-tab-view vpage vpagex qx-tab-page vboxn hboxn stack qx-stack stackn vpagex-once vpagex!)
(defmacro tabview ((&rest iargs) &rest kids)
`(make-kid 'qx-tab-view
,@iargs
:kids (c? (the-kids ,@kids))))
(defmacro vpagex ((&rest layout-iargs)(name &rest iargs) &rest kids)
`(make-kid 'qx-tab-page
:md-name ,name
,@iargs
:register? t
:bookmark? t
:layout(c? (mk-layout self 'qx-vbox ,@layout-iargs))
:kids (c? (when (eq self (value (u^ qx-tab-view)))
(the-kids ,@kids)))))
(defmacro vpagex! ((&rest layout-iargs)(name &rest iargs) &rest kids)
`(make-kid 'qx-tab-page
:md-name ,name
,@iargs
:register? t
:bookmark? t
:layout(c? (mk-layout self 'qx-vbox ,@layout-iargs))
:kids (c? (the-kids ,@kids))))
(defmacro vpagex-once ((&rest layout-iargs)(name &rest iargs) &rest kids)
`(make-kid 'qx-tab-page
:md-name ,name
,@iargs
:register? t
:bookmark? t
:layout(c? (mk-layout self 'qx-vbox ,@layout-iargs))
:kids (c?once (when (eq self (value (u^ qx-tab-view)))
(the-kids ,@kids)))))
(defmacro vpage ((&rest layout-iargs)( &rest iargs) &rest kids)
`(make-kid 'qx-tab-page
,@iargs
:layout(c? (mk-layout self 'qx-vbox ,@layout-iargs))
:kids (c? (when (eq self (value (u^ qx-tab-view)))
(the-kids ,@kids)))))
(defmacro checkgroupbox ((&rest layo-iargs)(&rest iargs) &rest kids)
;;; unfinished....
`(make-kid 'qx-check-group-box
,@iargs
:layout (c? (mk-layout self 'qx-vbox ,@layo-iargs))
:kids (c? (the-kids ,@kids))))
;;;(defmacro vbox ((&rest layout-iargs)(&rest compo-iargs) &rest kids)
;;; `(make-kid 'qxl-stack
;;; ,@compo-iargs
;;; :layout-iargs (list ,@layout-iargs)
;;; :kids (c? (the-kids ,@kids))))
(defmacro vboxn ((&rest layout-iargs)(&rest compo-iargs) &rest kids)
"vbox where kids are altered procedurally"
`(make-kid 'qx-composite
,@compo-iargs
:layout (c? (mk-layout self 'qx-vbox ,@layout-iargs))
:kids (c-in (the-kids ,@kids))))
(defmacro vbox ((&rest layout-iargs)(&rest compo-iargs) &rest kids)
"vbox where kids are altered procedurally"
`(make-kid 'qx-composite
,@compo-iargs
:layout (c? (mk-layout self 'qx-vbox ,@layout-iargs))
:kids (c? (the-kids ,@kids))))
(defmacro hboxn ((&rest layout-iargs)(&rest compo-iargs) &rest kids)
"hbox where kids are altered procedurally"
`(make-kid 'qx-composite
,@compo-iargs
:layout (c? (mk-layout self 'qx-hbox ,@layout-iargs))
:kids (c-in (the-kids ,@kids))))
(defmacro hbox ((&rest layout-iargs)(&rest compo-iargs) &rest kids)
"vbox where kids are altered procedurally"
`(make-kid 'qx-composite
,@compo-iargs
:layout (c? (mk-layout self 'qx-hbox ,@layout-iargs))
:kids (c? (the-kids ,@kids))))
(defmacro grid ((&rest layout-iargs)(&rest compo-iargs) &rest kids)
"vbox where kids are altered procedurally"
`(make-kid 'qx-composite
,@compo-iargs
:layout (c? (mk-layout self 'qx-grid ,@layout-iargs))
:kids (c? (the-kids ,@kids))))
(defmd qxl-stack (qx-composite)
layout-iargs
:layout (c? (make-layout self 'qx-vbox (^layout-iargs))))
(export! qxl-row qxl-flow vbox grid)
(defmd qxl-row (qx-composite)
layout-iargs
:layout (c? (make-layout self 'qx-hbox (^layout-iargs))))
(defmd qxl-flow (qx-composite)
layout-iargs
:layout (c? (make-layout self 'qx-flow (^layout-iargs))))
(defmacro flow ((&rest layout-iargs)(&rest compo-iargs) &rest kids)
`(make-kid 'qx-composite
,@compo-iargs
:layout (c? (mk-layout self 'qx-flow ,@layout-iargs))
:kids (c? (the-kids ,@kids))))
(defmacro lbl (label-form &rest iargs)
`(make-kid 'qx-label
:text$ ,label-form
,@iargs))
(export! rtf scroller qx-scroll img qx-image qxl-stack flow qx-flow)
(defmacro rtf (label-form &rest iargs)
`(make-kid 'qx-label
:text$ ,label-form
:rich t
,@iargs))
(defmacro img (url &rest iargs)
`(make-kid 'qx-image
:source ,url
,@iargs))
(defmacro checkbox (model label &rest iargs)
`(make-kid 'qx-check-box
:md-name ,model
:label ,label
,@iargs))
(defmacro selectbox (name (&rest iargs) &body kids)
`(make-kid 'qx-select-box
:md-name ,name
,@iargs
:kids (c? (the-kids ,@kids))))
(export! qxlist)
(defmacro qxlist (name (&rest iargs) &body kids)
`(make-kid 'qx-list
:md-name ,name
,@iargs
:kids (c? (the-kids ,@kids))))
(defmacro combobox (name (&rest iargs) &rest kids)
`(make-kid 'qx-combo-box
:md-name ,name
,@iargs
:onkeypress (lambda (self req)
(let* ((key (req-val req "keyId"))
(jsv (req-val req "value"))
(v (cvtjs jsv)))
(setf (^value) (cond
((= 1 (length key))
(conc$ v key))
((string-equal key "Backspace")
(subseq v 0 (max 0 (1- (length v)))))
(t v)))))
:kids (c? (the-kids ,@kids))))
(defmacro textfield (name &rest iargs)
`(make-kid 'qx-text-field
:md-name ,name
,@iargs))
(export! textfield)
(defmacro button (label (&rest iargs) &key onexec)
`(make-kid 'qx-button
:label ,label
,@iargs
:onexecute (lambda (self req)
(declare (ignorable self req))
,onexec)))