-
Notifications
You must be signed in to change notification settings - Fork 13
/
drag-and-drop-test.html
375 lines (326 loc) · 12 KB
/
drag-and-drop-test.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Drag and drop test</title>
<style>
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
.instructions {
display: none;
}
.instructions, .events, .output {
pointer-events: none;
}
.clear-events {
-webkit-appearance: none;
-moz-appearance: none;
cursor: pointer;
display: inline-block;
font-size: 1rem;
border: 2px solid #ccc;
background: white;
border-radius: 3px;
outline: none;
pointer-events: all;
}
.droptarget {
position: absolute;
right: 20px;
top: 20px;
width: 200px;
height: 200px;
background: #eee;
}
.status, .events {
font-family: monospace;
white-space: pre-wrap;
}
</style>
</head>
<body>
<h1>Drag and Drop test</h1>
<div class="output">
<h2>Status:</h2>
<p class="status"></p>
<h2>Events: <button class="clear-events">✖</button></h2>
<p class="events"></p>
</div>
<div class="droptarget"></div>
<script>
(function () {
var logDragOverEvents = false;
var events = [];
var currentEvent = null;
var draggingInPage = false;
var draggingFiles = false;
var isFolder = false;
var detectedMimeTypes = null;
var draggingInDropTarget = false;
var eventFilesProperty = 'unknown';
var mimeTypeProperty = 'unknown';
var dragCounter = 0;
updateOutput();
document.body.addEventListener('dragenter', onBodyDragDropEvent);
document.body.addEventListener('dragover', onBodyDragDropEvent);
document.body.addEventListener('dragleave', onBodyDragDropEvent);
document.body.addEventListener('drop', onBodyDragDropEvent);
document.querySelector('.clear-events').addEventListener('click', function () {
events = [];
updateOutput();
});
var droptarget = document.querySelector('.droptarget');
droptarget.addEventListener('dragenter', onDropTargetEvent);
droptarget.addEventListener('dragover', onDropTargetEvent);
droptarget.addEventListener('dragleave', onDropTargetEvent);
droptarget.addEventListener('drop', onDropTargetEvent);
function onBodyDragDropEvent(event) {
draggingFiles = eventHasFiles(event);
var mimeTypes = getMimeTypesFromEvent(event);
detectedMimeTypes = mimeTypes;
if (event.type !== 'dragover' || logDragOverEvents) {
var loggedEvent = {
'@@class': getObjectClassName(event),
type: event.type,
target: event.target,
files: draggingFiles
};
if (event.srcElement !== undefined && event.srcElement !== event.target) {
loggedEvent.srcElement = event.srcElement;
}
if (event.fromElement) {
loggedEvent.fromElement = event.fromElement;
}
if (event.relatedTarget) {
loggedEvent.relatedTarget = event.relatedTarget;
}
if (event.defaultPrevented) {
loggedEvent.defaultPrevented = event.defaultPrevented;
}
if (event.currentTarget !== document.body) {
loggedEvent.currentTarget = event.currentTarget;
}
if (mimeTypes) {
loggedEvent.mimeTypes = mimeTypes;
}
events.push(formatValue(loggedEvent).replace(/\n\s*/g, ' '));
currentEvent = {
'@@class': getObjectClassName(event),
type: event.type,
target: event.target,
currentTarget: event.currentTarget,
relatedTarget: event.relatedTarget,
srcElement: event.srcElement
};
if (event.dataTransfer) {
currentEvent.dataTransfer = {
'@@class': getObjectClassName(event.dataTransfer),
dropEffect: event.dataTransfer.dropEffect,
effectAllowed: event.dataTransfer.effectAllowed,
types: asArray(event.dataTransfer.types)
};
currentEvent.dataTransfer.files = event.dataTransfer.files
&& asArray(event.dataTransfer.files, function (file) {
return {
'@@class': getObjectClassName(file),
name: file.name,
size: file.size,
type: file.type,
lastModified: file.lastModified,
lastModifiedDate: file.lastModifiedDate
};
});
currentEvent.dataTransfer.items = event.dataTransfer.items
&& asArray(event.dataTransfer.items, function (item) {
var entry = item.webkitGetAsEntry && item.webkitGetAsEntry();
var itemClone = {
'@@class': getObjectClassName(item),
kind: item.kind,
type: item.type
};
if (item.kind === 'file') {
itemClone['webkitGetAsEntry()'] = entry && {
'@@class': getObjectClassName(entry),
isDirectory: entry.isDirectory,
isFile: entry.isFile,
name: entry.name,
fullPath: entry.fullPath
};
}
return itemClone;
});
}
}
if (event.type !== 'dragleave') {
event.preventDefault();
}
if (event.type === 'dragenter') {
dragCounter += 1;
} else if (event.type === 'dragleave') {
dragCounter = dragCounter > 0 ? dragCounter - 1 : 0;
} else if (event.type === 'drop') {
dragCounter = 0;
}
draggingInPage = dragCounter > 0;
isFolder = eventHasFolders(event);
updateOutput();
}
var dropTargetCounter = 0;
function onDropTargetEvent(event) {
switch (event.type) {
case 'dragenter':
dropTargetCounter += 1;
draggingInDropTarget = true;
updateOutput();
break;
case 'dragleave':
dropTargetCounter -= 1;
draggingInDropTarget = (dropTargetCounter > 0);
updateOutput();
break;
case 'drop':
dropTargetCounter = 0;
draggingInDropTarget = false;
updateOutput();
}
// event.preventDefault();
}
function updateOutput() {
var status = {
'dragging anything on page?': draggingInPage,
'dragging files on page?': draggingFiles,
'dragging a folder?': isFolder,
'detected mime types': detectedMimeTypes,
'dragging in gray drop target?': draggingInDropTarget,
'client reports mime types on drag': DataTransfer && 'items' in DataTransfer.prototype,
'client supports webkitGetAsEntry': typeof DataTransferItem !== 'undefined' && !!DataTransferItem.prototype.webkitGetAsEntry,
'property used to detect files in drag events': eventFilesProperty,
'property used to detect mime types of dragged files': mimeTypeProperty,
'current event': currentEvent
};
document.querySelector('.status').innerText = formatValue(status);
document.querySelector('.events').innerText = events.join('\n');
document.querySelector('.clear-events').style.display = events.length ? '' : 'none';
}
function eventHasFiles(event) {
if (event.type === 'drop') {
return !!event.files;
}
var types = event.dataTransfer && event.dataTransfer && event.dataTransfer.types;
if (!types) {
return null;
} else if (typeof types.contains === 'function') {
eventFilesProperty = 'dataTransfer.types.contains()';
return types.contains('Files');
} else if (typeof types.indexOf === 'function') {
eventFilesProperty = 'dataTransfer.types.indexOf()';
return types.indexOf('Files') >= 0;
} else if (typeof types.length === 'number') {
eventFilesProperty = 'dataTransfer.item()';
return types.indexOf('Files') >= 0;
}
return false;
}
function eventHasFolders(event) {
var items = event.dataTransfer && event.dataTransfer.items;
if (items && items.length > 0) {
for (var index = 0; index < items.length; index++) {
if (items[index].webkitGetAsEntry) {
var entry = items[index].webkitGetAsEntry();
if (entry && entry.isDirectory) {
return true;
}
}
if (items[index].type === '') {
// This is prone to errors, since "" is the mime type of files without extension
// see README.md
return true;
}
}
}
return false;
}
function getMimeTypesFromEvent(event) {
var dataTransfer = event.dataTransfer || (event.originalEvent && event.originalEvent.dataTransfer);
if (!dataTransfer) {
return null;
} else if (dataTransfer.files && (dataTransfer.files.length > 0 || event.type === 'drop')) {
mimeTypeProperty = 'dataTransfer.files';
var mimeTypeArray = new Array(dataTransfer.files.length);
for (var i = 0; i < mimeTypeArray.length; i++) {
mimeTypeArray[i] = dataTransfer.files.item(i).type;
}
return mimeTypeArray;
} else if (dataTransfer.items && dataTransfer.items.length > 0) {
mimeTypeProperty = 'dataTransfer.items';
return [].slice.call(dataTransfer.items)
.filter(function (item) { return item.kind === 'file'; })
.map(function (item) { return item.type; });
} else if ('mozItemCount' in dataTransfer) {
mimeTypeProperty = 'dataTransfer.mozItemCount';
var fakeMimeTypes = new Array(dataTransfer.mozItemCount);
for (var j = 0; j < fakeMimeTypes.length; j++) {
fakeMimeTypes[j] = 'unknown';
}
} else {
return null;
}
}
window.formatValue = formatValue;
function formatValue(value) {
return JSON.stringify(value, elementStringifier, 4)
.replace(/\{\n\s*"@@class": "([^"]+)",?/g, '$1 {')
.replace(/\[\n\s*"@@class{([^"}]+)\}",?/g, '$1 [')
.replace(/"([^"]+)": "@@undefined"/g, '"$1": undefined')
.replace(/"([^"]+)": "@@element\{([^}"\n]+?)\}"/g, '"$1": <$2>')
.replace(/"([^"]+)": /g, '$1: ');
function elementStringifier(key, value) {
if (value === undefined) {
return '@@undefined';
} else if (typeof value === 'object' && value instanceof HTMLElement) {
var elementName = value.nodeName.toLowerCase();
var id = ('#' + value.getAttribute('id')).replace(/^#(null)?$/, '');
var classes = (value.getAttribute('class') || '')
.trim()
.split(/\s+/)
.map(function (cls) { return cls ? '.' + cls : '' })
.join('');
return '@@element{' + elementName + id + classes + '}';
} else if (typeof value === 'object' && Array.isArray(value) && value['@@class']) {
return ['@@class{' + value['@@class'] + '}'].concat(value);
} else {
return value;
}
};
}
function getObjectClassName(obj) {
if (typeof obj !== 'object' || !obj) {
throw new TypeError();
}
var matches = Object.prototype.toString.call(obj).match(/^\[object (.+)\]$/);
if (!matches) {
throw new Error();
}
return matches[1];
}
function asArray(arrayLike, mappingFunction) {
if (Array.isArray(arrayLike)) {
return mappingFunction ? arrayLike.map(mappingFunction) : arrayLike;
} else {
var array = mappingFunction
? Array.prototype.map.call(arrayLike, mappingFunction)
: Array.prototype.slice.call(arrayLike);
array['@@class'] = getObjectClassName(arrayLike);
return array;
}
}
}());
</script>
</body>
</html>