diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2017-07-03 13:36:17 +0200 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-07-03 14:36:17 +0300 |
commit | 6ecef502864ed201b468a68c00676beb401f21c7 (patch) | |
tree | 2f96341ebcb02bced9092e4d3196266ee2eb2740 /client/src | |
parent | 4d085fd8b3589db4fbbdcd52643cee4b20f70e31 (diff) | |
download | vaadin-framework-6ecef502864ed201b468a68c00676beb401f21c7.tar.gz vaadin-framework-6ecef502864ed201b468a68c00676beb401f21c7.zip |
Add criteria API to make it easier to set acceptance criteria for drag and drop (#9605)
Fixes #9600
Diffstat (limited to 'client/src')
-rw-r--r-- | client/src/main/java/com/vaadin/client/extensions/DragSourceExtensionConnector.java | 5 | ||||
-rw-r--r-- | client/src/main/java/com/vaadin/client/extensions/DropTargetExtensionConnector.java | 42 |
2 files changed, 43 insertions, 4 deletions
diff --git a/client/src/main/java/com/vaadin/client/extensions/DragSourceExtensionConnector.java b/client/src/main/java/com/vaadin/client/extensions/DragSourceExtensionConnector.java index 313c990074..b91b0aace0 100644 --- a/client/src/main/java/com/vaadin/client/extensions/DragSourceExtensionConnector.java +++ b/client/src/main/java/com/vaadin/client/extensions/DragSourceExtensionConnector.java @@ -412,6 +412,11 @@ public class DragSourceExtensionConnector extends AbstractExtensionConnector { for (String type : getState().types) { orderedData.put(type, getState().data.get(type)); } + + // Add payload for comparing against acceptance criteria + getState().payload.values().forEach(payload -> orderedData + .put(payload.getPayloadString(), payload.getValue())); + return orderedData; } diff --git a/client/src/main/java/com/vaadin/client/extensions/DropTargetExtensionConnector.java b/client/src/main/java/com/vaadin/client/extensions/DropTargetExtensionConnector.java index 7e64b0d305..3791db53b4 100644 --- a/client/src/main/java/com/vaadin/client/extensions/DropTargetExtensionConnector.java +++ b/client/src/main/java/com/vaadin/client/extensions/DropTargetExtensionConnector.java @@ -17,8 +17,10 @@ package com.vaadin.client.extensions; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import com.google.gwt.core.client.JsArrayString; import com.google.gwt.dom.client.DataTransfer; @@ -34,6 +36,7 @@ import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.dnd.DropEffect; import com.vaadin.shared.ui.dnd.DropTargetRpc; import com.vaadin.shared.ui.dnd.DropTargetState; +import com.vaadin.shared.ui.dnd.criteria.Payload; import com.vaadin.ui.dnd.DropTargetExtension; import elemental.events.Event; @@ -320,12 +323,43 @@ public class DropTargetExtensionConnector extends AbstractExtensionConnector { // Currently Safari, Edge and IE don't follow the spec by allowing drop // if those don't match - if (getState().dropCriteria != null) { - return executeScript(event, getState().dropCriteria); + // Allow by default when criteria not set + boolean allowed = true; + + // Execute criteria script + if (getState().criteriaScript != null) { + allowed = executeScript(event, getState().criteriaScript); + } + + // Execute criterion defined via API + if (allowed && getState().criteria != null && !getState().criteria + .isEmpty()) { + + // Collect payload data types + Set<Payload> payloadSet = new HashSet<>(); + JsArrayString typesJsArray = getTypes(event.getDataTransfer()); + for (int i = 0; i < typesJsArray.length(); i++) { + String type = typesJsArray.get(i); + + if (type.startsWith(Payload.ITEM_PREFIX)) { + payloadSet.add(Payload.parse(type)); + } + } + + // Compare payload against criteria + switch (getState().criteriaMatch) { + case ALL: + allowed = getState().criteria.stream() + .allMatch(criterion -> criterion.resolve(payloadSet)); + break; + case ANY: + default: + allowed = getState().criteria.stream() + .anyMatch(criterion -> criterion.resolve(payloadSet)); + } } - // Allow when criteria not set - return true; + return allowed; } /** |