im_old
Clone
https://git.tahinli.com/Tahinli/im_old.git
0 watching·0 forks·Rust·GPL-3.0·755.6 KiB·84318dc·Tahinli·2 months ago·77 commits
| 1 | //! The themed dropdown, ported from izlek's `dropdown.rs`: a native |
| 2 | //! `<select>`'s popup follows the browser's chrome, not the design — only |
| 3 | //! Chromium's `::picker(select)` themes it, Firefox and WebKit never see it. |
| 4 | //! So every `select.auth-input` is progressively enhanced: the native |
| 5 | //! element stays in the DOM hidden (its `name` and value still post |
| 6 | //! unchanged) while a trigger button and a portaled, fixed-position panel |
| 7 | //! take over the look. A pick writes the native select and dispatches a |
| 8 | //! bubbling `change`, which is what the soft-nav's `data-autosubmit` |
| 9 | //! listener turns into a filter submit — zero extra wiring. |
| 10 | //! |
| 11 | //! Unlike izlek's, this port needs no node-ownership registry: im's soft |
| 12 | //! swap rebuilds `<body>` wholesale, client-made nodes die with it, and the |
| 13 | //! fresh select is simply enhanced again — `enhanceAll` runs on every |
| 14 | //! evaluation and through `window.__imDdEnhance` after every adopt. |
| 15 | |
| 16 | use topcoat::Result; |
| 17 | use topcoat::context::Cx; |
| 18 | use topcoat::view::{Unescaped, View, view}; |
| 19 | |
| 20 | pub async fn dropdown_script(cx: &Cx) -> Result<impl View + '_> { |
| 21 | const JS: &str = "\ |
| 22 | (function () {\ |
| 23 | function enhanceAll() { document.querySelectorAll('select.auth-input').forEach(enhance); }\ |
| 24 | window.__imDdEnhance = enhanceAll;\ |
| 25 | if (window.__imDd) { enhanceAll(); return; }\ |
| 26 | window.__imDd = true;\ |
| 27 | function opts(select) { return Array.prototype.slice.call(select.options); }\ |
| 28 | function closeAll() {\ |
| 29 | document.querySelectorAll('.dd-panel.dd-open').forEach(function (panel) {\ |
| 30 | panel.classList.remove('dd-open');\ |
| 31 | if (panel.__ddTrigger) { panel.__ddTrigger.setAttribute('aria-expanded', 'false'); }\ |
| 32 | });\ |
| 33 | }\ |
| 34 | function place(panel, trigger) {\ |
| 35 | var r = trigger.getBoundingClientRect();\ |
| 36 | var h = panel.offsetHeight;\ |
| 37 | var w = panel.offsetWidth;\ |
| 38 | var top = r.bottom + 4;\ |
| 39 | if (top + h > window.innerHeight && r.top - h - 4 >= 0) { top = r.top - h - 4; }\ |
| 40 | top = Math.max(4, Math.min(top, window.innerHeight - h - 4));\ |
| 41 | var left = Math.max(4, Math.min(r.left, window.innerWidth - w - 4));\ |
| 42 | panel.style.left = left + 'px';\ |
| 43 | panel.style.top = top + 'px';\ |
| 44 | panel.style.minWidth = r.width + 'px';\ |
| 45 | }\ |
| 46 | function visibleRows(panel) { return Array.prototype.slice.call(panel.querySelectorAll('.dd-option:not(.dd-option-hidden)')); }\ |
| 47 | function activate(panel, row) {\ |
| 48 | panel.querySelectorAll('.dd-option-active').forEach(function (r) { r.classList.remove('dd-option-active'); });\ |
| 49 | if (!row) { return; }\ |
| 50 | row.classList.add('dd-option-active');\ |
| 51 | if (panel.__ddSearch) { row.scrollIntoView({ block: 'nearest' }); } else { row.focus(); }\ |
| 52 | }\ |
| 53 | function isTypeKey(e) { return e.key && e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey; }\ |
| 54 | function typeIntoSearch(search, e) {\ |
| 55 | search.focus();\ |
| 56 | search.value += e.key;\ |
| 57 | search.dispatchEvent(new Event('input', { bubbles: true }));\ |
| 58 | }\ |
| 59 | function filterRows(panel, query) {\ |
| 60 | var qTr = query.toLocaleLowerCase('tr');\ |
| 61 | var qEn = query.toLowerCase();\ |
| 62 | panel.querySelectorAll('.dd-option').forEach(function (r) {\ |
| 63 | var t = r.textContent;\ |
| 64 | var miss = t.toLocaleLowerCase('tr').indexOf(qTr) === -1 && t.toLowerCase().indexOf(qEn) === -1;\ |
| 65 | r.classList.toggle('dd-option-hidden', qEn !== '' && miss);\ |
| 66 | });\ |
| 67 | var vis = visibleRows(panel);\ |
| 68 | activate(panel, panel.querySelector('.dd-option-selected:not(.dd-option-hidden)') || vis[0]);\ |
| 69 | }\ |
| 70 | function pick(select, trigger, panel, row) {\ |
| 71 | select.value = row.dataset.value;\ |
| 72 | panel.querySelectorAll('.dd-option').forEach(function (r) {\ |
| 73 | r.classList.toggle('dd-option-selected', r === row);\ |
| 74 | r.setAttribute('aria-selected', r === row ? 'true' : 'false');\ |
| 75 | });\ |
| 76 | trigger.textContent = row.textContent;\ |
| 77 | panel.classList.remove('dd-open');\ |
| 78 | trigger.setAttribute('aria-expanded', 'false');\ |
| 79 | trigger.focus();\ |
| 80 | select.dispatchEvent(new Event('change', { bubbles: true }));\ |
| 81 | }\ |
| 82 | function openPanel(select, trigger, panel) {\ |
| 83 | closeAll();\ |
| 84 | var search = panel.__ddSearch;\ |
| 85 | if (search) {\ |
| 86 | search.value = '';\ |
| 87 | panel.querySelectorAll('.dd-option').forEach(function (r) { r.classList.remove('dd-option-hidden'); });\ |
| 88 | }\ |
| 89 | panel.classList.add('dd-open');\ |
| 90 | place(panel, trigger);\ |
| 91 | trigger.setAttribute('aria-expanded', 'true');\ |
| 92 | activate(panel, panel.querySelector('.dd-option-selected') || panel.querySelector('.dd-option'));\ |
| 93 | if (search) { search.focus(); }\ |
| 94 | }\ |
| 95 | function fillRows(panel, select) {\ |
| 96 | panel.querySelectorAll('.dd-option').forEach(function (r) { r.remove(); });\ |
| 97 | opts(select).forEach(function (opt) {\ |
| 98 | var row = document.createElement('button');\ |
| 99 | row.type = 'button';\ |
| 100 | row.className = 'dd-option' + (opt.selected ? ' dd-option-selected' : '');\ |
| 101 | row.textContent = opt.textContent;\ |
| 102 | row.dataset.value = opt.value;\ |
| 103 | row.setAttribute('role', 'option');\ |
| 104 | row.setAttribute('aria-selected', opt.selected ? 'true' : 'false');\ |
| 105 | panel.appendChild(row);\ |
| 106 | });\ |
| 107 | }\ |
| 108 | function rowsMatch(panel, select) {\ |
| 109 | var rows = panel.querySelectorAll('.dd-option');\ |
| 110 | var all = opts(select);\ |
| 111 | if (rows.length !== all.length) { return false; }\ |
| 112 | for (var i = 0; i < all.length; i++) {\ |
| 113 | if (rows[i].dataset.value !== all[i].value) { return false; }\ |
| 114 | if (rows[i].textContent !== all[i].textContent) { return false; }\ |
| 115 | }\ |
| 116 | return true;\ |
| 117 | }\ |
| 118 | function resync(select) {\ |
| 119 | var trigger = select.__ddTrigger, panel = select.__ddPanel;\ |
| 120 | if (!trigger || !panel) { return; }\ |
| 121 | select.classList.add('dd-native');\ |
| 122 | if (panel.classList.contains('dd-open')) { return; }\ |
| 123 | if (!rowsMatch(panel, select)) { fillRows(panel, select); }\ |
| 124 | var current = select.options[select.selectedIndex];\ |
| 125 | var label = current ? current.textContent : '';\ |
| 126 | if (trigger.textContent !== label) { trigger.textContent = label; }\ |
| 127 | panel.querySelectorAll('.dd-option').forEach(function (r) {\ |
| 128 | var on = !!current && r.dataset.value === current.value;\ |
| 129 | r.classList.toggle('dd-option-selected', on);\ |
| 130 | r.setAttribute('aria-selected', on ? 'true' : 'false');\ |
| 131 | });\ |
| 132 | }\ |
| 133 | function enhance(select) {\ |
| 134 | if (select.dataset.ddDone) { resync(select); return; }\ |
| 135 | select.dataset.ddDone = '1';\ |
| 136 | var trigger = document.createElement('button');\ |
| 137 | trigger.type = 'button';\ |
| 138 | trigger.className = select.className + ' dd-trigger';\ |
| 139 | var current = select.options[select.selectedIndex];\ |
| 140 | trigger.textContent = current ? current.textContent : '';\ |
| 141 | trigger.setAttribute('aria-haspopup', 'listbox');\ |
| 142 | trigger.setAttribute('aria-expanded', 'false');\ |
| 143 | select.parentNode.insertBefore(trigger, select);\ |
| 144 | select.classList.add('dd-native');\ |
| 145 | var panel = document.createElement('div');\ |
| 146 | panel.className = 'dd-panel';\ |
| 147 | panel.setAttribute('role', 'listbox');\ |
| 148 | panel.__ddTrigger = trigger;\ |
| 149 | trigger.__ddPanel = panel;\ |
| 150 | trigger.__ddSelect = select;\ |
| 151 | select.__ddTrigger = trigger;\ |
| 152 | select.__ddPanel = panel;\ |
| 153 | var allOpts = opts(select);\ |
| 154 | var search = null;\ |
| 155 | if (allOpts.length > 7 || select.hasAttribute('data-search')) {\ |
| 156 | search = document.createElement('input');\ |
| 157 | search.type = 'text';\ |
| 158 | search.className = 'dd-search';\ |
| 159 | panel.appendChild(search);\ |
| 160 | panel.__ddSearch = search;\ |
| 161 | }\ |
| 162 | fillRows(panel, select);\ |
| 163 | document.body.appendChild(panel);\ |
| 164 | trigger.addEventListener('click', function (e) {\ |
| 165 | e.stopPropagation();\ |
| 166 | if (panel.classList.contains('dd-open')) { closeAll(); } else { openPanel(select, trigger, panel); }\ |
| 167 | });\ |
| 168 | trigger.addEventListener('keydown', function (e) {\ |
| 169 | if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {\ |
| 170 | e.preventDefault();\ |
| 171 | if (!panel.classList.contains('dd-open')) { openPanel(select, trigger, panel); return; }\ |
| 172 | var vis = visibleRows(panel);\ |
| 173 | var idx = vis.indexOf(panel.querySelector('.dd-option-active')) + (e.key === 'ArrowDown' ? 1 : -1);\ |
| 174 | if (idx >= 0 && idx < vis.length) { activate(panel, vis[idx]); }\ |
| 175 | } else if (e.key === 'Enter' && panel.classList.contains('dd-open')) {\ |
| 176 | e.preventDefault();\ |
| 177 | var active = panel.querySelector('.dd-option-active');\ |
| 178 | if (active) { pick(select, trigger, panel, active); }\ |
| 179 | } else if (search && isTypeKey(e)) {\ |
| 180 | e.preventDefault();\ |
| 181 | if (!panel.classList.contains('dd-open')) { openPanel(select, trigger, panel); }\ |
| 182 | typeIntoSearch(search, e);\ |
| 183 | }\ |
| 184 | });\ |
| 185 | if (search) {\ |
| 186 | search.addEventListener('input', function () { filterRows(panel, search.value); });\ |
| 187 | }\ |
| 188 | panel.addEventListener('keydown', function (e) {\ |
| 189 | if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {\ |
| 190 | e.preventDefault();\ |
| 191 | var vis = visibleRows(panel);\ |
| 192 | var idx = vis.indexOf(panel.querySelector('.dd-option-active')) + (e.key === 'ArrowDown' ? 1 : -1);\ |
| 193 | if (idx >= 0 && idx < vis.length) { activate(panel, vis[idx]); }\ |
| 194 | } else if (e.key === 'Enter') {\ |
| 195 | e.preventDefault();\ |
| 196 | var vis = visibleRows(panel);\ |
| 197 | var active = panel.querySelector('.dd-option-active') || (vis.length === 1 ? vis[0] : null);\ |
| 198 | if (active) { pick(select, trigger, panel, active); }\ |
| 199 | } else if (search && e.target !== search && isTypeKey(e)) {\ |
| 200 | e.preventDefault();\ |
| 201 | typeIntoSearch(search, e);\ |
| 202 | }\ |
| 203 | });\ |
| 204 | panel.addEventListener('click', function (e) {\ |
| 205 | e.stopPropagation();\ |
| 206 | var row = e.target.closest('.dd-option');\ |
| 207 | if (row) { pick(select, trigger, panel, row); }\ |
| 208 | });\ |
| 209 | }\ |
| 210 | document.addEventListener('keydown', function (e) {\ |
| 211 | if (e.key !== 'Escape') { return; }\ |
| 212 | var panel = document.querySelector('.dd-panel.dd-open');\ |
| 213 | if (!panel) { return; }\ |
| 214 | var trigger = panel.__ddTrigger;\ |
| 215 | closeAll();\ |
| 216 | if (trigger) { trigger.focus(); }\ |
| 217 | });\ |
| 218 | document.addEventListener('click', function (e) {\ |
| 219 | var el = e.target;\ |
| 220 | if (!el || !el.closest) { return; }\ |
| 221 | if (el.closest('.dd-panel') || el.closest('.dd-trigger')) { return; }\ |
| 222 | if (el.closest('a, button, input:not([type=hidden]), textarea')) { return; }\ |
| 223 | var box = el.nodeType === 1 ? el : el.parentNode;\ |
| 224 | for (var depth = 0; depth < 3 && box && box.querySelectorAll; depth++) {\ |
| 225 | var found = box.querySelectorAll('.dd-trigger');\ |
| 226 | if (found.length === 1 && found[0].parentNode === box && found[0].__ddPanel) {\ |
| 227 | var t = found[0];\ |
| 228 | e.stopPropagation();\ |
| 229 | e.preventDefault();\ |
| 230 | if (t.__ddPanel.classList.contains('dd-open')) { closeAll(); }\ |
| 231 | else { openPanel(t.__ddSelect, t, t.__ddPanel); }\ |
| 232 | return;\ |
| 233 | }\ |
| 234 | box = box.parentNode;\ |
| 235 | }\ |
| 236 | }, true);\ |
| 237 | document.addEventListener('click', closeAll);\ |
| 238 | window.addEventListener('scroll', function (e) {\ |
| 239 | var t = e.target;\ |
| 240 | if (t && t.nodeType === 1 && t.classList.contains('dd-panel')) { return; }\ |
| 241 | closeAll();\ |
| 242 | }, true);\ |
| 243 | enhanceAll();\ |
| 244 | })();"; |
| 245 | Ok(view! { cx => <script>(Unescaped::new_unchecked(JS))</script> }) |
| 246 | } |