/
Grid.java
283 lines (223 loc) · 6.17 KB
/
Grid.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package org.rapidoid.gui;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.commons.Coll;
import org.rapidoid.gui.base.AbstractWidget;
import org.rapidoid.html.Tag;
import org.rapidoid.html.tag.TdTag;
import org.rapidoid.lambda.Lmbd;
import org.rapidoid.lambda.Mapper;
import org.rapidoid.model.Item;
import org.rapidoid.model.Items;
import org.rapidoid.model.Models;
import org.rapidoid.model.Property;
import org.rapidoid.u.U;
import org.rapidoid.util.Msc;
import org.rapidoid.var.Var;
import org.rapidoid.wrap.BoolWrap;
import java.util.Iterator;
import java.util.List;
/*
* #%L
* rapidoid-gui
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
@Authors("Nikolche Mihajlovski")
@Since("2.0.0")
public class Grid extends AbstractWidget<Grid> {
private volatile Iterable<?> items;
private volatile String orderBy;
private volatile int pageSize = 10;
private volatile String[] columns = {};
private volatile String[] headers = {};
private volatile String rowCmd;
private volatile Mapper<Object, String> toUri;
@Override
protected Object render() {
Pager pager = noPager();
boolean paging = pageSize > 0;
Iterable<?> rows;
BoolWrap isLastPage = new BoolWrap();
if (paging) {
String pageParam = "_p" + seq("pager");
pager = GUI.pager(pageParam).min(1);
Integer size = Coll.getSizeOrNull(items);
if (size != null) {
int pages = (int) Math.ceil(size / (double) pageSize);
pager.max(pages);
}
rows = Msc.getPage(items, pager.pageNumber(), pageSize, size, isLastPage);
} else {
rows = items;
}
return renderGridPage(pager, rows, isLastPage.value);
}
private Object renderGridPage(Pager pager, Iterable<?> rows, boolean isLastPage) {
Iterator<?> it = rows.iterator();
boolean hasData = it.hasNext();
if (isLastPage || !hasData) {
pager.max(pager.pageNumber());
}
if (!hasData) {
return U.list(noDataAvailable(), pager); // no data
}
Class<?> type = it.next().getClass();
Items itemsModel;
if (rows instanceof Items) {
itemsModel = (Items) rows;
} else {
itemsModel = Models.beanItems(type, U.array(rows));
}
final List<Property> props = itemsModel.properties(columns);
boolean ordered = !U.isEmpty(orderBy);
Var<String> order = null;
String currentOrder = orderBy;
if (ordered) {
order = GUI.local("_o" + seq("order"), orderBy);
currentOrder = order.get();
itemsModel = itemsModel.orderedBy(currentOrder);
}
Tag header = tableHeader(props, order);
Tag body = tableBody(props, itemsModel);
return fullTable(header, body, pager);
}
protected Tag noDataAvailable() {
return GUI.h4("No data available!");
}
protected Pager noPager() {
return null;
}
protected Tag fullTable(Tag header, Tag body, Pager pager) {
return GUI.row(GUI.table_(GUI.thead(header), body), pager);
}
protected Tag tableBody(final List<Property> props, Items pageOrAll) {
Tag body = GUI.tbody();
for (Item item : pageOrAll) {
Tag row = itemRow(props, item);
body = body.append(row);
}
return body;
}
protected Tag tableHeader(final List<Property> props, Var<String> order) {
Tag header = tr();
for (int i = 0; i < props.size(); i++) {
Property prop = props.get(i);
Tag sortIcon = null;
String caption = U.notEmpty(headers) && headers.length > i ? headers[i] : null;
caption = U.or(caption, prop.caption());
Object sort;
if (order != null) {
String currentOrder = order.get();
if (currentOrder.equals(prop.name())) {
sortIcon = GUI.fa("sort-amount-asc");
}
if (currentOrder.equals("-" + prop.name())) {
sortIcon = GUI.fa("sort-amount-desc");
}
sort = GUI.a_void(caption, " ", sortIcon).cmd("_sort", order, prop.name());
} else {
sort = caption;
}
header = header.append(th(sort));
}
return header;
}
protected Tag itemRow(List<Property> properties, Item item) {
Tag row = tr();
for (Property prop : properties) {
Object value = prop.get(item);
value = U.or(value, "");
row = row.append(cell(GUI.display(value)));
}
if (rowCmd != null) {
row = row.cmd(rowCmd, item.value());
row = row.class_("pointer");
} else {
String js = onClickScript(item);
if (U.notEmpty(js)) {
row = row.onclick(js);
row = row.class_("pointer");
}
}
return row;
}
protected String onClickScript(Item item) {
String uri;
if (toUri != null) {
uri = Lmbd.eval(toUri, item.value());
} else {
uri = GUI.uriFor(item.value());
if (U.notEmpty(uri)) {
uri = Msc.uri(uri, "view");
}
}
return U.notEmpty(uri) ? U.frmt("Rapidoid.goAt('%s');", uri) : null;
}
protected TdTag cell(Object value) {
return td(value);
}
public Iterable<?> items() {
return items;
}
public Grid items(Iterable<?> items) {
U.must(!(items instanceof Items));
this.items = items;
return this;
}
public String orderBy() {
return orderBy;
}
public Grid orderBy(String orderBy) {
this.orderBy = orderBy;
return this;
}
public int pageSize() {
return pageSize;
}
public Grid pageSize(int pageSize) {
this.pageSize = pageSize;
return this;
}
public String[] columns() {
return columns;
}
public Grid columns(String... columns) {
this.columns = columns;
return this;
}
public String rowCmd() {
return rowCmd;
}
public Grid rowCmd(String rowCmd) {
this.rowCmd = rowCmd;
return this;
}
public Mapper<Object, String> toUri() {
return toUri;
}
public Grid toUri(Mapper<Object, String> toUri) {
this.toUri = toUri;
return this;
}
public String[] headers() {
return headers;
}
public Grid headers(String... headers) {
this.headers = headers;
return this;
}
}