-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_drop.js
74 lines (74 loc) · 2.25 KB
/
_drop.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
// TODO: Remove event to desactivate it
var _drop = function(elem, proc, params){
function uploadObject(elem, proc, params){
if(typeof elem == 'function'){
proc = elem;
elem = document;
}
if(elem===undefined) this.elem = document;
else this.elem = elem;
this.files;
this.index = 0;
this.proc = proc;
this.params = params;
var self = this;
// TODO: replace on by delegate // retrive option by data
_dom(this.elem).on('dragover', function(e){
if(self.proc.dragover!==undefined) self.proc.dragover.bind(self, e)();
e.preventDefault(); e.stopPropagation();
return false;
});
_dom(this.elem).on('dragenter', function(e){
if(self.proc.dragenter!==undefined) self.proc.dragenter.bind(self, e)();
e.preventDefault(); e.stopPropagation();
return false;
});
_dom(this.elem).on('dragleave', function(e){
if(self.proc.dragleave!==undefined) self.proc.dragleave.bind(self, e)();
e.preventDefault(); e.stopPropagation();
return false;
});
_dom(this.elem).on('drop', function(e){
if(self.proc.drop!==undefined) self.proc.drop.bind(self, e)();
e.preventDefault(); e.stopPropagation();
if(e.dataTransfer){
if(e.dataTransfer.files.length){
e.preventDefault(); e.stopPropagation();
self.files = e.dataTransfer.files;
self.load();
}
} return false;
});
this.load = function(index){
if(index===undefined) index = 0;
var f = this.files[index];
var reader = new FileReader();
var folder = '';
reader.readAsDataURL(f);
reader.onerror = function(e){
if(self.proc.error!==undefined) self.proc.error.bind(self, e)();
else self.next();
};
reader.onprogress = function(e){
if(self.proc.progress!==undefined) self.proc.progress.bind(self, e)();
};
reader.onload = function(e){
e.strfile = e.target.result.split(',')[1];
if(self.proc.load!==undefined) self.proc.load.bind(self, e)();
else if(typeof proc == 'function') self.proc.bind(self, e)();
};
};
this.next = function(){
if(this.files.length>this.index+1){
this.index ++;
this.load(this.index);
}else{
this.index = 0;
this.files = undefined;
if(self.proc.end!==undefined) self.proc.end.bind(self)();
}
};
}
return new uploadObject(elem, proc, params);
};
_drop.onFunctions = [];