Include the prefilled domain in the list, in the correct sort order

This commit is contained in:
Andy Balaam 2020-09-24 09:07:17 +01:00
parent 70d484d8a1
commit e8b9390343

View file

@ -26,7 +26,7 @@ for (var i = 0; i < paramPairsLength; i++) {
delete i
delete paramPair
function add_instance(text, disabled, selected) {
function add_instance(text, disabled, selected, value) {
const opt = document.createElement('option');
opt.innerText = text;
if (disabled) {
@ -35,6 +35,9 @@ function add_instance(text, disabled, selected) {
if (selected) {
opt.setAttribute('selected', true);
}
if (value !== undefined) {
opt.value = value;
}
choose_instance.appendChild(opt);
}
@ -75,16 +78,23 @@ choose_instance.addEventListener('focus', function (e) {
return response.json();
})
.then(servers => {
remove_loading_instance();
const chosen_instance = choose_instance.value;
const domains = servers.map(obj => obj.domain);
if (domains.indexOf(chosen_instance) === -1) {
domains.push(chosen_instance);
}
domains.sort();
choose_instance.innerHTML = "";
add_instance("-- Choose an instance --", false, false, "")
for (const domain of domains) {
add_instance(domain);
const selected = (domain === chosen_instance);
add_instance(domain, false, selected);
}
})
.catch(error => {
remove_loading_instance();
add_instance("--LOADING FAILED--", true);
choose_instance.innerHTML = "";
add_instance("-- LOADING FAILED! --", true, false, "");
console.error(
'Failed to fetch servers list from joinmastodon.', error);
});