|
| 1 | +// Speaker Audio Upload Admin JavaScript |
| 2 | +(function($) { |
| 3 | + 'use strict'; |
| 4 | + |
| 5 | + $(document).ready(function() { |
| 6 | + console.log('Speaker upload admin script loaded'); |
| 7 | + |
| 8 | + // Find the audio file input |
| 9 | + var audioFileInput = $('#audio-file-input'); |
| 10 | + var variantUrisTextarea = $('textarea[name="varianturis"]'); |
| 11 | + |
| 12 | + console.log('Audio file input found:', audioFileInput.length); |
| 13 | + console.log('Variant URIs textarea found:', variantUrisTextarea.length); |
| 14 | + |
| 15 | + if (audioFileInput.length === 0) { |
| 16 | + console.error('Audio file input not found!'); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + // Create upload button |
| 21 | + var uploadButton = $('<button type="button" class="default" style="margin-top: 10px; margin-left: 10px;">Upload Audio File</button>'); |
| 22 | + |
| 23 | + // Add button after the file input |
| 24 | + audioFileInput.after(uploadButton); |
| 25 | + console.log('Upload button added'); |
| 26 | + |
| 27 | + // Handle file upload |
| 28 | + uploadButton.on('click', function() { |
| 29 | + var file = audioFileInput[0].files[0]; |
| 30 | + if (!file) { |
| 31 | + alert('Please select an audio file first.'); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + // Show loading state |
| 36 | + uploadButton.prop('disabled', true).text('Uploading...'); |
| 37 | + |
| 38 | + // Create FormData |
| 39 | + var formData = new FormData(); |
| 40 | + formData.append('audio_file', file); |
| 41 | + |
| 42 | + // Get the current speaker ID from the URL |
| 43 | + var urlParts = window.location.pathname.split('/'); |
| 44 | + var speakerId = null; |
| 45 | + |
| 46 | + // Look for the speaker ID in the URL pattern /admin/rw/speaker/{id}/change/ |
| 47 | + for (var i = 0; i < urlParts.length; i++) { |
| 48 | + if (urlParts[i] === 'speaker' && i + 1 < urlParts.length) { |
| 49 | + speakerId = urlParts[i + 1]; |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + console.log('URL parts:', urlParts); |
| 55 | + console.log('Uploading to speaker ID:', speakerId); |
| 56 | + |
| 57 | + if (!speakerId) { |
| 58 | + alert('Could not determine speaker ID from URL'); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // Make AJAX request |
| 63 | + $.ajax({ |
| 64 | + url: '/admin/rw/speaker/' + speakerId + '/upload-audio/', |
| 65 | + type: 'POST', |
| 66 | + data: formData, |
| 67 | + processData: false, |
| 68 | + contentType: false, |
| 69 | + headers: { |
| 70 | + 'X-CSRFToken': $('[name=csrfmiddlewaretoken]').val() |
| 71 | + }, |
| 72 | + success: function(data) { |
| 73 | + console.log('Upload response:', data); |
| 74 | + if (data.success) { |
| 75 | + // Update the varianturis textarea |
| 76 | + var currentUris = variantUrisTextarea.val().split('\n').filter(function(uri) { |
| 77 | + return uri.trim(); |
| 78 | + }); |
| 79 | + currentUris.push(data.uri); |
| 80 | + variantUrisTextarea.val(currentUris.join('\n')); |
| 81 | + |
| 82 | + // Clear the file input |
| 83 | + audioFileInput.val(''); |
| 84 | + |
| 85 | + // Show success message |
| 86 | + alert('Audio file uploaded successfully! URI added to varianturis.'); |
| 87 | + } else { |
| 88 | + alert('Upload failed: ' + data.error); |
| 89 | + } |
| 90 | + }, |
| 91 | + error: function(xhr, status, error) { |
| 92 | + console.error('Upload error:', xhr.responseText); |
| 93 | + var errorMsg = 'Upload failed: ' + error; |
| 94 | + try { |
| 95 | + var response = JSON.parse(xhr.responseText); |
| 96 | + if (response.error) { |
| 97 | + errorMsg = 'Upload failed: ' + response.error; |
| 98 | + } |
| 99 | + } catch (e) { |
| 100 | + // Use default error message |
| 101 | + } |
| 102 | + alert(errorMsg); |
| 103 | + }, |
| 104 | + complete: function() { |
| 105 | + // Reset button state |
| 106 | + uploadButton.prop('disabled', false).text('Upload Audio File'); |
| 107 | + } |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + // Simple fix: just make text dark |
| 112 | + var audioFileField = audioFileInput.closest('.form-row'); |
| 113 | + if (audioFileField.length) { |
| 114 | + // Force text colors to be visible |
| 115 | + audioFileField.find('label').css({ |
| 116 | + 'color': '#000 !important', |
| 117 | + 'font-weight': 'bold !important' |
| 118 | + }); |
| 119 | + |
| 120 | + audioFileField.find('.help, .helptext').css({ |
| 121 | + 'color': '#333 !important', |
| 122 | + 'font-style': 'italic !important' |
| 123 | + }); |
| 124 | + |
| 125 | + // Make sure all text in the field is dark |
| 126 | + audioFileField.css('color', '#000 !important'); |
| 127 | + } |
| 128 | + }); |
| 129 | +})(django.jQuery); |
0 commit comments