From 64aa510abf6d260ce964827249ca6476358f676f Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Tue, 10 Mar 2020 15:36:58 +0200 Subject: [PATCH] Bind events in image viewer only once --- client/components/ImageViewer.vue | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/client/components/ImageViewer.vue b/client/components/ImageViewer.vue index c52d5211..4aa1aa84 100644 --- a/client/components/ImageViewer.vue +++ b/client/components/ImageViewer.vue @@ -76,34 +76,32 @@ export default { }, }, watch: { - link() { + link(newLink, oldLink) { // TODO: history.pushState - if (this.link === null) { + if (newLink === null) { + this.$root.$off("escapekey", this.closeViewer); + this.$root.$off("resize", this.correctPosition); + Mousetrap.unbind("left", this.previous); + Mousetrap.unbind("right", this.next); return; } this.setPrevNextImages(); - this.$root.$on("resize", this.correctPosition); + if (!oldLink) { + this.$root.$on("escapekey", this.closeViewer); + this.$root.$on("resize", this.correctPosition); + Mousetrap.bind("left", this.previous); + Mousetrap.bind("right", this.next); + } }, }, - mounted() { - this.$root.$on("escapekey", this.closeViewer); - Mousetrap.bind("left", this.previous); - Mousetrap.bind("right", this.next); - }, - destroyed() { - this.$root.$off("escapekey", this.closeViewer); - Mousetrap.unbind("left", this.previous); - Mousetrap.unbind("right", this.next); - }, methods: { closeViewer() { if (this.link === null) { return; } - this.$root.$off("resize", this.correctPosition); this.channel = null; this.previousImage = null; this.nextImage = null;