Use 0-based index in tab positions (fixes #46)
This commit is contained in:
parent
15812d2e1f
commit
5fc15ab42b
5 changed files with 20 additions and 33 deletions
30
src/index.ts
30
src/index.ts
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue