Skip to content

Commit

Permalink
feat: implement max col count feature (#6581)
Browse files Browse the repository at this point in the history
* feat: implement max col count feature

* feat: add getter for maximum column count

* Update vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java

Co-authored-by: Tomi Virkki <[email protected]>

* Update vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java

Co-authored-by: Tomi Virkki <[email protected]>

---------

Co-authored-by: Tomi Virkki <[email protected]>
  • Loading branch information
ugur-vaadin and tomivirkki authored Aug 28, 2024
1 parent 5a82f19 commit 5aec673
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
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 @@ -146,6 +146,28 @@ public void removeAll() {
updateClient();
}

/**
* Returns the maximum column count of the dashboard.
*
* @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 maxColCount) {
getStyle().set("--vaadin-dashboard-col-max-count",
maxColCount == null ? null : String.valueOf(maxColCount));
}

@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 @@ -214,6 +214,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

0 comments on commit 5aec673

Please sign in to comment.