2018-05-28 15:09:11 +02:00
|
|
|
import { SCROLLING_SPEED } from '../constants';
|
|
|
|
|
2017-08-29 13:56:54 +02:00
|
|
|
export default class List {
|
2018-05-21 18:01:03 +02:00
|
|
|
constructor({ element }) {
|
|
|
|
Object.assign(this, { element });
|
|
|
|
|
2017-08-29 13:56:54 +02:00
|
|
|
this.scrollPos = this.element.scrollTop;
|
|
|
|
this.height = this.element.offsetHeight;
|
|
|
|
this.hasChildren = !!this.element.children;
|
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
this.element.innerHTML = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
append(node) {
|
|
|
|
this.element.appendChild(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
getChild(selector) {
|
|
|
|
return this.element.querySelector(selector);
|
|
|
|
}
|
2018-05-28 15:09:11 +02:00
|
|
|
|
|
|
|
scrollToTop() {
|
|
|
|
this.element.scrollTop = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollToChoice(choice, direction) {
|
|
|
|
if (!choice) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dropdownHeight = this.element.offsetHeight;
|
|
|
|
const choiceHeight = choice.offsetHeight;
|
|
|
|
// Distance from bottom of element to top of parent
|
|
|
|
const choicePos = choice.offsetTop + choiceHeight;
|
|
|
|
// Scroll position of dropdown
|
|
|
|
const containerScrollPos = this.element.scrollTop + dropdownHeight;
|
|
|
|
// Difference between the choice and scroll position
|
|
|
|
const endpoint =
|
|
|
|
direction > 0
|
|
|
|
? this.element.scrollTop + choicePos - containerScrollPos
|
|
|
|
: choice.offsetTop;
|
|
|
|
|
|
|
|
requestAnimationFrame(time => {
|
|
|
|
this._animateScroll(time, endpoint, direction);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_scrollDown(scrollPos, strength, endpoint) {
|
|
|
|
const easing = (endpoint - scrollPos) / strength;
|
|
|
|
const distance = easing > 1 ? easing : 1;
|
|
|
|
|
|
|
|
this.element.scrollTop = scrollPos + distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
_scrollUp(scrollPos, strength, endpoint) {
|
|
|
|
const easing = (scrollPos - endpoint) / strength;
|
|
|
|
const distance = easing > 1 ? easing : 1;
|
|
|
|
|
|
|
|
this.element.scrollTop = scrollPos - distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
_animateScroll(time, endpoint, direction) {
|
|
|
|
const strength = SCROLLING_SPEED;
|
|
|
|
const choiceListScrollTop = this.element.scrollTop;
|
|
|
|
let continueAnimation = false;
|
|
|
|
|
|
|
|
if (direction > 0) {
|
|
|
|
this._scrollDown(choiceListScrollTop, strength, endpoint);
|
|
|
|
|
|
|
|
if (choiceListScrollTop < endpoint) {
|
|
|
|
continueAnimation = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._scrollUp(choiceListScrollTop, strength, endpoint);
|
|
|
|
|
|
|
|
if (choiceListScrollTop > endpoint) {
|
|
|
|
continueAnimation = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (continueAnimation) {
|
2018-05-28 17:22:22 +02:00
|
|
|
requestAnimationFrame(() => {
|
2018-05-28 15:09:11 +02:00
|
|
|
this._animateScroll(time, endpoint, direction);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 13:56:54 +02:00
|
|
|
}
|