-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFilesDrop.js
More file actions
67 lines (60 loc) · 2.18 KB
/
Copy pathFilesDrop.js
File metadata and controls
67 lines (60 loc) · 2.18 KB
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
define([
], function(
) {
"use strict";
/**
* FilesDrop provides a drop zone and on-click-upload interface for
* files.
*
* The `handleFiles` argument is a function with the argument: aFileList
* where FileList is a https://developer.mozilla.org/en-US/docs/Web/API/FileList
*
* If `handleFiles.needsPubSub` is truish, the function is called with
* the arguments: pubsub, aFileList
*
* The function `loadFontsFromFileInput` of the `loadFonts` is made as
* a `handleFiles` function for this case, also with needsPubSub = true.
*/
function FilesDrop(container, pubSub, handleFiles) {
this._container = container;
this._pubSub = pubSub;
this._makeFileInput(
handleFiles.needsPubSub
? handleFiles.bind(null, this._pubSub)
: handleFiles
, container);
}
var _p = FilesDrop.prototype;
_p._makeFileInput = function (handleFiles, element) {
var hiddenFileInput = element.ownerDocument.createElement('input');
hiddenFileInput.setAttribute('type', 'file');
hiddenFileInput.setAttribute('multiple', 'multiple');
hiddenFileInput.style.display = 'none'; // can be hidden, no problem
// for the file dialogue
function fileInputChange(e) {
/*jshint validthis:true, unused:vars*/
handleFiles(this.files);
}
function forwardClick(e) {
/*jshint unused:vars*/
// forward the click => opens the file dialogue
hiddenFileInput.click();
}
// for drag and drop
function noAction(e) {
e.stopPropagation();
e.preventDefault();
}
function drop(e) {
e.stopPropagation();
e.preventDefault();
handleFiles(e.dataTransfer.files);
}
hiddenFileInput.addEventListener('change', fileInputChange);
element.addEventListener('click', forwardClick);
element.addEventListener("dragenter", noAction);
element.addEventListener("dragover", noAction);
element.addEventListener("drop", drop);
};
return FilesDrop;
});