Skip to content

Commit 4234eaf

Browse files
authored
Merge pull request #286 from jenkinsci/type-aware-sort
Add new generic `DetailedCell` to provide type aware sorting
2 parents 978332b + 7068b11 commit 4234eaf

File tree

6 files changed

+131
-52
lines changed

6 files changed

+131
-52
lines changed

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<revision>1.12.1-1</revision>
2222
<changelist>-SNAPSHOT</changelist>
2323

24+
<json-unit-assertj.version>2.35.0</json-unit-assertj.version>
2425
<module.name>${project.groupId}.datatables</module.name>
2526
</properties>
2627

@@ -64,6 +65,18 @@
6465

6566
<!-- Test Dependencies -->
6667

68+
<dependency>
69+
<groupId>net.javacrumbs.json-unit</groupId>
70+
<artifactId>json-unit-assertj</artifactId>
71+
<version>${json-unit-assertj.version}</version>
72+
<scope>test</scope>
73+
<exclusions>
74+
<exclusion>
75+
<groupId>org.ow2.asm</groupId>
76+
<artifactId>asm</artifactId>
77+
</exclusion>
78+
</exclusions>
79+
</dependency>
6780
<dependency>
6881
<groupId>io.jenkins.plugins</groupId>
6982
<artifactId>plugin-util-api</artifactId>
@@ -213,6 +226,9 @@
213226
<packages combine.children="append">
214227
<package>io.jenkins.plugins.datatables</package>
215228
</packages>
229+
<excludes>
230+
<exclude>.*DetailedCell.*</exclude>
231+
</excludes>
216232
<entryPointClassPackage>io.jenkins.plugins.datatables.assertions</entryPointClassPackage>
217233
</configuration>
218234
</plugin>

src/main/java/io/jenkins/plugins/datatables/TableConfiguration.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.fasterxml.jackson.core.JsonProcessingException;
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
9-
import io.jenkins.plugins.datatables.options.SelectStyle;
109

1110
/**
1211
* Provides a configuration for the whole DataTable. This is merged with a default configuration in table.js.
@@ -162,4 +161,52 @@ public String getConfiguration() {
162161
@JsonSerialize
163162
private static class EmptyConfigObject {
164163
}
164+
165+
/**
166+
* The possible values of the <a href="https://datatables.net/reference/option/select.style">select.style</a> option,
167+
* which is used for enabling the selection for datatables and setting its style (the default is {@link #OS}.
168+
*
169+
* @author Florian Orendi
170+
*/
171+
public enum SelectStyle {
172+
/**
173+
* Selection can only be performed via the API.
174+
*/
175+
API("api"),
176+
177+
/**
178+
* Only a single item can be selected, any other selected items will be automatically
179+
* deselected when a new item is selected.
180+
*/
181+
SINGLE("single"),
182+
183+
/**
184+
* Multiple items can be selected. Selection is performed by simply clicking on the items to be selected.
185+
*/
186+
MULTI("multi"),
187+
188+
/**
189+
* Operating System (OS) style selection.
190+
* This is the most comprehensive option and provides complex behaviours such as ctrl/cmd clicking
191+
* to select / deselect individual items, shift clicking to select ranges
192+
* and an unmodified click to select a single item.
193+
*/
194+
OS("os"),
195+
196+
/**
197+
* A hybrid between the {@link #OS} style and {@link #MULTI},
198+
* allowing easy multi-row selection without immediate de-selection when clicking on a row.
199+
*/
200+
MULTI_SHIFT("multi+shift");
201+
202+
private final String style;
203+
204+
SelectStyle(final String style) {
205+
this.style = style;
206+
}
207+
208+
public String getStyle() {
209+
return style;
210+
}
211+
}
165212
}

src/main/java/io/jenkins/plugins/datatables/TableModel.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ public String getTableConfigurationDefinition() {
6969
/**
7070
* A column value attribute that provides a {@code display} and {@code sort} property so that a JQuery DataTables
7171
* can use different properties to sort and display a column.
72+
*
73+
* @deprecated use the generic class {@link DetailedCell}
7274
*/
75+
@Deprecated
7376
public static class DetailedColumnDefinition {
7477
private final String display;
7578
private final String sort;
@@ -97,4 +100,38 @@ public String getSort() {
97100
return sort;
98101
}
99102
}
103+
104+
/**
105+
* A table cell that provides a {@code display} and {@code sort} property so that a JQuery DataTables
106+
* can use different properties to sort and display a column.
107+
*
108+
* @param <T> the type of the sort column
109+
*/
110+
public static class DetailedCell<T> {
111+
private final String display;
112+
private final T sort;
113+
114+
/**
115+
* Creates a new {@link DetailedCell}.
116+
*
117+
* @param display
118+
* the value that should be used to display the cell
119+
* @param sort
120+
* the value that should be used to sort the cell
121+
*
122+
* @see <a href="https://datatables.net/reference/option/columns.type">DataTables Column Types</a>
123+
*/
124+
public DetailedCell(final String display, final T sort) {
125+
this.display = display;
126+
this.sort = sort;
127+
}
128+
129+
public String getDisplay() {
130+
return display;
131+
}
132+
133+
public T getSort() {
134+
return sort;
135+
}
136+
}
100137
}

src/main/java/io/jenkins/plugins/datatables/options/SelectStyle.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/test/java/io/jenkins/plugins/datatables/TableConfigurationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package io.jenkins.plugins.datatables;
22

3-
import io.jenkins.plugins.datatables.options.SelectStyle;
43
import org.junit.jupiter.api.Test;
54

5+
import io.jenkins.plugins.datatables.TableConfiguration.SelectStyle;
6+
67
import static io.jenkins.plugins.datatables.TableConfigurationAssert.*;
78

89
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.jenkins.plugins.datatables;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import io.jenkins.plugins.datatables.TableModel.DetailedCell;
6+
7+
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.*;
8+
9+
/**
10+
* Tests the class {@link TableModel}.
11+
*
12+
* @author Ullrich Hafner
13+
*/
14+
class TableModelTest {
15+
@Test
16+
void shouldCreateStringSortingCell() {
17+
DetailedCell<String> cell = new DetailedCell<>("Display", "Sort");
18+
19+
assertThatJson(cell).isEqualTo("{display: \"Display\", sort: \"Sort\"}");
20+
}
21+
22+
@Test
23+
void shouldCreateIntSortingCell() {
24+
DetailedCell<Integer> cell = new DetailedCell<>("Display", 123);
25+
26+
assertThatJson(cell).isEqualTo("{display: \"Display\", sort: 123}");
27+
}
28+
}

0 commit comments

Comments
 (0)