From ea22a7d17dcc82d2e28dbb0051fbb6904fd7c98d Mon Sep 17 00:00:00 2001 From: vt512 Date: Wed, 5 Dec 2018 10:53:34 +0100 Subject: Add more context information to criteriaScript in GridDropTargetConnector (#11321) * Add more context information to criteriaScript in GridDropTargetConnector When using drag and drop in Grids and TreeGrids a criteriaScript can be specified. However, this criteriaScript only gets an event as input parameter. It would be helpful if the criteriaScript would get more information: - the DropLocation - the targetElement which is used as a base for the DropLocation This change provides a protected method in DropTargetExtensionConnector which decides, if the drop is allowed by the criteriaScript. This method is overriden in GridDropTargetConnector to provide the both parameters dropLocation and targetElement. * add Test UI for criteriaScript with targetElement and dropLocation * add some description for the Test UI --- .../components/grid/GridDropCriteriaScript.java | 86 ++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 uitest/src/main/java/com/vaadin/tests/components/grid/GridDropCriteriaScript.java (limited to 'uitest') diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridDropCriteriaScript.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridDropCriteriaScript.java new file mode 100644 index 0000000000..3d036a3d3b --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridDropCriteriaScript.java @@ -0,0 +1,86 @@ +package com.vaadin.tests.components.grid; + +import com.vaadin.annotations.Theme; +import com.vaadin.annotations.Widgetset; +import com.vaadin.server.VaadinRequest; +import com.vaadin.shared.ui.ContentMode; +import com.vaadin.shared.ui.grid.DropLocation; +import com.vaadin.shared.ui.grid.DropMode; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Label; +import com.vaadin.ui.components.grid.GridDragSource; +import com.vaadin.ui.components.grid.GridDropTarget; + +import java.util.ArrayList; +import java.util.List; + +@Theme("valo") +@Widgetset("com.vaadin.DefaultWidgetSet") +public class GridDropCriteriaScript extends AbstractTestUI { + + class GridItem { + public final String caption; + public final DropLocation dropLocation; + + public GridItem(String caption, DropLocation dropLocation) { + this.caption = caption; + this.dropLocation = dropLocation; + } + + public String getCaption() { + return caption; + } + + public DropLocation getDropLocation() { + return dropLocation; + } + } + + @Override + protected void setup(VaadinRequest request) { + getUI().setMobileHtml5DndEnabled(true); + + final Label label = new Label("

Test for existance of targetElement " + + "and dropLocation in criteriaScript

" + + "

Drag one of the grid items.

" + + "

While dragging, hints in form of lines show " + + "where the item is allowed to be dropped.

" + + "

Test passed:" + "

" + "

" + "

Test failed:" + "

" + "

", ContentMode.HTML); + + final Grid grid = new Grid<>(); + grid.addColumn(GridItem::getCaption); + grid.setStyleGenerator( + gridItem -> "dropLocation-" + gridItem.getDropLocation()); + + new GridDragSource<>(grid); + + final GridDropTarget dropTarget = new GridDropTarget<>(grid, + DropMode.ON_TOP_OR_BETWEEN); + dropTarget.setDropCriteriaScript( + "return targetElement.classList.contains('dropLocation-' + dropLocation);"); + + grid.setItems(createItems()); + + addComponents(label, grid); + } + + private List createItems() { + final ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + for (DropLocation dropLocation : new DropLocation[] { + DropLocation.ON_TOP, DropLocation.ABOVE, + DropLocation.BELOW }) { + list.add(new GridItem(i + ": " + dropLocation.name(), + dropLocation)); + } + } + return list; + } + +} -- cgit v1.2.3