From a6db997391493f1fc62b924df6e438eeb31987c6 Mon Sep 17 00:00:00 2001 From: nklhrstv Date: Fri, 15 May 2020 15:03:16 +0300 Subject: [PATCH] stateChanged event emitted from KeyboardNavigation --- .../KeyboardNavigation/KeyboardNavigation.js | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/services/KeyboardNavigation/KeyboardNavigation.js b/src/services/KeyboardNavigation/KeyboardNavigation.js index 2225ce185..ded82ce20 100644 --- a/src/services/KeyboardNavigation/KeyboardNavigation.js +++ b/src/services/KeyboardNavigation/KeyboardNavigation.js @@ -1,8 +1,13 @@ // Copyright (C) 2017-2020 Smart code 203358507 +const EventEmitter = require('events'); + function KeyboardNavigation() { let active = false; + const events = new EventEmitter(); + events.on('error', () => { }); + function onKeyDown(event) { if (event.keyboardNavigationPrevented || event.target.tagName === 'INPUT') { return; @@ -61,17 +66,8 @@ function KeyboardNavigation() { } } } - function start() { - if (active) { - return; - } - - window.addEventListener('keydown', onKeyDown); - active = true; - } - function stop() { - window.removeEventListener('keydown', onKeyDown); - active = false; + function onStateChanged() { + events.emit('stateChanged'); } Object.defineProperties(this, { @@ -84,8 +80,20 @@ function KeyboardNavigation() { } }); - this.start = start; - this.stop = stop; + this.start = function() { + if (active) { + return; + } + + window.addEventListener('keydown', onKeyDown); + active = true; + onStateChanged(); + }; + this.stop = function() { + window.removeEventListener('keydown', onKeyDown); + active = false; + onStateChanged(); + }; } module.exports = KeyboardNavigation;