re-organized a little, and added support for calling make-presentation with a class...
[clinton/lisp-on-lines.git] / src / mewa / slot-presentations.lisp
1 (in-package :it.bese.ucw)
2
3
4 (defslot-presentation clsql-wall-time-slot-presentation ()
5 ()
6 (:type-name clsql-sys:wall-time))
7
8 (defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation) instance)
9 (let ((date (call-next-method)))
10 (when date (multiple-value-bind (y m d) (clsql:time-ymd date)
11 (format nil "~a/~a/~a" m d y)))))
12
13 (defmethod (setf presentation-slot-value) ((value string) (slot clsql-wall-time-slot-presentation) instance)
14 (setf (presentation-slot-value slot instance) (clsql:parse-date-time (remove #\Space value))))
15
16 (defmethod label :around ((slot clsql-wall-time-slot-presentation))
17 (concatenate 'string (call-next-method) " (mm/dd/yyyy)"))
18
19 (defmethod present-slot ((slot clsql-wall-time-slot-presentation) instance)
20 (let ((date (presentation-slot-value slot instance))
21 (input-id (string (gensym))))
22 (if (and date (not (editablep slot)))
23 (<:span (<:as-html date)))
24 (when (editablep slot)
25 (<ucw:input :accessor (presentation-slot-value slot instance) :id input-id)
26 (<:script :type "text/javascript"
27 (<:as-is (format nil "
28 Calendar.setup({
29 inputField : \"~a\",
30 ifFormat : \"%m/%d/%Y\",
31 });" input-id))))))
32
33 (defslot-presentation mewa-relation-slot-presentation ()
34 ((slot-name :accessor slot-name :initarg :slot-name)
35 (foreign-instance :accessor foreign-instance)
36 (linkedp :accessor linkedp :initarg :linkedp :initform t))
37 (:type-name relation))
38
39 (defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
40 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
41 (let* ((i (foreign-instance slot))
42 (pres (mewa::make-presentation
43 i
44 :type :one-line
45 :initargs (list
46 :global-properties
47 (list :editablep nil :linkedp nil)))))
48 (when (ucw::parent slot) (setf (component.place pres) (component.place (ucw::parent slot))))
49 (flet ((render () (when i (<ucw:render-component :component pres))))
50 (cond
51 ((editablep slot)
52 (render)
53 (<ucw:a :action (search-records slot i) (<:as-html " (search)"))
54 (<ucw:a :action (create-record slot i) (<:as-html " (new)")))
55 ((linkedp slot)
56 (<ucw:a :action (view-instance slot i)
57 (render)))
58 (t
59 (render))))))
60
61 (defmethod present-slot ((slot mewa-relation-slot-presentation) instance)
62 (present-relation slot instance))
63
64 (defslot-presentation foreign-key-slot-presentation (mewa-relation-slot-presentation)
65 ()
66 (:type-name foreign-key)
67 (:default-initargs))
68
69 (defaction view-instance ((self component) instance &rest initargs)
70 (call-component (parent self) (apply #'mewa:make-presentation instance initargs)))
71
72 (defmethod present-slot :before ((slot foreign-key-slot-presentation) instance)
73 (setf (foreign-instance slot) (meta-model:explode-foreign-key instance (slot-name slot))))
74
75 ;;;; HAS MANY
76 (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
77 ()
78 (:type-name has-many))
79
80 (defmethod present-slot ((slot has-many-slot-presentation) instance)
81 (let ((i (get-foreign-instances slot instance))
82 (linkedp (linkedp slot)))
83 (<:ul
84 (dolist (s i)
85 (let ((s s))
86 (setf (foreign-instance slot) s)
87 (<ucw:a :action (view-instance slot s :initargs `(:global-properties ,(list :linkedp t :editablep nil)))
88 (<:li (setf (linkedp slot) nil)
89 (present-relation slot instance))))))))
90
91
92 (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
93 (slot-value instance (slot-name slot)))
94
95 (defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
96 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
97 (current :accessor current :initform 0)
98 (len :accessor len )
99 (instances :accessor instances))
100
101 (:type-name has-very-many))
102
103 (defmethod list-next ((slot has-very-many-slot-presentation))
104 (setf (current slot) (incf (current slot) (number-to-display slot)))
105 (when (< (len slot) (current slot))
106 (setf (current slot) (- (number-to-display slot) (len slot)))))
107
108 (defmethod list-prev ((slot has-very-many-slot-presentation))
109 (setf (current slot) (decf (current slot) (number-to-display slot)))
110 (when (> 0 (current slot))
111 ;;what to do here is open to debate
112 (setf (current slot) (- (len slot)(number-to-display slot) ))))
113
114
115 (defmethod present-slot ((slot has-very-many-slot-presentation) instance)
116 ;;(<:as-html "isance: " instance)
117 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
118 (let ((self (parent slot)))
119 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
120 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
121 (<ucw:a :action (list-next slot) (<:as-html ">>"))
122 (call-next-method)
123 (<:as-html "total :" (len slot)))
124
125 (defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
126 (let ((f (call-next-method)))
127 (setf (len slot) (length f))
128 (setf (instances slot) f)
129 (loop for cons on (nthcdr (current slot) f)
130 for i from 0 upto (number-to-display slot)
131 collect (car cons))))
132
133 (defslot-presentation has-a-slot-presentation (one-of-presentation)
134 ((key :initarg :key :accessor key))
135 (:type-name has-a))
136
137 (defmethod get-foreign-slot-value ((slot has-a-slot-presentation) (object t) (slot-name t))
138 (slot-value object slot-name))
139
140 (defmethod present-slot ((slot has-a-slot-presentation) instance)
141 (<:as-html (presentation-slot-value slot instance))
142 (if (editablep slot)
143 (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
144 (when (allow-nil-p slot)
145 (<ucw:option :value nil (<:as-html (none-label slot))))
146 (dolist (option (get-foreign-instances (presentation slot) instance))
147 (setf (instance (presentation slot)) option)
148 (<ucw:option :value (get-foreign-slot-value slot option (key slot)) (present (presentation slot)))))
149 (if (presentation-slot-value slot instance)
150 (progn
151 (setf (instance (presentation slot)) (presentation-slot-value slot instance))
152 (present (presentation slot)))
153 (<:as-html "--"))))