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