From e27cb3f123dfb6ecc277cfb7320c97c2500a1ab6 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Thu, 24 Sep 2020 08:39:45 +0100 Subject: [PATCH] Don't remove the prefilled entry when loading the instances --- src/script/index.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/script/index.js b/src/script/index.js index fdd609c..7465075 100644 --- a/src/script/index.js +++ b/src/script/index.js @@ -38,6 +38,19 @@ function add_instance(text, disabled, selected) { choose_instance.appendChild(opt); } +function add_loading_instance() { + add_instance("... loading ...", true); +} + +function remove_loading_instance() { + const last_index = choose_instance.children.length - 1; + const last_entry = choose_instance.children[last_index]; + + if (last_entry.innerText === "... loading ...") { + last_entry.remove(); + } +} + function parseUrl(url) { const parser = document.createElement('a'); parser.href = url; @@ -52,10 +65,8 @@ if (prefillInstance != null) { choose_instance.addEventListener('focus', function (e) { if (choose_instance.children.length < 3) { - if (choose_instance.children.length > 1) { - choose_instance.children[1].remove(); - } - add_instance("... loading ...", true); + remove_loading_instance(); + add_loading_instance(); fetch('https://api.joinmastodon.org/servers') .then(response => { if (!response.ok) { @@ -64,7 +75,7 @@ choose_instance.addEventListener('focus', function (e) { return response.json(); }) .then(servers => { - choose_instance.children[1].remove(); + remove_loading_instance(); const domains = servers.map(obj => obj.domain); domains.sort(); for (const domain of domains) { @@ -72,7 +83,7 @@ choose_instance.addEventListener('focus', function (e) { } }) .catch(error => { - choose_instance.children[1].remove(); + remove_loading_instance(); add_instance("--LOADING FAILED--", true); console.error( 'Failed to fetch servers list from joinmastodon.', error);