Fix missing `without-special-symbol-access' in `funcall-with-attribute-context'
[clinton/lisp-on-lines.git] / src / ucw / html-description.lisp
CommitLineData
4358148e 1(in-package :lisp-on-lines)
2
81d70610 3(export '(html-description) (find-package :lisp-on-lines))
4358148e 4
b7657b86 5(defvar *escape-html* t)
6
46440824 7(defmethod generic-format ((display ucw-core:component) string &rest args)
b7657b86 8 (<:as-html (with-output-to-string (stream)
9 (apply #'call-next-method stream string args))))
10
4358148e 11(define-description html-description ()
81d70610 12 ())
13
81d70610 14(define-description t ()
6de8d300 15 ((css-class :value "lol-description" :activep nil)
4358148e 16 (dom-id :function (lambda (x)
17 (declare (ignore x))
18 (symbol-name
6de8d300 19 (gensym "DOM-ID-")))
20 :activep nil))
4358148e 21 (:in-description html-description))
22
23(define-layered-class html-attribute ()
24 ((css-class :accessor attribute-css-class
25 :initform "lol-attribute")
2548f054 26 (dom-id :accessor attribute-dom-id :initform nil)
f56d6e7e 27 (value-tag :accessor attribute-html-tag :initform nil :initarg :html-tag)
2548f054 28 (display-empty-label :accessor attribute-display-empty-label-p :initarg :display-empty-label-p :initform t)))
4358148e 29
30(define-layered-class standard-attribute
31 :in-layer #.(defining-description 'html-description)
32 (html-attribute)
33 ())
34
6de8d300 35(define-layered-function display-html-attribute-label (object attribute)
36 (:method (object attribute)
2548f054 37
6de8d300 38 (let ((label (attribute-label attribute)))
2548f054 39 (when (or label (attribute-display-empty-label-p attribute))
40 (<:td (<:label
6de8d300 41 :class "lol-attribute-label"
42 (when label
43 (<:as-html
44 (with-output-to-string (*display*)
2548f054 45 (display-attribute-label attribute)))))))))
b7657b86 46 (:method
47 :in-layer #.(defining-description 'inline)
48 (object attribute)
49 (let ((label (attribute-label attribute)))
50 (when label
f56d6e7e 51 (<:as-html (display-attribute-label attribute))))))
6de8d300 52
53(define-layered-function display-html-attribute-value (object attribute)
54 (:method (object attribute)
f56d6e7e 55
2548f054 56 (<:td
f56d6e7e 57 :class "lol-attribute-value"
58 (<:as-html
59 (display-attribute-value attribute))))
b7657b86 60
61 (:method
62 :in-layer #.(defining-description 'inline) (object attribute)
63 (display-attribute-value attribute)))
6de8d300 64
f56d6e7e 65
6de8d300 66(define-layered-function display-html-attribute (object attribute)
b7657b86 67
6de8d300 68 (:method (object attribute)
2548f054 69 (<:tr
f56d6e7e 70 :class (format nil "~A lol-attribute" (attribute-css-class attribute))
b7657b86 71 (when (attribute-dom-id attribute)
72 :id (attribute-dom-id attribute))
73 (display-html-attribute-label object attribute)
74 (display-html-attribute-value object attribute)))
75
76 (:method
77 :in-layer #.(defining-description 'inline)
78 (object attribute)
79 (<:span
6de8d300 80 :class (attribute-css-class attribute)
81 (when (attribute-dom-id attribute)
82 :id (attribute-dom-id attribute))
f56d6e7e 83 (<:span :class "lol-attribute-label"
84 (display-html-attribute-label object attribute))
85 (<:span :class "lol-attribute-value"
86 (display-html-attribute-value object attribute)))))
b7657b86 87
88(define-layered-method display-using-description
89 :in-layer #.(defining-description 'html-description)
90 :around ((attribute standard-attribute) display object &rest args)
eeed4326 91 (declare (ignore args))
b7657b86 92 (display-html-attribute object attribute))
93
94
2548f054 95(defun capture-description (attribute function)
96 (let ((obj (described-object (attribute-description attribute))))
97 (lambda (&rest args)
98 (dletf (((described-object attribute) obj))
99 (apply function args)))))
100
f4efa7ff 101(defun make-attribute-value-writer (attribute)
eeed4326 102 (let ((obj (described-object (attribute-description attribute)))
103 (value (attribute-value attribute))
104 (desc (attribute-description attribute)))
f4efa7ff 105 (lambda (val)
eeed4326 106 (dletf (((described-object (attribute-description attribute)) obj))
2548f054 107 (with-active-descriptions (editable)
108 (unless (and (unbound-slot-value-p value)
109 (equal "" val))
eeed4326 110 (with-described-object (obj desc)
111 (setf (attribute-value attribute)
112 (parse-attribute-value attribute val)))))))))
2548f054 113
f4efa7ff 114
2548f054 115(defmethod html-attribute-value (attribute)
116 (let ((val (attribute-value attribute)))
117 (if (unbound-slot-value-p val)
118 ""
119 val)))
f4efa7ff 120
121(defmethod display-html-attribute-editor (attribute editor)
46440824 122 (<ucw:input :type "text"
2548f054 123 :reader (html-attribute-value attribute)
f4efa7ff 124 :writer (make-attribute-value-writer attribute)))
125
2548f054 126(defmethod display-html-attribute-editor ((attribute slot-definition-attribute) editor)
127 (call-next-method))
128
f4efa7ff 129(defmethod display-html-attribute-editor (attribute (editor password-attribute-editor))
46440824 130 (<ucw:input :type "password"
2548f054 131 :reader (html-attribute-value attribute)
f4efa7ff 132 :writer (make-attribute-value-writer attribute)))
133
f4efa7ff 134(define-layered-method display-attribute-editor
135 :in-layer #.(defining-description 'html-description) (attribute)
136 (display-html-attribute-editor attribute (attribute-editor attribute)))
137
6de8d300 138
139(define-layered-method display-html-attribute-value
140 :in-layer #.(defining-description 'editable) (object attribute)
b5cc8f37 141 #+nil(<:as-html (princ-to-string (attribute-editp attribute)))
f4efa7ff 142 (if (attribute-editp attribute)
f56d6e7e 143 (<:td
eeed4326 144 :class "lol-attribute-value" (display-attribute-editor attribute))
f56d6e7e 145 (call-next-method)))
6de8d300 146
b7657b86 147(define-layered-function display-html-description (description display object &optional next-method)
148 (:method (description display object &optional (next-method #'display-using-description))
eeed4326 149 (let ((dom-id (find-attribute description 'dom-id))
150 (css-class (find-attribute description 'dom-id)))
151 (<:table
b7657b86 152 :class (list (attribute-value css-class) "lol-description" "t")
153 :id (attribute-value dom-id)
2548f054 154 (funcall next-method)
155 (<:br :class "clear")))))
6de8d300 156
157
158(define-layered-method display-html-description
b7657b86 159 :in-layer #.(defining-description 'inline) (description display object &optional next-method)
eeed4326 160 (let ((dom-id (find-attribute description 'dom-id))
161 (css-class (find-attribute description 'dom-id)))
6de8d300 162 (<:span
eeed4326 163 :class (list (attribute-value css-class) "lol-description")
164 :id (attribute-value dom-id)
165 (funcall next-method))))
b7657b86 166
6de8d300 167
168(define-display
e8d4fa45 169 :in-description html-description ((description t)
46440824 170 (display ucw-core:component)
e8d4fa45 171 object)
b7657b86 172 (display-html-description description display object (lambda ()
173 (call-next-method))))
174
2548f054 175(define-layered-method display-html-attribute-value
176 (object (attribute list-attribute))
ec6dde1e 177 (let ((val (attribute-value attribute)))
178 (when (listp val)
179 (<:ul
180 (arnesi:dolist* (item (attribute-value attribute))
46440824 181
182 (dletf (((attribute-object attribute) item))
183 (<:li (apply #'display *display* item (slot-value attribute 'item-args)))))))))