-
Notifications
You must be signed in to change notification settings - Fork 38
/
jquery.fs.dropper.js
386 lines (319 loc) · 8.27 KB
/
jquery.fs.dropper.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
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
/*
* Dropper v1.0.1 - 2015-04-04
* A jQuery plugin for simple drag and drop uploads. Part of the Formstone Library.
* http://classic.formstone.it/dropper/
*
* Copyright 2015 Ben Plum; MIT Licensed
*/
;(function ($, window) {
"use strict";
var supported = (window.File && window.FileReader && window.FileList);
/**
* @options
* @param action [string] "Where to submit uploads"
* @param label [string] <'Drag and drop files or click to select'> "Dropzone text"
* @param maxQueue [int] <2> "Number of files to simultaneously upload"
* @param maxSize [int] <5242880> "Max file size allowed"
* @param postData [object] "Extra data to post with upload"
* @param postKey [string] <'file'> "Key to upload file as"
*/
var options = {
action: "",
label: "Drag and drop files or click to select",
maxQueue: 2,
maxSize: 5242880, // 5 mb
postData: {},
postKey: "file"
};
/**
* @events
* @event start.dropper ""
* @event complete.dropper ""
* @event fileStart.dropper ""
* @event fileProgress.dropper ""
* @event fileComplete.dropper ""
* @event fileError.dropper ""
*/
var pub = {
/**
* @method
* @name defaults
* @description Sets default plugin options
* @param opts [object] <{}> "Options object"
* @example $.dropper("defaults", opts);
*/
defaults: function(opts) {
options = $.extend(options, opts || {});
return (typeof this === 'object') ? $(this) : true;
}
};
/**
* @method private
* @name _init
* @description Initializes plugin
* @param opts [object] "Initialization options"
*/
function _init(opts) {
var $items = $(this);
if (supported) {
// Settings
opts = $.extend({}, options, opts);
// Apply to each element
for (var i = 0, count = $items.length; i < count; i++) {
_build($items.eq(i), opts);
}
}
return $items;
}
/**
* @method private
* @name _build
* @description Builds each instance
* @param $nav [jQuery object] "Target jQuery object"
* @param opts [object] <{}> "Options object"
*/
function _build($dropper, opts) {
opts = $.extend({}, opts, $dropper.data("dropper-options"));
var html = "";
html += '<div class="dropper-dropzone">';
html += opts.label;
html += '</div>';
html += '<input class="dropper-input" type="file"';
if (opts.maxQueue > 1) {
html += ' multiple';
}
html += '>';
$dropper.addClass("dropper")
.append(html);
var data = $.extend({
$dropper: $dropper,
$input: $dropper.find(".dropper-input"),
queue: [],
total: 0,
uploading: false
}, opts);
$dropper.on("click.dropper", ".dropper-dropzone", data, _onClick)
.on("dragenter.dropper", data, _onDragEnter)
.on("dragover.dropper", data, _onDragOver)
.on("dragleave.dropper", data, _onDragOut)
.on("drop.dropper", ".dropper-dropzone", data, _onDrop)
.data("dropper", data);
data.$input.on("change.dropper", data, _onChange);
}
/**
* @method private
* @name _onClick
* @description Handles click to dropzone
* @param e [object] "Event data"
*/
function _onClick(e) {
e.stopPropagation();
e.preventDefault();
var data = e.data;
data.$input.trigger("click");
}
/**
* @method private
* @name _onChange
* @description Handles change to hidden input
* @param e [object] "Event data"
*/
function _onChange(e) {
e.stopPropagation();
e.preventDefault();
var data = e.data,
files = data.$input[0].files;
if (files.length) {
_handleUpload(data, files);
}
}
/**
* @method private
* @name _onDragEnter
* @description Handles dragenter to dropzone
* @param e [object] "Event data"
*/
function _onDragEnter(e) {
e.stopPropagation();
e.preventDefault();
var data = e.data;
data.$dropper.addClass("dropping");
}
/**
* @method private
* @name _onDragOver
* @description Handles dragover to dropzone
* @param e [object] "Event data"
*/
function _onDragOver(e) {
e.stopPropagation();
e.preventDefault();
var data = e.data;
data.$dropper.addClass("dropping");
}
/**
* @method private
* @name _onDragOut
* @description Handles dragout to dropzone
* @param e [object] "Event data"
*/
function _onDragOut(e) {
e.stopPropagation();
e.preventDefault();
var data = e.data;
data.$dropper.removeClass("dropping");
}
/**
* @method private
* @name _onDrop
* @description Handles drop to dropzone
* @param e [object] "Event data"
*/
function _onDrop(e) {
e.preventDefault();
var data = e.data,
files = e.originalEvent.dataTransfer.files;
data.$dropper.removeClass("dropping");
_handleUpload(data, files);
}
/**
* @method private
* @name _handleUpload
* @description Handles new files
* @param data [object] "Instance data"
* @param files [object] "File list"
*/
function _handleUpload(data, files) {
var newFiles = [];
for (var i = 0; i < files.length; i++) {
var file = {
index: data.total++,
file: files[i],
name: files[i].name,
size: files[i].size,
started: false,
complete: false,
error: false,
transfer: null
};
newFiles.push(file);
data.queue.push(file);
}
if (!data.uploading) {
$(window).on("beforeunload.dropper", function(){
return 'You have uploads pending, are you sure you want to leave this page?';
});
data.uploading = true;
}
data.$dropper.trigger("start.dropper", [ newFiles ]);
_checkQueue(data);
}
/**
* @method private
* @name _checkQueue
* @description Checks and updates file queue
* @param data [object] "Instance data"
*/
function _checkQueue(data) {
var transfering = 0,
newQueue = [];
// remove lingering items from queue
for (var i in data.queue) {
if (data.queue.hasOwnProperty(i) && !data.queue[i].complete && !data.queue[i].error) {
newQueue.push(data.queue[i]);
}
}
data.queue = newQueue;
for (var j in data.queue) {
if (data.queue.hasOwnProperty(j)) {
if (!data.queue[j].started) {
var formData = new FormData();
formData.append(data.postKey, data.queue[j].file);
for (var k in data.postData) {
if (data.postData.hasOwnProperty(k)) {
formData.append(k, data.postData[k]);
}
}
_uploadFile(data, data.queue[j], formData);
}
transfering++;
if (transfering >= data.maxQueue) {
return;
} else {
i++;
}
}
}
if (transfering === 0) {
$(window).off("beforeunload.dropper");
data.uploading = false;
data.$dropper.trigger("complete.dropper");
}
}
/**
* @method private
* @name _uploadFile
* @description Uploads file
* @param data [object] "Instance data"
* @param file [object] "Target file"
* @param formData [object] "Target form"
*/
function _uploadFile(data, file, formData) {
if (file.size >= data.maxSize) {
file.error = true;
data.$dropper.trigger("fileError.dropper", [ file, "Too large" ]);
_checkQueue(data);
} else {
file.started = true;
file.transfer = $.ajax({
url: data.action,
data: formData,
type: "POST",
contentType:false,
processData: false,
cache: false,
xhr: function() {
var $xhr = $.ajaxSettings.xhr();
if ($xhr.upload) {
$xhr.upload.addEventListener("progress", function(e) {
var percent = 0,
position = e.loaded || e.position,
total = e.total;
if (e.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
data.$dropper.trigger("fileProgress.dropper", [ file, percent ]);
}, false);
}
return $xhr;
},
beforeSend: function(e) {
data.$dropper.trigger("fileStart.dropper", [ file ]);
},
success: function(response, status, jqXHR) {
file.complete = true;
data.$dropper.trigger("fileComplete.dropper", [ file, response ]);
_checkQueue(data);
},
error: function(jqXHR, status, error) {
file.error = true;
data.$dropper.trigger("fileError.dropper", [ file, error ]);
_checkQueue(data);
}
});
}
}
$.fn.dropper = function(method) {
if (pub[method]) {
return pub[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return _init.apply(this, arguments);
}
return this;
};
$.dropper = function(method) {
if (method === "defaults") {
pub.defaults.apply(this, Array.prototype.slice.call(arguments, 1));
}
};
})(jQuery, window);