Created
October 19, 2011 03:04
-
-
Save bearice/1297390 to your computer and use it in GitHub Desktop.
jQuery plugin for <del>Dungeon and Dragons</del> Drag and Drop Event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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