| 1 | // The task thread's quiet "Asking Engineering · change" line. Without a |
| 2 | // choice Equipo decides (Review when the question addresses it, else |
| 3 | // Engineering), and the line follows what is typed; change picks the |
| 4 | // other specialist explicitly. Without script Equipo decides. |
| 5 | (function () { |
| 6 | "use strict"; |
| 7 | var line = document.querySelector("[equipo-ask-target]"); |
| 8 | var root = document.querySelector("[data-chat-stream][data-task]"); |
| 9 | if (!line || !root) return; |
| 10 | var form = line.closest("form"); |
| 11 | var input = form.querySelector("textarea"); |
| 12 | var chosen = form.querySelector("[data-ask-specialist]"); |
| 13 | var label = line.querySelector("[data-ask-line]"); |
| 14 | var change = line.querySelector("[data-ask-change]"); |
| 15 | var sep = line.querySelector("[data-ask-sep]"); |
| 16 | var choices; |
| 17 | function readChoices() { choices = (line.dataset.choices || "").split(",").filter(Boolean); } |
| 18 | readChoices(); |
| 19 | var review = /\breview(?:er|ers|ed|ing|s)?\b/i; |
| 20 | function name(s) { return root.dataset["name" + s.charAt(0).toUpperCase() + s.slice(1)] || s; } |
| 21 | function current() { |
| 22 | if (chosen.value) return chosen.value; |
| 23 | if (choices.indexOf("review") >= 0 && review.test(input.value)) return "review"; |
| 24 | return choices[0]; |
| 25 | } |
| 26 | function show() { |
| 27 | var c = current(); |
| 28 | if (!c) return; |
| 29 | var who = name(c); |
| 30 | label.textContent = (root.dataset.askFormat || "%s").replace("%s", who); |
| 31 | input.placeholder = (root.dataset.askPlaceholder || "%s").replace("%s", who); |
| 32 | input.setAttribute("aria-label", input.placeholder); |
| 33 | } |
| 34 | function offerChange() { |
| 35 | if (!change) return; |
| 36 | change.hidden = choices.length < 2; |
| 37 | if (sep) sep.hidden = change.hidden; |
| 38 | } |
| 39 | offerChange(); |
| 40 | if (change) { |
| 41 | change.addEventListener("click", function () { |
| 42 | var at = choices.indexOf(current()); |
| 43 | chosen.value = choices[(at + 1) % choices.length]; |
| 44 | show(); |
| 45 | input.focus({ preventScroll: true }); |
| 46 | }); |
| 47 | } |
| 48 | input.addEventListener("input", show); |
| 49 | // A refreshed thread (chat-stream.js) can name another maker or offer |
| 50 | // other choices: a choice no longer offered is dropped. |
| 51 | root.addEventListener("equipo:names", function () { |
| 52 | readChoices(); |
| 53 | if (chosen.value && choices.indexOf(chosen.value) < 0) chosen.value = ""; |
| 54 | offerChange(); |
| 55 | show(); |
| 56 | }); |
| 57 | // After a question goes, the next one starts undecided again. |
| 58 | form.addEventListener("equipo:sent", function () { chosen.value = ""; show(); }); |
| 59 | show(); |
| 60 | })(); |
| 61 | |