Skip to content

Commit

Permalink
Properly scroll to contact
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Mar 27, 2019
1 parent 93a774a commit 606bcc1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
14 changes: 1 addition & 13 deletions src/components/ContactDetails.vue
Expand Up @@ -403,7 +403,7 @@ export default {
},
/**
* Select a contac, and update the localContact
* Select a contact, and update the localContact
* Fetch updated data if necessary
* Scroll to the selected contact if exists
*
Expand Down Expand Up @@ -455,18 +455,6 @@ export default {
this.localContact = localContact
this.loadingData = false
}
// scroll to selected contact if any
let list = document.getElementById('contacts-list')
let item = document.querySelector('#' + btoa(contact.key).slice(0, -2))
if (item) {
let isAbove = list.scrollTop > item.offsetTop
let isUnder = item.offsetTop + item.offsetHeight > list.scrollTop + list.offsetHeight
// check if contact outside visible list area
if (isAbove || isUnder) {
list.scrollTo(0, item.offsetTop - item.offsetHeight / 2)
}
}
}
},
Expand Down
61 changes: 57 additions & 4 deletions src/components/ContactsList.vue
Expand Up @@ -24,13 +24,16 @@
<!-- same uid can coexists between different addressbooks
so we need to use the addressbook id as key as well -->
<recycle-scroller
id="contacts-list" :class="{'icon-loading': loading, showdetails: selectedContact}" class="app-content-list"
id="contacts-list"
ref="scroller"
:class="{'icon-loading': loading, showdetails: selectedContact}"
class="app-content-list"
:items="list"
:item-size="68"
:item-size="itemHeight"
key-field="key">
<template v-slot="{ item, index }">
<contacts-list-item :contact="contacts[item.key]"
:search-query="searchQuery" :index="index" :key="item.key"
<contacts-list-item :key="item.key"
:contact="contacts[item.key]" :search-query="searchQuery" :index="index"
@deleted="selectContact" />
</template>
</recycle-scroller>
Expand Down Expand Up @@ -68,6 +71,12 @@ export default {
}
},
data() {
return {
itemHeight: 68
}
},
computed: {
selectedContact() {
return this.$route.params.selectedContact
Expand All @@ -77,6 +86,24 @@ export default {
}
},
watch: {
selectedContact: function(key) {
this.$nextTick(() => {
this.scrollToContact(key)
})
},
list: function(val, old) {
// we just loaded the list and the url already have a selected contact
// if not, the selectedContact watcher will take over
// to select the first entry
if (val.length !== 0 && old.length === 0 && this.selectedContact) {
this.$nextTick(() => {
this.scrollToContact(this.selectedContact)
})
}
}
},
methods: {
// Select closest contact on deletion
selectContact(oldIndex) {
Expand All @@ -87,6 +114,32 @@ export default {
this.$router.push({ name: 'contact', params: { selectedGroup: this.selectedGroup, selectedContact: newContact.key } })
}
}
},
/**
* Scroll to the desired contact if in the list and not visible
*
* @param {String} key the contact unique key
*/
scrollToContact(key) {
const item = this.$el.querySelector('#' + btoa(key).slice(0, -2))
// if the item is not visible in the list or barely visible
if (!(item && item.getBoundingClientRect().y > 50)) { // header height
const index = this.list.findIndex(contact => contact.key === key)
if (index > -1) {
this.$refs.scroller.scrollToItem(index)
}
}
// if item is a bit out (bottom) of the list, let's just scroll a bit to the top
if (item) {
const pos = item.getBoundingClientRect().y + this.itemHeight - (this.$el.offsetHeight + 50)
if (pos > 0) {
const scroller = this.$refs.scroller.$el
scroller.scrollTop = scroller.scrollTop + pos
}
}
}
}
}
Expand Down

0 comments on commit 606bcc1

Please sign in to comment.