65a2984551cd1cc6cc76731ae589b1c98093752a
1 // © 2013 Benoît Pin MINES ParisTech
5 // nombre maximun d'image chargées en local
8 DDFileUploader = function(dropbox
, uploadUrl
) {
9 this.dropbox
= dropbox
;
10 this.uploadUrl
= uploadUrl
;
12 this.progressBarMaxSize
= 200; // pixels
13 this.thumbnailSize
= 180;
14 this.previewQueue
= [];
15 this._previewQueueRunning
= false;
16 this.previewsLoaded
= 0;
17 this.uploadQueue
= [];
18 this._uploadQueueRunning
= false;
20 addListener(dropbox
, 'dragenter', function(evt
){self
.dragenter(evt
);});
21 addListener(dropbox
, 'dragover', function(evt
){self
.dragover(evt
);});
22 addListener(dropbox
, 'drop', function(evt
){self
.drop(evt
);});
26 DDFileUploader
.prototype.dragenter = function(evt
) {
28 disablePropagation(evt
);
31 DDFileUploader
.prototype.dragover = function(evt
) {
33 disablePropagation(evt
);
34 evt
= getEventObject(evt
);
35 var dt
= evt
.dataTransfer
;
36 dt
.dropEffect
= 'copy';
39 DDFileUploader
.prototype.drop = function(evt
) {
41 disablePropagation(evt
);
43 var dt
= evt
.dataTransfer
;
44 dt
.dropEffect
= 'copy';
45 this.handleFiles(dt
.files
);
48 // Methods about upload
49 DDFileUploader
.prototype.handleFiles = function(files
) {
51 for (i
= 0; i
< files
.length
; i
++) {
53 slide
= this.createSlide(file
);
54 this.previewQueuePush(slide
);
55 this.uploadQueuePush(slide
);
59 DDFileUploader
.prototype.upload = function(slide
) {
60 var reader
= new FileReader();
61 var req
= new XMLHttpRequest();
62 var file
= slide
.file
;
63 this.uploadedSlide
= slide
;
64 this.previewImg
= slide
.img
;
65 this.progressBar
= slide
.progressBar
;
68 addListener(req
.upload
, 'progress', function(evt
){self
.progressHandler(evt
);});
69 addListener(req
, 'readystatechange',
71 if (req
.readyState
=== 4) {
72 self
.uploadCompleteHandler(req
);
76 req
.open("PUT", this.uploadUrl
);
77 req
.setRequestHeader("Content-Type", file
.type
);
78 req
.setRequestHeader("X-File-Name", file
.name
);
79 addListener(reader
, 'load',
82 req
.sendAsBinary(evt
.target
.result
);
86 reader
.readAsBinaryString(file
);
89 DDFileUploader
.prototype.uploadCompleteHandler = function(req
) {
90 var slide
= this.uploadedSlide
;
91 this.uploadedSlide
.removeChild(slide
.label
);
92 this.uploadedSlide
.removeChild(slide
.progressBar
);
93 slide
.innerHTML
= req
.responseXML
.documentElement
.firstChild
.data
;
94 this.previewsLoaded
--;
95 this.previewQueueLoadNext();
96 this.uploadQueueLoadNext();
99 DDFileUploader
.prototype.progressHandler = function(evt
) {
100 if (evt
.lengthComputable
) {
101 var progress
= evt
.loaded
/ evt
.total
;
102 this.updateProgressBar(progress
);
103 var currentOpacity
= this.previewImg
.style
.opacity
;
104 this.previewImg
.style
.opacity
= Math
.max(currentOpacity
, progress
);
108 // Method about queues
110 DDFileUploader
.prototype.previewQueuePush = function(slide
) {
111 this.previewQueue
.push(slide
);
112 if (!this._previewQueueRunning
) {
113 this.startPreviewQueue();
117 DDFileUploader
.prototype.startPreviewQueue = function() {
118 this._previewQueueRunning
= true;
119 this.previewQueueLoadNext();
122 DDFileUploader
.prototype.previewQueueLoadNext = function() {
123 if (this.previewQueue
.length
&& this.previewsLoaded
< MAX_PREVIEW
) {
124 var slide
= this.previewQueue
.shift();
125 console
.info('previewQueueLoadNext', this.previewsLoaded
, slide
.file
.name
);
126 this.previewUploadedImage(slide
);
127 this.previewsLoaded
++;
130 console
.warn('previewQueueLoadNext skipped', this.previewsLoaded
);
131 this._previewQueueRunning
= false;
135 DDFileUploader
.prototype.uploadQueuePush = function(slide
) {
136 this.uploadQueue
.push(slide
);
137 if (!this._uploadQueueRunning
) {
138 this.startUploadQueue();
142 DDFileUploader
.prototype.startUploadQueue = function() {
143 this._uploadQueueRunning
= true;
144 this.uploadQueueLoadNext();
148 DDFileUploader
.prototype.uploadQueueLoadNext = function() {
149 var slide
= this.uploadQueue
.shift();
154 this._uploadQueueRunning
= false;
160 DDFileUploader
.prototype.createSlide = function(file
) {
161 var slide
= document
.createElement('span');
164 var a
= document
.createElement('a');
166 a
.className
= 'slide';
168 var img
= document
.createElement('img');
169 img
.className
= 'hidden';
170 var size
= this.thumbnailSize
;
172 img
.onload = function(evt
) {
173 if (img
.width
> img
.height
) { // landscape
174 img
.height
= Math
.round(size
* img
.height
/ img
.width
);
178 img
.width
= Math
.round(size
* img
.width
/ img
.height
);
181 img
.style
.marginLeft
= Math
.round((self
.slideSize
- img
.width
) / 2) + 'px';
182 img
.style
.marginTop
= Math
.round((self
.slideSize
- img
.height
) / 2) + 'px';
183 img
.style
.opacity
= 0.2;
184 img
.className
= undefined;
189 var label
= document
.createElement('span');
191 label
.className
= 'label';
192 label
.innerHTML
= file
.name
;
194 var progressBar
= document
.createElement('span');
195 progressBar
.className
= 'upload-progress';
196 slide
.progressBar
= progressBar
;
198 slide
.appendChild(a
);
199 slide
.appendChild(progressBar
);
200 slide
.appendChild(label
);
201 this.dropbox
.appendChild(slide
);
206 DDFileUploader
.prototype.updateProgressBar = function(progress
) {
207 // 0 <= progress <= 1
208 var size
= this.progressBarMaxSize
* progress
;
209 size
= Math
.round(size
);
210 this.progressBar
.style
.width
= size
+ 'px';
213 DDFileUploader
.prototype.previewUploadedImage = function(slide
) {
214 var reader
= new FileReader();
215 var size
= this.thumbnailSize
;
218 reader
.onload = function(evt
) {
219 slide
.img
.src
= evt
.target
.result
;
220 setTimeout(function(){self
.previewQueueLoadNext();}, 500);
222 reader
.readAsDataURL(slide
.file
);