-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support drag drop for dashboard (#6633)
* feat: support drag drop for dashboard * chore: run formatter * test: fix unit test assertion * test: update test route names to align with others * test: add its that test the server client communication * refactor: simplify js event workaround * fix: do not remove widgets on detach * refactor: remove item sanitization on drag reorder * refactor: check children of element and remove workaround * refactor: make dashboard not editable by default * test: use selenium for drag drop and add tests for editable
- Loading branch information
1 parent
d230767
commit 3ea96fb
Showing
15 changed files
with
747 additions
and
124 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...-tests/src/main/java/com/vaadin/flow/component/dashboard/tests/DashboardDragDropPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Copyright 2000-2024 Vaadin Ltd. | ||
* | ||
* This program is available under Vaadin Commercial License and Service Terms. | ||
* | ||
* See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full | ||
* license. | ||
*/ | ||
package com.vaadin.flow.component.dashboard.tests; | ||
|
||
import com.vaadin.flow.component.dashboard.Dashboard; | ||
import com.vaadin.flow.component.dashboard.DashboardSection; | ||
import com.vaadin.flow.component.dashboard.DashboardWidget; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.router.Route; | ||
|
||
/** | ||
* @author Vaadin Ltd | ||
*/ | ||
@Route("vaadin-dashboard/drag-drop") | ||
public class DashboardDragDropPage extends Div { | ||
|
||
public DashboardDragDropPage() { | ||
Dashboard dashboard = new Dashboard(); | ||
dashboard.setEditable(true); | ||
dashboard.setMinimumRowHeight("100px"); | ||
dashboard.setMaximumColumnWidth("400px"); | ||
|
||
DashboardWidget widget1 = new DashboardWidget(); | ||
widget1.setTitle("Widget 1"); | ||
|
||
DashboardWidget widget2 = new DashboardWidget(); | ||
widget2.setTitle("Widget 2"); | ||
|
||
dashboard.add(widget1, widget2); | ||
|
||
DashboardWidget widget1InSection1 = new DashboardWidget(); | ||
widget1InSection1.setTitle("Widget 1 in Section 1"); | ||
|
||
DashboardWidget widget2InSection1 = new DashboardWidget(); | ||
widget2InSection1.setTitle("Widget 2 in Section 1"); | ||
|
||
DashboardSection section1 = new DashboardSection("Section 1"); | ||
section1.add(widget1InSection1, widget2InSection1); | ||
|
||
dashboard.addSection(section1); | ||
|
||
DashboardWidget widgetInSection2 = new DashboardWidget(); | ||
widgetInSection2.setTitle("Widget in Section 2"); | ||
|
||
DashboardSection section2 = new DashboardSection("Section 2"); | ||
section2.add(widgetInSection2); | ||
|
||
dashboard.addSection(section2); | ||
|
||
NativeButton toggleAttached = new NativeButton("Toggle attached", e -> { | ||
if (dashboard.getParent().isPresent()) { | ||
dashboard.removeFromParent(); | ||
} else { | ||
add(dashboard); | ||
} | ||
}); | ||
toggleAttached.setId("toggle-attached"); | ||
|
||
NativeButton toggleEditable = new NativeButton("Toggle editable", | ||
e -> dashboard.setEditable(!dashboard.isEditable())); | ||
toggleEditable.setId("toggle-editable"); | ||
|
||
add(toggleAttached, toggleEditable, dashboard); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...on-tests/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardDragDropIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* Copyright 2000-2024 Vaadin Ltd. | ||
* | ||
* This program is available under Vaadin Commercial License and Service Terms. | ||
* | ||
* See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full | ||
* license. | ||
*/ | ||
package com.vaadin.flow.component.dashboard.tests; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openqa.selenium.interactions.Actions; | ||
|
||
import com.vaadin.flow.component.dashboard.testbench.DashboardElement; | ||
import com.vaadin.flow.testutil.TestPath; | ||
import com.vaadin.testbench.TestBenchElement; | ||
import com.vaadin.tests.AbstractComponentIT; | ||
|
||
/** | ||
* @author Vaadin Ltd | ||
*/ | ||
@TestPath("vaadin-dashboard/drag-drop") | ||
public class DashboardDragDropIT extends AbstractComponentIT { | ||
|
||
private DashboardElement dashboardElement; | ||
|
||
@Before | ||
public void init() { | ||
open(); | ||
dashboardElement = $(DashboardElement.class).waitForFirst(); | ||
} | ||
|
||
@Test | ||
public void reorderWidgetOnClientSide_itemsAreReorderedCorrectly() { | ||
var draggedWidget = dashboardElement.getWidgets().get(0); | ||
var targetWidget = dashboardElement.getWidgets().get(1); | ||
dragDropElement(draggedWidget, targetWidget); | ||
Assert.assertEquals(draggedWidget.getTitle(), | ||
dashboardElement.getWidgets().get(1).getTitle()); | ||
} | ||
|
||
@Test | ||
public void reorderSectionOnClientSide_itemsAreReorderedCorrectly() { | ||
var draggedSection = dashboardElement.getSections().get(1); | ||
var targetWidget = dashboardElement.getWidgets().get(0); | ||
dragDropElement(draggedSection, targetWidget); | ||
Assert.assertEquals(draggedSection.getTitle(), | ||
dashboardElement.getSections().get(0).getTitle()); | ||
} | ||
|
||
@Test | ||
public void reorderWidgetInSectionOnClientSide_itemsAreReorderedCorrectly() { | ||
var firstSection = dashboardElement.getSections().get(0); | ||
var draggedWidget = firstSection.getWidgets().get(0); | ||
var targetWidget = firstSection.getWidgets().get(1); | ||
dragDropElement(draggedWidget, targetWidget); | ||
firstSection = dashboardElement.getSections().get(0); | ||
Assert.assertEquals(draggedWidget.getTitle(), | ||
firstSection.getWidgets().get(1).getTitle()); | ||
} | ||
|
||
@Test | ||
public void detachReattach_reorderWidgetOnClientSide_itemsAreReorderedCorrectly() { | ||
clickElementWithJs("toggle-attached"); | ||
clickElementWithJs("toggle-attached"); | ||
dashboardElement = $(DashboardElement.class).waitForFirst(); | ||
reorderWidgetOnClientSide_itemsAreReorderedCorrectly(); | ||
} | ||
|
||
@Test | ||
public void setDashboardNotEditable_widgetCannotBeDragged() { | ||
var widget = dashboardElement.getWidgets().get(0); | ||
Assert.assertTrue(isHeaderActionsVisible(widget)); | ||
clickElementWithJs("toggle-editable"); | ||
Assert.assertFalse(isHeaderActionsVisible(widget)); | ||
} | ||
|
||
@Test | ||
public void setDashboardEditable_widgetCanBeDragged() { | ||
clickElementWithJs("toggle-editable"); | ||
clickElementWithJs("toggle-editable"); | ||
Assert.assertTrue( | ||
isHeaderActionsVisible(dashboardElement.getWidgets().get(0))); | ||
} | ||
|
||
private void dragDropElement(TestBenchElement draggedElement, | ||
TestBenchElement targetElement) { | ||
var dragHandle = getDragHandle(draggedElement); | ||
|
||
var yOffset = draggedElement.getLocation().getY() < targetElement | ||
.getLocation().getY() ? 10 : -10; | ||
var xOffset = draggedElement.getLocation().getX() < targetElement | ||
.getLocation().getX() ? 10 : -10; | ||
|
||
new Actions(driver).clickAndHold(dragHandle) | ||
.moveToElement(targetElement, xOffset, yOffset) | ||
.release(targetElement).build().perform(); | ||
} | ||
|
||
private static boolean isHeaderActionsVisible(TestBenchElement element) { | ||
TestBenchElement headerActions = element.$("*").withId("header-actions") | ||
.first(); | ||
return !"none".equals(headerActions.getCssValue("display")); | ||
} | ||
|
||
private static TestBenchElement getDragHandle(TestBenchElement element) { | ||
return element.$("*").withClassName("drag-handle").first(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.