Skip to content

Commit efc3f06

Browse files
committed
js: Add and utilize function isSpecialKeypress
This is a way better implementation instead of just checking whether the pressed key's label starts with an uppercase char. (e.g. `Escape`) Problem is, uppercase single chars also match… This could previously be observed when trying to type a value inside an operator input in the search bar. Normally this switches to the value input automatically, but an uppercase char was tried to identify as operator. In the `Completer` implementation this uncovered dead code on the other hand. `onKeyPress` seems to have had the job to hide the suggestions in case the user typed one manually. Upon testing this now with an corrected check (`event.key` alone does not match a suggestion as it's only single char) this worked but the suggestions appeared immediately again. I've removed it now as it was broken prior and doesn't make any sense either nowadays, suggestions need to be always visible in order to confirm the validity of the input.
1 parent 51be653 commit efc3f06

4 files changed

Lines changed: 25 additions & 16 deletions

File tree

asset/js/functions.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(function (root, factory) {
2+
"use strict";
3+
4+
if (typeof define === "function" && define.icinga) {
5+
define(["exports"], factory);
6+
} else {
7+
factory(root.iplWebFunctions = root.iplWebFunctions || {});
8+
}
9+
}(self, function (exports) {
10+
/**
11+
* Checks if the given keyboard event represents a special key press.
12+
*
13+
* @param {KeyboardEvent} event - The keyboard event to check.
14+
* @returns {boolean} True if the event represents a special key press, false otherwise.
15+
*/
16+
function isSpecialKeyPress(event) {
17+
return event.key.length > 1 || event.ctrlKey || event.metaKey;
18+
}
19+
20+
exports.isSpecialKeyPress = isSpecialKeyPress;
21+
}));

asset/js/widget/BaseInput.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
define(["../notjQuery", "Completer"], function ($, Completer) {
1+
define(["../notjQuery", "../functions", "Completer"], function ($, functions, Completer) {
22

33
"use strict";
44

@@ -803,7 +803,7 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
803803
let input = event.target;
804804
let termIndex = Number(input.parentNode.dataset.index);
805805

806-
if (this.hasSyntaxError(input) && ! (/[A-Z]/.test(event.key.charAt(0)) || event.ctrlKey || event.metaKey)) {
806+
if (this.hasSyntaxError(input) && ! functions.isSpecialKeyPress(event)) {
807807
// Clear syntax error flag if the user types entirely new input after having selected the entire input
808808
// (This way the input isn't empty but switches from input to input immediately, causing the clearing
809809
// in onInput to not work)

asset/js/widget/Completer.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -695,18 +695,6 @@ define(["../notjQuery"], function ($) {
695695
event.preventDefault();
696696
this.moveToSuggestion();
697697
}
698-
699-
break;
700-
default:
701-
if (/[A-Z]/.test(event.key.charAt(0)) || event.key === '"') {
702-
// Ignore control keys not resulting in new input data
703-
break;
704-
}
705-
706-
let typedSuggestion = this.termSuggestions.querySelector(`[value="${ event.key }"]`);
707-
if (typedSuggestion !== null) {
708-
this.hideSuggestions();
709-
}
710698
}
711699
}
712700

asset/js/widget/FilterInput.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
1+
define(["../notjQuery", "../functions", "BaseInput"], function ($, functions, BaseInput) {
22

33
"use strict";
44

@@ -1350,7 +1350,7 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
13501350
} else if (input.selectionStart !== input.selectionEnd) {
13511351
// In case the user selected a range of text, do nothing
13521352
return;
1353-
} else if (/[A-Z]/.test(event.key.charAt(0)) || event.ctrlKey || event.metaKey) {
1353+
} else if (functions.isSpecialKeyPress(event)) {
13541354
// Ignore control keys not resulting in new input data
13551355
// TODO: Remove this and move the entire block into `onInput`
13561356
// once Safari supports `InputEvent.data`

0 commit comments

Comments
 (0)