diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2017-05-16 10:21:31 +0300 |
---|---|---|
committer | Pekka Hyvönen <pekka@vaadin.com> | 2017-05-16 10:21:31 +0300 |
commit | eb743d965278d263a4c496bb4e39c067fe2b1a8c (patch) | |
tree | f820843370c55216a4ec9612f3e20569b3d1e40f /client | |
parent | 04e7259fb497e47bcd6d664e87c243db5badd934 (diff) | |
download | vaadin-framework-eb743d965278d263a4c496bb4e39c067fe2b1a8c.tar.gz vaadin-framework-eb743d965278d263a4c496bb4e39c067fe2b1a8c.zip |
Add API to store any type of data in the dataTransfer object (#9319)
Diffstat (limited to 'client')
3 files changed, 55 insertions, 25 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/GridDropTargetConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/GridDropTargetConnector.java index cc84392156..2ee5c253a9 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/GridDropTargetConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/GridDropTargetConnector.java @@ -15,6 +15,8 @@ */ package com.vaadin.client.connectors.grid; +import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Optional; @@ -81,8 +83,9 @@ public class GridDropTargetConnector extends DropTargetExtensionConnector { } @Override - protected void sendDropEventToServer(String dataTransferText, - String dropEffect, NativeEvent dropEvent) { + protected void sendDropEventToServer(List<String> types, + Map<String, String> data, String dropEffect, + NativeEvent dropEvent) { String rowKey = null; DropLocation dropLocation = null; @@ -95,7 +98,7 @@ public class GridDropTargetConnector extends DropTargetExtensionConnector { dropLocation = getDropLocation(targetRow.get(), dropEvent); } - getRpcProxy(GridDropTargetRpc.class).drop(dataTransferText, dropEffect, + getRpcProxy(GridDropTargetRpc.class).drop(types, data, dropEffect, rowKey, dropLocation); } 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 d714cf4016..552ff151cd 100644 --- a/client/src/main/java/com/vaadin/client/extensions/DragSourceExtensionConnector.java +++ b/client/src/main/java/com/vaadin/client/extensions/DragSourceExtensionConnector.java @@ -15,6 +15,9 @@ */ package com.vaadin.client.extensions; +import java.util.List; +import java.util.Map; + import com.google.gwt.animation.client.AnimationScheduler; import com.google.gwt.dom.client.DataTransfer; import com.google.gwt.dom.client.Element; @@ -182,14 +185,22 @@ public class DragSourceExtensionConnector extends AbstractExtensionConnector { // Set drag image setDragImage(nativeEvent); - // Set text data parameter - String dataTransferText = createDataTransferText(nativeEvent); + // Set data parameters + List<String> types = getState().types; + Map<String, String> data = getState().data; + for (String type : types) { + nativeEvent.getDataTransfer().setData(type, data.get(type)); + } + // Always set something as the text data, or DnD won't work in FF ! + String dataTransferText = createDataTransferText(nativeEvent); if (dataTransferText == null) { dataTransferText = ""; } - nativeEvent.getDataTransfer().setData(DragSourceState.DATA_TYPE_TEXT, - dataTransferText); + + // Override data type "text" when storing special data is needed + nativeEvent.getDataTransfer() + .setData(DragSourceState.DATA_TYPE_TEXT, dataTransferText); // Initiate firing server side dragstart event when there is a // DragStartListener attached on the server side @@ -252,7 +263,7 @@ public class DragSourceExtensionConnector extends AbstractExtensionConnector { * @return Textual data to be set for the event or {@literal null}. */ protected String createDataTransferText(NativeEvent dragStartEvent) { - return getState().dataTransferText; + return getState().data.get(DragSourceState.DATA_TYPE_TEXT); } /** 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 0f3d6413cb..060f2a5167 100644 --- a/client/src/main/java/com/vaadin/client/extensions/DropTargetExtensionConnector.java +++ b/client/src/main/java/com/vaadin/client/extensions/DropTargetExtensionConnector.java @@ -15,6 +15,12 @@ */ package com.vaadin.client.extensions; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.google.gwt.core.client.JsArrayString; import com.google.gwt.dom.client.DataTransfer; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.NativeEvent; @@ -24,7 +30,6 @@ import com.vaadin.client.ComponentConnector; import com.vaadin.client.ServerConnector; import com.vaadin.client.ui.AbstractComponentConnector; import com.vaadin.shared.ui.Connect; -import com.vaadin.shared.ui.dnd.DragSourceState; import com.vaadin.shared.ui.dnd.DropEffect; import com.vaadin.shared.ui.dnd.DropTargetRpc; import com.vaadin.shared.ui.dnd.DropTargetState; @@ -257,13 +262,18 @@ public class DropTargetExtensionConnector extends AbstractExtensionConnector { nativeEvent.preventDefault(); nativeEvent.stopPropagation(); - String dataTransferText = nativeEvent.getDataTransfer() - .getData(DragSourceState.DATA_TYPE_TEXT); - - String dropEffect = DragSourceExtensionConnector - .getDropEffect(nativeEvent.getDataTransfer()); - - sendDropEventToServer(dataTransferText, dropEffect, nativeEvent); + JsArrayString typesJsArray = getTypes( + nativeEvent.getDataTransfer()); + List<String> types = new ArrayList<>(); + Map<String, String> data = new HashMap<>(); + for (int i = 0; i < typesJsArray.length(); i++) { + String type = typesJsArray.get(i); + types.add(type); + data.put(type, nativeEvent.getDataTransfer().getData(type)); + } + + sendDropEventToServer(types, data, DragSourceExtensionConnector + .getDropEffect(nativeEvent.getDataTransfer()), nativeEvent); } removeTargetClassIndicator(nativeEvent); @@ -291,17 +301,18 @@ public class DropTargetExtensionConnector extends AbstractExtensionConnector { /** * Initiates a server RPC for the drop event. * - * @param dataTransferText - * Client side textual data that can be set for the drag source - * and is transferred to the drop target. + * @param types + * List of data types from {@code DataTransfer.types} object. + * @param data + * Map containing all types and corresponding data from the {@code + * DataTransfer} object. * @param dropEffect - * the desired drop effect - * @param dropEvent - * Client side drop event. + * The desired drop effect. */ - protected void sendDropEventToServer(String dataTransferText, - String dropEffect, NativeEvent dropEvent) { - getRpcProxy(DropTargetRpc.class).drop(dataTransferText, dropEffect); + protected void sendDropEventToServer(List<String> types, + Map<String, String> data, String dropEffect, + NativeEvent dropEvent) { + getRpcProxy(DropTargetRpc.class).drop(types, data, dropEffect); } /** @@ -337,6 +348,11 @@ public class DropTargetExtensionConnector extends AbstractExtensionConnector { getDropTargetElement().removeClassName(styleDragCenter); } + private native JsArrayString getTypes(DataTransfer dataTransfer) + /*-{ + return dataTransfer.types; + }-*/; + private native boolean executeScript(NativeEvent event, String script) /*-{ return new Function('event', script)(event); |