-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
82 lines (66 loc) · 1.42 KB
/
index.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
/**
* Module dependencies.
*/
var drop = require('drop');
var events = require('events');
var classes = require('classes');
/**
* Expose `DropAnywhere`.
*/
module.exports = DropAnywhere;
/**
* Make the document droppable and invoke `fn(err, upload)`.
*
* @param {Function} fn
* @api public
*/
function DropAnywhere(fn) {
if (!(this instanceof DropAnywhere)) return new DropAnywhere(fn);
this.callback = fn;
this.el = document.createElement('div');
this.el.id = 'drop-anywhere';
this.events = events(this.el, this);
this.classes = classes(this.el);
this.winEvents = events(window, this);
this.events.bind('click', 'hide');
this.events.bind('drop', 'hide');
this.events.bind('dragleave', 'hide');
this.winEvents.bind('dragenter', 'show');
this.drop = drop(this.el, this.callback);
this.add();
}
/**
* Add the element.
*/
DropAnywhere.prototype.add = function(){
document.body.appendChild(this.el);
};
/**
* Remove the element.
*/
DropAnywhere.prototype.remove = function(){
document.body.removeChild(this.el);
};
/**
* Show the dropzone.
*/
DropAnywhere.prototype.show = function(){
this.classes.add('show');
};
/**
* Hide the dropzone.
*/
DropAnywhere.prototype.hide = function(){
this.classes.remove('show');
};
/**
* Unbind.
*
* @api public
*/
DropAnywhere.prototype.unbind = function(){
this.remove();
this.winEvents.unbind();
this.events.unbind();
this.drop.unbind();
};