1 (in-package :it.bese.ucw
)
3 (defun multiple-value-funcall->list
(function &rest args
)
4 "The function to be called by m-v-bf"
5 (multiple-value-call #'list
(apply function args
)))
7 (defmacro multiple-value-bindf
(vars form
&body body
)
8 "Like M-V-B, only it works in actions. form must be a function call"
9 `(destructuring-bind ,vars
10 (multiple-value-funcall->list
#',(car form
) ,@(cdr form
))
14 ;;;; ** Textarea Slot Presentation
15 ;;;; This should really be in UCW.
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
))
23 (defmethod present-slot ((slot text-slot-presentation
) instance
)
25 (<ucw
:textarea
:accessor
(presentation-slot-value slot instance
)
28 (if (escape-html-p slot
)
29 (<:as-is
(presentation-slot-value slot instance
))
30 (<:as-html
(presentation-slot-value slot instance
)))))
35 (defcomponent mewa-slot-presentation
()
36 ((slot-name :accessor slot-name
39 "The name of the slot being accessed")
40 (fill-gaps-only-p :accessor fill-gaps-only-p
41 :initarg
:fill-gaps-only-p
44 "When nil, the instance is syncronised with the database.
45 When T, only the default value for primary keys and the joins are updated."))
46 (:documentation
"The superclass of all Mewa slot presentations"))
48 ;;;; this has to be in the eval when i would think
49 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
50 (defun generate-slot-presentation-definition-for-type (type)
51 (let* ((u-name (intern (format nil
"~A-SLOT-PRESENTATION" type
)))
52 (sp-name (intern (format nil
"MEWA-~A" u-name
)))
53 (t-name (intern (format nil
"MEWA-~A" type
))))
54 `(defslot-presentation ,sp-name
(,u-name mewa-slot-presentation
)
56 (:type-name
,t-name
)))))
58 (defmacro define-base-mewa-presentations
(&body types
)
59 "Define the mewa-slot-presentations by subclassing the base UCW ones"
60 `(progn ,@(mapcar #'generate-slot-presentation-definition-for-type
63 ;;;then actually define the base presentations :
64 (define-base-mewa-presentations
71 (defslot-presentation clsql-wall-time-slot-presentation
(mewa-relation-slot-presentation)
73 (:type-name clsql-sys
:wall-time
))
75 (defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation
) instance
)
76 (let ((date (call-next-method)))
77 (when date
(multiple-value-bind (y m d
) (clsql:time-ymd date
)
78 (format nil
"~a/~a/~a" m d y
)))))
80 (defmethod (setf presentation-slot-value
) ((value string
) (slot clsql-wall-time-slot-presentation
) instance
)
81 (let ((new-time (clsql:parse-date-time
(remove #\Space value
)))
82 (old-time (when (slot-boundp instance
(slot-name slot
))
83 (slot-value instance
(slot-name slot
)))))
84 (unless (or (eql old-time new-time
)
85 (when (and new-time old-time
)
86 (equal :equal
(clsql:time-compare new-time old-time
))))
87 (setf (presentation-slot-value slot instance
) new-time
))))
89 (defmethod label :around
((slot clsql-wall-time-slot-presentation
))
90 (concatenate 'string
(call-next-method) " (mm/dd/yyyy)"))
92 (defmethod present-slot ((slot clsql-wall-time-slot-presentation
) instance
)
93 (let ((date (presentation-slot-value slot instance
))
94 (input-id (string (gensym))))
95 (if (and date
(not (editablep slot
)))
96 (<:span
(<:as-html date
)))
97 (when (editablep slot
)
98 (<ucw
:input
:accessor
(presentation-slot-value slot instance
) :id input-id
)
99 (<:script
:type
"text/javascript"
100 (<:as-is
(format nil
"
103 ifFormat : \"%m/%d/%Y\",
106 (defslot-presentation mewa-relation-slot-presentation
(mewa-slot-presentation slot-presentation
)
107 ((foreign-instance :accessor foreign-instance
)
108 (linkedp :accessor linkedp
:initarg
:linkedp
:initform t
)
109 (creator :accessor creator
:initarg
:creator
:initform
:editor
))
110 (:type-name relation
))
112 (defaction search-records
((slot mewa-relation-slot-presentation
) instance
)
113 (multiple-value-bindf (finstance foreign-slot-name
)
114 (meta-model:explode-foreign-key instance
(slot-name slot
))
118 (make-instance (or (cadr (mewa:find-attribute finstance
:presentation-search
))
119 'mewa
::mewa-presentation-search
)
121 (mewa:make-presentation finstance
122 :type
:search-presentation
)
124 (mewa:make-presentation finstance
126 (setf (slot-value instance
(slot-name slot
)) (slot-value new-instance foreign-slot-name
))
127 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p self
)))))
129 (defaction create-record-on-foreign-key
((slot mewa-relation-slot-presentation
) instance
)
130 (multiple-value-bindf (finstance foreign-slot-name
)
131 (meta-model:explode-foreign-key instance
(slot-name slot
))
135 (mewa:make-presentation finstance
:type
(creator self
)))))
137 ;;;; TODO: this next bit is due to a bad design decision.
138 ;;;; Components should always have (ok) return self, but somewhere
139 ;;;; i've made in return (instance self) sometimes, and this
140 ;;;; bahaviour is totatlly fucked.
142 (when (typep new-instance
'mewa
::mewa
)
143 (setf new-instance
(instance new-instance
)))
145 ;;;; sorry about that, and now back t our regular program.
147 (meta-model:sync-instance new-instance
)
148 (setf (slot-value instance
(slot-name slot
)) (slot-value new-instance foreign-slot-name
))
149 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p self
)))))
152 (defmethod present-relation ((slot mewa-relation-slot-presentation
) instance
)
153 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
154 (let* ((i (foreign-instance slot
))
155 (pres (mewa::make-presentation
160 (list :editablep nil
:linkedp nil
)))))
161 (when (and (ucw::parent slot
) (slot-boundp slot
'ucw
::place
))
162 (setf (component.place pres
) (component.place
(ucw::parent slot
))))
163 (when i
(<ucw
:render-component
:component pres
))))
167 (defmethod present-slot ((slot mewa-relation-slot-presentation
) instance
)
168 (present-relation slot instance
))
170 (defslot-presentation foreign-key-slot-presentation
(mewa-relation-slot-presentation)
172 (:type-name foreign-key
)
175 (defaction view-instance
((self component
) instance
&rest initargs
)
176 (call-component (parent self
) (apply #'mewa
:make-presentation instance initargs
))
177 ;; the viewed instance could have been changed/deleted, so we sync this instance
178 (meta-model:sync-instance
(instance (parent self
))))
182 (defmethod present-slot :before
((slot foreign-key-slot-presentation
) instance
)
186 (defmethod present-slot :around
((slot foreign-key-slot-presentation
) instance
)
187 (setf (foreign-instance slot
)
188 (when (presentation-slot-value slot instance
)
189 (meta-model:explode-foreign-key instance
(slot-name slot
))))
190 (flet ((render () (when (foreign-instance slot
)(call-next-method))))
191 (if (slot-boundp slot
'place
)
195 (<ucw
:submit
:action
(search-records slot instance
) :value
"Search" :style
"display:inline")
196 (<ucw
:submit
:action
(create-record-on-foreign-key slot instance
) :value
"Add New" :style
"display:inline"))
198 (<ucw
:a
:action
(view-instance slot
(foreign-instance slot
))
202 ;; presentation is used only for rendering
206 (defslot-presentation has-many-slot-presentation
(mewa-relation-slot-presentation)
207 ((add-new-label :accessor add-new-label
:initarg
:add-new-label
:initform
"Add New"))
208 (:type-name has-many
))
210 (defaction add-to-has-many
((slot has-many-slot-presentation
) instance
)
211 ;; if the instance is not stored we must make sure to mark it stored now!
212 (unless (mewa::instance-is-stored-p instance
)
213 (setf (mewa::modifiedp
(parent self
)) t
))
214 ;; sync up the instance
215 (mewa:ensure-instance-sync
(parent slot
))
217 (multiple-value-bindf (class home foreign
)
218 (meta-model:explode-has-many instance
(slot-name slot
))
219 (let ((new (make-instance class
)))
220 (setf (slot-value new foreign
) (slot-value instance home
))
221 (meta-model:sync-instance new
:fill-gaps-only-p
(fill-gaps-only-p self
))
222 (call-component (parent slot
) (mewa:make-presentation new
:type
(creator slot
)))
223 (meta-model:sync-instance instance
))))
225 (defmethod present-slot ((slot has-many-slot-presentation
) instance
)
226 (when (slot-boundp slot
'place
)
227 (<ucw
:submit
:action
(add-to-has-many slot instance
) :value
(add-new-label slot
)))
228 (let ((i (get-foreign-instances slot instance
)))
233 (setf (foreign-instance slot
) s
)
234 (when (slot-boundp slot
'place
)
235 (<ucw
:a
:action
(view-instance slot s
:initargs
`(:global-properties
,(list :linkedp t
:editablep nil
)))
236 (<:li
(setf (linkedp slot
) nil
)
237 (present-relation slot instance
)))))))))
240 (defmethod get-foreign-instances ((slot has-many-slot-presentation
) instance
)
241 (slot-value instance
(slot-name slot
)))
243 (defmethod presentation-slot-value ((slot has-many-slot-presentation
) instance
)
244 (get-foreign-instances slot instance
))
246 (defslot-presentation has-very-many-slot-presentation
(has-many-slot-presentation)
247 ((number-to-display :accessor number-to-display
:initarg
:number-to-display
:initform
10)
248 (current :accessor current
:initform
0)
250 (instances :accessor instances
))
252 (:type-name has-very-many
))
254 (defmethod list-next ((slot has-very-many-slot-presentation
))
255 (setf (current slot
) (incf (current slot
) (number-to-display slot
)))
256 (when (< (len slot
) (current slot
))
257 (setf (current slot
) (- (number-to-display slot
) (len slot
)))))
259 (defmethod list-prev ((slot has-very-many-slot-presentation
))
260 (setf (current slot
) (decf (current slot
) (number-to-display slot
)))
261 (when (> 0 (current slot
))
262 ;;what to do here is open to debate
263 (setf (current slot
) (- (len slot
)(number-to-display slot
) ))))
266 (defmethod present-slot ((slot has-very-many-slot-presentation
) instance
)
267 ;;(<:as-html "isance: " instance)
268 (if (slot-boundp slot
'place
)
270 (<ucw
:a
:action
(list-prev slot
) (<:as-html
"<<"))
271 (let ((self (parent slot
)))
272 (<ucw
:a
:action
(call-component self
(mewa:make-presentation
(car (slot-value instance
(slot-name slot
))) :type
:listing
:initargs
(list :instances
(instances slot
))))
273 (<:as-html
(label slot
) (format nil
" ~a-~a " (current slot
) (+ (current slot
) (number-to-display slot
))))))
274 (<ucw
:a
:action
(list-next slot
) (<:as-html
">>"))
276 (<:as-html
"total :" (len slot
)))
279 (defmethod get-foreign-instances :around
((slot has-very-many-slot-presentation
) instance
)
280 (let ((f (call-next-method)))
281 (setf (len slot
) (length f
))
282 (setf (instances slot
) f
)
283 (loop for cons on
(nthcdr (current slot
) f
)
284 for i from
0 upto
(number-to-display slot
)
285 collect
(car cons
))))
287 (defslot-presentation has-a-slot-presentation
(one-of-presentation)
288 ((key :initarg
:key
:accessor key
))
291 (defmethod get-foreign-slot-value ((slot has-a-slot-presentation
) (object t
) (slot-name t
))
292 (slot-value object slot-name
))
294 (defmethod present-slot ((slot has-a-slot-presentation
) instance
)
295 (<:as-html
(presentation-slot-value slot instance
))
297 (<ucw
:select
:accessor
(presentation-slot-value slot instance
) :test
#'equalp
298 (when (allow-nil-p slot
)
299 (<ucw
:option
:value nil
(<:as-html
(none-label slot
))))
300 (dolist (option (get-foreign-instances (presentation slot
) instance
))
301 (setf (instance (presentation slot
)) option
)
302 (<ucw
:option
:value
(get-foreign-slot-value slot option
(key slot
)) (present (presentation slot
)))))
303 (if (presentation-slot-value slot instance
)
305 (setf (instance (presentation slot
)) (presentation-slot-value slot instance
))
306 (present (presentation slot
)))