230ead5cae3cad7628e7756b0f6fb0fe59b73d9d
[clinton/lisp-on-lines.git] / src / mewa / presentations.lisp
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
44 (defaction select-from-listing ((listing mewa-list-presentation) object index)
45 (answer object))
46
47 (defmethod render-list-row ((listing mewa-list-presentation) object index)
48 (<:tr :class "item-row"
49 (<:td :align "center" :valign "top"
50 (when (ucw::editablep listing)
51 (let ((object object))
52 (<ucw:input :type "submit"
53 :action (edit-from-listing listing object index)
54 :value (ucw::edit-label listing))))
55 (<:as-is " ")
56 (when (ucw::deleteablep listing)
57 (let ((index index))
58 (<ucw:input :type "submit"
59 :action (delete-from-listing listing object index)
60 :value (ucw::delete-label listing))))
61 (when (selectablep listing)
62 (let ((index index))
63 (<ucw:input :type "submit"
64 :action (select-from-listing listing object index)
65 :value (select-label listing)))))
66 (dolist (slot (slots listing))
67 (<:td :class "data-cell" (present-slot slot object)))
68 (<:td :class "index-number-cell")
69 ))
70
71 (defmethod get-all-instances ((self mewa-list-presentation))
72 (instances self))
73
74
75 ;;; searching
76 (defgeneric search-expr (criteria instance)
77 (:documentation "Return ready to apply criteria.
78 What to do with it is backend dependent."))
79
80 (defmacro def-search-expr (((self criteria-type)) (model-expr &body body))
81 `(defmethod search-expr ((,self ,criteria-type) instance)
82 (,model-expr
83 instance
84 (ucw::slot-name (ucw::presentation ,self))
85 ,@body)))
86
87 (defmethod search-expr ((self ucw::negated-criteria) instance)
88 (when (ucw::criteria self)
89 (meta-model:expr-not
90 instance
91 (search-expr (ucw::criteria self) instance))))
92
93 (def-search-expr ((self ucw::string-starts-with))
94 (meta-model:expr-starts-with (ucw::search-text self)))
95
96 (def-search-expr ((self ucw::string-ends-with))
97 (meta-model:expr-ends-with (ucw::search-text self)))
98
99 (def-search-expr ((self ucw::string-contains))
100 (meta-model:expr-contains (ucw::search-text self)))
101
102 (def-search-expr ((self ucw::number-less-than))
103 (meta-model:expr-< (ucw::number-input self)))
104
105 (def-search-expr ((self ucw::number-greater-than))
106 (meta-model:expr-> (ucw::number-input self)))
107
108 (def-search-expr ((self ucw::number-equal-to))
109 (meta-model:expr-= (ucw::number-input self)))
110
111 (defcomponent mewa-presentation-search (ucw::presentation-search)
112 ((display-results-p :accessor display-results-p :initarg :display-results-p :initform nil)))
113
114 (defmethod instance ((self mewa:mewa-presentation-search))
115 (instance (ucw::search-presentation self)))
116
117 (defmethod search-expr ((self mewa:mewa-presentation-search) instance)
118 (apply #'meta-model:expr-and instance
119 (mapcan (lambda (c) (let ((e (search-expr c instance)))
120 (if (listp e) e (list e))))
121 (ucw::criteria self))))
122
123 (defmethod search-query ((self mewa:mewa-presentation-search))
124 (search-expr self (instance self)))
125
126 (defmethod valid-instances ((self mewa:mewa-presentation-search))
127 (meta-model:select-instances (instance self) (search-query self)))
128
129 (defmethod get-all-instances ((self mewa-presentation-search))
130 (meta-model:select-instances (instance self)))
131
132 (defmethod ok ((self mewa-presentation-search) &optional arg)
133 (declare (ignore arg))
134 (setf (ucw::instances (ucw::list-presentation self)) (valid-instances self))
135 (setf (display-results-p self) t))
136
137
138 (defmethod render-on ((res response) (self mewa-presentation-search))
139 ;(<:as-html (search-query self))
140 (ucw::render-criteria res self)
141 (<ucw:input :type "submit" :value "Search" :action (ok self))
142 (when (display-results-p self)
143 (let ((listing (ucw::list-presentation self)))
144 (setf
145 (slot-value listing 'ucw::calling-component) (slot-value self 'ucw::calling-component)
146 (slot-value listing 'ucw::place) (slot-value self 'ucw::place)
147 (slot-value listing 'ucw::continuation) (slot-value self 'ucw::continuation))
148
149 (render-on res listing))))
150
151 ;;;;
152 (defcomponent dont-show-unset-slots ()())
153
154 (defmethod slots :around ((self dont-show-unset-slots))
155 (remove-if-not #'(lambda (s) (let ((s (presentation-slot-value s (instance self))))
156 (and s (not (equal "" s)))))
157 (call-next-method)))