On va essayer d'afficher le résultat de la XHR.
[Portfolio.git] / skins / fileupload.js
1 // © 2013 Benoît Pin MINES ParisTech
2 var DDFileUploader;
3
4 (function(){
5
6 DDFileUploader = function(dropbox, uploadUrl) {
7 this.dropbox = dropbox;
8 this.uploadUrl = uploadUrl;
9 this.slideSize = 222;
10 this.progressBarMaxSize = 200; // pixels
11 this.thumbnailSize = 180;
12 this.previewQueue = [];
13 this._previewQueueRunning = false;
14 this.uploadQueue = [];
15 this._uploadQueueRunning = false;
16 var self = this;
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);});
20 };
21
22 // Drag and drop
23 DDFileUploader.prototype.dragenter = function(evt) {
24 disableDefault(evt);
25 disablePropagation(evt);
26 };
27
28 DDFileUploader.prototype.dragover = function(evt) {
29 disableDefault(evt);
30 disablePropagation(evt);
31 evt = getEventObject(evt);
32 var dt = evt.dataTransfer;
33 dt.dropEffect = 'copy';
34 };
35
36 DDFileUploader.prototype.drop = function(evt) {
37 disableDefault(evt);
38 disablePropagation(evt);
39 getEventObject(evt);
40 var dt = evt.dataTransfer;
41 dt.dropEffect = 'copy';
42 this.handleFiles(dt.files);
43 };
44
45 // Methods about upload
46 DDFileUploader.prototype.handleFiles = function(files) {
47 var file, i, slide;
48 for (i = 0; i < files.length; i++) {
49 file = files[i];
50 slide = this.createSlide(file);
51 // this.previewQueuePush(slide);
52 this.uploadQueuePush(slide);
53 }
54 };
55
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;
63 var self = this;
64
65 addListener(req.upload, 'progress', function(evt){self.progressHandler(evt);});
66 addListener(req.upload, 'load', function(evt){self.uploadCompleteHandler(evt);});
67
68 req.open("PUT", this.uploadUrl);
69 req.setRequestHeader("Content-Type", file.type);
70 req.setRequestHeader("X-File-Name", file.name);
71 addListener(reader, 'load',
72 function(evt){
73 try {
74 req.sendAsBinary(evt.target.result);
75 }
76 catch(e){}
77 });
78 reader.readAsBinaryString(file);
79 };
80
81 DDFileUploader.prototype.uploadCompleteHandler = function(evt) {
82 var slide = this.uploadedSlide;
83 this.uploadedSlide.removeChild(slide.label);
84 this.uploadedSlide.removeChild(slide.progressBar);
85 this.uploadQueueLoadNext();
86 var req = getTargetedObject(evt);
87 console.log(req);
88 // this.slide.innerHTML = req.responseXML.documentElement
89 };
90
91 DDFileUploader.prototype.progressHandler = function(evt) {
92 if (evt.lengthComputable) {
93 var progress = evt.loaded / evt.total;
94 this.updateProgressBar(progress);
95 var currentOpacity = this.previewImg.style.opacity;
96 this.previewImg.style.opacity = Math.max(currentOpacity, progress);
97 }
98 };
99
100 // Method about queues
101
102 DDFileUploader.prototype.previewQueuePush = function(slide) {
103 this.previewQueue.push(slide);
104 if (!this._previewQueueRunning) {
105 this.startPreviewQueue();
106 }
107 };
108
109 DDFileUploader.prototype.startPreviewQueue = function() {
110 this._previewQueueRunning = true;
111 this.previewQueueLoadNext();
112 };
113
114 DDFileUploader.prototype.previewQueueLoadNext = function() {
115 var slide = this.previewQueue.shift();
116 if (slide) {
117 this.previewUploadedImage(slide);
118 }
119 else {
120 this._previewQueueRunning = false;
121 }
122 };
123
124 DDFileUploader.prototype.uploadQueuePush = function(slide) {
125 this.uploadQueue.push(slide);
126 if (!this._uploadQueueRunning) {
127 this.startUploadQueue();
128 }
129 };
130
131 DDFileUploader.prototype.startUploadQueue = function() {
132 this._uploadQueueRunning = true;
133 this.uploadQueueLoadNext();
134 };
135
136
137 DDFileUploader.prototype.uploadQueueLoadNext = function() {
138 var slide = this.uploadQueue.shift();
139 if (slide) {
140 this.upload(slide);
141 }
142 else {
143 this._uploadQueueRunning = false;
144 }
145 };
146
147
148 // User interface
149 DDFileUploader.prototype.createSlide = function(file) {
150 var slide = document.createElement('span');
151 slide.file = file;
152
153 var a = document.createElement('a');
154 a.href = '#';
155 a.className = 'slide';
156
157 var img = document.createElement('img');
158 img.className = 'hidden';
159 var size = this.thumbnailSize;
160 var self = this;
161 img.onload = function(evt) {
162 if (img.width > img.height) { // landscape
163 img.height = Math.round(size * img.height / img.width);
164 img.width = size;
165 }
166 else {
167 img.width = Math.round(size * img.width / img.height);
168 img.height = size;
169 }
170 img.style.marginLeft = Math.round((self.slideSize - img.width) / 2) + 'px';
171 img.style.marginTop = Math.round((self.slideSize - img.height) / 2) + 'px';
172 img.style.opacity = 0.2;
173 img.className = undefined;
174 };
175 a.appendChild(img);
176 slide.img = img;
177
178 var label = document.createElement('span');
179 slide.label = label;
180 label.className = 'label';
181 label.innerHTML = file.name;
182
183 var progressBar = document.createElement('span');
184 progressBar.className = 'upload-progress';
185 slide.progressBar = progressBar;
186
187 slide.appendChild(a);
188 slide.appendChild(progressBar);
189 slide.appendChild(label);
190 this.dropbox.appendChild(slide);
191
192 return slide;
193 };
194
195 DDFileUploader.prototype.updateProgressBar = function(progress) {
196 // 0 <= progress <= 1
197 var size = this.progressBarMaxSize * progress;
198 size = Math.round(size);
199 this.progressBar.style.width = size + 'px';
200 };
201
202 DDFileUploader.prototype.previewUploadedImage = function(slide) {
203 var reader = new FileReader();
204 var size = this.thumbnailSize;
205 var self = this;
206
207 reader.onload = function(evt) {
208 slide.img.src = evt.target.result;
209 setTimeout(function(){self.previewQueueLoadNext();}, 500);
210 };
211 reader.readAsDataURL(slide.file);
212 };
213
214 }());