made the *-view macros actually work as intended
[clinton/lisp-on-lines.git] / src / presentations.lisp
CommitLineData
687d5f1c 1(in-package :mewa)
2
d5e996b3
DC
3
4
5 (defun split-list (n list)
6 (loop for cons on list
7 by #'(lambda (x) (nthcdr n x))
8 if (< 0 n)
9 collect (loop for atom in cons
10 repeat n
11 collect atom)
12 else return nil))
13
7f16078d 14(defaction edit-instance ((self mewa))
15 (call-presentation (instance self) :type :editor))
16
687d5f1c 17;;;one-line objects
18(defcomponent mewa-one-line-presentation (mewa one-line-presentation)
19 ()
d5e996b3
DC
20 (:default-initargs
21 :attributes-getter #'one-line-attributes-getter
22 :global-properties '(:editablep nil)))
687d5f1c 23
24(defmethod one-line-attributes-getter ((self mewa))
d5e996b3
DC
25 (or (meta-model::find-slots-of-type (instance self))
26 (meta-model::list-keys (instance self))))
687d5f1c 27
28;;;objects
38a016c7
DC
29(defcomponent mewa-object-presentation (mewa object-presentation)
30 ((instance :accessor instance :initarg :instance :initform nil)))
687d5f1c 31
d5e996b3
DC
32(defcomponent mewa-viewer (mewa-object-presentation)
33 ()
34 (:default-initargs
35 :global-properties '(:editablep nil)))
36
37(defcomponent mewa-editor (mewa-object-presentation)
38 ()
39 (:default-initargs
40 :global-properties '(:editablep t)))
41
42(defcomponent mewa-creator (mewa-editor)
43 ())
44
5f0b37e7
DC
45(defmethod present ((pres mewa-object-presentation))
46 (<:table :class (css-class pres)
47 (dolist (slot (slots pres))
48 (<:tr :class "presentation-slot-row"
49 (present-slot-as-row pres slot))))
50 (render-options pres (instance pres)))
51
38a016c7 52(defmethod present-slot-as-row ((pres mewa-object-presentation) (slot slot-presentation))
5f0b37e7
DC
53 (<:td :class "presentation-slot-label" (<:as-html (label slot)))
54 (<:td :class "presentation-slot-value" (present-slot slot (instance pres))))
55
56
6460c439 57(defcomponent two-column-presentation (mewa-object-presentation) ())
58
59(defmethod present ((pres two-column-presentation))
60
61 (<:table :class (css-class pres)
62 (loop for slot on (slots pres) by #'cddr
63 do
64 (<:tr :class "presentation-slot-row"
65 (<:td :class "presentation-slot-label"
66 (<:as-html (label (first slot))))
67 (<:td :class "presentation-slot-value"
68 (present-slot (first slot) (instance pres)))
69 (when (second slot)
70 (<:td :class "presentation-slot-label"
71 (<:as-html (label (second slot))))
72 (<:td :class "presentation-slot-value"
73 (present-slot (second slot) (instance pres))))))
74 (render-options pres (instance pres))))
75
76
687d5f1c 77;;;lists
38a016c7
DC
78(defcomponent mewa-list-presentation (mewa list-presentation)
79 ((instances :accessor instances :initarg :instances :initform nil)
687d5f1c 80 (instance :accessor instance)
81 (select-label :accessor select-label :initform "select" :initarg :select-label)
8e6e6b56 82 (selectablep :accessor selectablep :initform t :initarg :selectablep)
38a016c7 83 (deleteablep :accessor deletablep :initarg :deletablep :initform nil)
8e6e6b56 84 (viewablep :accessor viewablep :initarg :viewablep :initform nil)))
687d5f1c 85
86(defaction select-from-listing ((listing mewa-list-presentation) object index)
87 (answer object))
88
89(defmethod render-list-row ((listing mewa-list-presentation) object index)
90 (<:tr :class "item-row"
91 (<:td :align "center" :valign "top"
38a016c7 92 (when (editablep listing)
687d5f1c 93 (let ((object object))
94 (<ucw:input :type "submit"
95 :action (edit-from-listing listing object index)
38a016c7 96 :value (edit-label listing))))
687d5f1c 97 (<:as-is " ")
38a016c7 98 (when (deleteablep listing)
687d5f1c 99 (let ((index index))
100 (<ucw:input :type "submit"
101 :action (delete-from-listing listing object index)
38a016c7 102 :value (delete-label listing))))
687d5f1c 103 (when (selectablep listing)
104 (let ((index index))
105 (<ucw:input :type "submit"
106 :action (select-from-listing listing object index)
8e6e6b56
DC
107 :value (select-label listing))))
108 (when (viewablep listing)
109 (let ((index index))
110 (<ucw:input :type "submit"
111 :action (call-component listing (make-presentation object))
112 :value "view"))))
687d5f1c 113 (dolist (slot (slots listing))
114 (<:td :class "data-cell" (present-slot slot object)))
6fc8733b 115 (<:td :class "index-number-cell")
687d5f1c 116 ))
117
118(defmethod get-all-instances ((self mewa-list-presentation))
119 (instances self))
aef3b247 120
121
5f0b37e7
DC
122;;;; * Presentation Searches
123
124
125;;;; ** "search all fields" criteria
126
9d6c69fb
DC
127(defgeneric search-expr (criteria instance)
128 (:documentation "Return ready to apply criteria.
129 What to do with it is backend dependent."))
aef3b247 130
9d6c69fb
DC
131(defmacro def-search-expr (((self criteria-type)) (model-expr &body body))
132 `(defmethod search-expr ((,self ,criteria-type) instance)
133 (,model-expr
134 instance
38a016c7 135 (slot-name (presentation ,self))
9d6c69fb
DC
136 ,@body)))
137
38a016c7
DC
138(defmethod search-expr ((self negated-criteria) instance)
139 (when (criteria self)
9d6c69fb
DC
140 (meta-model:expr-not
141 instance
38a016c7 142 (search-expr (criteria self) instance))))
9d6c69fb 143
38a016c7
DC
144(def-search-expr ((self string-starts-with))
145 (meta-model:expr-starts-with (search-text self)))
9d6c69fb 146
38a016c7
DC
147(def-search-expr ((self string-ends-with))
148 (meta-model:expr-ends-with (search-text self)))
9d6c69fb 149
38a016c7
DC
150(def-search-expr ((self string-contains))
151 (meta-model:expr-contains (search-text self)))
9d6c69fb 152
38a016c7
DC
153(def-search-expr ((self number-less-than))
154 (meta-model:expr-< (number-input self)))
9d6c69fb 155
38a016c7
DC
156(def-search-expr ((self number-greater-than))
157 (meta-model:expr-> (number-input self)))
9d6c69fb 158
38a016c7
DC
159(def-search-expr ((self number-equal-to))
160 (meta-model:expr-= (number-input self)))
9d6c69fb 161
5f0b37e7
DC
162
163
38a016c7 164(defcomponent mewa-presentation-search (presentation-search)
5f0b37e7
DC
165 ((display-results-p :accessor display-results-p :initarg :display-results-p :initform nil)
166 (criteria-input :accessor criteria-input :initform "")
167 (new-criteria :accessor new-criteria :initform nil)))
9d6c69fb
DC
168
169(defmethod instance ((self mewa:mewa-presentation-search))
38a016c7 170 (instance (search-presentation self)))
9d6c69fb
DC
171
172(defmethod search-expr ((self mewa:mewa-presentation-search) instance)
173 (apply #'meta-model:expr-and instance
5f0b37e7 174 (mapcan (lambda (c) (let ((e (search-expr c instance)))
9d6c69fb 175 (if (listp e) e (list e))))
38a016c7 176 (criteria self))))
9d6c69fb 177
9d6c69fb
DC
178(defmethod search-query ((self mewa:mewa-presentation-search))
179 (search-expr self (instance self)))
180
181(defmethod valid-instances ((self mewa:mewa-presentation-search))
182 (meta-model:select-instances (instance self) (search-query self)))
183
184(defmethod get-all-instances ((self mewa-presentation-search))
185 (meta-model:select-instances (instance self)))
aef3b247 186
187(defmethod ok ((self mewa-presentation-search) &optional arg)
188 (declare (ignore arg))
38a016c7 189 (setf (instances (list-presentation self)) (valid-instances self))
aef3b247 190 (setf (display-results-p self) t))
191
4e7631c9 192
38a016c7 193(defmethod set-search-input-for-criteria ((criteria criteria) (input t))
5f0b37e7
DC
194 (error "No search-input-for-criteria method for ~A : ~A" criteria input))
195
38a016c7
DC
196(defmethod set-search-input-for-criteria ((c string-criteria) input)
197 (setf (search-text c) input))
5f0b37e7 198
38a016c7 199(defmethod set-search-input-for-criteria ((c negated-criteria) i)
5f0b37e7
DC
200 nil)
201
202
38a016c7 203(defmethod mewa-add-criteria ((self component) (criteria criteria))
5f0b37e7 204 (set-search-input-for-criteria criteria (criteria-input self))
38a016c7 205 (add-criteria self criteria))
5f0b37e7 206
38a016c7
DC
207(defmethod find-default-criteria (c mewa-string-slot-presentation)
208 'string-contains)
5f0b37e7
DC
209
210
211
212(defmethod render-criteria ((res response) (s mewa-presentation-search))
213 (setf (criteria-input s) "")
214 (<:ul
38a016c7 215 (dolist (c (criteria s))
5f0b37e7
DC
216 (<:li (render-on res c)
217 (let ((c c))
38a016c7 218 (<ucw:input :action (drop-criteria s c) :type "submit" :value "eliminate"))))
5f0b37e7
DC
219 (<:li
220 "Search For: "
221 (<ucw:input :type "text" :accessor (criteria-input s))
222 " Using : "
223 (<ucw:select :accessor (new-criteria s)
38a016c7 224 (dolist (criteria (applicable-criteria s))
5f0b37e7
DC
225 (<ucw:option :value criteria (<:as-html (label criteria)))))
226 (<ucw:input :type "submit" :action (mewa-add-criteria s (new-criteria s))
227 :value "add"))))
228
229(defmethod submit-search ((s mewa-presentation-search))
230 (with-slots (criteria-input) s
231
232 (unless (or (null criteria-input)
233 (string-equal "" (remove #\Space criteria-input)))
234
235 (mewa-add-criteria s (new-criteria s)))
236
237 (ok s)))
238
aef3b247 239(defmethod render-on ((res response) (self mewa-presentation-search))
7c3aade7 240 ;(<:as-html (search-query self))
5f0b37e7
DC
241 (render-criteria res self)
242 (<ucw:input :type "submit" :value "Search" :action (submit-search self))
aef3b247 243 (when (display-results-p self)
38a016c7 244 (let ((listing (list-presentation self)))
9d6c69fb
DC
245 (setf
246 (slot-value listing 'ucw::calling-component) (slot-value self 'ucw::calling-component)
247 (slot-value listing 'ucw::place) (slot-value self 'ucw::place)
248 (slot-value listing 'ucw::continuation) (slot-value self 'ucw::continuation))
249
bbf64329 250 (render-on res listing))))
251
5f0b37e7
DC
252
253;;;;
bbf64329 254(defcomponent dont-show-unset-slots ()())
255
256(defmethod slots :around ((self dont-show-unset-slots))
257 (remove-if-not #'(lambda (s) (let ((s (presentation-slot-value s (instance self))))
258 (and s (not (equal "" s)))))
259 (call-next-method)))