@@ -5,43 +5,210 @@ Helper library to simplify **HTML5 Drag and Drop** in Dart.
5
5
6
6
## Features ##
7
7
* Make any HTML Element ` draggable ` .
8
- * Create ` dropzone ` s and connect them with ` draggable ` s .
8
+ * Create ` dropzones ` and connect them with ` draggables ` .
9
9
* Rearrange elements with ` sortable ` (similar to jQuery UI Sortable).
10
+ * Support for ` touch events ` on touch screen devices.
10
11
* Same functionality and API for IE9+, Firefox, Chrome and Safari.
11
12
* Uses fast native HTML5 Drag and Drop of the browser whenever possible.
12
13
* For browsers that do not support some features, the behaviour is emulated.
13
14
This is the case for IE9 and partly for IE10 (when custom drag images are
14
15
used).
15
16
16
17
## Demo ##
17
- See [ HTML5 Drag and Drop in action] ( http://edu.makery.ch/projects/dart-html5-drag-and-drop ) (with code examples).
18
+ See [ HTML5 Drag and Drop in action] ( http://edu.makery.ch/projects/dart-html5-drag-and-drop )
19
+ (with code examples).
18
20
19
21
All examples are also available in the ` example ` directory on GitHub.
20
22
21
23
## Installation ##
22
24
23
- ### 1. Add Dependency ###
25
+ ### Add Dependency ###
24
26
Add the folowing to your ** pubspec.yaml** and run ** pub install**
25
27
``` yaml
26
28
dependencies:
27
29
html5_dnd: any
28
30
```
29
31
30
- ### 2. Import ###
31
- Import the ` html5_dnd ` library in your Dart code. If you're using the Sortable
32
- functionality, also add ` html5_sortable ` .
32
+ ### Import ###
33
+ Import the ` html5_dnd ` library in your Dart code.
33
34
34
35
``` dart
35
36
import 'package:html5_dnd/html5_dnd.dart';
36
- import 'package:html5_dnd/html5_sortable.dart';
37
37
38
38
// ...
39
39
```
40
40
41
- ### 3. Use it ###
42
- See the demo page above or the ` example ` directory to see how to use it. There
43
- are also plenty of comments in the code if you're interested in some finer
44
- details.
41
+ ## Usage ##
42
+ See the demo page above or the ` example ` directory to see some live examples
43
+ with code.
44
+
45
+ In general, to make drag and drop work, we will have to do two things:
46
+ 1 . Create draggables by installing HTML elements in a ` DraggableGroup ` .
47
+ 2 . Create dropzones by installing HTML elements in a ` DropzoneGroup ` .
48
+
49
+ To make elements sortable it's even easier: Create sortables by installing HTML
50
+ elements in a ` SortableGroup ` . The ` SortableGroup ` will make the installed
51
+ elements both into draggables and dropzones and thus creates sortable behaviour.
52
+
53
+
54
+ ### Disable Touch Support ###
55
+ There is a global property called ` enableTouchEvents ` which is ` true ` by
56
+ default. This means that touch events are automatically enabled on devices that
57
+ support it. If touch support should not be used even on touch devices, set this
58
+ flag to ` false ` .
59
+
60
+
61
+ ### Draggables ###
62
+ Any HTML element can be made draggable. First we'll have to create a
63
+ ` DraggableGroup ` that manages draggable elements. The ` DraggableGroup ` holds
64
+ all options for dragging and provides event streams we can listen to.
65
+
66
+ This is how a ` DraggableGroup ` is created. With ` install(...) ` or
67
+ ` installAll(...) ` elements are made draggable and registered in the group.
68
+
69
+ ``` dart
70
+ // Creating a DraggableGroup and installing some elements.
71
+ DraggableGroup dragGroup = new DraggableGroup();
72
+ dragGroup.installAll(queryAll('.my-draggables'));
73
+ ```
74
+
75
+ With ` uninstall(...) ` or ` uninstallAll(...) ` draggables can be removed from
76
+ the group and the draggable behaviour is uninstalled.
77
+
78
+ #### Draggable Options ####
79
+ The ` DraggableGroup ` has two constructor options:
80
+
81
+ * The ` dragImageFunction ` is used to provide a custom ` DragImage ` . If no
82
+ ` dragImageFunction ` is supplied, the drag image is created from the HTML
83
+ element of the draggable.
84
+ * If a ` handle ` is provided, it is used as query String to find a subelement of
85
+ draggable elements. The drag is then restricted to that subelement.
86
+
87
+ ``` dart
88
+ // Create a custom drag image from a png.
89
+ ImageElement png = new ImageElement(src: 'icons/smiley-happy.png');
90
+ DragImage img = new DragImage(png, 0, 0);
91
+
92
+ // Always return the same DragImage here. We could also create a different image
93
+ // for each draggable.
94
+ DragImageFunction imageFunction = (Element draggable) {
95
+ return img;
96
+ };
97
+
98
+ // Create DraggableGroup with custom drag image and a handle.
99
+ DraggableGroup dragGroup = new DraggableGroup(
100
+ dragImageFunction: imageFunction, handle: '.my-handle');
101
+ ```
102
+
103
+ Other options of the ` DraggableGroup ` :
104
+
105
+ ``` dart
106
+ DraggableGroup dragGroup = new DraggableGroup();
107
+
108
+ // CSS class set to html body during drag.
109
+ dragGroup.dragOccurringClass = 'dnd-drag-occurring';
110
+
111
+ // CSS class set to the draggable element during drag.
112
+ dragGroup.draggingClass = 'dnd-dragging';
113
+
114
+ // CSS class set to the dropzone when a draggable is dragged over it.
115
+ dragGroup.overClass = 'dnd-over';
116
+
117
+ // Changes mouse cursor when this draggable is dragged over a draggable.
118
+ dragGroup.dropEffect = DROP_EFFECT_COPY;
119
+ ```
120
+
121
+ #### Draggable Events ####
122
+ We can listen to ` dragStart ` , ` drag ` , and ` dragEnd ` events of a
123
+ ` DraggableGroup ` .
124
+
125
+ ``` dart
126
+ DraggableGroup dragGroup = new DraggableGroup();
127
+
128
+ dragGroup.onDragStart.listen((DraggableEvent event) => print('drag started'));
129
+ dragGroup.onDrag.listen((DraggableEvent event) => print('dragging'));
130
+ dragGroup.onDragEnd.listen((DraggableEvent event) => print('drag ended'));
131
+ ```
132
+
133
+
134
+ ### Dropzones ###
135
+ Any HTML element can be made to a dropzone. Similar to how draggables are
136
+ created, we create a dropzones:
137
+
138
+ ``` dart
139
+ // Creating a DropzoneGroup and installing some elements.
140
+ DropzoneGroup dropGroup = new DropzoneGroup();
141
+ dropGroup.installAll(queryAll('.my-dropzones'));
142
+ ```
143
+
144
+ #### Dropzone Options ####
145
+ The ` DropzoneGroup ` has an option to specify which ` DraggableGroup ` s it accepts.
146
+ If no accept group is specified, the ` DropzoneGroup ` will accept all draggables.
147
+
148
+ ``` dart
149
+ DraggableGroup dragGroup = new DraggableGroup();
150
+ // ... install some draggable elements ...
151
+
152
+ DropzoneGroup dropGroup = new DropzoneGroup();
153
+ // ... install some dropzone elements ...
154
+
155
+ // Make dropGroup only accept draggables from dragGroup.
156
+ dropGroup.accept.add(dragGroup);
157
+ ```
158
+
159
+ #### Dropzone Events ####
160
+ We can listen to ` dragEnter ` , ` dragOver ` , ` dragLeave ` , and ` drop ` events of a
161
+ ` DropzoneGroup ` .
162
+
163
+ ``` dart
164
+ DropzoneGroup dropGroup = new DropzoneGroup();
165
+
166
+ dropGroup.onDragEnter.listen((DropzoneEvent event) => print('drag entered'));
167
+ dropGroup.onDragOver.listen((DropzoneEvent event) => print('dragging over'));
168
+ dropGroup.onDragLeave.listen((DropzoneEvent event) => print('drag left'));
169
+ dropGroup.onDrop.listen((DropzoneEvent event) => print('dropped inside'));
170
+ ```
171
+
172
+
173
+ ### Sortables ###
174
+ For reordering of HTML elements we can use sortables.
175
+
176
+ ``` dart
177
+ // Creating a SortableGroup and installing some elements.
178
+ SortableGroup sortGroup = new SortableGroup();
179
+ sortGroup.installAll(queryAll('.my-sortables'));
180
+ ```
181
+
182
+ Note: All sortables are at the same time draggables and dropzones. This means
183
+ we can set all options of ` DraggableGroup ` and ` DropzoneGroup ` on sortables and
184
+ listen to all their events.
185
+
186
+ #### Sortable Options ####
187
+ In addition to the inherited ` DraggableGroup ` and ` DropzoneGroup ` options,
188
+ ` SortableGroup ` as the following options:
189
+
190
+ ``` dart
191
+ SortableGroup sortGroup = new SortableGroup();
192
+
193
+ // CSS class set to placeholder element.
194
+ sortGroup.placeholderClass = 'dnd-placeholder';
195
+
196
+ // If true, forces the placeholder to have the computed size of the dragged element.
197
+ sortGroup.forcePlaceholderSize = true;
198
+
199
+ // Must be set to true for sortable grids. This ensures that different sized
200
+ // items in grids are handled correctly.
201
+ sortGroup.isGrid = false;
202
+ ```
203
+
204
+ #### Sortable Events ####
205
+ There is one additional event for ` SortableGroup ` s:
206
+
207
+ ``` dart
208
+ SortableGroup sortGroup = new SortableGroup();
209
+
210
+ sortGroup.onSortUpdate.listen((SortableEvent event) => print('elements were sorted'));
211
+ ```
45
212
46
213
47
214
## Thanks and Contributions ##
@@ -54,5 +221,6 @@ If you'd like to contribute, you're welcome to report issues or
54
221
[ repository on GitHub] ( https://github.com/marcojakob/dart-html5-dnd ) .
55
222
56
223
224
+
57
225
## License ##
58
226
The MIT License (MIT)
0 commit comments