1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/static/js/searchable_select.js
catborise 7196871880 fix(ui): pages have a main heading, a skip link and labelled search boxes
An accessibility sweep (axe-core on 22 pages in four themes, and the
keyboard) found:

- No page had an h1: the page heading was an h3, and pages with their
  own heading (sign-in, Computes, errors) kept an empty one. The heading
  is an h1 at the same size; those pages leave the empty one out, and
  Balance has a hidden h1 beside its breadcrumb. Tests check every page,
  an error page and the lock page for one h1 and no empty heading.
- Keyboard users went through the whole menu on every page: a "Skip to
  content" link shows when it has the focus, and brings the content up
  below the sticky navbar. The OTP QR page takes only a signed token from
  its fragment, not the link's #main.
- Tom Select moves a field's label to its own box, leaving the hidden
  select it keeps unnamed (the Tags field of a host, the owner forms'
  user and VM): the label names it too.
- Put in maintenance was yellow text on white (1.6:1), an outline
  secondary button now.
- The pool page's Volumes heading skipped a level.
2026-11-21 15:16:49 +03:00

50 lines
2.1 KiB
JavaScript

// Tom Select moves the field's label to its own box: the hidden <select> it
// keeps is named by that label too, for screen readers that still meet it.
function labelOriginal() {
var label = document.getElementById(this.input.id + "-ts-label");
if (label) {
this.input.setAttribute("aria-labelledby", label.id);
}
}
// A <select data-searchable> of many users or VMs gets a search box (Tom Select).
document.querySelectorAll("select[data-searchable]").forEach(function (select) {
new TomSelect(select, {
onInitialize: labelOriginal,
// the box leaves its focused, taller look at once: else the next tap's
// button moves away under the finger on a phone
onItemAdd: function () { this.blur(); },
});
});
// A <select multiple data-tags>: the tags there are, and a new one typed (the
// server checks it) of at most 12 characters (computes.models.TAG_LENGTH), as
// the tags are kept: without the spaces a paste brings, in lower case ("Prod"
// is "prod"). A name that cannot be a tag gets the rule (data-rule), not silence.
function tagName(input) {
return input.trim().toLowerCase();
}
function isTagName(input) {
return /^[a-z0-9][a-z0-9._-]{0,11}$/.test(tagName(input));
}
document.querySelectorAll("select[data-tags]").forEach(function (select) {
new TomSelect(select, {
onInitialize: labelOriginal,
create: function (input) {
return {value: tagName(input), text: tagName(input)};
},
createFilter: function (input) {
return isTagName(input) && !Object.hasOwn(this.options, tagName(input));
},
persist: false,
render: {
// the page's own words (data-add, data-rule): the library's are English
option_create: function (data, escape) {
return '<div class="create">' + escape(select.dataset.add.replace("%(tag)s", tagName(data.input))) + "</div>";
},
no_results: function (data, escape) {
return isTagName(data.input) ? null : '<div class="no-results">' + escape(select.dataset.rule) + "</div>";
},
},
});
});