Selects wear izlek's themed dropdown — trigger, portaled panel, keyboard and panel search — and the logs filter row gains a live search box that narrows detail, actor and kind as you type
Browse files at this commit7756871f116edf5dd1ea4916904e647caca1a890Parent: 1d5ec7d8 changed files +540 -15
- crates/im-core/src/events.rs +27 -0
- crates/im-web/assets/main.css +81 -0
- crates/im-web/src/admin.rs +10 -0
- crates/im-web/src/dropdown.rs +246 -0
- crates/im-web/src/i18n.rs +3 -0
- crates/im-web/src/layout.rs +89 -15
- crates/im-web/src/main.rs +1 -0
- style/main.scss +83 -0
crates/im-core/src/events.rsmodified+27 -0
| @@ -41,6 +41,10 @@ pub async fn log(store: &Store, kind: &str, actor: Option<&str>, detail: Option< | |||
| 41 | pub struct EventFilter { | 41 | pub struct EventFilter { |
| 42 | pub kind: Option<String>, | 42 | pub kind: Option<String>, |
| 43 | pub actor: Option<String>, | 43 | pub actor: Option<String>, |
| 44 | + /// Free text against detail, actor and kind — ASCII case-folded | ||
| 45 | + /// substring, no wildcards, like izlek's board search but in SQL (the | ||
| 46 | + /// fold must live in the predicate or the keyset pages lie). | ||
| 47 | + pub q: Option<String>, | ||
| 44 | /// Half-open day range `[from, to)`; both ends UTC stamps. | 48 | /// Half-open day range `[from, to)`; both ends UTC stamps. |
| 45 | pub day: Option<(time::OffsetDateTime, time::OffsetDateTime)>, | 49 | pub day: Option<(time::OffsetDateTime, time::OffsetDateTime)>, |
| 46 | } | 50 | } |
| @@ -85,6 +89,13 @@ fn filter_sql(filter: &EventFilter, params: &mut Vec<turso::Value>) -> String { | |||
| 85 | params.push(store::stamp(*from).expect("rfc3339 of range start").into()); | 89 | params.push(store::stamp(*from).expect("rfc3339 of range start").into()); |
| 86 | params.push(store::stamp(*to).expect("rfc3339 of range end").into()); | 90 | params.push(store::stamp(*to).expect("rfc3339 of range end").into()); |
| 87 | } | 91 | } |
| 92 | + if let Some(q) = &filter.q { | ||
| 93 | + sql += " AND (lower(coalesce(detail, '')) LIKE ? OR lower(coalesce(actor, '')) LIKE ? OR lower(kind) LIKE ?)"; | ||
| 94 | + let needle = q.to_lowercase(); | ||
| 95 | + params.push(format!("%{needle}%").into()); | ||
| 96 | + params.push(format!("%{needle}%").into()); | ||
| 97 | + params.push(format!("%{needle}%").into()); | ||
| 98 | + } | ||
| 88 | sql | 99 | sql |
| 89 | } | 100 | } |
| 90 | 101 | ||
| @@ -260,6 +271,22 @@ mod tests { | |||
| 260 | assert_eq!(events.len(), 2); | 271 | assert_eq!(events.len(), 2); |
| 261 | assert_eq!(events[0].kind, "login_fail"); | 272 | assert_eq!(events[0].kind, "login_fail"); |
| 262 | assert_eq!(events[1].actor.as_deref(), Some("ann@example.com")); | 273 | assert_eq!(events[1].actor.as_deref(), Some("ann@example.com")); |
| 274 | + // Search: substring hits detail, actor and kind; a miss admits nothing. | ||
| 275 | + for (q, want) in [("password", 1), ("ann@", 1), ("login", 2), ("zzz", 0)] { | ||
| 276 | + let hits = list_filtered( | ||
| 277 | + &store, | ||
| 278 | + 10, | ||
| 279 | + &EventPage::Newest, | ||
| 280 | + Dir::Newest, | ||
| 281 | + &EventFilter { | ||
| 282 | + q: Some(q.to_string()), | ||
| 283 | + ..Default::default() | ||
| 284 | + }, | ||
| 285 | + ) | ||
| 286 | + .await | ||
| 287 | + .unwrap(); | ||
| 288 | + assert_eq!(hits.len(), want, "search {q:?}"); | ||
| 289 | + } | ||
| 263 | } | 290 | } |
| 264 | 291 | ||
| 265 | #[tokio::test] | 292 | #[tokio::test] |
crates/im-web/assets/main.cssmodified+81 -0
| @@ -1,3 +1,4 @@ | |||
| 1 | +@charset "UTF-8"; | ||
| 1 | [data-ui=ledger] .auth-title { | 2 | [data-ui=ledger] .auth-title { |
| 2 | font-family: var(--font-display); | 3 | font-family: var(--font-display); |
| 3 | font-size: 22px; | 4 | font-size: 22px; |
| @@ -1103,3 +1104,83 @@ input[type=checkbox] { | |||
| 1103 | flex-wrap: wrap; | 1104 | flex-wrap: wrap; |
| 1104 | } | 1105 | } |
| 1105 | } | 1106 | } |
| 1107 | +.dd-native { | ||
| 1108 | + display: none; | ||
| 1109 | +} | ||
| 1110 | + | ||
| 1111 | +.dd-trigger { | ||
| 1112 | + display: flex; | ||
| 1113 | + align-items: center; | ||
| 1114 | + font: inherit; | ||
| 1115 | + text-align: left; | ||
| 1116 | + cursor: pointer; | ||
| 1117 | +} | ||
| 1118 | +.dd-trigger::after { | ||
| 1119 | + content: "▾"; | ||
| 1120 | + margin-left: auto; | ||
| 1121 | + padding-left: 8px; | ||
| 1122 | + color: var(--muted); | ||
| 1123 | + font-size: 11px; | ||
| 1124 | +} | ||
| 1125 | + | ||
| 1126 | +.dd-panel { | ||
| 1127 | + position: fixed; | ||
| 1128 | + z-index: 60; | ||
| 1129 | + max-height: 260px; | ||
| 1130 | + overflow-y: auto; | ||
| 1131 | + background: var(--surface); | ||
| 1132 | + border: 1px solid var(--border-strong); | ||
| 1133 | + border-radius: var(--radius); | ||
| 1134 | + box-shadow: var(--shadow-2); | ||
| 1135 | + padding: 8px; | ||
| 1136 | + display: none; | ||
| 1137 | +} | ||
| 1138 | +.dd-panel.dd-open { | ||
| 1139 | + display: block; | ||
| 1140 | +} | ||
| 1141 | + | ||
| 1142 | +.dd-option { | ||
| 1143 | + display: block; | ||
| 1144 | + width: 100%; | ||
| 1145 | + padding: 6px 10px; | ||
| 1146 | + border: none; | ||
| 1147 | + border-radius: var(--radius-sm); | ||
| 1148 | + background: none; | ||
| 1149 | + font: inherit; | ||
| 1150 | + font-size: 13px; | ||
| 1151 | + text-align: left; | ||
| 1152 | + color: var(--ink); | ||
| 1153 | + white-space: nowrap; | ||
| 1154 | + cursor: pointer; | ||
| 1155 | + transition: background-color var(--dur-fast) var(--ease); | ||
| 1156 | +} | ||
| 1157 | +.dd-option:hover, .dd-option:focus, .dd-option.dd-option-active { | ||
| 1158 | + background: var(--row-hover); | ||
| 1159 | + outline: none; | ||
| 1160 | +} | ||
| 1161 | + | ||
| 1162 | +.dd-option-selected { | ||
| 1163 | + background: var(--accent-tint); | ||
| 1164 | + color: var(--accent-ink); | ||
| 1165 | +} | ||
| 1166 | + | ||
| 1167 | +.dd-option-hidden { | ||
| 1168 | + display: none; | ||
| 1169 | +} | ||
| 1170 | + | ||
| 1171 | +.dd-search { | ||
| 1172 | + display: block; | ||
| 1173 | + width: 100%; | ||
| 1174 | + margin-bottom: 6px; | ||
| 1175 | + padding: 8px 10px; | ||
| 1176 | + border: none; | ||
| 1177 | + border-bottom: 1px solid var(--border); | ||
| 1178 | + background: none; | ||
| 1179 | + font: inherit; | ||
| 1180 | + font-size: 13px; | ||
| 1181 | + color: var(--ink); | ||
| 1182 | +} | ||
| 1183 | + | ||
| 1184 | +.logs-filters .logs-q { | ||
| 1185 | + flex: 2 1 200px; | ||
| 1186 | +} | ||
crates/im-web/src/admin.rsmodified+10 -0
| @@ -661,6 +661,9 @@ async fn logs_section(cx: &Cx, lang: i18n::Lang) -> Result<String, topcoat::Erro | |||
| 661 | kind: pick("kind"), | 661 | kind: pick("kind"), |
| 662 | actor: pick("actor"), | 662 | actor: pick("actor"), |
| 663 | day, | 663 | day, |
| 664 | + q: pick("q") | ||
| 665 | + .map(|raw| raw.trim().to_string()) | ||
| 666 | + .filter(|s| !s.is_empty()), | ||
| 664 | }; | 667 | }; |
| 665 | let dir = if pick("dir").as_deref() == Some("oldest") { | 668 | let dir = if pick("dir").as_deref() == Some("oldest") { |
| 666 | events::Dir::Oldest | 669 | events::Dir::Oldest |
| @@ -713,6 +716,9 @@ async fn logs_section(cx: &Cx, lang: i18n::Lang) -> Result<String, topcoat::Erro | |||
| 713 | if dir == events::Dir::Oldest { | 716 | if dir == events::Dir::Oldest { |
| 714 | suffix += "&dir=oldest"; | 717 | suffix += "&dir=oldest"; |
| 715 | } | 718 | } |
| 719 | + if let Some(q) = &filter.q { | ||
| 720 | + suffix += &format!("&q={}", crate::oidc::urlencode(q)); | ||
| 721 | + } | ||
| 716 | 722 | ||
| 717 | let kinds = events::distinct_kinds(store).await?; | 723 | let kinds = events::distinct_kinds(store).await?; |
| 718 | let actors = events::distinct_actors(store).await?; | 724 | let actors = events::distinct_actors(store).await?; |
| @@ -823,6 +829,8 @@ async fn logs_section(cx: &Cx, lang: i18n::Lang) -> Result<String, topcoat::Erro | |||
| 823 | <input class="auth-input" type="date" name="to" value="{to_value}" data-autosubmit></label> | 829 | <input class="auth-input" type="date" name="to" value="{to_value}" data-autosubmit></label> |
| 824 | <label class="auth-field"><span class="auth-label">{dir_label}</span> | 830 | <label class="auth-field"><span class="auth-label">{dir_label}</span> |
| 825 | <select class="auth-input" name="dir" data-autosubmit>{dir_options}</select></label> | 831 | <select class="auth-input" name="dir" data-autosubmit>{dir_options}</select></label> |
| 832 | + <label class="auth-field logs-q"><span class="auth-label">{search_label}</span> | ||
| 833 | + <input class="auth-input" type="text" name="q" value="{q_value}"></label> | ||
| 826 | </form> | 834 | </form> |
| 827 | {body} | 835 | {body} |
| 828 | {foot} | 836 | {foot} |
| @@ -834,6 +842,8 @@ async fn logs_section(cx: &Cx, lang: i18n::Lang) -> Result<String, topcoat::Erro | |||
| 834 | from_label = t(lang, Key::FromLabel), | 842 | from_label = t(lang, Key::FromLabel), |
| 835 | to_label = t(lang, Key::ToLabel), | 843 | to_label = t(lang, Key::ToLabel), |
| 836 | dir_label = t(lang, Key::OrderLabel), | 844 | dir_label = t(lang, Key::OrderLabel), |
| 845 | + search_label = t(lang, Key::SearchLabel), | ||
| 846 | + q_value = escape(filter.q.as_deref().unwrap_or("")), | ||
| 837 | from_value = escape(&pick("from").unwrap_or_default()), | 847 | from_value = escape(&pick("from").unwrap_or_default()), |
| 838 | to_value = escape(&pick("to").unwrap_or_default()), | 848 | to_value = escape(&pick("to").unwrap_or_default()), |
| 839 | dir_options = format!( | 849 | dir_options = format!( |
crates/im-web/src/dropdown.rsadded+246 -0
| @@ -0,0 +1,246 @@ | |||
| 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}; | ||
| 19 | + | ||
| 20 | +pub async fn dropdown_script(cx: &Cx) -> Result { | ||
| 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 | + view! { cx => <script>(Unescaped::new_unchecked(JS))</script> } | ||
| 246 | +} | ||
crates/im-web/src/i18n.rsmodified+3 -0
| @@ -181,6 +181,7 @@ pub enum Key { | |||
| 181 | OlderLink, | 181 | OlderLink, |
| 182 | LogsEmpty, | 182 | LogsEmpty, |
| 183 | OrderLabel, | 183 | OrderLabel, |
| 184 | + SearchLabel, | ||
| 184 | 185 | ||
| 185 | // Landing password pane. | 186 | // Landing password pane. |
| 186 | AccountPasswordNote, | 187 | AccountPasswordNote, |
| @@ -546,6 +547,8 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | |||
| 546 | (LogsEmpty, En) => "Nothing matches.", | 547 | (LogsEmpty, En) => "Nothing matches.", |
| 547 | (OrderLabel, En) => "Order", | 548 | (OrderLabel, En) => "Order", |
| 548 | (OrderLabel, Tr) => "Sıra", | 549 | (OrderLabel, Tr) => "Sıra", |
| 550 | + (SearchLabel, En) => "Search", | ||
| 551 | + (SearchLabel, Tr) => "Ara", | ||
| 549 | (LogsEmpty, Tr) => "Eşleşen kayıt yok.", | 552 | (LogsEmpty, Tr) => "Eşleşen kayıt yok.", |
| 550 | 553 | ||
| 551 | (AccountPasswordNote, En) => "Changing it signs every other device out — this one stays.", | 554 | (AccountPasswordNote, En) => "Changing it signs every other device out — this one stays.", |
crates/im-web/src/layout.rsmodified+89 -15
| @@ -199,6 +199,57 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | |||
| 199 | var nav = 0; | 199 | var nav = 0; |
| 200 | function step() { nav += 1; return nav; } | 200 | function step() { nav += 1; return nav; } |
| 201 | 201 | ||
| 202 | + // Field preservation, izlek's model: only a dirty or focused field is | ||
| 203 | + // worth saving, and the caret offsets come along — a live search typing | ||
| 204 | + // through a refresh keeps both focus and cursor. | ||
| 205 | + function keyOf(el) { | ||
| 206 | + var form = el.form ? (el.form.getAttribute('action') || '') : ''; | ||
| 207 | + return form + '|' + (el.name || '') + '|' + (el.type || '') + '|' + (el.tagName || ''); | ||
| 208 | + } | ||
| 209 | + function editable(el) { | ||
| 210 | + return el.type !== 'hidden' && el.type !== 'password' && el.type !== 'file'; | ||
| 211 | + } | ||
| 212 | + function captureFields() { | ||
| 213 | + var active = document.activeElement, out = []; | ||
| 214 | + document.querySelectorAll('input, textarea, select').forEach(function (el) { | ||
| 215 | + if (!editable(el)) { return; } | ||
| 216 | + var moved; | ||
| 217 | + if (el.type === 'checkbox' || el.type === 'radio') { moved = el.checked !== el.defaultChecked; } | ||
| 218 | + else if (el.tagName === 'SELECT') { | ||
| 219 | + var def = -1; | ||
| 220 | + for (var j = 0; j < el.options.length; j++) { if (el.options[j].defaultSelected) { def = j; break; } } | ||
| 221 | + if (def < 0) { def = 0; } | ||
| 222 | + moved = el.selectedIndex !== def; | ||
| 223 | + } else { moved = el.value !== el.defaultValue; } | ||
| 224 | + var focused = el === active; | ||
| 225 | + if (!moved && !focused) { return; } | ||
| 226 | + var start = -1, end = -1; | ||
| 227 | + try { if (el.selectionStart != null) { start = el.selectionStart; end = el.selectionEnd; } } catch (err) { } | ||
| 228 | + out.push({ k: keyOf(el), v: el.value, c: el.checked, i: el.selectedIndex, f: focused, s: start, e: end }); | ||
| 229 | + }); | ||
| 230 | + return out; | ||
| 231 | + } | ||
| 232 | + function restoreFields(saved) { | ||
| 233 | + if (!saved || !saved.length) { return; } | ||
| 234 | + var fields = document.querySelectorAll('input, textarea, select'); | ||
| 235 | + saved.forEach(function (was) { | ||
| 236 | + var el = null; | ||
| 237 | + for (var i = 0; i < fields.length; i++) { | ||
| 238 | + if (keyOf(fields[i]) === was.k) { el = fields[i]; break; } | ||
| 239 | + } | ||
| 240 | + if (!el) { return; } | ||
| 241 | + if (el.type === 'checkbox' || el.type === 'radio') { el.checked = was.c; } | ||
| 242 | + else if (el.tagName === 'SELECT') { el.selectedIndex = was.i; } | ||
| 243 | + else { el.value = was.v; } | ||
| 244 | + if (was.f) { | ||
| 245 | + try { | ||
| 246 | + el.focus(); | ||
| 247 | + if (was.s >= 0 && el.setSelectionRange) { el.setSelectionRange(was.s, was.e); } | ||
| 248 | + } catch (err) { } | ||
| 249 | + } | ||
| 250 | + }); | ||
| 251 | + } | ||
| 252 | + | ||
| 202 | function adopt(doc, preserve) { | 253 | function adopt(doc, preserve) { |
| 203 | document.title = doc.title; | 254 | document.title = doc.title; |
| 204 | var root = doc.documentElement; | 255 | var root = doc.documentElement; |
| @@ -206,14 +257,7 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | |||
| 206 | if (root.hasAttribute(a)) { document.documentElement.setAttribute(a, root.getAttribute(a)); } | 257 | if (root.hasAttribute(a)) { document.documentElement.setAttribute(a, root.getAttribute(a)); } |
| 207 | else { document.documentElement.removeAttribute(a); } | 258 | else { document.documentElement.removeAttribute(a); } |
| 208 | }); | 259 | }); |
| 209 | - var fields = null; | 260 | + var keep = preserve ? captureFields() : null; |
| 210 | - if (preserve) { | ||
| 211 | - fields = {}; | ||
| 212 | - document.querySelectorAll('input[name], select[name], textarea[name]').forEach(function (f) { | ||
| 213 | - if (f.type === 'password' || f.type === 'file' || f.type === 'hidden') { return; } | ||
| 214 | - fields[f.name] = f.value; | ||
| 215 | - }); | ||
| 216 | - } | ||
| 217 | document.body.replaceChildren(); | 261 | document.body.replaceChildren(); |
| 218 | Array.from(doc.body.childNodes).forEach(function (n) { document.body.appendChild(document.importNode(n, true)); }); | 262 | Array.from(doc.body.childNodes).forEach(function (n) { document.body.appendChild(document.importNode(n, true)); }); |
| 219 | document.querySelectorAll('script').forEach(function (old) { | 263 | document.querySelectorAll('script').forEach(function (old) { |
| @@ -221,11 +265,8 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | |||
| 221 | s.textContent = old.textContent; | 265 | s.textContent = old.textContent; |
| 222 | old.replaceWith(s); | 266 | old.replaceWith(s); |
| 223 | }); | 267 | }); |
| 224 | - if (fields) { | 268 | + if (window.__imDdEnhance) { window.__imDdEnhance(); } |
| 225 | - document.querySelectorAll('input[name], select[name], textarea[name]').forEach(function (f) { | 269 | + if (keep) { restoreFields(keep); } |
| 226 | - if (Object.prototype.hasOwnProperty.call(fields, f.name)) { f.value = fields[f.name]; } | ||
| 227 | - }); | ||
| 228 | - } | ||
| 229 | } | 270 | } |
| 230 | 271 | ||
| 231 | function swap(html, url, fresh, push) { | 272 | function swap(html, url, fresh, push) { |
| @@ -285,9 +326,41 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | |||
| 285 | c.form.requestSubmit(); | 326 | c.form.requestSubmit(); |
| 286 | }); | 327 | }); |
| 287 | 328 | ||
| 329 | + // izlek's __izQuery: a filtered view asked for in place — the URL swaps | ||
| 330 | + // under replaceState (no history entry per keystroke), fields and caret | ||
| 331 | + // come through the adopt. | ||
| 332 | + window.__imQuery = function (url) { | ||
| 333 | + var mark = step(); | ||
| 334 | + fetch(url, { headers: { accept: 'text/html' } }) | ||
| 335 | + .then(function (r) { return r.text().then(function (text) { return { text: text, url: r.url }; }); }) | ||
| 336 | + .then(function (res) { | ||
| 337 | + if (mark !== nav) { return; } | ||
| 338 | + var doc = new DOMParser().parseFromString(res.text, 'text/html'); | ||
| 339 | + if (!doc.body) { return; } | ||
| 340 | + var x = window.scrollX, y = window.scrollY; | ||
| 341 | + adopt(doc, true); | ||
| 342 | + history.replaceState(null, '', res.url); | ||
| 343 | + window.scrollTo(x, y); | ||
| 344 | + }) | ||
| 345 | + .catch(function () {}); | ||
| 346 | + }; | ||
| 347 | + | ||
| 348 | + // The logs search box: two hundred still milliseconds after the last key, | ||
| 349 | + // the whole filter form rides the query — kind, actor, dates, direction | ||
| 350 | + // and q together. | ||
| 351 | + var searchTimer = null; | ||
| 352 | + document.addEventListener('input', function (e) { | ||
| 353 | + var el = e.target; | ||
| 354 | + if (!el || el.name !== 'q' || !el.closest || !el.closest('.logs-filters')) { return; } | ||
| 355 | + if (searchTimer) { clearTimeout(searchTimer); } | ||
| 356 | + searchTimer = setTimeout(function () { | ||
| 357 | + searchTimer = null; | ||
| 358 | + if (!el.form) { return; } | ||
| 359 | + window.__imQuery((el.form.getAttribute('action') || location.pathname) + '?' + new URLSearchParams(new FormData(el.form)).toString()); | ||
| 360 | + }, 200); | ||
| 361 | + }); | ||
| 362 | + | ||
| 288 | window.__imRefresh = function () { | 363 | window.__imRefresh = function () { |
| 289 | - var active = document.activeElement; | ||
| 290 | - if (active && /^(INPUT|SELECT|TEXTAREA)$/.test(active.tagName)) { return; } | ||
| 291 | fetch(location.href, { headers: { accept: 'text/html' } }) | 364 | fetch(location.href, { headers: { accept: 'text/html' } }) |
| 292 | .then(function (r) { return r.text(); }) | 365 | .then(function (r) { return r.text(); }) |
| 293 | .then(function (html) { | 366 | .then(function (html) { |
| @@ -356,6 +429,7 @@ pub async fn shell(cx: &Cx, title: &str, viewer: Option<&User>, stage: Result) - | |||
| 356 | (soft_nav_script(cx).await?) | 429 | (soft_nav_script(cx).await?) |
| 357 | if viewer.is_some() { | 430 | if viewer.is_some() { |
| 358 | (live_script(cx).await?) | 431 | (live_script(cx).await?) |
| 432 | + (crate::dropdown::dropdown_script(cx).await?) | ||
| 359 | } | 433 | } |
| 360 | </body> | 434 | </body> |
| 361 | </html> | 435 | </html> |
crates/im-web/src/main.rsmodified+1 -0
| @@ -13,6 +13,7 @@ use topcoat::router::{BodyLimit, Router, RouterBuilderDiscoverExt, route}; | |||
| 13 | mod admin; | 13 | mod admin; |
| 14 | mod auth; | 14 | mod auth; |
| 15 | mod config; | 15 | mod config; |
| 16 | +mod dropdown; | ||
| 16 | mod i18n; | 17 | mod i18n; |
| 17 | mod layout; | 18 | mod layout; |
| 18 | mod live; | 19 | mod live; |
style/main.scssmodified+83 -0
| @@ -1132,3 +1132,86 @@ input[type="checkbox"] { accent-color: var(--accent); } | |||
| 1132 | .admin-shell { padding: 16px 12px 32px; } | 1132 | .admin-shell { padding: 16px 12px 32px; } |
| 1133 | .admin-invite { flex-wrap: wrap; } | 1133 | .admin-invite { flex-wrap: wrap; } |
| 1134 | } | 1134 | } |
| 1135 | + | ||
| 1136 | +// --- Themed dropdown (izlek's dropdown, on im's tokens) -------------------- | ||
| 1137 | +// The native select hides but keeps posting; the trigger wears its classes | ||
| 1138 | +// (auth-input included), so the closed control already matches the design. | ||
| 1139 | + | ||
| 1140 | +.dd-native { display: none; } | ||
| 1141 | + | ||
| 1142 | +.dd-trigger { | ||
| 1143 | + display: flex; | ||
| 1144 | + align-items: center; | ||
| 1145 | + font: inherit; | ||
| 1146 | + text-align: left; | ||
| 1147 | + cursor: pointer; | ||
| 1148 | + | ||
| 1149 | + &::after { | ||
| 1150 | + content: "▾"; | ||
| 1151 | + margin-left: auto; | ||
| 1152 | + padding-left: 8px; | ||
| 1153 | + color: var(--muted); | ||
| 1154 | + font-size: 11px; | ||
| 1155 | + } | ||
| 1156 | +} | ||
| 1157 | + | ||
| 1158 | +.dd-panel { | ||
| 1159 | + position: fixed; | ||
| 1160 | + z-index: 60; | ||
| 1161 | + max-height: 260px; | ||
| 1162 | + overflow-y: auto; | ||
| 1163 | + background: var(--surface); | ||
| 1164 | + border: 1px solid var(--border-strong); | ||
| 1165 | + border-radius: var(--radius); | ||
| 1166 | + box-shadow: var(--shadow-2); | ||
| 1167 | + padding: 8px; | ||
| 1168 | + display: none; | ||
| 1169 | + | ||
| 1170 | + &.dd-open { display: block; } | ||
| 1171 | +} | ||
| 1172 | + | ||
| 1173 | +.dd-option { | ||
| 1174 | + display: block; | ||
| 1175 | + width: 100%; | ||
| 1176 | + padding: 6px 10px; | ||
| 1177 | + border: none; | ||
| 1178 | + border-radius: var(--radius-sm); | ||
| 1179 | + background: none; | ||
| 1180 | + font: inherit; | ||
| 1181 | + font-size: 13px; | ||
| 1182 | + text-align: left; | ||
| 1183 | + color: var(--ink); | ||
| 1184 | + white-space: nowrap; | ||
| 1185 | + cursor: pointer; | ||
| 1186 | + transition: background-color var(--dur-fast) var(--ease); | ||
| 1187 | + | ||
| 1188 | + &:hover, | ||
| 1189 | + &:focus, | ||
| 1190 | + &.dd-option-active { | ||
| 1191 | + background: var(--row-hover); | ||
| 1192 | + outline: none; | ||
| 1193 | + } | ||
| 1194 | +} | ||
| 1195 | + | ||
| 1196 | +.dd-option-selected { | ||
| 1197 | + background: var(--accent-tint); | ||
| 1198 | + color: var(--accent-ink); | ||
| 1199 | +} | ||
| 1200 | + | ||
| 1201 | +.dd-option-hidden { display: none; } | ||
| 1202 | + | ||
| 1203 | +.dd-search { | ||
| 1204 | + display: block; | ||
| 1205 | + width: 100%; | ||
| 1206 | + margin-bottom: 6px; | ||
| 1207 | + padding: 8px 10px; | ||
| 1208 | + border: none; | ||
| 1209 | + border-bottom: 1px solid var(--border); | ||
| 1210 | + background: none; | ||
| 1211 | + font: inherit; | ||
| 1212 | + font-size: 13px; | ||
| 1213 | + color: var(--ink); | ||
| 1214 | +} | ||
| 1215 | + | ||
| 1216 | +// The search box in the logs filter row takes the room the selects leave. | ||
| 1217 | +.logs-filters .logs-q { flex: 2 1 200px; } | ||