diff --git a/assets/js/components/Loadpoints.vue b/assets/js/components/Loadpoints.vue index 5e932cb53..6fbd2b2ea 100644 --- a/assets/js/components/Loadpoints.vue +++ b/assets/js/components/Loadpoints.vue @@ -27,7 +27,7 @@ :batteryConfigured="batteryConfigured" class="h-100" :class="{ 'loadpoint-unselected': !selected(index) }" - @click="scrollTo(index)" + @click="goTo(index)" /> @@ -37,7 +37,7 @@ :key="index" class="btn btn-sm btn-link p-0 mx-1 indicator d-flex justify-content-center align-items-center evcc-default-text" :class="{ 'indicator--selected': selected(index) }" - @click="scrollTo(index)" + @click="goTo(index)" > 0) { + this.$refs.carousel.scrollTo({ top: 0, left: this.left(this.selectedIndex) }); + } + this.$refs.carousel.addEventListener("scroll", this.handleCarouselScroll); }, unmounted() { if (this.$refs.carousel) { @@ -89,23 +99,33 @@ export default { handleCarouselScroll() { const { scrollLeft } = this.$refs.carousel; const { offsetWidth } = this.$refs.carousel.children[0]; - this.selectedIndex = Math.round((scrollLeft - 7.5) / offsetWidth); + this.highlightedIndex = Math.round((scrollLeft - 7.5) / offsetWidth); + + // save scroll position to url if not changing for 2s + clearTimeout(this.scrollTimeout); + this.scrollTimeout = setTimeout(() => { + if (this.highlightedIndex !== this.selectedIndex) { + this.$emit("index-changed", this.highlightedIndex); + } + }, 2000); + }, + goTo(index) { + this.$emit("index-changed", index); }, isCharging(lp) { return lp.charging && lp.chargePower > 0; }, selected(index) { - return this.selectedIndex === index; + return this.highlightedIndex === index; + }, + left(index) { + return this.$refs.carousel.children[0].offsetWidth * index; }, scrollTo(index) { - if (this.selectedIndex === index) { - return; - } - this.selectedIndex = index; + this.highlightedIndex = index; const $carousel = this.$refs.carousel; - const width = $carousel.children[0].offsetWidth; $carousel.style.scrollSnapType = "none"; - $carousel.scrollTo({ top: 0, left: 7.5 + width * index, behavior: "smooth" }); + $carousel.scrollTo({ top: 0, left: this.left(index), behavior: "smooth" }); clearTimeout(this.snapTimeout); this.snapTimeout = setTimeout(() => { diff --git a/assets/js/components/Site.vue b/assets/js/components/Site.vue index 217ea270c..160a25609 100644 --- a/assets/js/components/Site.vue +++ b/assets/js/components/Site.vue @@ -42,7 +42,7 @@
@@ -84,6 +86,7 @@ export default { mixins: [formatter, collector], props: { loadpoints: Array, + selectedLoadpointIndex: Number, notifications: Array, offline: Boolean, @@ -190,6 +193,11 @@ export default { }; }, }, + methods: { + selectedLoadpointChanged(index) { + this.$router.push({ query: { lp: index + 1 } }); + }, + }, };