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 f6b1ee8985 fix(tags): sixth audit: the tag box tells its rule, trims a paste; clear keeps the focus
- A name typed or pasted into the tag box that cannot be a tag shows the
  rule (letters a-z, digits, dots, dashes, underscores; at most 12
  characters) instead of nothing; the spaces a paste brings are dropped;
  "constructor" is no longer taken for a tag there is.
- "Clear the filters" hides with the message it is in: the search box takes
  the focus.
2026-11-19 10:03:50 +03:00

39 lines
1.7 KiB
JavaScript

// 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, {
// 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, {
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>";
},
},
});
});