Use an input with associatied datalist instead of select, for instances list
This commit is contained in:
parent
7850cece52
commit
d4bf9e6456
2 changed files with 11 additions and 60 deletions
|
@ -28,12 +28,9 @@ html(lang="en")
|
||||||
label(for="text") Post text
|
label(for="text") Post text
|
||||||
textarea#text(rows=6, name="text", required)
|
textarea#text(rows=6, name="text", required)
|
||||||
section
|
section
|
||||||
|
datalist#instances_list
|
||||||
label(for="choose_instance") Choose your Mastodon instance
|
label(for="choose_instance") Choose your Mastodon instance
|
||||||
select#choose_instance()
|
input#instance(type="url", name="instance", placeholder="https://", list="instances_list", required)
|
||||||
option(value="") -- Choose an instance --
|
|
||||||
section
|
|
||||||
label(for="instance") Or type the instance URL
|
|
||||||
input#instance(type="url", name="instance", placeholder="https://", required)
|
|
||||||
|
|
||||||
section.remember
|
section.remember
|
||||||
input#remember(type="checkbox", name="remember")
|
input#remember(type="checkbox", name="remember")
|
||||||
|
|
|
@ -9,12 +9,12 @@ function normalizeUrl(url) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const instance = document.getElementById('instance');
|
const instance = document.getElementById('instance');
|
||||||
const choose_instance = document.getElementById('choose_instance');
|
const instances_list = document.getElementById('instances_list');
|
||||||
|
|
||||||
var prefillInstance = window.localStorage.getItem('mastodon_instance');
|
var prefillInstance = window.localStorage.getItem('mastodon_instance');
|
||||||
|
|
||||||
var paramPairs = window.location.search.substr(1).split('&');
|
var paramPairs = window.location.search.substr(1).split('&');
|
||||||
var paramPairsLength = paramPairs.length;
|
var paramPairsLength = paramPairs.length;
|
||||||
|
|
||||||
for (var i = 0; i < paramPairsLength; i++) {
|
for (var i = 0; i < paramPairsLength; i++) {
|
||||||
var paramPair = paramPairs[i].split('=');
|
var paramPair = paramPairs[i].split('=');
|
||||||
if (paramPair[0] === 'text') {
|
if (paramPair[0] === 'text') {
|
||||||
|
@ -26,43 +26,7 @@ for (var i = 0; i < paramPairsLength; i++) {
|
||||||
delete i
|
delete i
|
||||||
delete paramPair
|
delete paramPair
|
||||||
|
|
||||||
function add_instance(text, disabled, selected, value) {
|
|
||||||
const opt = document.createElement('option');
|
|
||||||
opt.innerText = text;
|
|
||||||
if (disabled) {
|
|
||||||
opt.setAttribute('disabled', true);
|
|
||||||
}
|
|
||||||
if (selected) {
|
|
||||||
opt.setAttribute('selected', true);
|
|
||||||
}
|
|
||||||
if (value !== undefined) {
|
|
||||||
opt.value = value;
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
return parser;
|
|
||||||
}
|
|
||||||
|
|
||||||
function instances_loading_error() {
|
function instances_loading_error() {
|
||||||
choose_instance.innerHTML = "";
|
|
||||||
add_instance("-- LOADING FAILED! --", true, false, "");
|
|
||||||
console.error('Failed to fetch servers list from joinmastodon.');
|
console.error('Failed to fetch servers list from joinmastodon.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,32 +38,26 @@ function instances_loaded() {
|
||||||
|
|
||||||
const servers = JSON.parse(this.responseText);
|
const servers = JSON.parse(this.responseText);
|
||||||
|
|
||||||
const chosen_instance = choose_instance.value;
|
const chosen_instance = instance.value;
|
||||||
const domains = servers.map(obj => obj.domain);
|
const domains = servers.map(obj => obj.domain);
|
||||||
if (domains.indexOf(chosen_instance) === -1) {
|
if (domains.indexOf(chosen_instance) === -1) {
|
||||||
domains.push(chosen_instance);
|
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) {
|
||||||
const selected = (domain === chosen_instance);
|
const opt = document.createElement('option');
|
||||||
add_instance(domain, false, selected);
|
opt.value = normalizeUrl(domain);
|
||||||
|
instances_list.appendChild(opt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prefillInstance != null) {
|
if (prefillInstance != null) {
|
||||||
const url = normalizeUrl(prefillInstance);
|
instance.value = normalizeUrl(prefillInstance);
|
||||||
instance.value = url;
|
|
||||||
add_instance(parseUrl(url).hostname, false, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
choose_instance.addEventListener('focus', function (e) {
|
instance.addEventListener('focus', function (e) {
|
||||||
if (choose_instance.children.length < 3) {
|
if (instances_list.children.length === 0) {
|
||||||
remove_loading_instance();
|
|
||||||
add_loading_instance();
|
|
||||||
|
|
||||||
const req = new XMLHttpRequest();
|
const req = new XMLHttpRequest();
|
||||||
req.addEventListener('load', instances_loaded);
|
req.addEventListener('load', instances_loaded);
|
||||||
req.addEventListener('error', instances_loading_error);
|
req.addEventListener('error', instances_loading_error);
|
||||||
|
@ -108,10 +66,6 @@ choose_instance.addEventListener('focus', function (e) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
choose_instance.addEventListener('change', function (e) {
|
|
||||||
instance.value = normalizeUrl(choose_instance.value);
|
|
||||||
});
|
|
||||||
|
|
||||||
document
|
document
|
||||||
.getElementById('form')
|
.getElementById('form')
|
||||||
.addEventListener('submit', function (e) {
|
.addEventListener('submit', function (e) {
|
||||||
|
|
Reference in a new issue