From 3b9e389616ea3980b449ca15467de03c5f5b9d44 Mon Sep 17 00:00:00 2001 From: Mathieu Morainville Date: Tue, 13 Mar 2018 11:16:47 +0100 Subject: [PATCH] fix: remove the side-effects by removing only our event listener --- src/components/VTour.vue | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/components/VTour.vue b/src/components/VTour.vue index 64bd44e..2efa0fd 100644 --- a/src/components/VTour.vue +++ b/src/components/VTour.vue @@ -56,28 +56,13 @@ export default { this.$tours[this.name] = this if (this.customOptions.useKeyboardNavigation) { - window.addEventListener('keyup', e => { - // TODO: debug mode - // console.log('[Vue Tour] A keyup event occured:', e) - switch (e.keyCode) { - case KEYS.ARROW_RIGHT: - this.nextStep() - break - case KEYS.ARROW_LEFT: - this.previousStep() - break - case KEYS.ESCAPE: - this.stop() - break - } - }) + window.addEventListener('keyup', this.handleKeyup) } }, beforeDestroy () { - // Remove the keyup listener if it has been defined. - // Might have side-effects if the user already defined a listener on keyup. + // Remove the keyup listener if it has been defined if (this.customOptions.useKeyboardNavigation) { - window.removeEventListener('keyup') + window.removeEventListener('keyup', this.handleKeyup) } }, computed: { @@ -118,6 +103,22 @@ export default { }, stop () { this.currentStep = -1 + }, + + handleKeyup (e) { + // TODO: debug mode + // console.log('[Vue Tour] A keyup event occured:', e) + switch (e.keyCode) { + case KEYS.ARROW_RIGHT: + this.nextStep() + break + case KEYS.ARROW_LEFT: + this.previousStep() + break + case KEYS.ESCAPE: + this.stop() + break + } } } }