-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag-and-drop-sample-detailed.html
493 lines (434 loc) · 21.2 KB
/
drag-and-drop-sample-detailed.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<!DOCTYPE HTML>
<html>
<head>
<style>
div {
border: 2px solid black;
width: 100%;
}
#draggable-div-with-pointer-events-none {
pointer-events: none;
}
.drop-container {
border: unset;
display: grid;
grid-auto-flow: column;
grid-template-rows: 1fr 4fr;
column-gap: 10px;
}
#drop-zone-with-link-overwrite {
position: relative;
}
#link-overwrite {
border: 1px solid gray;
position: absolute;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<div class="drop-container">
Drop Zone:
<div id="drop-zone"></div>
Not-Drop Zone:
<div id="not-drop-zone"></div>
Drop Zone with link overwrite:
<div id="drop-zone-with-link-overwrite">
<span id="link-overwrite">Dragging over this content will try to add/update the link in the drag data, but drag data can only be changed in dragstart.</span>
</div>
Copy Drop Zone:
<div id="copy-drop-zone"></div>
Move Drop Zone:
<div id="move-drop-zone"></div>
Link Drop Zone:
<div id="link-drop-zone"></div>
</div>
<br/>
<a href="http://www.bing.com">Link to Bing.com</a>
<br/>
<br/>
<a href="http://www.bing.com" id="link-with-custom-drag-image">Link to Bing.com with custom drag image</a>
<br/>
<br/>
<img src="https://cdn.imgbin.com/17/20/25/imgbin-globe-flags-of-the-world-world-globe-globe-of-flag-illustration-MBARv8bBX4kYF2ATR5QPVJWC6.jpg" width="100px" height="100px"/>
<br/>
<br/>
<div draggable="true" title="Draggable div">Draggable div</div>
<br/>
<div draggable="false">Non-draggable div</div>
<br/>
<div title="Default draggable div" id="default-draggable-div">Default draggable div</div>
<br/>
<div draggable="true" title="Draggable div with pointer-events:none" id="draggable-div-with-pointer-events-none">Draggable div with pointer-events: none</div>
<br/>
<div draggable="true" title="Draggable div with custom drag image" id="draggable-div-with-custom-drag-image">Draggable div with custom drag image</div>
<br/>
<div draggable="true" title="Draggable div with copy effect allowed" id="draggable-div-with-copy-effect-allowed">Draggable div with copy effect allowed</div>
<br/>
<div draggable="true" title="Draggable div with move effect allowed" id="draggable-div-with-move-effect-allowed">Draggable div with move effect allowed</div>
<br/>
<div draggable="true" title="Draggable div with link effect allowed" id="draggable-div-with-link-effect-allowed">Draggable div with link effect allowed</div>
<br/>
<div draggable="true" title="Draggable div with copyMove effect allowed" id="draggable-div-with-copy-move-effect-allowed">Draggable div with copyMove effect allowed</div>
<br/>
<div draggable="true" title="Draggable div with copyLink effect allowed" id="draggable-div-with-copy-link-effect-allowed">Draggable div with copyLink effect allowed</div>
<br/>
<div draggable="true" title="Draggable div with linkMove effect allowed" id="draggable-div-with-link-move-effect-allowed">Draggable div with linkMove effect allowed</div>
<br/>
<div draggable="true" title="Draggable div with no effects allowed" id="draggable-div-with-no-effects-allowed">Draggable div with no effects allowed</div>
<br/>
<b>Logs:</b>
<div id="logs"></div>
<script>
function printData(dataTransfer, printTarget) {
let dataTransferTypes = dataTransfer.types;
for (let i = 0; i < dataTransferTypes.length; i++) {
let type = dataTransferTypes[i];
let data = dataTransfer.getData(type);
printTarget.innerHTML += `dataTransferType: ${type}, data: "${data}"<br/>`;
}
}
function setCustomDragImage(dataTransfer) {
let img = new Image();
img.setAttribute('src', 'test.jpg');
img.setAttribute('width', '10px');
img.setAttribute('height', '10px');
dataTransfer.setDragImage(img, 0, 0);
}
let dropZone = document.getElementById('drop-zone');
dropZone.addEventListener('dragenter', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'dropZoneDragEnter fired<br/>';
e.preventDefault();
});
dropZone.addEventListener('dragover', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'dropZoneDragOver fired<br/>';
e.preventDefault();
});
dropZone.addEventListener('dragleave', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'dropZoneDragLeave fired<br/>';
});
dropZone.addEventListener('drop', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'dropZoneDrop fired<br/>';
e.preventDefault();
printData(e.dataTransfer, dropZone);
});
let notDropZone = document.getElementById('not-drop-zone');
notDropZone.addEventListener('dragenter', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'notDropZoneDragEnter fired<br/>';
e.preventDefault();
});
notDropZone.addEventListener('dragover', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'notDropZoneDragOver fired<br/>';
e.preventDefault();
});
notDropZone.addEventListener('dragleave', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'notDropZoneDragLeave fired<br/>';
});
let dropZoneWithLinkOverwrite = document.getElementById('drop-zone-with-link-overwrite');
dropZoneWithLinkOverwrite.addEventListener('dragenter', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'dropZoneWithLinkOverwriteDragEnter fired<br/>';
});
dropZoneWithLinkOverwrite.addEventListener('dragover', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'dropZoneWithLinkOverwriteDragOver fired<br/>';
e.preventDefault();
});
dropZoneWithLinkOverwrite.addEventListener('dragleave', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'dropZoneWithLinkOverwriteDragLeave fired<br/>';
});
dropZoneWithLinkOverwrite.addEventListener('drop', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'dropZoneWithLinkOverwriteDrop fired<br/>';
e.preventDefault();
printData(e.dataTransfer, dropZoneWithLinkOverwrite);
});
let linkOverwrite = document.getElementById('link-overwrite');
linkOverwrite.addEventListener('dragenter', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'linkOverwriteDragEnter fired<br/>';
let dataTransfer = e.dataTransfer;
dataTransfer.setData('text/uri-list', 'http://www.google.com');
let uriList = "http://www.google.com\nhttp://www.bing.com";
console.log(uriList);
});
linkOverwrite.addEventListener('dragover', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'linkOverwriteDragOver fired<br/>';
});
let copyDropZone = document.getElementById('copy-drop-zone');
copyDropZone.addEventListener('dragenter', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'copyDropZoneDragEnter fired<br/>';
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
});
copyDropZone.addEventListener('dragover', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'copyDropZoneDragOver fired<br/>';
e.preventDefault();
});
copyDropZone.addEventListener('dragleave', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'copyDropZoneDragLeave fired<br/>';
});
copyDropZone.addEventListener('drop', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'copyDropZoneDrop fired<br/>';
e.preventDefault();
printData(e.dataTransfer, copyDropZone);
});
let moveDropZone = document.getElementById('move-drop-zone');
moveDropZone.addEventListener('dragenter', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'moveDropZoneDragEnter fired<br/>';
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
});
moveDropZone.addEventListener('dragover', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'moveDropZoneDragOver fired<br/>';
e.preventDefault();
});
moveDropZone.addEventListener('dragleave', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'moveDropZoneDragLeave fired<br/>';
});
moveDropZone.addEventListener('drop', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'moveDropZoneDrop fired<br/>';
e.preventDefault();
printData(e.dataTransfer, moveDropZone);
});
let linkDropZone = document.getElementById('link-drop-zone');
linkDropZone.addEventListener('dragenter', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'linkDropZoneDragEnter fired<br/>';
e.preventDefault();
e.dataTransfer.dropEffect = 'link';
});
linkDropZone.addEventListener('dragover', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'linkDropZoneDragOver fired<br/>';
e.preventDefault();
});
linkDropZone.addEventListener('dragleave', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'linkDropZoneDragLeave fired<br/>';
});
linkDropZone.addEventListener('drop', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'linkDropZoneDrop fired<br/>';
e.preventDefault();
printData(e.dataTransfer, linkDropZone);
});
let linkWithCustomDragImage = document.getElementById('link-with-custom-drag-image');
linkWithCustomDragImage.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'linkWithCustomDragImageDragStart fired<br/>';
setCustomDragImage(e.dataTransfer);
});
linkWithCustomDragImage.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'linkWithCustomDragImageDrag fired<br/>';
});
linkWithCustomDragImage.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'linkWithCustomDragImageDragEnd fired<br/>';
});
let draggableDiv = document.querySelector('div[draggable="true"]');
draggableDiv.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivDragStart fired<br/>';
e.dataTransfer.setData('text/plain', draggableDiv.innerText);
});
draggableDiv.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivDrag fired<br/>';
});
draggableDiv.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivDragEnd fired<br/>';
});
let nonDraggableDiv = document.querySelector('div[draggable="false"]');
nonDraggableDiv.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'nonDraggableDivDragStart fired<br/>';
e.dataTransfer.setData('text/plain', nonDraggableDiv.innerText);
});
nonDraggableDiv.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'nonDraggableDivDrag fired<br/>';
});
nonDraggableDiv.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'nonDraggableDivDragEnd fired<br/>';
});
let defaultDraggableDiv = document.getElementById('default-draggable-div');
defaultDraggableDiv.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'defaultDraggableDivDragStart fired<br/>';
e.dataTransfer.setData('text/plain', defaultDraggableDiv.innerText);
});
defaultDraggableDiv.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'defaultDraggableDivDrag fired<br/>';
});
defaultDraggableDiv.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'defaultDraggableDivDragEnd fired<br/>';
});
let draggableDivWithPointerEventsNone = document.getElementById('draggable-div-with-pointer-events-none');
draggableDivWithPointerEventsNone.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithPointerEventsNoneDragStart fired<br/>';
e.dataTransfer.setData('text/plain', draggableDivWithPointerEventsNone.innerText);
});
draggableDivWithPointerEventsNone.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithPointerEventsNoneDrag fired<br/>';
});
draggableDivWithPointerEventsNone.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithPointerEventsNoneDragEnd fired<br/>';
});
let draggableDivWithCustomDragImage = document.getElementById('draggable-div-with-custom-drag-image');
draggableDivWithCustomDragImage.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithCustomDragImageDragStart fired<br/>';
let dataTransfer = e.dataTransfer;
dataTransfer.setData('text/plain', draggableDivWithCustomDragImage.innerText);
setCustomDragImage(dataTransfer);
});
draggableDivWithCustomDragImage.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithCustomDragImageDrag fired<br/>';
});
draggableDivWithCustomDragImage.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithCustomDragImageDragEnd fired<br/>';
});
let draggableDivWithCopyEffectAllowed = document.getElementById('draggable-div-with-copy-effect-allowed');
draggableDivWithCopyEffectAllowed.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithCopyEffectAllowedDragStart fired<br/>';
let dataTransfer = e.dataTransfer;
dataTransfer.setData('text/plain', draggableDivWithCopyEffectAllowed.innerText);
dataTransfer.effectAllowed = 'copy';
});
draggableDivWithCopyEffectAllowed.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithCopyEffectAllowedDrag fired<br/>';
});
draggableDivWithCopyEffectAllowed.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithCopyEffectAllowedDragEnd fired<br/>';
});
let draggableDivWithMoveEffectAllowed = document.getElementById('draggable-div-with-move-effect-allowed');
draggableDivWithMoveEffectAllowed.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithMoveEffectAllowedDragStart fired<br/>';
let dataTransfer = e.dataTransfer;
dataTransfer.setData('text/plain', draggableDivWithMoveEffectAllowed.innerText);
dataTransfer.effectAllowed = 'move';
});
draggableDivWithMoveEffectAllowed.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithMoveEffectAllowedDrag fired<br/>';
});
draggableDivWithMoveEffectAllowed.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithMoveEffectAllowedDragEnd fired<br/>';
});
let draggableDivWithLinkEffectAllowed = document.getElementById('draggable-div-with-link-effect-allowed');
draggableDivWithLinkEffectAllowed.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithLinkEffectAllowedDragStart fired<br/>';
let dataTransfer = e.dataTransfer;
dataTransfer.setData('text/plain', draggableDivWithLinkEffectAllowed.innerText);
dataTransfer.effectAllowed = 'link';
});
draggableDivWithLinkEffectAllowed.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithLinkEffectAllowedDrag fired<br/>';
});
draggableDivWithLinkEffectAllowed.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithLinkEffectAllowedDragEnd fired<br/>';
});
let draggableDivWithCopyMoveEffectAllowed = document.getElementById('draggable-div-with-copy-move-effect-allowed');
draggableDivWithCopyMoveEffectAllowed.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithCopyMoveEffectAllowedDragStart fired<br/>';
let dataTransfer = e.dataTransfer;
dataTransfer.setData('text/plain', draggableDivWithCopyMoveEffectAllowed.innerText);
dataTransfer.effectAllowed = 'copyMove';
});
draggableDivWithCopyMoveEffectAllowed.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithCopyMoveEffectAllowedDrag fired<br/>';
});
draggableDivWithCopyMoveEffectAllowed.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithCopyMoveEffectAllowedDragEnd fired<br/>';
});
let draggableDivWithCopyLinkEffectAllowed = document.getElementById('draggable-div-with-copy-link-effect-allowed');
draggableDivWithCopyLinkEffectAllowed.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithCopyLinkEffectAllowedDragStart fired<br/>';
let dataTransfer = e.dataTransfer;
dataTransfer.setData('text/plain', draggableDivWithCopyLinkEffectAllowed.innerText);
dataTransfer.effectAllowed = 'copyLink';
});
draggableDivWithCopyLinkEffectAllowed.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithCopyLinkEffectAllowedDrag fired<br/>';
});
draggableDivWithCopyLinkEffectAllowed.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithCopyLinkEffectAllowedDragEnd fired<br/>';
});
let draggableDivWithLinkMoveEffectAllowed = document.getElementById('draggable-div-with-link-move-effect-allowed');
draggableDivWithLinkMoveEffectAllowed.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithLinkMoveEffectAllowedDragStart fired<br/>';
let dataTransfer = e.dataTransfer;
dataTransfer.setData('text/plain', draggableDivWithLinkMoveEffectAllowed.innerText);
dataTransfer.effectAllowed = 'linkMove';
});
draggableDivWithLinkMoveEffectAllowed.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithLinkMoveEffectAllowedDrag fired<br/>';
});
draggableDivWithLinkMoveEffectAllowed.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithLinkMoveEffectAllowedDragEnd fired<br/>';
});
let draggableDivWithNoEffectsAllowed = document.getElementById('draggable-div-with-no-effects-allowed');
draggableDivWithNoEffectsAllowed.addEventListener('dragstart', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithNoEffectsAllowedDragStart fired<br/>';
let dataTransfer = e.dataTransfer;
dataTransfer.setData('text/plain', draggableDivWithNoEffectsAllowed.innerText);
dataTransfer.effectAllowed = 'none';
});
draggableDivWithNoEffectsAllowed.addEventListener('drag', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithNoEffectsAllowedDrag fired<br/>';
});
draggableDivWithNoEffectsAllowed.addEventListener('dragend', (e) => {
let logs = document.getElementById('logs');
logs.innerHTML += 'draggableDivWithNoEffectsAllowedDragEnd fired<br/>';
});
</script>
</body>
</html>