Added AJAX support
[clinton/lisp-on-lines.git] / src / mewa / slot-presentations.lisp
CommitLineData
579597e3 1(in-package :it.bese.ucw)
2
19a82bd5 3(defun multiple-value-funcall->list (function &rest args)
4e2ecf69 4 "The function to be called by m-v-bf"
19a82bd5 5 (multiple-value-call #'list (apply function args)))
6
7(defmacro multiple-value-bindf (vars form &body body)
4e2ecf69 8 "Like M-V-B, only it works in actions. form must be a function call"
19a82bd5 9 `(destructuring-bind ,vars
10 (multiple-value-funcall->list #',(car form) ,@(cdr form))
11 ,@body))
579597e3 12
44108fd6
DC
13
14;;;; ** Textarea Slot Presentation
15;;;; This should really be in UCW.
16
17(defslot-presentation text-slot-presentation ()
18 ((rows :initarg :rows :accessor rows :initform nil)
19 (columns :initarg :columns :accessor columns :initform nil)
20 (html-contentp :initarg :escape-html-p :accessor escape-html-p :initform nil))
21 (:type-name text))
22
23(defmethod present-slot ((slot text-slot-presentation) instance)
24 (if (editablep slot)
25 (<ucw:textarea :accessor (presentation-slot-value slot instance)
26 :rows (rows slot)
27 :cols (columns slot))
28 (if (escape-html-p slot)
29 (<:as-is (presentation-slot-value slot instance))
30 (<:as-html (presentation-slot-value slot instance)))))
31
32
e9454185
DC
33(defcomponent mewa-slot-presentation ()
34 ((slot-name :accessor slot-name
35 :initarg :slot-name
36 :documentation
37 "The name of the slot being accessed")
38 (fill-gaps-only-p :accessor fill-gaps-only-p
39 :initarg :fill-gaps-only-p
40 :initform nil
41 :documentation
42 "When nil, the instance is syncronised with the database.
68a53dce
DC
43When T, only the default value for primary keys and the joins are updated.")
44 (show-label-p :accessor show-label-p :initarg :show-label-p :initform t))
e9454185
DC
45 (:documentation "The superclass of all Mewa slot presentations"))
46
47;;;; this has to be in the eval when i would think
48(eval-when (:compile-toplevel :load-toplevel :execute)
49 (defun generate-slot-presentation-definition-for-type (type)
50 (let* ((u-name (intern (format nil "~A-SLOT-PRESENTATION" type)))
51 (sp-name (intern (format nil "MEWA-~A" u-name)))
52 (t-name (intern (format nil "MEWA-~A" type))))
53 `(defslot-presentation ,sp-name (,u-name mewa-slot-presentation)
54 ()
55 (:type-name ,t-name)))))
56
57(defmacro define-base-mewa-presentations (&body types)
58 "Define the mewa-slot-presentations by subclassing the base UCW ones"
59 `(progn ,@(mapcar #'generate-slot-presentation-definition-for-type
60 types)))
61
62;;;then actually define the base presentations :
63(define-base-mewa-presentations
64 boolean
65 string
66 number
67 integer
68 currency)
9d6c69fb 69
f4170204 70(defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
579597e3 71 ()
72 (:type-name clsql-sys:wall-time))
73
74(defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation) instance)
75 (let ((date (call-next-method)))
76 (when date (multiple-value-bind (y m d) (clsql:time-ymd date)
77 (format nil "~a/~a/~a" m d y)))))
78
79(defmethod (setf presentation-slot-value) ((value string) (slot clsql-wall-time-slot-presentation) instance)
f4170204 80 (let ((new-time (clsql:parse-date-time (remove #\Space value)))
81 (old-time (when (slot-boundp instance (slot-name slot))
82 (slot-value instance (slot-name slot)))))
83 (unless (or (eql old-time new-time)
ec4f5786 84 (when (and new-time old-time)
85 (equal :equal (clsql:time-compare new-time old-time))))
86 (setf (presentation-slot-value slot instance) new-time ))))
579597e3 87
19531fbd 88(defmethod label :around ((slot clsql-wall-time-slot-presentation))
89 (concatenate 'string (call-next-method) " (mm/dd/yyyy)"))
90
579597e3 91(defmethod present-slot ((slot clsql-wall-time-slot-presentation) instance)
92 (let ((date (presentation-slot-value slot instance))
93 (input-id (string (gensym))))
94 (if (and date (not (editablep slot)))
95 (<:span (<:as-html date)))
96 (when (editablep slot)
97 (<ucw:input :accessor (presentation-slot-value slot instance) :id input-id)
98 (<:script :type "text/javascript"
99 (<:as-is (format nil "
100 Calendar.setup({
101 inputField : \"~a\",
102 ifFormat : \"%m/%d/%Y\",
103 });" input-id))))))
104
e9454185
DC
105(defslot-presentation mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation)
106 ((foreign-instance :accessor foreign-instance)
569ad9e6
DC
107 (linkedp :accessor linkedp :initarg :linkedp :initform t)
108 (creator :accessor creator :initarg :creator :initform :editor))
579597e3 109 (:type-name relation))
110
7d87c1d2 111(defaction search-records ((slot mewa-relation-slot-presentation) instance)
e9454185
DC
112 (multiple-value-bindf (finstance foreign-slot-name)
113 (meta-model:explode-foreign-key instance (slot-name slot))
114 (let ((new-instance
115 (call-component
5f0b37e7 116 (parent slot)
e9454185
DC
117 (make-instance (or (cadr (mewa:find-attribute finstance :presentation-search))
118 'mewa::mewa-presentation-search)
119 :search-presentation
120 (mewa:make-presentation finstance
121 :type :search-presentation)
122 :list-presentation
123 (mewa:make-presentation finstance
124 :type :listing)))))
125 (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
f1ce8b6e 126 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
e9454185 127
569ad9e6 128(defaction create-record-on-foreign-key ((slot mewa-relation-slot-presentation) instance)
e9454185
DC
129 (multiple-value-bindf (finstance foreign-slot-name)
130 (meta-model:explode-foreign-key instance (slot-name slot))
131 (let ((new-instance
132 (call-component
133 (parent slot)
569ad9e6
DC
134 (mewa:make-presentation finstance :type (creator self)))))
135
136 ;;;; TODO: this next bit is due to a bad design decision.
137 ;;;; Components should always have (ok) return self, but somewhere
138 ;;;; i've made in return (instance self) sometimes, and this
139 ;;;; bahaviour is totatlly fucked.
140
141 (when (typep new-instance 'mewa::mewa)
142 (setf new-instance (instance new-instance)))
143
144 ;;;; sorry about that, and now back t our regular program.
145
146 (meta-model:sync-instance new-instance)
e9454185
DC
147 (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
148 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
569ad9e6 149
e9454185 150
579597e3 151(defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
152 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
19531fbd 153 (let* ((i (foreign-instance slot))
579597e3 154 (pres (mewa::make-presentation
155 i
156 :type :one-line
157 :initargs (list
158 :global-properties
19531fbd 159 (list :editablep nil :linkedp nil)))))
9d6c69fb 160 (when (and (ucw::parent slot) (slot-boundp slot 'ucw::place))
498061f6 161 (setf (component.place pres) (component.place (ucw::parent slot))))
162 (when i (<ucw:render-component :component pres))))
579597e3 163
7d87c1d2 164
165
579597e3 166(defmethod present-slot ((slot mewa-relation-slot-presentation) instance)
167 (present-relation slot instance))
168
169(defslot-presentation foreign-key-slot-presentation (mewa-relation-slot-presentation)
170 ()
171 (:type-name foreign-key)
172 (:default-initargs))
173
174(defaction view-instance ((self component) instance &rest initargs)
12dcf3d4 175 (call-component (parent self) (apply #'mewa:make-presentation instance initargs))
176 ;; the viewed instance could have been changed/deleted, so we sync this instance
177 (meta-model:sync-instance (instance (parent self))))
579597e3 178
498061f6 179
569ad9e6
DC
180
181(defmethod present-slot :before ((slot foreign-key-slot-presentation) instance)
182 ())
183
184
185(defmethod present-slot :around ((slot foreign-key-slot-presentation) instance)
f1ce8b6e
DC
186 (setf (foreign-instance slot)
187 (when (presentation-slot-value slot instance)
188 (meta-model:explode-foreign-key instance (slot-name slot))))
f1ce8b6e
DC
189 (flet ((render () (when (foreign-instance slot)(call-next-method))))
190 (if (slot-boundp slot 'place)
191 (cond
192 ((editablep slot)
193 (render)
194 (<ucw:submit :action (search-records slot instance) :value "Search" :style "display:inline")
569ad9e6 195 (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline"))
f1ce8b6e
DC
196 ((linkedp slot)
197 (<ucw:a :action (view-instance slot (foreign-instance slot))
198 (render)))
199 (t
200 (render)))
201 ;; presentation is used only for rendering
202 (render))))
579597e3 203
d2512426
DC
204
205;;;; * AJAX stuff
206
207;;;; TODO: This search stuff should probably me refactored elsewhere
208
209(defmethod find-slots-of-type (model &key (type 'string)
210 (types '((string)) types-supplied-p))
211 "returns a list of slots matching TYPE, or matching any of TYPES"
212 (let (ty)
213 (if types-supplied-p
214 (setf ty types)
215 (setf ty (list type)))
216 (remove nil (mapcar #'(lambda (st) (when (member (second st) ty)
217 (first st)))
218 (lisp-on-lines::list-slot-types model)))))
219
220(defslot-presentation ajax-foreign-key-slot-presentation (foreign-key-slot-presentation)
221 ((search-slots :accessor search-slots :initarg :search-slots :initform nil)
222 (live-search
223 :accessor live-search
224 :component (lisp-on-lines:auto-complete
225 :values-generator
226 (lambda (value)
227 (when (< 0 (length value))
228 (limited-word-search 'person '(first-name last-name company-name) (list value))))
229
230 :render (lambda (x)
231 (<:as-html (if (> (length (last-name x)) 0)
232 (strcat (last-name x) ", ")
233 " ")
234 (first-name x)" " (company-name x)))
235 :as-value (lambda (x) x)
236 :submit-on-click-p nil)))
237 (:type-name ajax-foreign-key))
238
239
240(defmethod shared-initialize :after ((slot ajax-foreign-key-slot-presentation) slots &rest args)
241 ;; If no search-slots than use the any slots of type string
242 (unless (search-slots slot)
243 (setf (search-slots slot) t)
244 (let ((l (live-search slot)))
245 (setf (lisp-on-lines::values-generator l) t))))
246
247
248(defmethod present-slot :around ((slot ajax-foreign-key-slot-presentation) instance)
249 (setf (foreign-instance slot)
250 (when (presentation-slot-value slot instance)
251 (meta-model:explode-foreign-key instance (slot-name slot))))
252 (flet ((render () (when (foreign-instance slot)(call-next-method))))
253 (if (slot-boundp slot 'place)
254 (cond
255 ((editablep slot)
256
257 (<ucw:submit :action (search-records slot instance) :value "find" :style "display:inline"))
258 ((linkedp slot)
259 (<ucw:a :action (view-instance slot (foreign-instance slot))
260 (render)))
261 (t
262 (render)))
263 ;; presentation is used only for rendering
264 (render))))
265
579597e3 266;;;; HAS MANY
267(defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
12dcf3d4 268 ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
579597e3 269 (:type-name has-many))
270
498061f6 271(defaction add-to-has-many ((slot has-many-slot-presentation) instance)
8e6e6b56
DC
272 ;; if the instance is not stored we must make sure to mark it stored now!
273 (unless (mewa::instance-is-stored-p instance)
274 (setf (mewa::modifiedp (parent self)) t))
275 ;; sync up the instance
85281029 276 (mewa:ensure-instance-sync (parent slot))
8e6e6b56 277
85281029
DC
278 (multiple-value-bindf (class home foreign)
279 (meta-model:explode-has-many instance (slot-name slot))
498061f6 280 (let ((new (make-instance class)))
281 (setf (slot-value new foreign) (slot-value instance home))
e9454185 282 (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
569ad9e6 283 (call-component (parent slot) (mewa:make-presentation new :type (creator slot)))
e9454185 284 (meta-model:sync-instance instance))))
498061f6 285
579597e3 286(defmethod present-slot ((slot has-many-slot-presentation) instance)
f1ce8b6e
DC
287 (when (slot-boundp slot 'place)
288 (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot)))
289 (let ((i (get-foreign-instances slot instance)))
290
498061f6 291 (<:ul
292 (dolist (s i)
293 (let ((s s))
294 (setf (foreign-instance slot) s)
e9454185
DC
295 (when (slot-boundp slot 'place)
296 (<ucw:a :action (view-instance slot s :initargs `(:global-properties ,(list :linkedp t :editablep nil)))
498061f6 297 (<:li (setf (linkedp slot) nil)
e9454185 298 (present-relation slot instance)))))))))
579597e3 299
300
301(defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
302 (slot-value instance (slot-name slot)))
303
ec4f5786 304(defmethod presentation-slot-value ((slot has-many-slot-presentation) instance)
305 (get-foreign-instances slot instance))
306
579597e3 307(defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
308 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
309 (current :accessor current :initform 0)
310 (len :accessor len )
311 (instances :accessor instances))
312
313 (:type-name has-very-many))
314
315(defmethod list-next ((slot has-very-many-slot-presentation))
316 (setf (current slot) (incf (current slot) (number-to-display slot)))
317 (when (< (len slot) (current slot))
318 (setf (current slot) (- (number-to-display slot) (len slot)))))
319
320(defmethod list-prev ((slot has-very-many-slot-presentation))
321 (setf (current slot) (decf (current slot) (number-to-display slot)))
322 (when (> 0 (current slot))
323 ;;what to do here is open to debate
324 (setf (current slot) (- (len slot)(number-to-display slot) ))))
325
326
327(defmethod present-slot ((slot has-very-many-slot-presentation) instance)
328 ;;(<:as-html "isance: " instance)
f1ce8b6e
DC
329 (if (slot-boundp slot 'place)
330 (progn
331 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
332 (let ((self (parent slot)))
333 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
334 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
335 (<ucw:a :action (list-next slot) (<:as-html ">>"))
336 (call-next-method)
337 (<:as-html "total :" (len slot)))
338 (call-next-method)))
579597e3 339
340(defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
341 (let ((f (call-next-method)))
342 (setf (len slot) (length f))
343 (setf (instances slot) f)
344 (loop for cons on (nthcdr (current slot) f)
345 for i from 0 upto (number-to-display slot)
346 collect (car cons))))
347
348(defslot-presentation has-a-slot-presentation (one-of-presentation)
349 ((key :initarg :key :accessor key))
350 (:type-name has-a))
351
352(defmethod get-foreign-slot-value ((slot has-a-slot-presentation) (object t) (slot-name t))
353 (slot-value object slot-name))
354
355(defmethod present-slot ((slot has-a-slot-presentation) instance)
356 (<:as-html (presentation-slot-value slot instance))
357 (if (editablep slot)
358 (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
359 (when (allow-nil-p slot)
360 (<ucw:option :value nil (<:as-html (none-label slot))))
361 (dolist (option (get-foreign-instances (presentation slot) instance))
362 (setf (instance (presentation slot)) option)
363 (<ucw:option :value (get-foreign-slot-value slot option (key slot)) (present (presentation slot)))))
364 (if (presentation-slot-value slot instance)
365 (progn
366 (setf (instance (presentation slot)) (presentation-slot-value slot instance))
367 (present (presentation slot)))
85281029 368 (<:as-html "--"))))
5f0b37e7 369
5f0b37e7 370
5f0b37e7 371
2cb4247d
DC
372
373
374
375