Added a file PS-DOM with handy utility macros for DOM functionality, e.g. Lispy ways...
[clinton/parenscript.git] / src / lib / ps-dom.lisp
1 (in-package "PARENSCRIPT")
2
3 ;; Utilities for accessing standard DOM functionality in a Lispier, PSier way.
4
5 (defpsmacro inner-html (el)
6 `(@ ,el :inner-h-t-m-l))
7
8 (defpsmacro uri-encode (str)
9 `(if (null ,str) "" (encode-u-r-i-component ,str)))
10
11 (defpsmacro attribute (el attr)
12 `((@ ,el :get-attribute) ,attr))
13
14 (defun assert-is-one-of (val options)
15 (unless (member val options)
16 (error "~s is not one of ~s" val options)))
17
18 (defpsmacro offset (what el)
19 (if (consp what)
20 `(offset ,(eval what) ,el)
21 (progn (assert-is-one-of what '(:top :left :height :width :bottom :right))
22 (if (member what '(:top :left :height :width))
23 `(@ ,el ,(intern (format nil "OFFSET-~a" what)))
24 (aif (assoc what '((:bottom :top :height) (:right :left :width)))
25 `(+ (offset ,(second it) ,el) (offset ,(third it) ,el)))))))
26
27 (defpsmacro scroll (what el)
28 (assert-is-one-of what '(:top :left :right :bottom :width :height))
29 (cond ((member what '(:top :left :width :height))
30 `(@ ,el ,(intern (format nil "SCROLL-~a" what))))
31 ((eq what :right)
32 `(+ (scroll :left ,el) (offset :width ,el)))
33 ((eq what :bottom)
34 `(+ (scroll :top ,el) (offset :height ,el)))))
35
36 (defpsmacro inner (what el)
37 (assert-is-one-of what '(:width :height))
38 `(@ ,el ,(intern (format nil "INNER-~a" what))))
39
40 (defpsmacro client (what el)
41 (assert-is-one-of what '(:width :height))
42 `(@ ,el ,(intern (format nil "CLIENT-~a" what))))