|
140 | 140 | }); |
141 | 141 | }, |
142 | 142 |
|
| 143 | + /** |
| 144 | + * Subroutine for converting the file size into a string |
| 145 | + * |
| 146 | + * @param int fileSize file size in byte |
| 147 | + * |
| 148 | + * @return string strFileSize file size as byte, KiB or MiB |
| 149 | + */ |
| 150 | + sizeToString: function(fileSize) { |
| 151 | + let strFileSize = ''; |
| 152 | + |
| 153 | + fileSize = parseInt(fileSize); |
| 154 | + |
| 155 | + if (fileSize < 1024) { |
| 156 | + strFileSize = fileSize + ' {{ lang("BYTES_SHORT")|e("js") }}'; |
| 157 | + } else if (fileSize < 1048576) { |
| 158 | + strFileSize = Math.round(fileSize / 10.24) / 100 + ' {{ lang("KIB")|e("js") }}'; |
| 159 | + } else { |
| 160 | + strFileSize = Math.round(fileSize / 10485.76) / 100 + ' {{ lang("MIB")|e("js") }}'; |
| 161 | + } |
| 162 | + |
| 163 | + return(strFileSize); |
| 164 | + }, |
| 165 | + |
143 | 166 | /** |
144 | 167 | * Rotate the image clockwiese |
145 | 168 | * |
|
232 | 255 | beforeSend: function(xhr, settings) { |
233 | 256 | $(button).find('>:first-child').attr('class', 'icon fa-refresh fa-spin fa-fw'); |
234 | 257 | $('.imcger-iupl-button button').prop('disabled', true).css('cursor','not-allowed'); |
| 258 | + $('.attach-row[data-attach-id="' + attach_id + '"]') .find('.file-status').removeClass('file-uploaded ').addClass('file-working'); |
| 259 | + |
235 | 260 | }, |
236 | 261 | }); |
237 | 262 |
|
238 | 263 | // Code to run if the request succeeds (is done); |
239 | | - ajaxReq.done(function(json) { |
240 | | - if (json.status < 3) { |
241 | | - imcger.imgUpload.updateAttId(json.oldAttachId, json.newAttachId); |
| 264 | + ajaxReq.done(function(response) { |
| 265 | + if (typeof response !== 'object') { |
| 266 | + return; |
| 267 | + } |
| 268 | + |
| 269 | + if (response.status < 3) { |
| 270 | + imcger.imgUpload.updateAttId(response.oldAttachId, response.newAttachId); |
242 | 271 | imcger.imgUpload.image.imgOrientationValue[index] = 0; |
243 | | - imcger.imgUpload.image.setImgSize(json.newAttachId); |
| 272 | + |
| 273 | + // Update row with new image size |
| 274 | + if (response.fileSize) { |
| 275 | + let strFileSize = imcger.imgUpload.image.sizeToString(response.fileSize); |
| 276 | + |
| 277 | + if (strFileSize) { |
| 278 | + $('.attach-row[data-attach-id="' + response.newAttachId + '"] .file-size').html(strFileSize); |
| 279 | + } |
| 280 | + } |
244 | 281 |
|
245 | 282 | // Display a message when a warning occurs |
246 | | - if (json.message) { |
247 | | - phpbb.alert(json.title, json.message); |
| 283 | + if (response.message) { |
| 284 | + phpbb.alert(response.title, response.message); |
248 | 285 | } |
249 | | - } else if (json.status == 3) { |
| 286 | + } else if (response.status == 3) { |
250 | 287 | window.location.assign(window.location.href.replace(window.location.hash, '')); |
251 | 288 | } else { |
252 | | - phpbb.alert(json.title, json.message); |
| 289 | + phpbb.alert(response.title, response.message); |
253 | 290 | } |
254 | 291 | }); |
255 | 292 |
|
|
263 | 300 | ajaxReq.always(function(xhr, status) { |
264 | 301 | $(button).find('>:first-child').attr('class', 'icon fa-save fa-fw'); |
265 | 302 | $('.imcger-iupl-button button').prop('disabled', false).css('cursor','pointer'); |
| 303 | + $('.attach-row[data-attach-id="' + xhr.newAttachId + '"]') .find('.file-status').removeClass('file-working').addClass('file-uploaded'); |
266 | 304 | }); |
267 | 305 | }, |
268 | 306 |
|
269 | 307 | /** |
270 | | - * Send a request to the Server to get the image filesize |
271 | | - * and update the row with it |
| 308 | + * Sends a request to the server to get the file size |
| 309 | + * of the image and update the row with it |
272 | 310 | * |
273 | 311 | * @param int attach_id attach id from attach image |
274 | 312 | */ |
275 | 313 | setImgSize: function(attach_id) { |
276 | | - let imgURL = $('#img-' + attach_id).attr('src').replace('&t=1', ''), |
277 | | - ajaxReq = $.ajax({ |
| 314 | + const imgURL = $('#img-' + attach_id).attr('src').replace('&t=1', ''); |
| 315 | + |
| 316 | + let ajaxReq = $.ajax({ |
278 | 317 | url: imgURL, |
279 | 318 | data: null, |
280 | | - async: false, |
281 | 319 | type: 'HEAD', |
282 | 320 | dataType: "json", |
283 | 321 | timeout: 5000, |
284 | 322 | }); |
285 | 323 |
|
286 | | - ajaxReq.done(function(json) { |
287 | | - let fileSize = ajaxReq.getResponseHeader('Content-Length'), |
288 | | - strFileSize = ''; |
289 | | - |
290 | | - if (fileSize < 1024) { |
291 | | - strFileSize = fileSize + ' {{ lang("BYTES_SHORT")|e("js") }}'; |
292 | | - } else if (fileSize < 1048576) { |
293 | | - strFileSize = Math.round(fileSize / 10.24) / 100 + ' {{ lang("KIB")|e("js") }}'; |
294 | | - } else { |
295 | | - strFileSize = Math.round(fileSize / 10485.76) / 100 + ' {{ lang("MIB")|e("js") }}'; |
296 | | - } |
| 324 | + ajaxReq.done(function(response, status, xhr) { |
| 325 | + let strFileSize = imcger.imgUpload.image.sizeToString(xhr.getResponseHeader('Content-Length')); |
297 | 326 |
|
298 | 327 | if (strFileSize) { |
299 | 328 | $('.attach-row[data-attach-id="' + attach_id + '"] .file-size').html(strFileSize); |
300 | 329 | } |
301 | 330 | }); |
302 | | - } |
| 331 | + }, |
303 | 332 | } |
304 | 333 |
|
305 | 334 | /** |
|
477 | 506 | } else { |
478 | 507 | // Do nothing wenn click on image |
479 | 508 | $link.attr('onclick', 'return false;'); |
| 509 | + $link.css('cursor','default'); |
480 | 510 | } |
481 | 511 |
|
482 | 512 | // Hack for vertically centered image |
|
553 | 583 | } else { |
554 | 584 | // Do nothing wenn click on image |
555 | 585 | $link.attr('onclick', 'return false;'); |
| 586 | + $link.css('cursor','default'); |
556 | 587 | } |
557 | 588 |
|
558 | 589 | // Add thumnail and buttons to rotate it |
|
573 | 604 | } |
574 | 605 |
|
575 | 606 | $(this).css('margin-top', ((maxImgHeight - parseInt(imgHeight)) / 2)); |
576 | | - |
577 | | - imcger.imgUpload.image.setImgSize(attach.attach_id); |
578 | 607 | }); |
| 608 | + |
| 609 | + // Update row with the image file size |
| 610 | + imcger.imgUpload.image.setImgSize(attach.attach_id); |
579 | 611 | } else { |
580 | 612 | $link.attr('href', url).html(attach.real_filename); |
581 | 613 | $row.find('.file-name').html($link); |
|
0 commit comments