Skip to content

Instantly share code, notes, and snippets.

@restlessmedia
Last active August 29, 2015 14:04
Show Gist options
  • Save restlessmedia/2317b8337aa20b7cec97 to your computer and use it in GitHub Desktop.
Save restlessmedia/2317b8337aa20b7cec97 to your computer and use it in GitHub Desktop.
DropZone - jQuery drag and drop wrapper
// var upload = function (url, files) {
// var data = new FormData();
// app.utils.forEach(files, function () {
// data.append('file', this);
// });
// return send({ type: 'POST', url: url, data: data, contentType: false, processData: false });
// }
(function ($, app) {
var addHandler = function (element, type, handler) {
$(element).on(type, function (e) {
e.stopPropagation();
e.preventDefault();
handler.call(this, this, e);
});
};
var DropZone = function (element, dropped) {
if (typeof element === 'string') {
element = document.getElementById(element);
}
this.element = element;
if (dropped) {
this.drop(dropped);
}
};
DropZone.prototype.enter = function (handler) {
addHandler(this.element, 'dragenter', handler);
};
DropZone.prototype.over = function (handler) {
addHandler(this.element, 'dragover', handler);
};
DropZone.prototype.drop = function (handler) {
addHandler(this.element, 'drop', handler);
};
app.dropZone = {
attach: function (element, dropped) {
return new DropZone(element, dropped);
}
}
} ($, app));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment