42616a93ec8cd9fe4d8dd145438548fee2feee66
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.onload = function(evt) {
107 // req.sendAsBinary(evt.target.result);
109 reader
.readAsBinaryString(file
);
112 DDFileUploader
.prototype.uploadCompleteHandler = function(evt
) {
113 this.progressBar
.parentNode
.removeChild(this.progressBar
);
116 DDFileUploader
.prototype.progressHandler = function(evt
) {
117 if (evt
.lengthComputable
) {
118 var progress
= evt
.loaded
/ evt
.total
;
119 this.updateProgressBar(progress
);
120 this.previewImg
.style
.opacity
=progress
;
124 DDFileUploader
.prototype.previewUploadedImage = function(file
) {
125 var reader
= new FileReader();
126 var img
= this.previewImg
;
127 var size
= this.thumbnailSize
;
129 img
.className
= 'hidden';
131 reader
.onload = function(evt
) {
132 img
.src
= evt
.target
.result
;
134 reader
.readAsDataURL(file
);