]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/trace.js
Merge remote-tracking branch 'upstream/pull/7618'
[rails.git] / app / assets / javascripts / trace.js
1 $(function () {
2   const $tagstring = $("#trace_tagstring");
3   const $help = $tagstring.siblings(".form-text");
4   const help = $help.text();
5   const $tags = $("<ul>")
6     .addClass("list-unstyled d-inline-flex flex-wrap column-gap-1 ms-1 mb-0")
7     .appendTo($tagstring.parent());
8
9   function updateTags() {
10     const tagstring = $tagstring.val();
11     let names, delimitedHelp;
12
13     // Split the tags the same way as Trace#tagstring= does, which
14     // only counts ASCII whitespace, unlike \s and trim()
15     if (tagstring.includes(",")) {
16       names = tagstring.split(",")
17         .map(name => name.replace(/^[\t\n\v\f\r ]+|[\t\n\v\f\r ]+$/g, ""))
18         .filter(name => name);
19       delimitedHelp = OSM.i18n.t("javascripts.traces.tagstring.comma_delimited");
20     } else {
21       names = tagstring.split(/[\t\n\v\f\r ]+/).filter(name => name);
22       delimitedHelp = OSM.i18n.t("javascripts.traces.tagstring.space_delimited");
23     }
24
25     // Nothing to show unless there's more than one tag
26     if (names.length < 2) names = [];
27
28     $help.text(names.length > 0 ? delimitedHelp : help);
29
30     const items = names
31       .map(name => $("<li>").addClass("badge text-bg-secondary mt-1").text(name));
32     $tags.empty().append(...items);
33   }
34
35   $tagstring.on("input", updateTags);
36   updateTags();
37 });