a98dda55fac2b3c6ba6e043aca990b40a7c39a67
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.
45 DDFileUploaderBase
.prototype.upload = function(slide
) {
46 var reader
= new FileReader();
47 var req
= new XMLHttpRequest();
48 var file
= slide
.file
;
49 this.uploadedSlide
= slide
;
50 this.previewImg
= slide
.img
;
51 this.progressBar
= slide
.progressBar
;
54 addListener(req
.upload
, 'progress', function(evt
){self
.progressHandler(evt
);});
55 addListener(req
, 'readystatechange',
57 if (req
.readyState
=== 4) {
58 self
.uploadCompleteHandler(req
);
62 req
.open("PUT", this.uploadUrl
);
63 req
.setRequestHeader("Content-Type", file
.type
);
64 req
.setRequestHeader("X-File-Name", file
.name
);
65 addListener(reader
, 'load',
68 req
.sendAsBinary(evt
.target
.result
);
72 reader
.readAsBinaryString(file
);
75 DDFileUploaderBase
.prototype.uploadCompleteHandler = function(req
) {
76 var slide
= this.uploadedSlide
;
77 this.uploadedSlide
.removeChild(slide
.label
);
78 this.uploadedSlide
.removeChild(slide
.progressBar
);
79 var fragment
= getCopyOfNode(req
.responseXML
.documentElement
.firstChild
);
80 var img
= fragment
.getElementsByTagName('img')[0];
81 if (req
.status
=== 200) {
83 var existing
= this.existingSlides
[img
.src
];
85 existing
.src
= existing
.src
+ '?' + Math
.random().toString();
88 slide
.img
.parentNode
.removeChild(slide
.img
);
89 slide
.img
= undefined;
90 slide
.parentNode
.removeChild(slide
);
92 else if(req
.status
=== 201) {
94 img
.onload = function(evt
) {
95 // accelerate GC before replacing
97 slide
.img
.parentNode
.removeChild(slide
.img
);
98 slide
.img
= undefined;
99 slide
.parentNode
.replaceChild(fragment
, slide
);
102 this.previewsLoaded
--;
103 this.previewQueueLoadNext();
104 this.uploadQueueLoadNext();
107 DDFileUploaderBase
.prototype.progressHandler = function(evt
) {
108 if (evt
.lengthComputable
) {
109 var progress
= evt
.loaded
/ evt
.total
;
110 this.updateProgressBar(progress
);
111 var currentOpacity
= this.previewImg
.style
.opacity
;
112 this.previewImg
.style
.opacity
= Math
.max(currentOpacity
, progress
);
116 // Methods about queue
117 DDFileUploaderBase
.prototype.uploadQueuePush = function(slide
) {
118 this.uploadQueue
.push(slide
);
119 if (!this._uploadQueueRunning
) {
120 this.startUploadQueue();
124 DDFileUploaderBase
.prototype.startUploadQueue = function() {
125 this._uploadQueueRunning
= true;
126 this.uploadQueueLoadNext();
129 DDFileUploaderBase
.prototype.uploadQueueLoadNext = function() {
130 var slide
= this.uploadQueue
.shift();
135 this._uploadQueueRunning
= false;