diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-06-25 22:30:42 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-06-25 22:30:42 +0300 |
commit | 0c1859477d1a39b3b2e75e039ef002a2b071290b (patch) | |
tree | 9ae15287640dec2ac3fe6867671eae41b331a51e | |
parent | 664811562a4d5b8b777b3ff7d3585793a9888490 (diff) |
Extract functions in keyhandler.js file
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
-rw-r--r-- | js/src/keyhandler.js | 58 |
1 files changed, 37 insertions, 21 deletions
diff --git a/js/src/keyhandler.js b/js/src/keyhandler.js index 5184964e38..561c9b169c 100644 --- a/js/src/keyhandler.js +++ b/js/src/keyhandler.js @@ -1,13 +1,13 @@ -// global var that holds: 0- if ctrl key is not pressed 1- if ctrl key is pressed -var ctrlKeyHistory = 0; +// var that holds: 0- if ctrl key is not pressed 1- if ctrl key is pressed +let ctrlKeyHistory = 0; /** * Allows moving around inputs/select by Ctrl+arrows * * @param {object} event data */ -function onKeyDownArrowsHandler (event) { +const onKeyDownArrowsHandler = function (event) { var e = event || window.event; var o = (e.srcElement || e.target); @@ -76,13 +76,13 @@ function onKeyDownArrowsHandler (event) { var id = 'field_' + y + '_' + x; var nO = document.getElementById(id); - if (! nO) { + if (!nO) { id = 'field_' + y + '_' + x + '_0'; nO = document.getElementById(id); } // skip non existent fields - if (! nO) { + if (!nO) { return; } @@ -95,7 +95,7 @@ function onKeyDownArrowsHandler (event) { if (isFirefox) { var ffcheck = 0; var ffversion; - for (ffversion = 3 ; ffversion < 25 ; ffversion++) { + for (ffversion = 3; ffversion < 25; ffversion++) { var isFirefoxV24 = navigator.userAgent.toLowerCase().indexOf('firefox/' + ffversion) > -1; if (isFirefoxV24) { ffcheck = 1; @@ -123,18 +123,34 @@ function onKeyDownArrowsHandler (event) { nO.select(); } e.returnValue = false; -} - -window.AJAX.registerTeardown('keyhandler.js', function () { - $(document).off('keydown keyup', '#table_columns'); - $(document).off('keydown keyup', 'table.insertRowTable'); -}); - -window.AJAX.registerOnload('keyhandler.js', function () { - $(document).on('keydown keyup', '#table_columns', function (event) { - onKeyDownArrowsHandler(event.originalEvent); - }); - $(document).on('keydown keyup', 'table.insertRowTable', function (event) { - onKeyDownArrowsHandler(event.originalEvent); - }); -}); +}; + +const KeyHandlerEvents = { + /** + * @return {function} + */ + off: function () { + return function () { + $(document).off('keydown keyup', '#table_columns'); + $(document).off('keydown keyup', 'table.insertRowTable'); + }; + }, + /** + * @return {function} + */ + on: function () { + return function () { + $(document).on('keydown keyup', '#table_columns', function (event) { + onKeyDownArrowsHandler(event.originalEvent); + }); + $(document).on('keydown keyup', 'table.insertRowTable', function (event) { + onKeyDownArrowsHandler(event.originalEvent); + }); + }; + } +}; + +window.KeyHandlerEvents = KeyHandlerEvents; + +window.AJAX.registerTeardown('keyhandler.js', window.KeyHandlerEvents.off()); +window.AJAX.registerOnload('keyhandler.js', window.KeyHandlerEvents.on()); |