980fb4441be02ac7fe279acf499e8e8ac3dd237c
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.previewImg
= slide
.img
;
61 this.progressBar
= slide
.progressBar
;
64 addListener(req
.upload
, 'progress', function(evt
){self
.progressHandler(evt
);});
65 addListener(req
.upload
, 'load', function(evt
){self
.uploadCompleteHandler(evt
);});
67 req
.open("PUT", this.uploadUrl
+ '/' + file
.name
);
68 req
.setRequestHeader("Content-Type", file
.type
);
69 addListener(reader
, 'load', function(evt
){req
.sendAsBinary(evt
.target
.result
);});
70 reader
.readAsBinaryString(file
);
73 DDFileUploader
.prototype.uploadCompleteHandler = function(evt
) {
74 this.progressBar
.parentNode
.removeChild(this.progressBar
);
75 this.uploadQueueLoadNext();
78 DDFileUploader
.prototype.progressHandler = function(evt
) {
79 if (evt
.lengthComputable
) {
80 var progress
= evt
.loaded
/ evt
.total
;
81 this.updateProgressBar(progress
);
82 var currentOpacity
= this.previewImg
.style
.opacity
;
83 this.previewImg
.style
.opacity
= Math
.max(currentOpacity
, progress
);
87 // Method about queues
89 DDFileUploader
.prototype.previewQueuePush = function(slide
) {
90 this.previewQueue
.push(slide
);
91 if (!this._previewQueueRunning
) {
92 this.startPreviewQueue();
96 DDFileUploader
.prototype.startPreviewQueue = function() {
97 this._previewQueueRunning
= true;
98 this.previewQueueLoadNext();
101 DDFileUploader
.prototype.previewQueueLoadNext = function() {
102 var slide
= this.previewQueue
.shift();
104 this.previewUploadedImage(slide
);
107 this._previewQueueRunning
= false;
111 DDFileUploader
.prototype.uploadQueuePush = function(slide
) {
112 this.uploadQueue
.push(slide
);
113 if (!this._uploadQueueRunning
) {
114 this.startUploadQueue();
118 DDFileUploader
.prototype.startUploadQueue = function() {
119 this._uploadQueueRunning
= true;
120 this.uploadQueueLoadNext();
124 DDFileUploader
.prototype.uploadQueueLoadNext = function() {
125 var slide
= this.uploadQueue
.shift();
130 this._uploadQueueRunning
= false;
136 DDFileUploader
.prototype.createSlide = function(file
) {
137 var slide
= document
.createElement('span');
140 var a
= document
.createElement('a');
142 a
.className
= 'slide';
144 var img
= document
.createElement('img');
145 img
.className
= 'hidden';
146 var size
= this.thumbnailSize
;
148 img
.onload = function(evt
) {
149 if (img
.width
> img
.height
) { // landscape
150 img
.height
= Math
.round(size
* img
.height
/ img
.width
);
154 img
.width
= Math
.round(size
* img
.width
/ img
.height
);
157 img
.style
.marginLeft
= Math
.round((self
.slideSize
- img
.width
) / 2) + 'px';
158 img
.style
.marginTop
= Math
.round((self
.slideSize
- img
.height
) / 2) + 'px';
159 img
.style
.opacity
= 0.2;
160 img
.className
= undefined;
165 var progressBar
= document
.createElement('span');
166 progressBar
.className
= 'upload-progress';
167 slide
.progressBar
= progressBar
;
169 slide
.appendChild(a
);
170 slide
.appendChild(progressBar
);
171 this.dropbox
.appendChild(slide
);
176 DDFileUploader
.prototype.updateProgressBar = function(progress
) {
177 // 0 <= progress <= 1
178 var size
= this.progressBarMaxSize
* progress
;
179 size
= Math
.round(size
);
180 this.progressBar
.style
.width
= size
+ 'px';
183 DDFileUploader
.prototype.previewUploadedImage = function(slide
) {
184 var reader
= new FileReader();
185 var size
= this.thumbnailSize
;
188 reader
.onload = function(evt
) {
189 slide
.img
.src
= evt
.target
.result
;
190 setTimeout(function(){self
.previewQueueLoadNext();}, 500);
192 reader
.readAsDataURL(slide
.file
);