8e09907dcde5b2541e1ff4c3d6abd2623dae48cd
1 // © 2013 Benoît Pin MINES ParisTech
6 DDFileUploader = function(dropbox
, uploadUrl
) {
7 this.dropbox
= dropbox
;
8 this.uploadUrl
= uploadUrl
;
10 this.progressBarMaxSize
= 200; // pixels
11 this.thumbnailSize
= 180;
12 this.previewQueue
= [];
13 this._previewQueueRunning
= false;
14 this.uploadQueue
= [];
15 this._uploadQueueRunning
= false;
17 addListener(dropbox
, 'dragenter', function(evt
){self
.dragenter(evt
);});
18 addListener(dropbox
, 'dragover', function(evt
){self
.dragover(evt
);});
19 addListener(dropbox
, 'drop', function(evt
){self
.drop(evt
);});
23 DDFileUploader
.prototype.dragenter = function(evt
) {
25 disablePropagation(evt
);
28 DDFileUploader
.prototype.dragover = function(evt
) {
30 disablePropagation(evt
);
31 evt
= getEventObject(evt
);
32 var dt
= evt
.dataTransfer
;
33 dt
.dropEffect
= 'copy';
36 DDFileUploader
.prototype.drop = function(evt
) {
38 disablePropagation(evt
);
40 var dt
= evt
.dataTransfer
;
41 dt
.dropEffect
= 'copy';
42 this.handleFiles(dt
.files
);
45 // Methods about upload
46 DDFileUploader
.prototype.handleFiles = function(files
) {
48 for (i
= 0; i
< files
.length
; i
++) {
50 slide
= this.createSlide(file
);
51 this.previewQueuePush(slide
);
52 this.uploadQueuePush(slide
);
56 DDFileUploader
.prototype.upload = function(slide
) {
57 var reader
= new FileReader();
58 var req
= new XMLHttpRequest();
59 var file
= slide
.file
;
60 this.uploadedSlide
= slide
;
61 this.previewImg
= slide
.img
;
62 this.progressBar
= slide
.progressBar
;
65 addListener(req
.upload
, 'progress', function(evt
){self
.progressHandler(evt
);});
66 addListener(req
.upload
, 'load', function(evt
){self
.uploadCompleteHandler(evt
);});
68 req
.open("PUT", this.uploadUrl
+ '/' + file
.name
);
69 req
.setRequestHeader("Content-Type", file
.type
);
70 addListener(reader
, 'load',
74 req
.sendAsBinary(evt
.target
.result
);
78 reader
.readAsBinaryString(file
);
81 DDFileUploader
.prototype.uploadCompleteHandler = function(evt
) {
82 var slide
= this.uploadedSlide
;
83 this.uploadedSlide
.removeChild(slide
.label
);
84 this.uploadedSlide
.removeChild(slide
.label
);
85 this.uploadQueueLoadNext();
88 DDFileUploader
.prototype.progressHandler = function(evt
) {
89 if (evt
.lengthComputable
) {
90 var progress
= evt
.loaded
/ evt
.total
;
91 this.updateProgressBar(progress
);
92 var currentOpacity
= this.previewImg
.style
.opacity
;
93 this.previewImg
.style
.opacity
= Math
.max(currentOpacity
, progress
);
97 // Method about queues
99 DDFileUploader
.prototype.previewQueuePush = function(slide
) {
100 this.previewQueue
.push(slide
);
101 if (!this._previewQueueRunning
) {
102 this.startPreviewQueue();
106 DDFileUploader
.prototype.startPreviewQueue = function() {
107 this._previewQueueRunning
= true;
108 this.previewQueueLoadNext();
111 DDFileUploader
.prototype.previewQueueLoadNext = function() {
112 var slide
= this.previewQueue
.shift();
114 this.previewUploadedImage(slide
);
117 this._previewQueueRunning
= false;
121 DDFileUploader
.prototype.uploadQueuePush = function(slide
) {
122 this.uploadQueue
.push(slide
);
123 if (!this._uploadQueueRunning
) {
124 this.startUploadQueue();
128 DDFileUploader
.prototype.startUploadQueue = function() {
129 this._uploadQueueRunning
= true;
130 this.uploadQueueLoadNext();
134 DDFileUploader
.prototype.uploadQueueLoadNext = function() {
135 var slide
= this.uploadQueue
.shift();
140 this._uploadQueueRunning
= false;
146 DDFileUploader
.prototype.createSlide = function(file
) {
147 var slide
= document
.createElement('span');
150 var a
= document
.createElement('a');
152 a
.className
= 'slide';
154 var img
= document
.createElement('img');
155 img
.className
= 'hidden';
156 var size
= this.thumbnailSize
;
158 img
.onload = function(evt
) {
159 if (img
.width
> img
.height
) { // landscape
160 img
.height
= Math
.round(size
* img
.height
/ img
.width
);
164 img
.width
= Math
.round(size
* img
.width
/ img
.height
);
167 img
.style
.marginLeft
= Math
.round((self
.slideSize
- img
.width
) / 2) + 'px';
168 img
.style
.marginTop
= Math
.round((self
.slideSize
- img
.height
) / 2) + 'px';
169 img
.style
.opacity
= 0.2;
170 img
.className
= undefined;
175 var label
= document
.createElement('span');
177 label
.className
= 'label';
178 label
.innerHTML
= file
.name
;
180 var progressBar
= document
.createElement('span');
181 progressBar
.className
= 'upload-progress';
182 slide
.progressBar
= progressBar
;
184 slide
.appendChild(a
);
185 slide
.appendChild(progressBar
);
186 slide
.appendChild(label
);
187 this.dropbox
.appendChild(slide
);
192 DDFileUploader
.prototype.updateProgressBar = function(progress
) {
193 // 0 <= progress <= 1
194 var size
= this.progressBarMaxSize
* progress
;
195 size
= Math
.round(size
);
196 this.progressBar
.style
.width
= size
+ 'px';
199 DDFileUploader
.prototype.previewUploadedImage = function(slide
) {
200 var reader
= new FileReader();
201 var size
= this.thumbnailSize
;
204 reader
.onload = function(evt
) {
205 slide
.img
.src
= evt
.target
.result
;
206 setTimeout(function(){self
.previewQueueLoadNext();}, 500);
208 reader
.readAsDataURL(slide
.file
);