Permalink
Browse files

Merge pull request #40 from hqmq/master

fixes issue #25 - multiple instances of filedrop
  • Loading branch information...
2 parents 9ffd990 + 54be5c6 commit f7585764e3e6f1e149c9c854f3dbe8bb5a43c231 @weixiyen weixiyen committed Feb 22, 2012
Showing with 117 additions and 157 deletions.
  1. +117 −157 jquery.filedrop.js
View
274 jquery.filedrop.js 100644 → 100755
@@ -24,96 +24,57 @@
*
*/
;(function($) {
- jQuery.event.props.push("dataTransfer");
- var opts = {},
- default_opts = {
- fallback_id: '',
- url: '',
- refresh: 1000,
- paramname: 'userfile',
- maxfiles: 25, // Ignored if queuefiles is set > 0
- maxfilesize: 1, // MB file size limit
- queuefiles: 0, // Max files before queueing (for large volume uploads)
- queuewait: 200, // Queue wait time if full
- data: {},
- headers: {},
- drop: empty,
- dragEnter: empty,
- dragOver: empty,
- dragLeave: empty,
- docEnter: empty,
- docOver: empty,
- docLeave: empty,
- beforeEach: empty,
- afterAll: empty,
- rename: empty,
- error: function(err, file, i) {
- alert(err);
- },
- uploadStarted: empty,
- uploadFinished: empty,
- progressUpdated: empty,
- speedUpdated: empty
- },
- errors = ["BrowserNotSupported", "TooManyFiles", "FileTooLarge"],
- doc_leave_timer,
- stop_loop = false,
- files_count = 0,
- files;
-
- function empty() {}
+ jQuery.event.props.push("dataTransfer");
- $.fn.filedrop = function(method) {
- var methods = {
- init : function(options) {
- // Already initialized
- if($(this).data('filedrop') != undefined || $(this).data('filedrop') != null)
- return false;
-
- var tb = new filedropInstance(this, options);
- tb.init();
- $(this).data('filedrop',tb);
+ var default_opts = {
+ fallback_id: '',
+ url: '',
+ refresh: 1000,
+ paramname: 'userfile',
+ maxfiles: 25, // Ignored if queuefiles is set > 0
+ maxfilesize: 1, // MB file size limit
+ queuefiles: 0, // Max files before queueing (for large volume uploads)
+ queuewait: 200, // Queue wait time if full
+ data: {},
+ headers: {},
+ drop: empty,
+ dragEnter: empty,
+ dragOver: empty,
+ dragLeave: empty,
+ docEnter: empty,
+ docOver: empty,
+ docLeave: empty,
+ beforeEach: empty,
+ afterAll: empty,
+ rename: empty,
+ error: function(err, file, i) {
+ alert(err);
},
-
- destroy : function( ) {
- $(this).data('filedrop').destroy();
- $(this).data('filedrop',null);
- }
- };
+ uploadStarted: empty,
+ uploadFinished: empty,
+ progressUpdated: empty,
+ speedUpdated: empty
+ },
+ errors = ["BrowserNotSupported", "TooManyFiles", "FileTooLarge"],
+ doc_leave_timer, stop_loop = false,
+ files_count = 0,
+ files;
+
+ $.fn.filedrop = function(options) {
+ var opts = $.extend({}, default_opts, options);
+
+ this.bind('drop', drop).bind('dragenter', dragEnter).bind('dragover', dragOver).bind('dragleave', dragLeave);
+ $(document).bind('drop', docDrop).bind('dragenter', docEnter).bind('dragover', docOver).bind('dragleave', docLeave);
+
+ $('#' + opts.fallback_id).change(function(e) {
+ opts.drop(e);
+ files = e.target.files;
+ files_count = files.length;
+ upload();
+ });
- if ( methods[method] ) {
- return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
- } else if ( typeof method === 'object' || ! method ) {
- return methods.init.apply( this, arguments );
- } else {
- $.error( 'Method ' + method + ' does not exist on jQuery.filedrop' );
- }
-
-
- function filedropInstance(element, options) {
-
- this.init = function() {
- opts = $.extend({}, default_opts, options);
-
- element.bind('drop.filedrop', drop).bind('dragenter.filedrop', dragEnter).bind('dragover.filedrop', dragOver).bind('dragleave.filedrop', dragLeave);
- $(document).bind('drop.filedrop', docDrop).bind('dragenter.filedrop', docEnter).bind('dragover.filedrop', docOver).bind('dragleave.filedrop', docLeave);
-
- $('#' + opts.fallback_id).bind('change.filedrop', function(e) {
- opts.drop(e);
- files = e.target.files;
- files_count = files.length;
- upload();
- });
- };
-
- this.destroy = function() {
- element.unbind('.filedrop');
- $(document).unbind('.filedrop');
- $('#' + opts.fallback_id).unbind('.filedrop');
- };
-
- function drop(e) {
+ function drop(e) {
opts.drop(e);
files = e.dataTransfer.files;
if (files === null || files === undefined) {
@@ -230,61 +191,61 @@
// Process an upload, recursive
var process = function() {
- var fileIndex;
-
- if (stop_loop) return false;
-
- // Check to see if are in queue mode
- if (opts.queuefiles > 0 && processingQueue.length >= opts.queuefiles) {
-
- return pause(opts.queuewait);
-
- } else {
-
- // Take first thing off work queue
- fileIndex = workQueue[0];
- workQueue.splice(0, 1);
-
- // Add to processing queue
- processingQueue.push(fileIndex);
-
- }
-
- try {
- if (beforeEach(files[fileIndex]) != false) {
- if (fileIndex === files_count) return;
- var reader = new FileReader(),
- max_file_size = 1048576 * opts.maxfilesize;
-
- reader.index = fileIndex;
- if (files[fileIndex].size > max_file_size) {
- opts.error(errors[2], files[fileIndex], fileIndex);
- // Remove from queue
- processingQueue.forEach(function(value, key) {
- if (value === fileIndex) processingQueue.splice(key, 1);
- });
- filesRejected++;
- return true;
- }
- reader.onloadend = send;
- reader.readAsBinaryString(files[fileIndex]);
-
- } else {
- filesRejected++;
- }
- } catch (err) {
- // Remove from queue
- processingQueue.forEach(function(value, key) {
- if (value === fileIndex) processingQueue.splice(key, 1);
- });
- opts.error(errors[0]);
- return false;
- }
-
- // If we still have work to do,
- if (workQueue.length > 0) {
- process();
- }
+ var fileIndex;
+
+ if (stop_loop) return false;
+
+ // Check to see if are in queue mode
+ if (opts.queuefiles > 0 && processingQueue.length >= opts.queuefiles) {
+
+ return pause(opts.queuewait);
+
+ } else {
+
+ // Take first thing off work queue
+ fileIndex = workQueue[0];
+ workQueue.splice(0, 1);
+
+ // Add to processing queue
+ processingQueue.push(fileIndex);
+
+ }
+
+ try {
+ if (beforeEach(files[fileIndex]) != false) {
+ if (fileIndex === files_count) return;
+ var reader = new FileReader(),
+ max_file_size = 1048576 * opts.maxfilesize;
+
+ reader.index = fileIndex;
+ if (files[fileIndex].size > max_file_size) {
+ opts.error(errors[2], files[fileIndex], fileIndex);
+ // Remove from queue
+ processingQueue.forEach(function(value, key) {
+ if (value === fileIndex) processingQueue.splice(key, 1);
+ });
+ filesRejected++;
+ return true;
+ }
+ reader.onloadend = send;
+ reader.readAsBinaryString(files[fileIndex]);
+
+ } else {
+ filesRejected++;
+ }
+ } catch (err) {
+ // Remove from queue
+ processingQueue.forEach(function(value, key) {
+ if (value === fileIndex) processingQueue.splice(key, 1);
+ });
+ opts.error(errors[0]);
+ return false;
+ }
+
+ // If we still have work to do,
+ if (workQueue.length > 0) {
+ process();
+ }
};
@@ -324,7 +285,6 @@
xhr.open("POST", opts.url, true);
xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + boundary);
- xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
// Add headers
$.each(opts.headers, function(k, v) {
@@ -430,19 +390,19 @@
opts.docLeave(e);
}, 200);
}
+ };
+ function empty() {}
- try {
- if (XMLHttpRequest.prototype.sendAsBinary) return;
- XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
- function byteValue(x) {
- return x.charCodeAt(0) & 0xff;
- }
- var ords = Array.prototype.map.call(datastr, byteValue);
- var ui8a = new Uint8Array(ords);
- this.send(ui8a.buffer);
- }
- } catch (e) {}
-
- } // End of fileDropInstance()
- }; // End of $.fn.filedrop()
-})(jQuery);
+ try {
+ if (XMLHttpRequest.prototype.sendAsBinary) return;
+ XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
+ function byteValue(x) {
+ return x.charCodeAt(0) & 0xff;
+ }
+ var ords = Array.prototype.map.call(datastr, byteValue);
+ var ui8a = new Uint8Array(ords);
+ this.send(ui8a.buffer);
+ }
+ } catch (e) {}
+
+})(jQuery);

0 comments on commit f758576

Please sign in to comment.