Use 0-based index in tab positions (fixes #46)

This commit is contained in:
Thomas Brouard 2022-05-25 10:53:37 +02:00
parent 15812d2e1f
commit 5fc15ab42b
5 changed files with 20 additions and 33 deletions

View file

@ -460,30 +460,24 @@ class Tab extends EventTarget {
setPosition(newPosition: number) {
const tabContainer = this.tabGroup.tabContainer;
const tabs = tabContainer.children;
const oldPosition = this.getPosition() - 1;
const length = tabContainer.childElementCount;
const thisPosition = this.getPosition();
const tabs = Array.from(tabContainer.children)
tabs.splice(thisPosition, 1);
if (newPosition < 0) {
newPosition += tabContainer.childElementCount;
newPosition += length;
if (newPosition < 0) {
newPosition = 0;
}
}
if (newPosition < length) {
tabContainer.insertBefore(this.tab, tabs[newPosition]);
} else {
if (newPosition > tabContainer.childElementCount) {
newPosition = tabContainer.childElementCount;
}
// Make 1 be leftmost position
newPosition--;
tabContainer.appendChild(this.tab);
}
if (newPosition > oldPosition) {
newPosition++;
}
tabContainer.insertBefore(tabs[oldPosition], tabs[newPosition]);
return this;
}
@ -496,10 +490,6 @@ class Tab extends EventTarget {
position -= this.tabGroup.tabContainer.childElementCount;
}
if (position >= 0) {
position++;
}
return position;
}