-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
230 lines (191 loc) · 3.89 KB
/
index.js
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
/**
* Module dependencies.
*/
var Emitter = require('emitter')
, classes = require('classes')
, Upload = require('upload')
, events = require('events')
/**
* Expose `Dropload`.
*/
module.exports = Dropload;
/**
* Types.
*/
var typeMap = {
'text/plain': 'text',
'text/uri-list': 'url',
'text/html': 'html'
};
/**
* Initialize a drop point
* on the given `el`.
*
* @param {Element} el
* @api public
*/
function Dropload(el) {
if (!(this instanceof Dropload)) return new Dropload(el);
Emitter.call(this);
this.el = el;
this.classes = classes(el);
this.events = events(el, this);
this.events.bind('drop');
this.events.bind('dragenter');
this.events.bind('dragleave');
this.events.bind('dragover');
this.ignored = {};
}
/**
* Mixin emitter.
*/
Emitter(Dropload.prototype);
/**
* Ignore `name`.
*
* @param {String} name
* @api private
*/
Dropload.prototype.ignore = function(name){
this.ignored[name] = true;
};
/**
* Check if `name` is ignored.
*
* @param {String} name
* @return {Boolean}
* @api private
*/
Dropload.prototype.ignoring = function(name){
return !! this.ignored[name];
};
/**
* Unbind event handlers.
*
* @api public
*/
Dropload.prototype.unbind = function(){
this.events.unbind();
};
/**
* Dragenter handler.
*/
Dropload.prototype.ondragenter = function(e){
this.classes.add('over');
};
/**
* Dragover handler.
*/
Dropload.prototype.ondragover = function(e){
e.preventDefault();
};
/**
* Dragleave handler.
*/
Dropload.prototype.ondragleave = function(e){
this.classes.remove('over');
};
/**
* Drop handler.
*/
Dropload.prototype.ondrop = function(e){
e.stopPropagation();
e.preventDefault();
this.classes.remove('over');
var items = e.dataTransfer.items;
var files = e.dataTransfer.files;
this.emit('drop', e);
if (items) this.directories(items);
if (items) this.items(items);
if (files) this.upload(files);
this.ignored = {};
};
/**
* Walk directories and upload files.
*
* Directories are considered "files",
* non-files return null for .webkitGetAsEntry()
* for example when dragging urls.
*
* @param {DataTransferItemList} items
* @api private
*/
Dropload.prototype.directories = function(items){
for (var i = 0; i < items.length; i++) {
var item = items[i];
if ('file' != item.kind) continue;
if (!item.webkitGetAsEntry) continue;
var entry = item.webkitGetAsEntry();
if (entry.isDirectory) {
this.ignore(entry.name);
this.walkEntry(entry);
}
}
};
/**
* Handle the given `items`.
*
* @param {DataTransferItemList}
* @api private
*/
Dropload.prototype.items = function(items){
for (var i = 0; i < items.length; i++) {
var item = items[i];
this.item(item);
}
};
/**
* Walk file entry recursively.
*
* @param {FileEntry} item
* @api private
*/
Dropload.prototype.walkEntry = function(item){
var self = this;
if (item.isFile) {
return item.file(function(file){
file.entry = item;
self.upload([file]);
});
}
if (item.isDirectory) {
var dir = item.createReader();
dir.readEntries(function(entries){
for (var i = 0; i < entries.length; i++) {
var name = entries[i].name;
if ('.' == name[0]) continue;
self.walkEntry(entries[i]);
}
})
}
};
/**
* Handle `item`.
*
* @param {Object} item
* @api private
*/
Dropload.prototype.item = function(item){
var self = this;
var type = typeMap[item.type];
item.getAsString(function(str){
self.emit(type, str, item);
});
};
/**
* Upload the given `files`.
*
* Presents each `file` in the FileList
* as an `Upload` via the "upload" event
* after it has been validated.
*
* @param {FileList} files
* @api public
*/
Dropload.prototype.upload = function(files){
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (this.ignoring(file.name)) continue;
this.emit('upload', new Upload(file));
}
};