further work on presentations - transformers

This commit is contained in:
David Botton 2022-02-14 18:04:58 -05:00
parent a8cc2f8105
commit fa80acb1a1
3 changed files with 49 additions and 20 deletions

View file

@ -18,30 +18,25 @@
;; link-form-element-to-slot ;; ;; link-form-element-to-slot ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro link-form-element-to-slot (clog-obj object accessor) (defmacro link-form-element-to-slot (clog-obj object accessor &key transform)
"Link changes to (value CLOG-OBJ) to (ACESSOR OBJECT)" "Link changes to (value CLOG-OBJ) to (ACESSOR OBJECT)"
`(set-on-change ,clog-obj `(link-form-element-to-object ,clog-obj (,accessor ,object) :transform ,transform))
(lambda (obj)
(declare (ignore obj))
(setf (,accessor ,object) (value ,clog-obj)))))
;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;
;; link-element-to-slot ;; ;; link-element-to-slot ;;
;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro link-element-to-slot (clog-obj object accessor) (defmacro link-element-to-slot (clog-obj object accessor &key transform)
"Link changes to (text CLOG-OBJ) to (ACESSOR OBJECT)" "Link changes to (text CLOG-OBJ) to (ACESSOR OBJECT)"
`(set-on-change ,clog-obj `(link-element-to-object ,clog-obj (,accessor ,object) :transform ,transform))
(lambda (obj)
(declare (ignore obj))
(setf (,accessor ,object) (text ,clog-obj)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; link-slot-to-form-element ;; ;; link-slot-to-form-element ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro link-slot-to-form-element (object accessor clog-obj) (defmacro link-slot-to-form-element (object accessor clog-obj)
"Link changes to lisp (ACCESSOR OBJECT) to (value CLOG-OBJ)" "Link changes to lisp (ACCESSOR OBJECT) to (value CLOG-OBJ). Only one
element can be bound at a time to a list object."
`(defmethod (setf ,accessor) :after (new-value (obj (eql ,object))) `(defmethod (setf ,accessor) :after (new-value (obj (eql ,object)))
(setf (value ,clog-obj) new-value))) (setf (value ,clog-obj) new-value)))
@ -50,7 +45,35 @@
;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro link-slot-to-element (object accessor clog-obj) (defmacro link-slot-to-element (object accessor clog-obj)
"Link changes to lisp (ACCESSOR OBJECT) to (text CLOG-OBJ)" "Link changes to lisp (ACCESSOR OBJECT) to (text CLOG-OBJ). Only one
element can be bound at a time to a list object."
`(defmethod (setf ,accessor) :after (new-value (obj (eql ,object))) `(defmethod (setf ,accessor) :after (new-value (obj (eql ,object)))
(setf (text ,clog-obj) new-value))) (setf (text ,clog-obj) new-value)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; link-form-element-to-object ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro link-form-element-to-object (clog-obj object &key transform)
"Link changes to (value CLOG-OBJ) to any lisp OBJECT"
`(set-on-change ,clog-obj
(lambda (obj)
(declare (ignore obj))
(let ((v (if ,transform
(funcall ,transform (value ,clog-obj))
(value ,clog-obj))))
(setf ,object v)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; link-element-to-object ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro link-element-to-object (clog-obj object &key transform)
"Link changes to (text CLOG-OBJ) to any lisp OBJECT"
`(set-on-change ,clog-obj
(lambda (obj)
(declare (ignore obj))
(let ((v (if ,transform
(funcall ,transform (text ,clog-obj))
(text ,clog-obj))))
(setf ,object v)))))

View file

@ -495,10 +495,12 @@ embedded in a native template application.)"
(defsection @clog-presentations (:title "CLOG Presentations") (defsection @clog-presentations (:title "CLOG Presentations")
"CLOG-Presentations - CLOG Presentations" "CLOG-Presentations - CLOG Presentations"
(link-form-element-to-slot macro) (link-form-element-to-slot macro)
(link-element-to-slot macro) (link-element-to-slot macro)
(link-slot-to-form-element macro) (link-slot-to-form-element macro)
(link-slot-to-element macro)) (link-slot-to-element macro)
(link-form-element-to-object macro)
(link-element-to-object macro))
(defsection @clog-panels (:title "CLOG Panels") (defsection @clog-panels (:title "CLOG Panels")
"CLOG-Panel - CLOG Panels" "CLOG-Panel - CLOG Panels"

View file

@ -20,11 +20,15 @@
(b1 (create-button body :content "Set (my-slot lisp-obj) Value")) (b1 (create-button body :content "Set (my-slot lisp-obj) Value"))
(b2 (create-button body :content "Get (my-slot lisp-obj) Value")) (b2 (create-button body :content "Get (my-slot lisp-obj) Value"))
(tmp (create-br body)) (tmp (create-br body))
(t1 (create-div body :content "[counter]"))) (t1 (create-div body :content "[counter]"))
(i3 (create-form-element body :text
:label (create-label body :content "Change my-count:"))))
;; We set up direct relationships between lisp obj and clog objects ;; We set up direct relationships between lisp obj and clog objects
(link-form-element-to-slot i1 lisp-obj my-slot) ;; any change to i1 will change my-slot (link-form-element-to-slot i1 lisp-obj my-slot) ;; any change to i1 will change my-slot
(link-slot-to-form-element lisp-obj my-slot i1) ;; any change to my-slot will change i1 (link-slot-to-form-element lisp-obj my-slot i1) ;; any change to my-slot will change i1
(link-slot-to-element lisp-obj my-count t1) ;; any change to my-count will chat t1 (link-slot-to-element lisp-obj my-count t1) ;; any change to my-count will change h1
(link-form-element-to-object
i3 (my-count lisp-obj) :transform #'parse-integer) ;; any change to i3 will change my-count
;; This change of my-slot will immediately change in the web page ;; This change of my-slot will immediately change in the web page
(setf (my-slot lisp-obj) "First Value") (setf (my-slot lisp-obj) "First Value")
(set-on-click b1 (set-on-click b1