|
1 | 1 | (function ($) { |
2 | 2 | $(document).ready(function () { |
3 | 3 | /** |
4 | | - * Ensure the correct alt field is wrapped properly. |
| 4 | + * Wrap ALT fields in a container if not already done. |
5 | 5 | */ |
6 | 6 | function wrapAltTextFields() { |
7 | | - $("input[name^='field_image'][name$='[alt]']").each(function () { |
| 7 | + $("input[name$='[alt]']").each(function () { |
8 | 8 | var $altField = $(this).closest(".form-item"); |
9 | 9 | if (!$altField.parent().hasClass("ai-alt-field-wrapper")) { |
10 | 10 | $altField.wrap('<div class="ai-alt-field-wrapper"></div>'); |
|
13 | 13 | } |
14 | 14 |
|
15 | 15 | /** |
16 | | - * Automatically trigger alt text generation on page load if needed. |
| 16 | + * Generate alt text for each item that was flagged in Backdrop.settings.openaiAlt. |
| 17 | + * (We only add items with empty alt in the PHP code, so we won't overwrite existing alt.) |
17 | 18 | */ |
18 | | - function triggerAutoGenerationIfNeeded() { |
| 19 | + function triggerAutoGenerationForAll() { |
19 | 20 | if (Backdrop.settings.openaiAlt) { |
20 | | - var fid = Backdrop.settings.openaiAlt.fid; |
21 | | - var fieldName = Backdrop.settings.openaiAlt.field_name; |
22 | | - var delta = Backdrop.settings.openaiAlt.delta; |
23 | | - |
24 | | - // Trigger AJAX call to generate Alt text |
25 | | - $.ajax({ |
26 | | - url: Backdrop.settings.basePath + "openai-alt/generate-alt-text", |
27 | | - type: "POST", |
28 | | - data: { fid: fid, field_name: fieldName, delta: delta }, |
29 | | - success: function (response) { |
30 | | - if (response.status === "success" && response.alt_text) { |
31 | | - var $altField = $("input[name='" + fieldName + "[und][" + delta + "][alt]']"); |
32 | | - $altField.val(response.alt_text).trigger("change"); |
33 | | - |
34 | | - // Ensure Backdrop recognizes the update |
35 | | - setTimeout(function () { |
36 | | - Backdrop.attachBehaviors(); |
37 | | - }, 500); |
38 | | - } |
39 | | - }, |
40 | | - error: function (xhr, status, error) { |
41 | | - console.error("❌ AJAX request failed:", status, error); |
42 | | - }, |
| 21 | + $.each(Backdrop.settings.openaiAlt, function (key, item) { |
| 22 | + // item = { fid, field_name, delta, target_id, ... } |
| 23 | + if (item.fid && item.field_name !== undefined && item.delta !== undefined) { |
| 24 | + generateAltText(item.fid, item.field_name, item.delta); |
| 25 | + } |
43 | 26 | }); |
44 | 27 | } |
45 | 28 | } |
46 | 29 |
|
47 | 30 | /** |
48 | | - * Handle AJAX responses for file uploads & alt text generation. |
| 31 | + * Helper: Actually call the endpoint, then place alt text in the correct input. |
49 | 32 | */ |
50 | | - $(document).ajaxComplete(function (event, xhr, settings) { |
51 | | - if (settings.url.includes("file/ajax")) { |
52 | | - setTimeout(function () { |
53 | | - if (Backdrop.settings.openaiAlt) { |
54 | | - triggerAutoGenerationIfNeeded(); |
55 | | - } |
56 | | - }, 500); |
57 | | - } |
58 | | - |
59 | | - // Handling response from alt text generation |
60 | | - if (settings.url.includes("openai-alt/generate-alt-text")) { |
61 | | - try { |
62 | | - var response = JSON.parse(xhr.responseText); |
| 33 | + function generateAltText(fid, fieldName, delta) { |
| 34 | + $.ajax({ |
| 35 | + url: Backdrop.settings.basePath + "openai-alt/generate-alt-text", |
| 36 | + type: "POST", |
| 37 | + data: { fid: fid, field_name: fieldName, delta: delta }, |
| 38 | + success: function (response) { |
63 | 39 | if (response.status === "success" && response.alt_text) { |
64 | | - var $altField = $("input[name^='field_image'][name$='[alt]']"); |
65 | | - $altField.val(response.alt_text).trigger("change"); |
| 40 | + // 1) Insert the result into the <input> for this field/delta. |
| 41 | + var selector = "input[name='" + fieldName + "[und][" + delta + "][alt]']"; |
| 42 | + $(selector).val(response.alt_text).trigger("change"); |
66 | 43 |
|
67 | | - // Ensure Backdrop recognizes the update |
68 | | - setTimeout(function () { |
69 | | - Backdrop.attachBehaviors(); |
70 | | - }, 500); |
| 44 | + // 2) Remove this item from openaiAlt so it won't auto-regenerate again. |
| 45 | + var key = fieldName + ":" + delta; |
| 46 | + if (Backdrop.settings.openaiAlt && Backdrop.settings.openaiAlt[key]) { |
| 47 | + delete Backdrop.settings.openaiAlt[key]; |
| 48 | + } |
71 | 49 | } |
72 | | - } catch (e) { |
73 | | - console.error("Error processing OpenAI alt text response:", e); |
74 | 50 | } |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * After any AJAX that includes "file/ajax" (i.e. new image added), |
| 56 | + * wait a moment, then try generating alt text for newly added items. |
| 57 | + */ |
| 58 | + $(document).ajaxComplete(function (event, xhr, settings) { |
| 59 | + if (settings.url.includes("file/ajax")) { |
| 60 | + setTimeout(triggerAutoGenerationForAll, 500); |
75 | 61 | } |
76 | 62 | }); |
77 | 63 |
|
78 | 64 | /** |
79 | | - * Handle image removal events. |
| 65 | + * Optional: If you remove an image, you might want to remove |
| 66 | + * that item from openaiAlt. Only do so if you have data attributes |
| 67 | + * for the remove button. Otherwise, leaving them won't matter |
| 68 | + * because we only run alt generation for items with an empty alt |
| 69 | + * (and that item is gone entirely anyway). |
80 | 70 | */ |
81 | 71 | $(document).on("click", ".file-remove-button", function () { |
82 | | - Backdrop.settings.openaiAlt = null; // Reset settings to allow new uploads |
| 72 | + // For a multi-value field, you might parse the delta from $(this).data('delta'). |
| 73 | + // For now, we can just do: |
| 74 | + // Backdrop.settings.openaiAlt = null; |
| 75 | + // But that nixes *all* items. So be careful. |
83 | 76 | }); |
84 | 77 |
|
85 | | - // **Run initial setup** |
| 78 | + // On page load, wrap all existing ALT fields, then generate for any that need it. |
86 | 79 | wrapAltTextFields(); |
87 | | - triggerAutoGenerationIfNeeded(); // Ensure auto-generation is triggered if needed |
| 80 | + triggerAutoGenerationForAll(); |
88 | 81 | }); |
89 | 82 | })(jQuery); |
0 commit comments