Added a file PS-DOM with handy utility macros for DOM functionality, e.g. Lispy ways...
authorDaniel Gackle <danielgackle@gmail.com>
Sun, 12 Apr 2009 06:51:27 +0000 (23:51 -0700)
committerVladimir Sedach <vsedach@gmail.com>
Sun, 12 Apr 2009 23:10:31 +0000 (17:10 -0600)
parenscript.asd
src/lib/ps-dom.lisp [new file with mode: 0644]
src/package.lisp

index 17eed58..86bae8a 100755 (executable)
@@ -30,7 +30,8 @@
                                      (:module :lib
                                               :components ((:file "ps-html")
                                                            (:file "ps-loop")
-                                                           (:file "ps-macro-lib"))
+                                                           (:file "ps-macro-lib")
+                                                           (:file "ps-dom"))
                                               :depends-on ("compilation-interface"))))
                (:module :runtime
                         :components ((:file "ps-runtime-lib"))
diff --git a/src/lib/ps-dom.lisp b/src/lib/ps-dom.lisp
new file mode 100644 (file)
index 0000000..2b54142
--- /dev/null
@@ -0,0 +1,42 @@
+(in-package "PARENSCRIPT")
+
+;; Utilities for accessing standard DOM functionality in a Lispier, PSier way.
+
+(defpsmacro inner-html (el)
+  `(@ ,el :inner-h-t-m-l))
+
+(defpsmacro uri-encode (str)
+  `(if (null ,str) "" (encode-u-r-i-component ,str)))
+
+(defpsmacro attribute (el attr)
+  `((@ ,el :get-attribute) ,attr))
+
+(defun assert-is-one-of (val options)
+  (unless (member val options)
+    (error "~s is not one of ~s" val options)))
+
+(defpsmacro offset (what el)
+  (if (consp what)
+      `(offset ,(eval what) ,el)
+      (progn (assert-is-one-of what '(:top :left :height :width :bottom :right))
+             (if (member what '(:top :left :height :width))
+                 `(@ ,el ,(intern (format nil "OFFSET-~a" what)))
+                 (aif (assoc what '((:bottom :top :height) (:right :left :width)))
+                      `(+ (offset ,(second it) ,el) (offset ,(third it) ,el)))))))
+
+(defpsmacro scroll (what el)
+  (assert-is-one-of what '(:top :left :right :bottom :width :height))
+  (cond ((member what '(:top :left :width :height))
+         `(@ ,el ,(intern (format nil "SCROLL-~a" what))))
+        ((eq what :right)
+         `(+ (scroll :left ,el) (offset :width ,el)))
+        ((eq what :bottom)
+         `(+ (scroll :top ,el) (offset :height ,el)))))
+
+(defpsmacro inner (what el)
+  (assert-is-one-of what '(:width :height))
+  `(@ ,el ,(intern (format nil "INNER-~a" what))))
+
+(defpsmacro client (what el)
+  (assert-is-one-of what '(:width :height))
+  `(@ ,el ,(intern (format nil "CLIENT-~a" what))))
index dc3a441..ac0280d 100644 (file)
       #:apply
       #:destructuring-bind
 
+      ;; DOM accessing utils
+      #:inner-html
+      #:uri-encode
+      #:attribute
+      #:offset
+      #:scroll
+      #:inner
+      #:client
+      
       ;; js runtime utils
       #:*ps-lisp-library*
       #:mapcar