lots of great changes to update along with maxwell 0.8
[clinton/lisp-on-lines.git] / src / mewa / presentations.lisp
... / ...
CommitLineData
1(in-package :mewa)
2
3(defaction edit-instance ((self mewa))
4 (call-presentation (instance self) :type :editor))
5
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
15(defcomponent mewa-object-presentation (mewa ucw:object-presentation) ())
16
17(defcomponent two-column-presentation (mewa-object-presentation) ())
18
19(defmethod present ((pres two-column-presentation))
20
21 (<:table :class (css-class pres)
22 (loop for slot on (slots pres) by #'cddr
23 do
24 (<:tr :class "presentation-slot-row"
25 (<:td :class "presentation-slot-label"
26 (<:as-html (label (first slot))))
27 (<:td :class "presentation-slot-value"
28 (present-slot (first slot) (instance pres)))
29 (when (second slot)
30 (<:td :class "presentation-slot-label"
31 (<:as-html (label (second slot))))
32 (<:td :class "presentation-slot-value"
33 (present-slot (second slot) (instance pres))))))
34 (render-options pres (instance pres))))
35
36
37;;;lists
38(defcomponent mewa-list-presentation (mewa ucw:list-presentation)
39 ((ucw::instances :accessor instances :initarg :instances :initform nil)
40 (instance :accessor instance)
41 (select-label :accessor select-label :initform "select" :initarg :select-label)
42 (selectablep :accessor selectablep :initform t :initarg :selectablep)
43 (ucw::deleteablep :accessor deletablep :initarg :deletablep :initform nil)
44 (viewablep :accessor viewablep :initarg :viewablep :initform nil)))
45
46(defaction select-from-listing ((listing mewa-list-presentation) object index)
47 (answer object))
48
49(defmethod render-list-row ((listing mewa-list-presentation) object index)
50 (<:tr :class "item-row"
51 (<:td :align "center" :valign "top"
52 (when (ucw::editablep listing)
53 (let ((object object))
54 (<ucw:input :type "submit"
55 :action (edit-from-listing listing object index)
56 :value (ucw::edit-label listing))))
57 (<:as-is " ")
58 (when (ucw::deleteablep listing)
59 (let ((index index))
60 (<ucw:input :type "submit"
61 :action (delete-from-listing listing object index)
62 :value (ucw::delete-label listing))))
63 (when (selectablep listing)
64 (let ((index index))
65 (<ucw:input :type "submit"
66 :action (select-from-listing listing object index)
67 :value (select-label listing))))
68 (when (viewablep listing)
69 (let ((index index))
70 (<ucw:input :type "submit"
71 :action (call-component listing (make-presentation object))
72 :value "view"))))
73 (dolist (slot (slots listing))
74 (<:td :class "data-cell" (present-slot slot object)))
75 (<:td :class "index-number-cell")
76 ))
77
78(defmethod get-all-instances ((self mewa-list-presentation))
79 (instances self))
80
81
82;;; searching
83(defgeneric search-expr (criteria instance)
84 (:documentation "Return ready to apply criteria.
85 What to do with it is backend dependent."))
86
87(defmacro def-search-expr (((self criteria-type)) (model-expr &body body))
88 `(defmethod search-expr ((,self ,criteria-type) instance)
89 (,model-expr
90 instance
91 (ucw::slot-name (ucw::presentation ,self))
92 ,@body)))
93
94(defmethod search-expr ((self ucw::negated-criteria) instance)
95 (when (ucw::criteria self)
96 (meta-model:expr-not
97 instance
98 (search-expr (ucw::criteria self) instance))))
99
100(def-search-expr ((self ucw::string-starts-with))
101 (meta-model:expr-starts-with (ucw::search-text self)))
102
103(def-search-expr ((self ucw::string-ends-with))
104 (meta-model:expr-ends-with (ucw::search-text self)))
105
106(def-search-expr ((self ucw::string-contains))
107 (meta-model:expr-contains (ucw::search-text self)))
108
109(def-search-expr ((self ucw::number-less-than))
110 (meta-model:expr-< (ucw::number-input self)))
111
112(def-search-expr ((self ucw::number-greater-than))
113 (meta-model:expr-> (ucw::number-input self)))
114
115(def-search-expr ((self ucw::number-equal-to))
116 (meta-model:expr-= (ucw::number-input self)))
117
118(defcomponent mewa-presentation-search (ucw::presentation-search)
119 ((display-results-p :accessor display-results-p :initarg :display-results-p :initform nil)))
120
121(defmethod instance ((self mewa:mewa-presentation-search))
122 (instance (ucw::search-presentation self)))
123
124(defmethod search-expr ((self mewa:mewa-presentation-search) instance)
125 (apply #'meta-model:expr-and instance
126 (mapcan (lambda (c) (let ((e (search-expr c instance)))
127 (if (listp e) e (list e))))
128 (ucw::criteria self))))
129
130(defmethod search-query ((self mewa:mewa-presentation-search))
131 (search-expr self (instance self)))
132
133(defmethod valid-instances ((self mewa:mewa-presentation-search))
134 (meta-model:select-instances (instance self) (search-query self)))
135
136(defmethod get-all-instances ((self mewa-presentation-search))
137 (meta-model:select-instances (instance self)))
138
139(defmethod ok ((self mewa-presentation-search) &optional arg)
140 (declare (ignore arg))
141 (setf (ucw::instances (ucw::list-presentation self)) (valid-instances self))
142 (setf (display-results-p self) t))
143
144
145(defmethod render-on ((res response) (self mewa-presentation-search))
146 ;(<:as-html (search-query self))
147 (ucw::render-criteria res self)
148 (<ucw:input :type "submit" :value "Search" :action (ok self))
149 (when (display-results-p self)
150 (let ((listing (ucw::list-presentation self)))
151 (setf
152 (slot-value listing 'ucw::calling-component) (slot-value self 'ucw::calling-component)
153 (slot-value listing 'ucw::place) (slot-value self 'ucw::place)
154 (slot-value listing 'ucw::continuation) (slot-value self 'ucw::continuation))
155
156 (render-on res listing))))
157
158;;;;
159(defcomponent dont-show-unset-slots ()())
160
161(defmethod slots :around ((self dont-show-unset-slots))
162 (remove-if-not #'(lambda (s) (let ((s (presentation-slot-value s (instance self))))
163 (and s (not (equal "" s)))))
164 (call-next-method)))