From 415d558c215af8ccef88704c689f6554510b4096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr> Date: Wed, 28 Jan 2015 15:18:09 +0100 Subject: [PATCH] =?UTF-8?q?Js:=20g=C3=A8re=20et=20affiche=20mieux=20les=20?= =?UTF-8?q?erreurs.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Resources/js/widget/file_upload.js | 61 ++++++++++++++++++------------ 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/Resources/js/widget/file_upload.js b/Resources/js/widget/file_upload.js index 7f5fed04..a38120a8 100644 --- a/Resources/js/widget/file_upload.js +++ b/Resources/js/widget/file_upload.js @@ -47,17 +47,28 @@ options.acceptFileTypes = new RegExp('^' + options.acceptFileTypes + '$'); } - var updateDisplay = function() { - var hasEntry = false; - $entries.find('.fileinput-entry').each(function() { - var data = $(this).data('data'), - inError = !!(data && data.files.error); - $(this).toggleClass('alert alert-danger', inError); - $(this).find('.error').toggle(inError); - hasEntry = true; - }); - $button.toggle(options.multiple || !hasEntry); - }; + var showError = function(entry, message) { + var $this = $(entry); + if(message) { + $this.find('.error').text(message); + } + $this.addClass('alert alert-danger'); + $this.find('.error').show(); + $this.find('.icon') + .removeClass('fa-circle-o-notch fa-spin fa-file-o') + .addClass('fa-exclamation-triangle'); + }, + updateDisplay = function() { + var hasEntry = false; + $entries.find('.fileinput-entry').each(function() { + var data = $(this).data('data'); + if(data && data.files.error) { + showError(this, data.files[0].error); + } + hasEntry = true; + }); + $button.toggle(options.multiple || !hasEntry); + }; $this.find('.size').each(function() { $(this).text(formatFileSize($(this).text(), $(this).data('size-precision'))); @@ -83,7 +94,7 @@ row.find('.name').text(file.name); row.find('.size').text(formatFileSize(file.size)); if (file.error) { - row.find('.error').text(file.error); + showError(row, file.error); } rows = rows.add(row); }); @@ -96,7 +107,7 @@ row.find('.size').text(formatFileSize(file.size)); if (file.error) { row.find('.name').text(file.name); - row.find('.error').text(file.error); + showError(row, file.error); } else { row.find('.name a') .text(file.name) @@ -123,19 +134,19 @@ createUrl, { file: { name: file.name, size: file.size, type: file.type, lastModified: file.lastModified } }, function(response) { - if(response.status == 201) { - file.icon = response.icon; - data.url = response.put_url + csrfQuery; - data.delete_url = response.delete_url; - data.delete_type = response.delete_type; - data.jqXHR = $this.fileupload('send', data); - } else { - file.error = response.message || ('Error #' + response.status); - data.files.error = true; - data.context.find('.error').text(file.error); - } + file.icon = response.icon; + data.url = response.put_url + csrfQuery; + data.delete_url = response.delete_url; + data.delete_type = response.delete_type; + data.jqXHR = $this.fileupload('send', data); } - ); + ) + .fail(function(jqXHR, textStatus, errorThrown) { + file.error = textStatus === "error" ? errorThrown : ('Error #' + jqXHR.status); + data.files.error = true; + showError(data.context, file.error); + }) + .always(updateDisplay); return false; }, progress: function (e, data) { -- GitLab