Skip to content

Commit

Permalink
Minimize code just for search bar
Browse files Browse the repository at this point in the history
  • Loading branch information
janfaracik committed Jul 12, 2022
1 parent 47ccdfb commit c6bb510
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 99 deletions.
50 changes: 7 additions & 43 deletions war/src/main/js/keyboard-shortcuts.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,15 @@
import hotkeys from "hotkeys-js"

const registeredShortcuts = {}

window.addEventListener("load", () => {
hotkeys("*", ({key}) => {
if (key === "Meta") {
key = "CMD";
}

[...document.querySelectorAll(".jenkins-keyboard-shortcut__item")]
.filter(shortcut => shortcut.textContent.toLowerCase() === key.toLowerCase())
.forEach(shortcut => {
shortcut.classList.add("jenkins-keyboard-shortcut__item--chosen")

setTimeout(function() {
shortcut.classList.remove("jenkins-keyboard-shortcut__item--chosen");
}, 500);
})
})

document.querySelectorAll("[data-keyboard-shortcut]").forEach(function(element) {
const shortcut = translateKeyboardShortcutForOS(element.dataset.keyboardShortcut)

if (registeredShortcuts[shortcut]) {
console.warn(`Shortcut '${shortcut}' is already registered by`, element)
return
}

registeredShortcuts[shortcut] = element
const searchBar = document.querySelector("#search-box")
searchBar.placeholder = searchBar.placeholder + ` (${translateKeyboardShortcutForOS("CMD+K")
.replace("CMD", "⌘")})`

hotkeys(shortcut, () => {
// Small delay to show animation
setTimeout(function() {
switch (element.tagName) {
case "A":
case "BUTTON":
element.click()
break;
case "INPUT":
element.focus()
break;
}
hotkeys(translateKeyboardShortcutForOS("CMD+K"), () => {
searchBar.focus()

// Returning false stops the event and prevents default browser events
return false
}, 50);
})
// Returning false stops the event and prevents default browser events
return false
})
})

Expand Down
55 changes: 0 additions & 55 deletions war/src/main/less/modules/keyboard-shortcuts.less

This file was deleted.

1 change: 0 additions & 1 deletion war/src/main/less/styles.less
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
@import "./modules/content-blocks";
@import "./modules/icon-size";
@import "./modules/icons";
@import "./modules/keyboard-shortcuts";
@import "./modules/page-footer";
@import "./modules/page-header";
@import "./modules/panes-and-bigtable";
Expand Down

0 comments on commit c6bb510

Please sign in to comment.