|
123 | 123 | padding-left: 0.5rem; |
124 | 124 | border-left: 1px solid #dbdbdb; |
125 | 125 | } |
| 126 | + |
| 127 | + /* Task search styles */ |
| 128 | + .task-search { |
| 129 | + position: relative; |
| 130 | + width: 300px; |
| 131 | + } |
| 132 | + .task-search-results { |
| 133 | + position: absolute; |
| 134 | + top: 100%; |
| 135 | + left: 0; |
| 136 | + right: 0; |
| 137 | + background: #fff; |
| 138 | + border: 1px solid #dbdbdb; |
| 139 | + border-radius: 4px; |
| 140 | + box-shadow: 0 4px 12px rgba(0,0,0,0.15); |
| 141 | + max-height: 400px; |
| 142 | + overflow-y: auto; |
| 143 | + z-index: 100; |
| 144 | + display: none; |
| 145 | + } |
| 146 | + .task-search-results.is-active { |
| 147 | + display: block; |
| 148 | + } |
| 149 | + .task-search-results a { |
| 150 | + display: block; |
| 151 | + padding: 0.75rem 1rem; |
| 152 | + color: #363636; |
| 153 | + border-bottom: 1px solid #f5f5f5; |
| 154 | + text-decoration: none; |
| 155 | + } |
| 156 | + .task-search-results a:last-child { |
| 157 | + border-bottom: none; |
| 158 | + } |
| 159 | + .task-search-results a:hover, |
| 160 | + .task-search-results a.is-selected { |
| 161 | + background-color: #485fc7; |
| 162 | + color: #fff; |
| 163 | + } |
| 164 | + .task-search-results .no-results { |
| 165 | + padding: 0.75rem 1rem; |
| 166 | + color: #7a7a7a; |
| 167 | + font-style: italic; |
| 168 | + } |
| 169 | + .task-search-results mark { |
| 170 | + background-color: #ffe08a; |
| 171 | + color: inherit; |
| 172 | + padding: 0; |
| 173 | + } |
| 174 | + .task-search-results a:hover mark, |
| 175 | + .task-search-results a.is-selected mark { |
| 176 | + background-color: rgba(255,255,255,0.3); |
| 177 | + } |
126 | 178 | </style> |
127 | 179 |
|
128 | 180 | <script> |
|
147 | 199 | }, 3000) |
148 | 200 | } |
149 | 201 | document.addEventListener('DOMContentLoaded', refresh) |
| 202 | + |
| 203 | + // Task search functionality |
| 204 | + function initTaskSearch() { |
| 205 | + const searchContainer = document.querySelector('.task-search') |
| 206 | + const searchInput = document.getElementById('task-search-input') |
| 207 | + const searchResults = document.getElementById('task-search-results') |
| 208 | + if (!searchInput || !searchResults || !searchContainer) return |
| 209 | + |
| 210 | + const tasksBasePath = searchContainer.dataset.tasksPath |
| 211 | + let tasks = [] |
| 212 | + let selectedIndex = -1 |
| 213 | + |
| 214 | + const loadTasks = () => { |
| 215 | + const taskLinks = document.querySelectorAll('a[href*="/tasks/"]') |
| 216 | + const taskSet = new Set() |
| 217 | + taskLinks.forEach(link => { |
| 218 | + const match = link.href.match(/\/tasks\/([^?/]+)/) |
| 219 | + if (match) { |
| 220 | + const taskName = decodeURIComponent(match[1]) |
| 221 | + if (taskName && !taskName.includes('runs')) { |
| 222 | + taskSet.add(taskName) |
| 223 | + } |
| 224 | + } |
| 225 | + }) |
| 226 | + tasks = Array.from(taskSet).sort() |
| 227 | + } |
| 228 | + |
| 229 | + const fuzzyMatch = (text, query) => { |
| 230 | + const lowerText = text.toLowerCase() |
| 231 | + const lowerQuery = query.toLowerCase() |
| 232 | + let score = 0 |
| 233 | + let textIndex = 0 |
| 234 | + let matchIndices = [] |
| 235 | + |
| 236 | + for (let i = 0; i < lowerQuery.length; i++) { |
| 237 | + const char = lowerQuery[i] |
| 238 | + const foundIndex = lowerText.indexOf(char, textIndex) |
| 239 | + if (foundIndex === -1) return null |
| 240 | + |
| 241 | + matchIndices.push(foundIndex) |
| 242 | + score += foundIndex === textIndex ? 10 : 1 |
| 243 | + if (foundIndex === 0 || text[foundIndex - 1] === ':') score += 5 |
| 244 | + textIndex = foundIndex + 1 |
| 245 | + } |
| 246 | + |
| 247 | + return { score, matchIndices } |
| 248 | + } |
| 249 | + |
| 250 | + const escapeHtml = (text) => { |
| 251 | + const div = document.createElement('div') |
| 252 | + div.textContent = text |
| 253 | + return div.innerHTML |
| 254 | + } |
| 255 | + |
| 256 | + const highlightMatch = (text, matchIndices) => { |
| 257 | + let result = '' |
| 258 | + let lastIndex = 0 |
| 259 | + matchIndices.forEach(idx => { |
| 260 | + result += escapeHtml(text.slice(lastIndex, idx)) |
| 261 | + result += '<mark>' + escapeHtml(text[idx]) + '</mark>' |
| 262 | + lastIndex = idx + 1 |
| 263 | + }) |
| 264 | + result += escapeHtml(text.slice(lastIndex)) |
| 265 | + return result |
| 266 | + } |
| 267 | + |
| 268 | + const renderResults = (query) => { |
| 269 | + if (!query.trim()) { |
| 270 | + searchResults.classList.remove('is-active') |
| 271 | + return |
| 272 | + } |
| 273 | + |
| 274 | + const matches = tasks |
| 275 | + .map(task => ({ task, match: fuzzyMatch(task, query) })) |
| 276 | + .filter(item => item.match) |
| 277 | + .sort((a, b) => b.match.score - a.match.score) |
| 278 | + .slice(0, 10) |
| 279 | + |
| 280 | + if (matches.length === 0) { |
| 281 | + searchResults.innerHTML = '<div class="no-results">No tasks found</div>' |
| 282 | + } else { |
| 283 | + searchResults.innerHTML = matches.map((item, index) => |
| 284 | + '<a href="' + tasksBasePath + '/' + encodeURIComponent(item.task) + '" class="' + (index === selectedIndex ? 'is-selected' : '') + '">' + highlightMatch(item.task, item.match.matchIndices) + '</a>' |
| 285 | + ).join('') |
| 286 | + } |
| 287 | + searchResults.classList.add('is-active') |
| 288 | + selectedIndex = -1 |
| 289 | + } |
| 290 | + |
| 291 | + const updateSelection = (items) => { |
| 292 | + items.forEach((item, index) => { |
| 293 | + item.classList.toggle('is-selected', index === selectedIndex) |
| 294 | + }) |
| 295 | + if (selectedIndex >= 0) { |
| 296 | + items[selectedIndex].scrollIntoView({ block: 'nearest' }) |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + const handleKeydown = (e) => { |
| 301 | + const items = searchResults.querySelectorAll('a') |
| 302 | + if (!items.length) return |
| 303 | + |
| 304 | + if (e.key === 'ArrowDown') { |
| 305 | + e.preventDefault() |
| 306 | + selectedIndex = Math.min(selectedIndex + 1, items.length - 1) |
| 307 | + updateSelection(items) |
| 308 | + } else if (e.key === 'ArrowUp') { |
| 309 | + e.preventDefault() |
| 310 | + selectedIndex = Math.max(selectedIndex - 1, -1) |
| 311 | + updateSelection(items) |
| 312 | + } else if (e.key === 'Enter' && selectedIndex >= 0) { |
| 313 | + e.preventDefault() |
| 314 | + items[selectedIndex].click() |
| 315 | + } else if (e.key === 'Escape') { |
| 316 | + searchResults.classList.remove('is-active') |
| 317 | + searchInput.blur() |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + searchInput.addEventListener('input', e => { |
| 322 | + loadTasks() |
| 323 | + renderResults(e.target.value) |
| 324 | + }) |
| 325 | + searchInput.addEventListener('keydown', handleKeydown) |
| 326 | + searchInput.addEventListener('focus', () => { |
| 327 | + loadTasks() |
| 328 | + if (searchInput.value.trim()) renderResults(searchInput.value) |
| 329 | + }) |
| 330 | + document.addEventListener('click', e => { |
| 331 | + if (!e.target.closest('.task-search')) { |
| 332 | + searchResults.classList.remove('is-active') |
| 333 | + } |
| 334 | + }) |
| 335 | + } |
| 336 | + document.addEventListener('DOMContentLoaded', initTaskSearch) |
150 | 337 | </script> |
151 | 338 | </head> |
152 | 339 |
|
|
0 commit comments