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