mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-10 09:52:18 +02:00
Refactor _scrollToChoice
This commit is contained in:
parent
1df8d81ae4
commit
a6bfdc0993
|
@ -1340,26 +1340,34 @@ class Choices {
|
||||||
(this.choiceList.element.scrollTop + choicePos) - containerScrollPos
|
(this.choiceList.element.scrollTop + choicePos) - containerScrollPos
|
||||||
) : choice.offsetTop;
|
) : choice.offsetTop;
|
||||||
|
|
||||||
|
const scrollDown = (scrollPos, strength) => {
|
||||||
|
const easing = (endPoint - scrollPos) / strength;
|
||||||
|
const distance = easing > 1 ? easing : 1;
|
||||||
|
|
||||||
|
this.choiceList.scrollTo(scrollPos + distance);
|
||||||
|
};
|
||||||
|
|
||||||
|
const scrollUp = (scrollPos, strength) => {
|
||||||
|
const easing = (scrollPos - endPoint) / strength;
|
||||||
|
const distance = easing > 1 ? easing : 1;
|
||||||
|
|
||||||
|
this.choiceList.scrollTo(scrollPos - distance);
|
||||||
|
};
|
||||||
|
|
||||||
const animateScroll = () => {
|
const animateScroll = () => {
|
||||||
const strength = SCROLLING_SPEED;
|
const strength = SCROLLING_SPEED;
|
||||||
const choiceListScrollTop = this.choiceList.element.scrollTop;
|
const choiceListScrollTop = this.choiceList.element.scrollTop;
|
||||||
let continueAnimation = false;
|
let continueAnimation = false;
|
||||||
let easing;
|
|
||||||
let distance;
|
|
||||||
|
|
||||||
if (direction > 0) {
|
if (direction > 0) {
|
||||||
easing = (endPoint - choiceListScrollTop) / strength;
|
scrollDown(choiceListScrollTop, strength);
|
||||||
distance = easing > 1 ? easing : 1;
|
|
||||||
|
|
||||||
this.choiceList.scrollTo(choiceListScrollTop + distance);
|
|
||||||
if (choiceListScrollTop < endPoint) {
|
if (choiceListScrollTop < endPoint) {
|
||||||
continueAnimation = true;
|
continueAnimation = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
easing = (choiceListScrollTop - endPoint) / strength;
|
scrollUp(choiceListScrollTop, strength);
|
||||||
distance = easing > 1 ? easing : 1;
|
|
||||||
|
|
||||||
this.choiceList.scrollTo(choiceListScrollTop - distance);
|
|
||||||
if (choiceListScrollTop > endPoint) {
|
if (choiceListScrollTop > endPoint) {
|
||||||
continueAnimation = true;
|
continueAnimation = true;
|
||||||
}
|
}
|
||||||
|
@ -1378,7 +1386,6 @@ class Choices {
|
||||||
}
|
}
|
||||||
|
|
||||||
_highlightChoice(el = null) {
|
_highlightChoice(el = null) {
|
||||||
// Highlight first element in dropdown
|
|
||||||
const choices = Array.from(this.dropdown.element.querySelectorAll('[data-choice-selectable]'));
|
const choices = Array.from(this.dropdown.element.querySelectorAll('[data-choice-selectable]'));
|
||||||
|
|
||||||
if (!choices.length) {
|
if (!choices.length) {
|
||||||
|
@ -1414,7 +1421,6 @@ class Choices {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Highlight given option, and set accessiblity attributes
|
|
||||||
passedEl.classList.add(this.config.classNames.highlightedState);
|
passedEl.classList.add(this.config.classNames.highlightedState);
|
||||||
passedEl.setAttribute('aria-selected', 'true');
|
passedEl.setAttribute('aria-selected', 'true');
|
||||||
|
|
||||||
|
@ -1622,10 +1628,8 @@ class Choices {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const templates = this.config.templates;
|
const { templates, classNames } = this.config;
|
||||||
const globalClasses = this.config.classNames;
|
return templates[template].call(this, classNames, ...args);
|
||||||
|
|
||||||
return templates[template].call(this, globalClasses, ...args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_createTemplates() {
|
_createTemplates() {
|
||||||
|
@ -1936,10 +1940,7 @@ class Choices {
|
||||||
}
|
}
|
||||||
|
|
||||||
_renderChoices() {
|
_renderChoices() {
|
||||||
// Get active groups/choices
|
const { activeGroups, activeChoices } = this._store;
|
||||||
const activeGroups = this._store.activeGroups;
|
|
||||||
const activeChoices = this._store.activeChoices;
|
|
||||||
|
|
||||||
let choiceListFragment = document.createDocumentFragment();
|
let choiceListFragment = document.createDocumentFragment();
|
||||||
|
|
||||||
// Clear choices
|
// Clear choices
|
||||||
|
|
Loading…
Reference in a new issue