From 170ee9ab90690ebe89cade2bae25ecbdf7472c11 Mon Sep 17 00:00:00 2001 From: Daniel Gackle Date: Sat, 11 Apr 2009 23:51:27 -0700 Subject: [PATCH] Added a file PS-DOM with handy utility macros for DOM functionality, e.g. Lispy ways of accessing offsetWidth, offsetHeight, etc. --- parenscript.asd | 3 ++- src/lib/ps-dom.lisp | 42 ++++++++++++++++++++++++++++++++++++++++++ src/package.lisp | 9 +++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/lib/ps-dom.lisp diff --git a/parenscript.asd b/parenscript.asd index 17eed58..86bae8a 100755 --- a/parenscript.asd +++ b/parenscript.asd @@ -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 index 0000000..2b54142 --- /dev/null +++ b/src/lib/ps-dom.lisp @@ -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)))) diff --git a/src/package.lisp b/src/package.lisp index dc3a441..ac0280d 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -196,6 +196,15 @@ #:apply #:destructuring-bind + ;; DOM accessing utils + #:inner-html + #:uri-encode + #:attribute + #:offset + #:scroll + #:inner + #:client + ;; js runtime utils #:*ps-lisp-library* #:mapcar -- 2.20.1