a67b3cf95a98e5ccc77a53e7e521768be11b8b4d
[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.previewImg = slide.img;
61 this.progressBar = slide.progressBar;
62 var self = this;
63
64 addListener(req.upload, 'progress', function(evt){self.progressHandler(evt);});
65 addListener(req.upload, 'load', function(evt){self.uploadCompleteHandler(evt);});
66
67 req.open("PUT", this.uploadUrl); // + '/' + file.name);
68 req.setRequestHeader("Content-Type", file.type);
69 req.setRequestHeader("X-File-Name", file.name);
70 addListener(reader, 'load', function(evt){req.sendAsBinary(evt.target.result);});
71 reader.readAsBinaryString(file);
72 };
73
74 DDFileUploader.prototype.uploadCompleteHandler = function(evt) {
75 this.progressBar.parentNode.removeChild(this.progressBar);
76 this.uploadQueueLoadNext();
77 };
78
79 DDFileUploader.prototype.progressHandler = function(evt) {
80 if (evt.lengthComputable) {
81 var progress = evt.loaded / evt.total;
82 this.updateProgressBar(progress);
83 var currentOpacity = this.previewImg.style.opacity;
84 this.previewImg.style.opacity = Math.max(currentOpacity, progress);
85 }
86 };
87
88 // Method about queues
89
90 DDFileUploader.prototype.previewQueuePush = function(slide) {
91 this.previewQueue.push(slide);
92 if (!this._previewQueueRunning) {
93 this.startPreviewQueue();
94 }
95 };
96
97 DDFileUploader.prototype.startPreviewQueue = function() {
98 this._previewQueueRunning = true;
99 this.previewQueueLoadNext();
100 };
101
102 DDFileUploader.prototype.previewQueueLoadNext = function() {
103 var slide = this.previewQueue.shift();
104 if (slide) {
105 this.previewUploadedImage(slide);
106 }
107 else {
108 this._previewQueueRunning = false;
109 }
110 };
111
112 DDFileUploader.prototype.uploadQueuePush = function(slide) {
113 this.uploadQueue.push(slide);
114 if (!this._uploadQueueRunning) {
115 this.startUploadQueue();
116 }
117 };
118
119 DDFileUploader.prototype.startUploadQueue = function() {
120 this._uploadQueueRunning = true;
121 this.uploadQueueLoadNext();
122 };
123
124
125 DDFileUploader.prototype.uploadQueueLoadNext = function() {
126 var slide = this.uploadQueue.shift();
127 if (slide) {
128 this.upload(slide);
129 }
130 else {
131 this._uploadQueueRunning = false;
132 }
133 };
134
135
136 // User interface
137 DDFileUploader.prototype.createSlide = function(file) {
138 var slide = document.createElement('span');
139 slide.file = file;
140
141 var a = document.createElement('a');
142 a.href = '#';
143 a.className = 'slide';
144
145 var img = document.createElement('img');
146 img.className = 'hidden';
147 var size = this.thumbnailSize;
148 var self = this;
149 img.onload = function(evt) {
150 if (img.width > img.height) { // landscape
151 img.height = Math.round(size * img.height / img.width);
152 img.width = size;
153 }
154 else {
155 img.width = Math.round(size * img.width / img.height);
156 img.height = size;
157 }
158 img.style.marginLeft = Math.round((self.slideSize - img.width) / 2) + 'px';
159 img.style.marginTop = Math.round((self.slideSize - img.height) / 2) + 'px';
160 img.style.opacity = 0.2;
161 img.className = undefined;
162 };
163 a.appendChild(img);
164 slide.img = img;
165
166 var progressBar = document.createElement('span');
167 progressBar.className = 'upload-progress';
168 slide.progressBar = progressBar;
169
170 slide.appendChild(a);
171 slide.appendChild(progressBar);
172 this.dropbox.appendChild(slide);
173
174 return slide;
175 };
176
177 DDFileUploader.prototype.updateProgressBar = function(progress) {
178 // 0 <= progress <= 1
179 var size = this.progressBarMaxSize * progress;
180 size = Math.round(size);
181 this.progressBar.style.width = size + 'px';
182 };
183
184 DDFileUploader.prototype.previewUploadedImage = function(slide) {
185 var reader = new FileReader();
186 var size = this.thumbnailSize;
187 var self = this;
188
189 reader.onload = function(evt) {
190 slide.img.src = evt.target.result;
191 setTimeout(function(){self.previewQueueLoadNext();}, 500);
192 };
193 reader.readAsDataURL(slide.file);
194 };
195
196 }());