Skip to content

Commit

Permalink
refactor: do not handle clients events if dashboard is not editable (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ugur-vaadin committed Oct 10, 2024
1 parent f254f87 commit ac41959
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@ void removeChild(Component child) {

private void onItemReorderEnd(
DashboardItemReorderEndEvent dashboardItemReorderEndEvent) {
if (!isEditable()) {
return;
}
JsonArray orderedItemsFromClient = dashboardItemReorderEndEvent
.getItems();
reorderItems(orderedItemsFromClient);
Expand All @@ -487,6 +490,9 @@ private void onItemReorderEnd(

private void onItemResizeEnd(
DashboardItemResizeEndEvent dashboardItemResizeEndEvent) {
if (!isEditable()) {
return;
}
DashboardWidget resizedWidget = dashboardItemResizeEndEvent
.getResizedWidget();
resizedWidget.setRowspan(dashboardItemResizeEndEvent.getRowspan());
Expand All @@ -495,6 +501,9 @@ private void onItemResizeEnd(

private void onItemRemoved(
DashboardItemRemovedEvent dashboardItemRemovedEvent) {
if (!isEditable()) {
return;
}
dashboardItemRemovedEvent.getRemovedItem().removeFromParent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void setup() {
super.setup();
dashboard = new Dashboard();
dashboard.add(new DashboardWidget(), new DashboardWidget());
dashboard.setEditable(true);
DashboardSection section = dashboard.addSection();
section.add(new DashboardWidget(), new DashboardWidget());
getUi().add(dashboard);
Expand All @@ -59,6 +60,15 @@ public void reorderWidgetInSection_orderIsUpdated() {
assertSectionWidgetReorder(2, 0, 1);
}

@Test
public void setDashboardNotEditable_reorderWidget_orderIsNotUpdated() {
dashboard.setEditable(false);
List<Integer> expectedRootLevelNodeIds = getRootLevelNodeIds();
reorderRootLevelItem(0, 1);
fireItemReorderEndEvent();
Assert.assertEquals(expectedRootLevelNodeIds, getRootLevelNodeIds());
}

private void fireItemReorderEndEvent() {
ComponentUtil.fireEvent(dashboard,
new DashboardItemReorderEndEvent(dashboard, false, itemsArray));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void setup() {
super.setup();
dashboard = new Dashboard();
dashboard.add(new DashboardWidget());
dashboard.setEditable(true);
DashboardSection section = dashboard.addSection();
section.add(new DashboardWidget());
getUi().add(dashboard);
Expand Down Expand Up @@ -63,6 +64,15 @@ public void resizeWidgetInSectionBothHorizontallyAndVertically_sizeIsUpdated() {
assertWidgetResized(1, 2, 2);
}

@Test
public void setDashboardNotEditable_resizeWidget_sizeIsNotUpdated() {
dashboard.setEditable(false);
DashboardWidget widgetToResize = dashboard.getWidgets().get(0);
fireItemResizeEndEvent(widgetToResize, 2, 2);
Assert.assertEquals(1, widgetToResize.getColspan());
Assert.assertEquals(1, widgetToResize.getRowspan());
}

private void assertWidgetResized(int widgetIndexToResize, int targetColspan,
int targetRowspan) {
DashboardWidget widgetToResize = dashboard.getWidgets()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
*/
package com.vaadin.flow.component.dashboard.tests;

import java.util.Set;
import java.util.stream.Collectors;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.flow.component.ComponentUtil;
import com.vaadin.flow.component.dashboard.Dashboard;
import com.vaadin.flow.component.dashboard.DashboardItemRemovedEvent;
import com.vaadin.flow.component.dashboard.DashboardSection;
import com.vaadin.flow.component.dashboard.DashboardWidget;
import com.vaadin.flow.component.html.Div;
Expand Down Expand Up @@ -815,4 +820,40 @@ public void detachDashboard_addWidget_reattachDashboard_widgetIsAdded() {
fakeClientCommunication();
assertChildComponents(dashboard, widget);
}

@Test
public void dashboardNotEditable_removeWidget_widgetIsNotRemoved() {
DashboardWidget widgetToRemove = new DashboardWidget();
dashboard.add(widgetToRemove);
fakeClientCommunication();
int expectedWidgetCount = dashboard.getWidgets().size();
int expectedNodeId = widgetToRemove.getElement().getNode().getId();
fireItemRemovedEvent(expectedNodeId);
Assert.assertEquals(expectedWidgetCount, dashboard.getWidgets().size());
Set<Integer> actualNodeIds = dashboard.getWidgets().stream()
.map(widget -> widget.getElement().getNode().getId())
.collect(Collectors.toSet());
Assert.assertTrue(actualNodeIds.contains(expectedNodeId));
}

@Test
public void setDashboardEditable_removeWidget_widgetIsRemoved() {
DashboardWidget widgetToRemove = new DashboardWidget();
dashboard.add(widgetToRemove);
dashboard.setEditable(true);
fakeClientCommunication();
int expectedWidgetCount = dashboard.getWidgets().size() - 1;
int nodeIdToBeRemoved = widgetToRemove.getElement().getNode().getId();
fireItemRemovedEvent(nodeIdToBeRemoved);
Assert.assertEquals(expectedWidgetCount, dashboard.getWidgets().size());
Set<Integer> actualNodeIds = dashboard.getWidgets().stream()
.map(widget -> widget.getElement().getNode().getId())
.collect(Collectors.toSet());
Assert.assertFalse(actualNodeIds.contains(nodeIdToBeRemoved));
}

private void fireItemRemovedEvent(int nodeId) {
ComponentUtil.fireEvent(dashboard,
new DashboardItemRemovedEvent(dashboard, false, nodeId));
}
}

0 comments on commit ac41959

Please sign in to comment.