diff --git a/asset/js/functions.js b/asset/js/functions.js new file mode 100644 index 00000000..359720d6 --- /dev/null +++ b/asset/js/functions.js @@ -0,0 +1,21 @@ +(function (root, factory) { + "use strict"; + + if (typeof define === "function" && define.icinga) { + define(["exports"], factory); + } else { + factory(root.iplWebFunctions = root.iplWebFunctions || {}); + } +}(self, function (exports) { + /** + * Checks if the given keyboard event represents a special key press. + * + * @param {KeyboardEvent} event - The keyboard event to check. + * @returns {boolean} True if the event represents a special key press, false otherwise. + */ + function isSpecialKeyPress(event) { + return event.key.length > 1 || event.ctrlKey || event.metaKey; + } + + exports.isSpecialKeyPress = isSpecialKeyPress; +})); diff --git a/asset/js/widget/BaseInput.js b/asset/js/widget/BaseInput.js index 360d5026..87598e03 100644 --- a/asset/js/widget/BaseInput.js +++ b/asset/js/widget/BaseInput.js @@ -1,4 +1,4 @@ -define(["../notjQuery", "Completer"], function ($, Completer) { +define(["../notjQuery", "../functions", "Completer"], function ($, functions, Completer) { "use strict"; @@ -803,7 +803,7 @@ define(["../notjQuery", "Completer"], function ($, Completer) { let input = event.target; let termIndex = Number(input.parentNode.dataset.index); - if (this.hasSyntaxError(input) && ! (/[A-Z]/.test(event.key.charAt(0)) || event.ctrlKey || event.metaKey)) { + if (this.hasSyntaxError(input) && ! functions.isSpecialKeyPress(event)) { // Clear syntax error flag if the user types entirely new input after having selected the entire input // (This way the input isn't empty but switches from input to input immediately, causing the clearing // in onInput to not work) diff --git a/asset/js/widget/Completer.js b/asset/js/widget/Completer.js index a11548a3..d6d36b0d 100644 --- a/asset/js/widget/Completer.js +++ b/asset/js/widget/Completer.js @@ -695,18 +695,6 @@ define(["../notjQuery"], function ($) { event.preventDefault(); this.moveToSuggestion(); } - - break; - default: - if (/[A-Z]/.test(event.key.charAt(0)) || event.key === '"') { - // Ignore control keys not resulting in new input data - break; - } - - let typedSuggestion = this.termSuggestions.querySelector(`[value="${ event.key }"]`); - if (typedSuggestion !== null) { - this.hideSuggestions(); - } } } diff --git a/asset/js/widget/FilterInput.js b/asset/js/widget/FilterInput.js index 3f1d9478..51cb0f78 100644 --- a/asset/js/widget/FilterInput.js +++ b/asset/js/widget/FilterInput.js @@ -1,4 +1,4 @@ -define(["../notjQuery", "BaseInput"], function ($, BaseInput) { +define(["../notjQuery", "../functions", "BaseInput"], function ($, functions, BaseInput) { "use strict"; @@ -1350,7 +1350,7 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) { } else if (input.selectionStart !== input.selectionEnd) { // In case the user selected a range of text, do nothing return; - } else if (/[A-Z]/.test(event.key.charAt(0)) || event.ctrlKey || event.metaKey) { + } else if (functions.isSpecialKeyPress(event)) { // Ignore control keys not resulting in new input data // TODO: Remove this and move the entire block into `onInput` // once Safari supports `InputEvent.data`