| 1 | (function () { |
| 2 | "use strict"; |
| 3 | var chat = document.querySelector("[equipo-conversation]"); |
| 4 | if (!chat) return; |
| 5 | var scroll = chat.querySelector("[equipo-scroll]"); |
| 6 | var announcements = chat.querySelector("[equipo-announcements]"); |
| 7 | var seen = new Set(); |
| 8 | var bottom = true, anchor = null, top = 0; |
| 9 | var composer = document.getElementById("composer"); |
| 10 | function remember() { |
| 11 | bottom = scroll.scrollHeight - scroll.clientHeight - scroll.scrollTop < 48; |
| 12 | top = scroll.scrollTop; |
| 13 | anchor = null; |
| 14 | var edge = scroll.getBoundingClientRect().top; |
| 15 | scroll.querySelectorAll("[data-message],[data-entry]").forEach(function (el) { |
| 16 | if (!anchor && el.getBoundingClientRect().bottom > edge) { |
| 17 | var attribute = el.hasAttribute("data-message") ? "data-message" : "data-entry"; |
| 18 | anchor = { attribute: attribute, key: el.getAttribute(attribute), offset: el.getBoundingClientRect().top - edge }; |
| 19 | } |
| 20 | }); |
| 21 | } |
| 22 | function messages(initial) { |
| 23 | var added = []; |
| 24 | chat.querySelectorAll("[data-message]").forEach(function (el) { |
| 25 | var id = el.getAttribute("data-message"); |
| 26 | if (!initial && !seen.has(id)) added.push(el.querySelector("strong").textContent + ": " + el.querySelector("[equipo-message-body]").textContent); |
| 27 | seen.add(id); |
| 28 | }); |
| 29 | if (added.length) announcements.textContent = added.join("\n"); |
| 30 | } |
| 31 | function restore() { |
| 32 | if (bottom) scroll.scrollTop = scroll.scrollHeight; |
| 33 | else { |
| 34 | scroll.scrollTop = top; |
| 35 | if (anchor) { |
| 36 | scroll.querySelectorAll("[data-message],[data-entry]").forEach(function (el) { |
| 37 | if (el.getAttribute(anchor.attribute) === anchor.key) { |
| 38 | scroll.scrollTop += el.getBoundingClientRect().top - scroll.getBoundingClientRect().top - anchor.offset; |
| 39 | } |
| 40 | }); |
| 41 | } |
| 42 | } |
| 43 | remember(); |
| 44 | } |
| 45 | scroll.addEventListener("scroll", remember, { passive: true }); |
| 46 | chat.addEventListener("keydown", function (e) { |
| 47 | if (!e.target.matches("#chat-body") || e.key !== "Enter" || e.shiftKey || e.isComposing || e.keyCode === 229) return; |
| 48 | e.preventDefault(); |
| 49 | if (e.target.form.getAttribute("aria-busy") !== "true") e.target.form.requestSubmit(e.target.form.querySelector("button[type=submit]")); |
| 50 | }); |
| 51 | // A click anywhere in the composer box puts the caret in the message field. |
| 52 | chat.addEventListener("click", function (e) { |
| 53 | var box = e.target.closest("#composer"); |
| 54 | if (!box || e.target.closest("textarea, button, select, a, input, label")) return; |
| 55 | var input = box.querySelector("textarea"); |
| 56 | if (!input || input.readOnly) return; |
| 57 | input.focus({ preventScroll: true }); |
| 58 | var end = input.value.length; |
| 59 | input.setSelectionRange(end, end); |
| 60 | }); |
| 61 | function retainHistory(changes) { |
| 62 | var list = chat.querySelector("[equipo-feed]"); |
| 63 | var dates = new Map(); |
| 64 | function collectDates(root) { |
| 65 | root.querySelectorAll("[equipo-day] time").forEach(function (el) { dates.set(el.getAttribute("datetime"), el.textContent); }); |
| 66 | } |
| 67 | collectDates(list); |
| 68 | var current = Array.from(list.querySelectorAll("[data-message]")); |
| 69 | var ids = new Set(current.map(function (el) { return el.dataset.message; })); |
| 70 | var first = current.length ? Number(current[0].dataset.sequence) : 0; |
| 71 | changes.forEach(function (change) { |
| 72 | Array.from(change.removedNodes).forEach(function (old) { |
| 73 | if (old.id !== "conversation-feed") return; |
| 74 | collectDates(old); |
| 75 | old.querySelectorAll("[data-message]").forEach(function (el) { |
| 76 | var sequence = Number(el.dataset.sequence); |
| 77 | if (ids.has(el.dataset.message) || sequence >= first) return; |
| 78 | var before = Array.from(list.children).find(function (item) { return Number(item.dataset.sequence) >= sequence; }); |
| 79 | list.insertBefore(el, before || null); |
| 80 | ids.add(el.dataset.message); |
| 81 | }); |
| 82 | }); |
| 83 | }); |
| 84 | list.querySelectorAll("[equipo-day]").forEach(function (el) { el.remove(); }); |
| 85 | var day = ""; |
| 86 | list.querySelectorAll("[data-message]").forEach(function (el) { |
| 87 | var date = el.querySelector("time").getAttribute("datetime").slice(0, 10); |
| 88 | if (date === day) return; |
| 89 | day = date; |
| 90 | var separator = document.createElement("li"), time = document.createElement("time"); |
| 91 | separator.setAttribute("equipo-day", ""); |
| 92 | time.setAttribute("datetime", date); |
| 93 | time.textContent = dates.get(date) || date; |
| 94 | separator.append(time); |
| 95 | list.insertBefore(separator, el); |
| 96 | }); |
| 97 | } |
| 98 | var observer = new MutationObserver(function (changes) { |
| 99 | var feedChanged = changes.some(function (c) { return Array.from(c.addedNodes).some(function (n) { return n.id === "conversation-feed"; }); }); |
| 100 | var next = document.getElementById("composer"); |
| 101 | if (next && next !== composer) { |
| 102 | var sent = composer && composer.getAttribute("aria-busy") === "true"; |
| 103 | composer = next; |
| 104 | if (sent) { bottom = true; composer.querySelector("textarea").focus({ preventScroll: true }); } |
| 105 | } |
| 106 | if (composer) { |
| 107 | var busy = composer.getAttribute("aria-busy") === "true"; |
| 108 | var input = composer.querySelector("textarea"); |
| 109 | if (input.readOnly !== busy) input.readOnly = busy; |
| 110 | } |
| 111 | if (feedChanged) { retainHistory(changes); messages(false); restore(); } |
| 112 | }); |
| 113 | observer.observe(chat, { childList: true, subtree: true, attributes: true, attributeFilter: ["aria-busy"] }); |
| 114 | messages(true); |
| 115 | restore(); |
| 116 | new ResizeObserver(restore).observe(scroll); |
| 117 | })(); |
| 118 | |