Bind events in image viewer only once

This commit is contained in:
Pavel Djundik 2020-03-10 15:36:58 +02:00
parent ef473b0f53
commit 64aa510abf
1 changed files with 12 additions and 14 deletions

View File

@ -76,34 +76,32 @@ export default {
}, },
}, },
watch: { watch: {
link() { link(newLink, oldLink) {
// TODO: history.pushState // 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; return;
} }
this.setPrevNextImages(); 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: { methods: {
closeViewer() { closeViewer() {
if (this.link === null) { if (this.link === null) {
return; return;
} }
this.$root.$off("resize", this.correctPosition);
this.channel = null; this.channel = null;
this.previousImage = null; this.previousImage = null;
this.nextImage = null; this.nextImage = null;