Line 0
Link Here
|
|
|
1 |
$(document).ready(function () { |
2 |
let auto_ill_el = "#confirmautoill-form #autoillbackends"; |
3 |
let auto_ill_message_el = "#confirmautoill-form #autoillbackend-message"; |
4 |
|
5 |
confirmAutoInit(); |
6 |
getBackendsAvailability(auto_backends, metadata); |
7 |
|
8 |
/** |
9 |
* Retrieves the backend availability for a given auto backend and metadata. |
10 |
* |
11 |
* @param {Object} auto_backend - The auto backend object. |
12 |
* @param {string} metadata - The metadata string. |
13 |
* @return {Promise} A Promise that resolves to the JSON response. |
14 |
*/ |
15 |
async function getBackendAvailability(auto_backend, metadata) { |
16 |
return $.ajax({ |
17 |
url: auto_backend.endpoint + metadata, |
18 |
type: "GET", |
19 |
dataType: "json", |
20 |
beforeSend: function () { |
21 |
_addBackendPlaceholderEl(auto_backend.name); |
22 |
_addBackendOption(auto_backend.name); |
23 |
_addVerifyingMessage(auto_backend.name); |
24 |
auto_backend.available = 0; |
25 |
}, |
26 |
success: function (data) { |
27 |
_addSuccessMessage(auto_backend.name); |
28 |
auto_backend.available = 1; |
29 |
}, |
30 |
error: function (request, textstatus) { |
31 |
if (textstatus === "timeout") { |
32 |
_addErrorMessage( |
33 |
auto_backend.name, |
34 |
__("Verification timed out.") |
35 |
); |
36 |
} else { |
37 |
let message = "Error"; |
38 |
if (request.hasOwnProperty("responseJSON")) { |
39 |
if (request.responseJSON.error) { |
40 |
message = request.responseJSON.error; |
41 |
} else if (request.responseJSON.errors) { |
42 |
message = request.responseJSON.errors |
43 |
.map(error => error.message) |
44 |
.join(", "); |
45 |
} |
46 |
} |
47 |
_addErrorMessage(auto_backend.name, message); |
48 |
} |
49 |
}, |
50 |
timeout: 10000, |
51 |
}); |
52 |
} |
53 |
|
54 |
/** |
55 |
* Asynchronously checks the availability of multiple auto backends. |
56 |
* |
57 |
* @param {Array} auto_backends - An array of auto backends to check availability for. |
58 |
* @param {Object} metadata - Additional metadata for the availability check. |
59 |
* @return {void} |
60 |
*/ |
61 |
function getBackendsAvailability(auto_backends, metadata) { |
62 |
let promises = []; |
63 |
for (const auto_backend of auto_backends) { |
64 |
try { |
65 |
const prom = getBackendAvailability(auto_backend, metadata); |
66 |
promises.push(prom); |
67 |
} catch (e) { |
68 |
console.log(e); |
69 |
} |
70 |
} |
71 |
Promise.allSettled(promises).then(() => { |
72 |
let auto_backend = auto_backends.find(backend => backend.available); |
73 |
if (typeof auto_backend === "undefined") { |
74 |
_setAutoBackend("Standard"); |
75 |
} else { |
76 |
_setAutoBackend(auto_backend.name); |
77 |
} |
78 |
$('#confirmautoill-form .action input[type="submit"]').prop( |
79 |
"disabled", |
80 |
false |
81 |
); |
82 |
}); |
83 |
_addBackendPlaceholderEl("Standard"); |
84 |
_addBackendOption("Standard"); |
85 |
} |
86 |
|
87 |
function _addSuccessMessage(auto_backend_name) { |
88 |
_removeVerifyingMessage(auto_backend_name); |
89 |
$(auto_ill_el + " > #backend-" + auto_backend_name).append( |
90 |
'<span class="text-success"><i class="fa-solid fa-check"></i> ' + |
91 |
__("Available.").format(auto_backend_name) + |
92 |
"</span>" |
93 |
); |
94 |
} |
95 |
|
96 |
function _addErrorMessage(auto_backend_name, message) { |
97 |
_removeVerifyingMessage(auto_backend_name); |
98 |
$(auto_ill_el + " > #backend-" + auto_backend_name).append( |
99 |
'<span class="text-danger"> <i class="fa-solid fa-xmark"></i> ' + |
100 |
__("Not readily available:").format(auto_backend_name) + |
101 |
" " + |
102 |
message + |
103 |
"</span>" |
104 |
); |
105 |
} |
106 |
|
107 |
function _addBackendOption(auto_backend_name) { |
108 |
$(auto_ill_el + " > #backend-" + auto_backend_name).append( |
109 |
' <input type="radio" id="' + |
110 |
auto_backend_name + |
111 |
'" name="backend" value="' + |
112 |
auto_backend_name + |
113 |
'">' + |
114 |
' <label for="' + |
115 |
auto_backend_name + |
116 |
'" class="radio">' + |
117 |
auto_backend_name + |
118 |
"</label> " |
119 |
); |
120 |
} |
121 |
|
122 |
function _addVerifyingMessage(auto_backend_name) { |
123 |
$(auto_ill_el + " > #backend-" + auto_backend_name).append( |
124 |
'<span id="verifying-availabilty" class="text-info"><i id="issues-table-load-delay-spinner" class="fa fa-spinner fa-pulse fa-fw"></i> ' + |
125 |
__("Verifying availability...").format(auto_backend_name) + |
126 |
"</span>" |
127 |
); |
128 |
} |
129 |
function _removeVerifyingMessage(auto_backend_name) { |
130 |
$( |
131 |
auto_ill_el + |
132 |
" #backend-" + |
133 |
auto_backend_name + |
134 |
" #verifying-availabilty" |
135 |
).remove(); |
136 |
} |
137 |
|
138 |
function _setAutoBackend(auto_backend_name) { |
139 |
$( |
140 |
'#confirmautoill-form #autoillbackends input[id="' + |
141 |
auto_backend_name + |
142 |
'"]' |
143 |
).prop("checked", true); |
144 |
$(auto_ill_message_el).html( |
145 |
__( |
146 |
"The recommended backend for your request is <strong>%s</strong>." |
147 |
).format(auto_backend_name) |
148 |
); |
149 |
} |
150 |
|
151 |
function _addBackendPlaceholderEl(auto_backend_name) { |
152 |
$(auto_ill_el).append('<div id="backend-' + auto_backend_name + '">'); |
153 |
} |
154 |
|
155 |
function confirmAutoInit() { |
156 |
$('#confirmautoill-form .action input[name="backend"]').remove(); |
157 |
$('#confirmautoill-form .action input[type="submit"]').prop( |
158 |
"disabled", |
159 |
true |
160 |
); |
161 |
$(auto_ill_message_el).html(__("Fetching backends availability...")); |
162 |
} |
163 |
}); |