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