Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement max col count feature #6581

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public DashboardPage() {
Dashboard dashboard = new Dashboard();
dashboard.add(widget1, widget2, widget3);

dashboard.setMaximumColumnCount(3);

NativeButton addWidgetAtIndex1 = new NativeButton(
"Add widget at index 1");
addWidgetAtIndex1.addClickListener(click -> {
Expand Down Expand Up @@ -69,7 +71,19 @@ public DashboardPage() {
removeAllWidgets.addClickListener(click -> dashboard.removeAll());
removeAllWidgets.setId("remove-all-widgets");

NativeButton setMaximumColumnCount1 = new NativeButton(
"Set maximum column count 1");
setMaximumColumnCount1
.addClickListener(click -> dashboard.setMaximumColumnCount(1));
setMaximumColumnCount1.setId("set-maximum-column-count-1");

NativeButton setMaximumColumnCountNull = new NativeButton(
"Set maximum column count null");
setMaximumColumnCountNull.addClickListener(
click -> dashboard.setMaximumColumnCount(null));
setMaximumColumnCountNull.setId("set-maximum-column-count-null");

add(addWidgetAtIndex1, removeFirstAndLastWidgets, removeAllWidgets,
dashboard);
setMaximumColumnCount1, setMaximumColumnCountNull, dashboard);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ public void removeAllWidgets_widgetsAreCorrectlyRemoved() {
assertWidgetsByTitle();
}

@Test
public void changeMaximumColumnCountTo1_widgetsShouldBeOnTheSameColumn() {
List<DashboardWidgetElement> widgets = dashboardElement.getWidgets();
// The first two widgets should initially be on the same horizontal line
int yOfWidget1 = widgets.get(0).getLocation().getY();
Assert.assertEquals(yOfWidget1, widgets.get(1).getLocation().getY());

clickElementWithJs("set-maximum-column-count-1");
// The first two widgets should be on the same vertical line
int xOfWidget1 = widgets.get(0).getLocation().getX();
Assert.assertEquals(xOfWidget1, widgets.get(1).getLocation().getX());
}

@Test
public void changeMaximumColumnCountToNull_widgetsShouldBeOnTheSameRow() {
clickElementWithJs("set-maximum-column-count-1");
List<DashboardWidgetElement> widgets = dashboardElement.getWidgets();
// The first two widgets should be on the same vertical line
int xOfWidget1 = widgets.get(0).getLocation().getX();
Assert.assertEquals(xOfWidget1, widgets.get(1).getLocation().getX());

clickElementWithJs("set-maximum-column-count-null");
// The widgets should be on the same horizontal line
int yOfWidget1 = widgets.get(0).getLocation().getY();
Assert.assertEquals(yOfWidget1, widgets.get(1).getLocation().getY());
}

private void assertWidgetsByTitle(String... expectedWidgetTitles) {
List<DashboardWidgetElement> widgets = dashboardElement.getWidgets();
List<String> widgetTitles = widgets.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,28 @@ public void removeAll() {
updateClient();
}

/**
* Returns maximum column count of the dashboard.
ugur-vaadin marked this conversation as resolved.
Show resolved Hide resolved
*
* @return the maximum column count of the dashboard
*/
public Integer getMaximumColumnCount() {
String maxColCount = getStyle().get("--vaadin-dashboard-col-max-count");
return maxColCount == null ? null : Integer.valueOf(maxColCount);
}

/**
* Sets the maximum column count of the dashboard.
*
* @param maxCount
* the new maximum column count. Pass in {@code null} to set the
* maximum column count back to the default value.
*/
public void setMaximumColumnCount(Integer maxCount) {
getStyle().set("--vaadin-dashboard-col-max-count",
maxCount == null ? null : String.valueOf(maxCount));
}
ugur-vaadin marked this conversation as resolved.
Show resolved Hide resolved

tomivirkki marked this conversation as resolved.
Show resolved Hide resolved
@Override
public Stream<Component> getChildren() {
return getWidgets().stream().map(Component.class::cast);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,45 @@ public void addWidgetToAnotherDashboard_widgetIsMoved() {
assertWidgets(newDashboard, widget);
}

@Test
public void setMaximumColumnCount_valueIsCorrectlySet() {
String propertyName = "--vaadin-dashboard-col-max-count";
int valueToSet = 5;
Assert.assertNull(dashboard.getStyle().get(propertyName));
dashboard.setMaximumColumnCount(valueToSet);
Assert.assertEquals(String.valueOf(valueToSet),
dashboard.getStyle().get(propertyName));
dashboard.setMaximumColumnCount(null);
Assert.assertNull(dashboard.getStyle().get(propertyName));
}

@Test
public void setMaximumColumnCountNull_propertyIsRemoved() {
dashboard.setMaximumColumnCount(5);
dashboard.setMaximumColumnCount(null);
Assert.assertNull(
dashboard.getStyle().get("--vaadin-dashboard-col-max-count"));
}

@Test
public void defaultMaximumColumnCountValueIsCorrectlyRetrieved() {
Assert.assertNull(dashboard.getMaximumColumnCount());
}

@Test
public void setMaximumColumnCount_valueIsCorrectlyRetrieved() {
Integer valueToSet = 5;
dashboard.setMaximumColumnCount(valueToSet);
Assert.assertEquals(valueToSet, dashboard.getMaximumColumnCount());
}

@Test
public void setMaximumColumnCountNull_valueIsCorrectlyRetrieved() {
dashboard.setMaximumColumnCount(5);
dashboard.setMaximumColumnCount(null);
Assert.assertNull(dashboard.getMaximumColumnCount());
}

private void fakeClientCommunication() {
ui.getInternals().getStateTree().runExecutionsBeforeClientResponse();
ui.getInternals().getStateTree().collectChanges(ignore -> {
Expand Down