Skip to content

Instantly share code, notes, and snippets.

@bearice
Created October 19, 2011 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bearice/1297390 to your computer and use it in GitHub Desktop.
Save bearice/1297390 to your computer and use it in GitHub Desktop.
jQuery plugin for <del>Dungeon and Dragons</del> Drag and Drop Event
/**
* jQuery plugin for <del>Dungeon and Dragons</del> Drag and Drop Event
*/
(function($){
$.fn.dragAndDropFiles = function(options){
if(typeof(window.FileReader)=== 'undefined')return;
var defaults = {
MIME : ["image/png","image/jpeg","image/bmp"],
//accept = function(e){},
//cancel = function(e){},
//data = function(e){},
//hoverClass = 'drag-hover',
};
options = $.extend(defaults, options);
this.bind('dragenter dragover',function(e){
e.preventDefault();
if(e.type=='dragover'){
return;
}
if(options.hoverClass){
$(this).addClass(options.hoverClass);
}
if(options.accept){
options.accept.call(this,e);
}
}).bind('dragleave dragend',function(e){
e.preventDefault();
//console.info(e);
if(options.hoverClass){
$(this).removeClass(options.hoverClass);
}
if(options.cancel){
options.cancel.call(this,e);
}
}).bind('drop',function(e){
var thiz = this
e.preventDefault();
//console.info(e);
if(options.hoverClass){
$(thiz).removeClass(options.hoverClass);
}
var file = e.originalEvent.dataTransfer.files[0];
console.log($.inArray(file.type,options.MIME));
if($.inArray(file.type,options.MIME)==-1){
return;
}
console.info(file);
var reader = new FileReader();
reader.onload = function (e) {
console.info(e);
if(options.data){
options.data.call(thiz,e);
}
};
reader.readAsDataURL(file);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment