Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions netbox/project-static/dist/netbox.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions netbox/project-static/dist/netbox.js.map

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions netbox/project-static/src/hotkeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const HOTKEYS: Record<string, () => void> = {
'/': focusGlobalSearch,
};

function focusGlobalSearch(): void {
const searchInput = document.querySelector<HTMLInputElement>('header input[name="q"]')!;
if (searchInput) {
searchInput.focus();
}
}

function handleKeydown(event: KeyboardEvent): void {
// Ignore hotkeys when focused on form elements or when modal is open
if ((event.target as Element).matches('input, textarea, select') || document.body.classList.contains('modal-open')) {
return;
}

const handler = HOTKEYS[event.key];
if (!handler) {
return;
}

event.preventDefault();
handler();
}

export function initHotkeys(): void {
document.addEventListener('keydown', handleKeydown);
}
2 changes: 2 additions & 0 deletions netbox/project-static/src/netbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { initDashboard } from './dashboard';
import { initRackElevation } from './racks';
import { initHtmx } from './htmx';
import { initSavedFilterSelect } from './forms/savedFiltersSelect';
import { initHotkeys } from './hotkeys';

function initDocument(): void {
for (const init of [
Expand All @@ -33,6 +34,7 @@ function initDocument(): void {
initRackElevation,
initHtmx,
initSavedFilterSelect,
initHotkeys,
]) {
init();
}
Expand Down