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