Get corrects contacts on scroll

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ (skjnldsv) 2017-08-25 04:49:42 +02:00
parent 614efe2bef
commit 3ea9002268
No known key found for this signature in database
GPG Key ID: FB5ACEED51955BF8
1 changed files with 9 additions and 23 deletions

View File

@ -108,37 +108,23 @@ angular.module('contactsApp')
}
});
var getVisibleNames = function getVisibleNames() {
function isScrolledIntoView(el) {
var elemTop = el.getBoundingClientRect().top;
var elemBottom = el.getBoundingClientRect().bottom;
var getVisibleContacts = function() {
var scrolled = $('.app-content-list').scrollTop();
var elHeight = $('.contacts-list').children().outerHeight(true);
var listHeight = $('.app-content-list').height();
var bothAboveViewport = elemBottom < 0;
var bothBelowViewPort = elemTop > window.innerHeight;
var isVisible = !bothAboveViewport && !bothBelowViewPort;
return isVisible;
}
var topContact = Math.round(scrolled/elHeight-1);
var contactsCount = Math.round(listHeight/elHeight+1);
var elements = Array.prototype.slice.call(document.querySelectorAll('.contact__icon:not(.ng-hide)'));
var names = elements
.filter(isScrolledIntoView)
.map(function (el) {
var siblings = Array.prototype.slice.call(el.parentElement.children);
var nameElement = siblings.find(function (sibling) {
return sibling.getAttribute('class').indexOf('content-list-item-line-one') !== -1;
});
return nameElement.innerText;
});
return names;
return ctrl.contactList.slice(topContact, topContact+contactsCount);
};
var timeoutId = null;
document.querySelector('.app-content-list').addEventListener('scroll', function () {
clearTimeout(timeoutId);
timeoutId = setTimeout(function () {
var names = getVisibleNames();
ContactService.getFullContacts(names);
var contacts = getVisibleContacts();
ContactService.getFullContacts(contacts);
}, 250);
});