Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

advanced-dragndrop.asciidoc 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. ---
  2. title: Drag and Drop
  3. order: 12
  4. layout: page
  5. ---
  6. [[advanced.dragndrop]]
  7. = Drag and Drop
  8. ((("Drag and Drop", id="term.advanced.dragndrop", range="startofrange")))
  9. IMPORTANT: This feature is currently being developed and only available in the Framework 8.1 prerelease versions, starting from 8.1.0.alpha1.
  10. Dragging an object from one location to another by grabbing it with mouse,
  11. holding the mouse button pressed, and then releasing the button to "drop" it to
  12. the other location is a common way to move, copy, or associate objects. For
  13. example, most operating systems allow dragging and dropping files between
  14. folders or dragging a document on a program to open it. Framework version 8.1 adds support for https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API[HTML5 drag and drop] features. This makes it possible to set components as drag sources that user can drag and drop, or to set them as drop targets to drop things on.
  15. == Drag Source
  16. Any component can be made a drag source that has textual data that is transferred when it is dragged and dropped.
  17. To make a component a drag source, you apply the [classname]#DragSourceExtension# to it. Then you can define the text to transfer, and the allowed drag effect.
  18. [source, java]
  19. ----
  20. Label draggableLabel = new Label("You can grab and drag me");
  21. DragSourceExtension<Label> dragSource = new DragSourceExtension<>(draggableLabel);
  22. // set the allowed effect
  23. dragSource.setEffectAllowed(EffectAllowed.MOVE);
  24. // set the text to transfer
  25. dragSource.setDataTransferText("hello receiver");
  26. ----
  27. The __effect allowed__ specifies the allowed effects that must match the __drop effect__ of the drop target. If these don't match, the drop event is never fired on the target. If multiple effects are allowed, the user can use the modifier keys to switch between the desired effects. The default effect and the modifier keys are system and browser dependent.
  28. The __data transfer text__ is textual data, that the drop target will receive in the __drop event__.
  29. The [classname]#DragStartEvent# is fired when the drag has started, and the [classname]#DragEndEvent# event when the drag has ended, either in a drop or a cancel.
  30. [source, java]
  31. ----
  32. dragSource.addDragStartListener(event ->
  33. event.getComponent().addStyleName("dragged")
  34. );
  35. dragSource.addDragEndListener(event ->
  36. event.getComponent().removeStyleName("dragged")
  37. );
  38. ----
  39. It is possible to transfer any Object as server side data to the drop target if both the drag source and drop target are placed in the same UI. This data is available in the drop event via the `DropEvent.getDragData()` method.
  40. [source, java]
  41. ----
  42. dragSource.addDragStartListener(event ->
  43. dragSource.setDragData(myObject);
  44. );
  45. dragSource.addDragEndListener(event ->
  46. dragSource.setDragData(null);
  47. };
  48. ----
  49. [NOTE]
  50. ====
  51. The browsers allow the user to select and drag and drop text, which may cause some issues when trying to drag a component that has text. You can fix this by setting the following style rule to the drag source component to prevent dragging of the text instead of the whole component.
  52. [source, css]
  53. ----
  54. user-select: none;
  55. ----
  56. ====
  57. [[advanced.dragndrop.drophandler]]
  58. == Drop Target
  59. The drag operations end when the mouse button is released on a valid drop target. It is then up to the target to react to the drop event and the data associated with the drag, set by the drag source.
  60. To make a component be a drop target, you apply the [classname]#DropTargetExtension# to it. The extension allows you to control when the drop is acceptable and then react to the drop event.
  61. [source, java]
  62. ----
  63. VerticalLayout dropTargetLayout = new VerticalLayout();
  64. dropTargetLayout.setCaption("Drop things inside me");
  65. dropTargetLayout.addStyleName(ValoTheme.LAYOUT_CARD);
  66. // make the layout accept drops
  67. DropTargetExtension<VerticalLayout> dropTarget = new DropTargetExtension<>(dropTargetLayout);
  68. // the drop effect must match the allowed effect in the drag source for a successful drop
  69. dropTarget.setDropEffect(DropEffect.MOVE);
  70. // catch the drops
  71. dropTarget.addDropListener(event -> {
  72. // if the drag source is in the same UI as the target
  73. Optional<AbstractComponent> dragSource = event.getDragSourceComponent();
  74. if (dragSource.isPresent() && dragSource.get() instanceof Label) {
  75. // move the label to the layout
  76. dropTargetLayout.addComponent(dragSource.get());
  77. // get possible transfer data
  78. String message = event.getDataTransferText();
  79. Notification.show("DropEvent with data transfer: "+ message);
  80. // handle possible server side drag data, if the drag source was in the same UI
  81. event.getDragData().ifPresent(data -> handleMyDragData((MyObject) data));
  82. }
  83. });
  84. ----
  85. When data is dragged over a drop target, the __v-drag-over__ class name is applied to the root element of the drop target component automatically.
  86. === Controlling When The Drop is Acceptable
  87. The __drop effect__ allows you to specify the desired drop effect, and for a succesful drop it must match the allowed effect that has been set for the drag source. Note that you can allow multiple effects, and that you should not rely on the default effect since it may vary between browsers.
  88. The __drag over criteria__ allows you determine whether the current drag source is allowed as a drop target, when the source is moved on top of the target. It is a script that is executed always when the `dragover` event is fired for the first time for this source, and returning `false` will prevent showing any drop effect. The script gets the `dragover` event as a parameter named `event`.
  89. The __drop criteria__ is similar to __drag over criteria__, but it is executed when the user has dropped the data by releasing the mouse button. The script gets the `drop` event as a parameter named `event`. Returning `false` will prevent the drop and no drop event is fired on the server side.
  90. ////
  91. TODO Add an example of drag over criteria and drop criteria
  92. ////
  93. ===
  94. ////
  95. TODO add back when supported with new API ?
  96. [[advanced.dragndrop.external]]
  97. == Dragging Files from Outside the Browser
  98. The [classname]#DropTargetExtension# allows dragging files from outside the
  99. browser and dropping them on a target component.
  100. Dropped files are automatically uploaded to the application and can be acquired from the
  101. wrapper with [methodname]#getFiles()#. The files are represented as
  102. [classname]#Html5File# objects as defined in the inner class. You can define an
  103. upload [classname]#Receiver# to receive the content of a file to an
  104. [classname]#OutputStream#.
  105. Dragging and dropping files to browser is supported in HTML 5 and requires a
  106. compatible browser, such as Mozilla Firefox 3.6 or newer.
  107. ////
  108. [[advanced.dragndrop.grid]]
  109. == Drag and Drop Rows in Grid
  110. It is possible to drag and drop the rows of a Grid component. This allows reordering of rows, dragging rows between different Grids, dragging rows outside of a Grid or dropping data onto rows.
  111. === Grid as a Drag Source
  112. A Grid component's rows can be made draggable by applying [classname]#GridDragSource# extension to the component. The extended Grid's rows become draggable, meaning that each row can be grabbed and moved by the mouse individually.
  113. When the Grid's selection mode is `SelectionMode.MULTI` and multiple rows are selected, it is possible to drag all the visible selected rows by grabbing one of them. However, when the grabbed row is not selected, only that one row will be dragged.
  114. The following example shows how you can define the allowed drag effect and customize the drag data with the drag data generator.
  115. [source,java]
  116. ----
  117. Grid<Person> grid = new Grid<>();
  118. // ...
  119. GridDragSource<Person> dragSource = new GridDragSource<>(grid);
  120. // set allowed effects
  121. dragSource.setEffectAllowed(EffectAllowed.MOVE);
  122. // set the drag data generator
  123. dragSource.setDragDataGenerator(person -> {
  124. JsonObject data = Json.createObject();
  125. data.put("name", person.getFirstName() + " " + person.getLastName());
  126. data.put("city", person.getAddress().getCity());
  127. return data;
  128. });
  129. ----
  130. The _drag data generator_ defines what data should be transferred when a row is dragged and dropped. The generator is executed for every inserted item and returns a `JsonObject` containing the data to be transferred for that item. The generated data is transferred as a JSON array using the HTML5 DataTransfer's data parameter of type `"text"`.
  131. When no generator is set, the whole row data is transferred as JSON, containing all the data generated by the attached [classname]#DataGenerator# instances, such as the row's content and its key.
  132. [NOTE]
  133. ====
  134. Note that calling the inherited `setDataTransferText(String data)` method is not supported, since the drag data is set for each row based on the data provided by the generator.
  135. ====
  136. The [classname]#GridDragStartEvent# is fired when dragging a row has started, and the [classname]#GridDragEndEvent# when the drag has ended, either in a drop or a cancel.
  137. [source,java]
  138. ----
  139. dragSource.addGridDragStartListener(event ->
  140. // Keep reference to the dragged items
  141. draggedItems = event.getDraggedItems()
  142. );
  143. // Add drag end listener
  144. dragSource.addGridDragEndListener(event -> {
  145. // If drop was successful, remove dragged items from source Grid
  146. if (event.getDropEffect() == DropEffect.MOVE) {
  147. ((ListDataProvider<Person>) grid.getDataProvider()).getItems()
  148. .removeAll(draggedItems);
  149. grid.getDataProvider().refreshAll();
  150. // Remove reference to dragged items
  151. draggedItems = null;
  152. }
  153. });
  154. ----
  155. The dragged rows can be accessed from both events using the `getDraggedItems()` method.
  156. === Grid as a Drop Target
  157. To make a Grid component's rows accept a drop event, apply the [classname]#GridDropTarget# extension to the component. When creating the extension, you need to specify where the transferred data can be dropped on.
  158. [source,java]
  159. ----
  160. Grid<Person> grid = new Grid<>();
  161. // ...
  162. GridDropTarget<Person> dropTarget = new GridDropTarget<>(grid, DropMode.BETWEEN);
  163. dropTarget.setDropEffect(DropEffect.MOVE);
  164. ----
  165. The _drop mode_ specifies the behaviour of the row when an element is dragged over or dropped onto it. Use `DropMode.ON_TOP` when you want to drop elements on top of a row and `DropMode.BETWEEN` when you want to drop elements between rows.
  166. The [classname]#GridDropEvent# is fired when data is dropped onto one of the Grid's rows. The following example shows how you can insert items into the Grid at the drop position. If the drag source is another Grid, you can access the generated drag data with the event's `getDataTransferText()` method.
  167. [source,java]
  168. ----
  169. dropTarget.addGridDropListener(event -> {
  170. // Accepting dragged items from another Grid in the same UI
  171. event.getDragSourceExtension().ifPresent(source -> {
  172. if (source instanceof GridDragSource) {
  173. // Get the target Grid's items
  174. ListDataProvider<Person> dataProvider = (ListDataProvider<Person>)
  175. event.getComponent().getDataProvider();
  176. List<Person> items = (List<Person>) dataProvider.getItems();
  177. // Calculate the target row's index
  178. int index = items.indexOf(event.getDropTargetRow()) + (
  179. event.getDropLocation() == DropLocation.BELOW ? 1 : 0);
  180. // Add dragged items to the target Grid
  181. items.addAll(index, draggedItems);
  182. dataProvider.refreshAll();
  183. // Show the dropped data
  184. Notification.show("Dropped row data: " + event.getDataTransferText());
  185. }
  186. });
  187. });
  188. ----
  189. (((range="endofrange", startref="term.advanced.dragndrop")))