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