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