Added a few attributes that are nice n easy to use for common cases
[clinton/lisp-on-lines.git] / src / attributes / standard-attributes.lisp
1 (in-package :lisp-on-lines)
2
3 ;;;; A few layers related to attributes
4 (deflayer omit-nil-attributes)
5
6 (defdisplay :in-layer omit-nil-attributes
7 :around ((attribute standard-attribute) object)
8 (when (attribute-value object attribute)
9 (call-next-method)))
10
11 (deflayer show-attribute-labels)
12
13 (defdisplay
14 :in-layer show-attribute-labels
15 :around ((attribute standard-attribute) object)
16 (<:span
17 :class "lol-label"
18 (<:as-html (or (label attribute) (attribute.name attribute)) " "))
19 (<:span
20 :class "lol-attribute"
21 (call-next-method)))
22
23 (deflayer use-pretty-labels)
24
25 (define-layered-method label
26 :in-layer use-pretty-labels
27 :around (standard-attribute)
28 (let ((label (call-next-method)))
29 (when label
30 (string-capitalize
31 (substitute #\Space #\- label)))))
32
33 (deflayer inspect-attributes)
34
35 (defdisplay :in-layer inspect-attributes
36 :around ((attribute standard-attribute) object)
37 (call-next-method)
38 (<ucw:a :action (ucw::call-inspector self attribute)
39 :title
40 (strcat "Inspect "
41 (attribute.name attribute) ":"
42 (description.type attribute) ":"
43 (type-of attribute))
44 (<:as-html "(i)")))
45
46 ;;;; Functional attributes
47 (defattribute display-attribute ()
48 ((display-arguments
49 :accessor display-arguments
50 :initarg :display
51 :special t
52 :initform nil))
53 (:type-name display)
54 (:documentation "Apply the display function to this object"))
55
56 (defdisplay ((attribute display-attribute) object)
57 (apply #'display self (attribute-value object attribute)
58 (display-arguments attribute)))
59
60 (defattribute function-attribute ()
61 ((function :accessor function-of
62 :initarg :function
63 :initform #'funcall
64 :special t))
65 (:type-name function)
66 (:documentation ""))
67
68 (defdisplay ((function function-attribute) object)
69 (funcall (function-of function)
70 (attribute-value object function)))
71
72
73 ;;;; Attribute Grouping
74 (defattribute attribute-group ()
75 ()
76 (:default-properties
77 :group nil)
78 (:type-name group))
79
80 (defdisplay ((group attribute-group) object)
81 (apply #'display self object
82 :attributes (attributes group)
83 (group group)))
84
85 ;;;; * Base Types
86
87 (defattribute base-attribute ()
88 ()
89 (:default-properties
90 :default-value ""))
91
92 (defdisplay ((base base-attribute) object)
93 (<:as-html (attribute-value object base)))
94
95 (defattribute base-attribute (ucw::string-field)
96 ()
97 (:in-layer editor)
98 (:default-properties
99 :callback nil
100 :default-value nil
101 :default-value-predicate #'null))
102
103 (define-layered-function display-value (attribute value)
104 (:method (attribute value)
105 (if (funcall (default-value-predicate attribute) value)
106 (default-value attribute)
107 value)))
108
109 (defdisplay
110 :in-layer editor ((field base-attribute) object)
111 (LET ((value (attribute-value (object field) field)))
112 (<:input
113 :NAME
114 (callback field)
115 :VALUE (escape-as-html (strcat (display-value field value)))
116 :TYPE
117 "text"
118 :ID
119 (DOM-ID FIELD)
120 :SIZE
121 (ucw::INPUT-SIZE FIELD))))
122
123 (defdisplay
124 :in-layer editor :around ((string base-attribute) object)
125 (dletf (((callback string)
126 (or (callback string)
127 (ucw::make-new-callback
128 #'(lambda (val)
129 (setf (attribute-value object string) val)))))
130 ((object string) object))
131 (call-next-method)))
132
133 ;;;; Strings
134
135 (defattribute string-attribute (base-attribute)
136 ()
137 (:type-name string)
138 (:default-properties
139 :escape-html-p t
140 :size nil
141 :max-length nil
142 :default-value ""))
143
144 (defdisplay :in-layer omit-nil-attributes
145 :around ((attribute string-attribute) object)
146 (when (< 0 (length (attribute-value object attribute)))
147 (call-next-method)))
148
149 ;;;; default
150 (defdisplay :in-layer viewer
151 ((string string-attribute) object)
152 (if (escape-html-p string)
153 (<:as-html (attribute-value object string))
154 (<:as-is (attribute-value object string))))
155
156
157 ;;;; editor
158 (defattribute string-attribute (base-attribute)
159 ()
160 (:in-layer editor)
161 (:default-properties
162 :callback nil))
163
164
165 (defattribute string-search-attribute (string-attribute)
166 ()
167 (:default-properties
168 ;; the func that find search results
169
170 :search-action #'(lambda ()
171 (with-call/cc
172 nil))
173 ;; when chosing from a list of results, this function selects one.
174 :select-function (constantly t))
175 (:type-name string-search))
176
177 (defdisplay
178 :in-layer editor :after ((search string-search-attribute) object)
179 (<:input
180 :TYPE "submit"
181 :VALUE "search"
182 :ONCLICK
183 (JS:JS-INLINE*
184 `(PROGN
185 (IT.BESE.UCW::SET-ACTION-PARAMETER
186 ,(IT.BESE.UCW::MAKE-NEW-ACTION
187 (IT.BESE.UCW::CONTEXT.CURRENT-FRAME *CONTEXT*)
188 (search-action search)))
189 (RETURN T)))))
190
191 ;;;; textarea
192
193 (defattribute text-attribute (string-attribute)
194 ()
195 (:type-name text))
196
197 (defdisplay :in-layer editor ((string text-attribute) object)
198 (<:textarea
199 :id (dom-id string)
200 :name (callback string)
201 (or (attribute-value object string) "")))
202
203
204
205 ;;;; WALL-TIME
206
207 (defattribute wall-time-attribute (string-attribute)
208 ()
209 (:type-name clsql-sys:wall-time))
210
211 (define-layered-method attribute-value (object (attribute wall-time-attribute))
212 (let ((date (call-next-method)))
213 (when date (multiple-value-bind (y m d) (clsql:time-ymd date)
214 (format nil "~a/~a/~a" m d y)))))
215
216 (defdisplay
217 ((time wall-time-attribute) object)
218 (<:as-html (attribute-value object time)))
219
220
221
222 (defattribute image ()
223 ()
224 (:default-properties
225 :css-class "lol-image"
226 :prefix "images/"))
227
228 (defdisplay ((buttons (eql 'image-editor-buttons)) object)
229 (<ucw:a :action (ok component object)
230 (<:as-html "select this image")))
231
232 (defdisplay ((image image) object)
233 (<:img
234 :class (or (css-class image) "lol-image")
235 :src (arnesi:strcat
236 (or (prefix image) "images/")
237 (escape-as-uri
238 (attribute-value object image)))))
239
240 (defdisplay
241 :in-layer editor ((image image) object)
242
243 (<:div
244 :class "lol-image-thumbnails"
245 (<:as-html "imagie")))
246
247
248
249
250
251