Utilisation de la nouvelle méthode Plinn.Folder.PlinnFolder.put_upload pour envoi...
[Plinn.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 console.info('load');
74 try {
75 req.sendAsBinary(evt.target.result);
76 }
77 catch(e){}
78 });
79 reader.readAsBinaryString(file);
80 };
81
82 DDFileUploader.prototype.uploadCompleteHandler = function(evt) {
83 var slide = this.uploadedSlide;
84 this.uploadedSlide.removeChild(slide.label);
85 this.uploadedSlide.removeChild(slide.progressBar);
86 this.uploadQueueLoadNext();
87 };
88
89 DDFileUploader.prototype.progressHandler = function(evt) {
90 if (evt.lengthComputable) {
91 var progress = evt.loaded / evt.total;
92 this.updateProgressBar(progress);
93 var currentOpacity = this.previewImg.style.opacity;
94 this.previewImg.style.opacity = Math.max(currentOpacity, progress);
95 }
96 };
97
98 // Method about queues
99
100 DDFileUploader.prototype.previewQueuePush = function(slide) {
101 this.previewQueue.push(slide);
102 if (!this._previewQueueRunning) {
103 this.startPreviewQueue();
104 }
105 };
106
107 DDFileUploader.prototype.startPreviewQueue = function() {
108 this._previewQueueRunning = true;
109 this.previewQueueLoadNext();
110 };
111
112 DDFileUploader.prototype.previewQueueLoadNext = function() {
113 var slide = this.previewQueue.shift();
114 if (slide) {
115 this.previewUploadedImage(slide);
116 }
117 else {
118 this._previewQueueRunning = false;
119 }
120 };
121
122 DDFileUploader.prototype.uploadQueuePush = function(slide) {
123 this.uploadQueue.push(slide);
124 if (!this._uploadQueueRunning) {
125 this.startUploadQueue();
126 }
127 };
128
129 DDFileUploader.prototype.startUploadQueue = function() {
130 this._uploadQueueRunning = true;
131 this.uploadQueueLoadNext();
132 };
133
134
135 DDFileUploader.prototype.uploadQueueLoadNext = function() {
136 var slide = this.uploadQueue.shift();
137 if (slide) {
138 this.upload(slide);
139 }
140 else {
141 this._uploadQueueRunning = false;
142 }
143 };
144
145
146 // User interface
147 DDFileUploader.prototype.createSlide = function(file) {
148 var slide = document.createElement('span');
149 slide.file = file;
150
151 var a = document.createElement('a');
152 a.href = '#';
153 a.className = 'slide';
154
155 var img = document.createElement('img');
156 img.className = 'hidden';
157 var size = this.thumbnailSize;
158 var self = this;
159 img.onload = function(evt) {
160 if (img.width > img.height) { // landscape
161 img.height = Math.round(size * img.height / img.width);
162 img.width = size;
163 }
164 else {
165 img.width = Math.round(size * img.width / img.height);
166 img.height = size;
167 }
168 img.style.marginLeft = Math.round((self.slideSize - img.width) / 2) + 'px';
169 img.style.marginTop = Math.round((self.slideSize - img.height) / 2) + 'px';
170 img.style.opacity = 0.2;
171 img.className = undefined;
172 };
173 a.appendChild(img);
174 slide.img = img;
175
176 var label = document.createElement('span');
177 slide.label = label;
178 label.className = 'label';
179 label.innerHTML = file.name;
180
181 var progressBar = document.createElement('span');
182 progressBar.className = 'upload-progress';
183 slide.progressBar = progressBar;
184
185 slide.appendChild(a);
186 slide.appendChild(progressBar);
187 slide.appendChild(label);
188 this.dropbox.appendChild(slide);
189
190 return slide;
191 };
192
193 DDFileUploader.prototype.updateProgressBar = function(progress) {
194 // 0 <= progress <= 1
195 var size = this.progressBarMaxSize * progress;
196 size = Math.round(size);
197 this.progressBar.style.width = size + 'px';
198 };
199
200 DDFileUploader.prototype.previewUploadedImage = function(slide) {
201 var reader = new FileReader();
202 var size = this.thumbnailSize;
203 var self = this;
204
205 reader.onload = function(evt) {
206 slide.img.src = evt.target.result;
207 setTimeout(function(){self.previewQueueLoadNext();}, 500);
208 };
209 reader.readAsDataURL(slide.file);
210 };
211
212 }());