675ff13e8fd6be1142f43c10e65be8cc3fe3e0fa
1 // © 2013 Benoît Pin MINES ParisTech
2 var DDFileUploaderBase
;
6 DDFileUploaderBase = function(dropbox
, uploadUrl
) {
7 this.dropbox
= dropbox
;
8 this.uploadUrl
= uploadUrl
;
10 this._uploadQueueRunning
= false;
12 addListener(dropbox
, 'dragenter', function(evt
){self
.dragenter(evt
);});
13 addListener(dropbox
, 'dragover', function(evt
){self
.dragover(evt
);});
14 addListener(dropbox
, 'drop', function(evt
){self
.drop(evt
);});
18 DDFileUploaderBase
.prototype.dragenter = function(evt
) {
20 disablePropagation(evt
);
23 DDFileUploaderBase
.prototype.dragover = function(evt
) {
25 disablePropagation(evt
);
26 evt
= getEventObject(evt
);
27 var dt
= evt
.dataTransfer
;
28 dt
.dropEffect
= 'copy';
31 DDFileUploaderBase
.prototype.drop = function(evt
) {
33 disablePropagation(evt
);
35 var dt
= evt
.dataTransfer
;
36 dt
.dropEffect
= 'copy';
37 this.handleFiles(dt
.files
);
40 // Methods about upload
41 DDFileUploaderBase
.prototype.handleFiles = function(files
) {
42 // To be implemented by descendant.
47 DDFileUploaderBase
.prototype.beforeUpload = function(item
) {
48 // To be implemented by decendant.
52 DDFileUploaderBase
.prototype.upload = function(item
) {
53 // item.file must be the file to be uploaded
54 this.beforeUpload(item
);
55 var reader
= new FileReader();
56 var req
= new XMLHttpRequest();
61 addListener(req
.upload
, 'progress', function(evt
){self
.progressHandler(evt
);});
62 addListener(req
, 'readystatechange',
64 if (req
.readyState
=== 4) {
65 self
.uploadCompleteHandler(req
);
69 req
.open("PUT", this.uploadUrl
);
70 req
.setRequestHeader("Content-Type", file
.type
);
71 req
.setRequestHeader("X-File-Name", encodeURI(file
.name
));
72 addListener(reader
, 'load',
75 req
.sendAsBinary(evt
.target
.result
);
79 reader
.readAsBinaryString(file
);
83 DDFileUploaderBase
.prototype.uploadCompleteHandlerCB = function(req
) {
84 // To be implemented by descendant.
87 DDFileUploaderBase
.prototype.uploadCompleteHandler = function(req
) {
88 this.uploadCompleteHandlerCB(req
);
89 this.uploadQueueLoadNext();
92 DDFileUploaderBase
.prototype.progressHandlerCB = function(progress
) {
93 // To be implemented by descendant.
97 DDFileUploaderBase
.prototype.progressHandler = function(evt
) {
98 if (evt
.lengthComputable
) {
99 var progress
= evt
.loaded
/ evt
.total
;
100 this.progressHandlerCB(progress
);
104 // Methods about queue
105 DDFileUploaderBase
.prototype.uploadQueuePush = function(item
) {
106 this.uploadQueue
.push(item
);
107 if (!this._uploadQueueRunning
) {
108 this.startUploadQueue();
112 DDFileUploaderBase
.prototype.startUploadQueue = function() {
113 this._uploadQueueRunning
= true;
114 this.uploadQueueLoadNext();
117 DDFileUploaderBase
.prototype.uploadQueueLoadNext = function() {
118 var item
= this.uploadQueue
.shift();
123 this._uploadQueueRunning
= false;