6ee52d5d6540ea135865ba2e5be0027826c45924
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;
13 addListener(dropbox
, 'dragenter', function(evt
){self
.dragenter(evt
);});
14 addListener(dropbox
, 'dragover', function(evt
){self
.dragover(evt
);});
15 addListener(dropbox
, 'drop', function(evt
){self
.drop(evt
);});
18 DDFileUploader
.prototype.dragenter = function(evt
) {
20 disablePropagation(evt
);
23 DDFileUploader
.prototype.dragover = function(evt
) {
25 disablePropagation(evt
);
26 evt
= getEventObject(evt
);
27 var dt
= evt
.dataTransfer
;
28 dt
.dropEffect
= 'copy';
32 DDFileUploader
.prototype.drop = function(evt
) {
34 disablePropagation(evt
);
36 var dt
= evt
.dataTransfer
;
37 dt
.dropEffect
= 'copy';
38 this.handleFiles(dt
.files
);
41 DDFileUploader
.prototype.handleFiles = function(files
) {
43 for (i
= 0; i
< files
.length
; i
++) {
46 this.previewUploadedImage(file
);
52 DDFileUploader
.prototype.createSlide = function() {
53 var slide
= document
.createElement('span');
55 var a
= document
.createElement('a');
57 a
.className
= 'slide';
59 var img
= document
.createElement('img');
60 this.previewImg
= img
;
61 var size
= this.thumbnailSize
;
63 img
.onload = function(evt
) {
64 if (img
.width
> img
.height
) { // landscape
65 img
.height
= Math
.round(size
* img
.height
/ img
.width
);
69 img
.width
= Math
.round(size
* img
.width
/ img
.height
);
72 img
.style
.marginLeft
= Math
.round((self
.slideSize
- img
.width
) / 2) + 'px';
73 img
.style
.marginTop
= Math
.round((self
.slideSize
- img
.height
) / 2) + 'px';
74 img
.className
= undefined;
78 var progressBar
= document
.createElement('span');
79 progressBar
.className
= 'upload-progress';
82 slide
.appendChild(progressBar
);
83 this.progressBar
= progressBar
;
84 this.dropbox
.appendChild(slide
);
87 DDFileUploader
.prototype.updateProgressBar = function(progress
) {
89 var size
= this.progressBarMaxSize
* progress
;
90 size
= Math
.round(size
);
91 this.progressBar
.style
.width
= size
+ 'px';
95 DDFileUploader
.prototype.upload = function(file
) {
96 var reader
= new FileReader();
97 var req
= new XMLHttpRequest();
100 addListener(req
.upload
, 'progress', function(evt
){self
.progressHandler(evt
);});
101 addListener(req
.upload
, 'load', function(evt
){self
.uploadCompleteHandler(evt
);});
103 req
.open("PUT", this.uploadUrl
+ '/' + file
.name
);
104 req
.setRequestHeader("Content-Type", file
.type
);
105 addListener(reader
, 'load', function(evt
){req
.sendAsBinary(evt
.target
.result
);});
106 reader
.readAsBinaryString(file
);
109 DDFileUploader
.prototype.uploadCompleteHandler = function(evt
) {
110 this.progressBar
.parentNode
.removeChild(this.progressBar
);
113 DDFileUploader
.prototype.progressHandler = function(evt
) {
114 if (evt
.lengthComputable
) {
115 var progress
= evt
.loaded
/ evt
.total
;
116 this.updateProgressBar(progress
);
117 this.previewImg
.style
.opacity
=progress
;
121 DDFileUploader
.prototype.previewUploadedImage = function(file
) {
122 var reader
= new FileReader();
123 var img
= this.previewImg
;
124 var size
= this.thumbnailSize
;
126 img
.className
= 'hidden';
128 reader
.onload = function(evt
) {
129 img
.src
= evt
.target
.result
;
131 reader
.readAsDataURL(file
);