|
| 1 | +//import $ from 'jquery'; |
| 2 | + |
| 3 | + |
| 4 | +jQuery(document).on('click', '.tli-image-upload', function(event) { |
| 5 | + $(this).siblings('input[type="file"]').click(); |
| 6 | +}); |
| 7 | + |
| 8 | + |
| 9 | +$(document).on('change', '#tli-article-editor-image-gallery input[type="file"]', function() { |
| 10 | + |
| 11 | + const files = this.files; |
| 12 | + if (files.length === 0) { |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + const formData = new FormData(); |
| 17 | + for (let i = 0; i < files.length; i++) { |
| 18 | + formData.append('images[]', files[i]); |
| 19 | + } |
| 20 | + |
| 21 | + const uploadCommandContainer = $('.tli-image-upload'); |
| 22 | + uploadCommandContainer.addClass('d-none'); |
| 23 | + |
| 24 | + const progressBarContainer = $('#tli-image-upload-progress'); |
| 25 | + progressBarContainer.removeClass('d-none'); |
| 26 | + |
| 27 | + const progressBar = progressBarContainer.find('.progress-bar'); |
| 28 | + progressBar.addClass('progress-bar-animated progress-bar-striped'); |
| 29 | + progressBar.removeClass('bg-success bg-danger'); |
| 30 | + |
| 31 | + $.ajax({ |
| 32 | + url: $('#tli-article-editor-image-gallery').data('save-url'), |
| 33 | + type: 'POST', |
| 34 | + data: formData, |
| 35 | + processData: false, |
| 36 | + contentType: false, |
| 37 | + xhr: function() { |
| 38 | + const xhr = new window.XMLHttpRequest(); |
| 39 | + // Upload progress |
| 40 | + xhr.upload.addEventListener('progress', function(evt) { |
| 41 | + if (evt.lengthComputable) { |
| 42 | + const percentComplete = Math.round((evt.loaded / evt.total) * 100); |
| 43 | + progressBar.width(percentComplete + '%'); |
| 44 | + progressBar.attr('aria-valuenow', percentComplete).text(percentComplete + '%'); |
| 45 | + } |
| 46 | + }, false); |
| 47 | + return xhr; |
| 48 | + }, |
| 49 | + success: function(response) { |
| 50 | + debugger; |
| 51 | + progressBar.addClass('bg-success'); |
| 52 | + //progressBarContainer.append('<div class="alert alert-success mt-2">✅ ' + response.message + '</div>'); |
| 53 | + }, |
| 54 | + error: function(jqXHR, textStatus, errorThrown) { |
| 55 | + debugger; |
| 56 | + progressBar.addClass('bg-danger'); |
| 57 | + //const errorMsg = jqXHR.responseJSON ? jqXHR.responseJSON.error : 'An unknown error occurred.'; |
| 58 | + //progressBarContainer.append('<div class="alert alert-danger mt-2">❌ ' + errorMsg + '</div>'); |
| 59 | + }, |
| 60 | + complete: function() { |
| 61 | + |
| 62 | + $(this).val(''); |
| 63 | + uploadCommandContainer.removeClass('d-none'); |
| 64 | + //progressBarContainer.addClass('d-none'); |
| 65 | + progressBar.removeClass('progress-bar-animated progress-bar-striped'); |
| 66 | + } |
| 67 | + }); |
| 68 | +}); |
0 commit comments