diff --git a/README.md b/README.md index 1a9432c..9ad9e82 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,28 @@ Set tab icon (a iconURL or an icon must be given). Get current tab icon URL / icon. +#### `tab.setPosition(newPosition)` + +Move tab to the specified position. A negative value is an offset from the right. + +To move a tab to the leftmost position: + +```javascript +tab.setPosition(1); +``` + +> Note: a position of 0 also moves the tab to the leftmost position + +To move a tab to the rightmost position: + +```javascript +tab.setPosition(-1); +``` + +#### `tab.getPosition(fromRight)` + +Get the tab position. If `fromRight` is true the index returned is negative and is the offset from the right. + #### `tab.activate()` Activate this tab. The class "active" is added to the active tab. diff --git a/index.js b/index.js index 0092357..10e9991 100644 --- a/index.js +++ b/index.js @@ -209,6 +209,51 @@ class Tab extends EventEmitter { return this.icon; } + setPosition (newPosition) { + let tabContainer = this.tabGroup.tabContainer; + let tabs = tabContainer.children; + let oldPosition = this.getPosition(); + + if (newPosition < 0) { + newPosition += tabContainer.childElementCount; + + if (newPosition < 0) { + newPosition = 0; + } + } else { + if (newPosition > tabContainer.childElementCount) { + newPosition = tabContainer.childElementCount; + } + + // Make 1 be leftmost position + newPosition--; + } + + if (newPosition > oldPosition) { + newPosition++; + } + + tabContainer.insertBefore(tabs[oldPosition], tabs[newPosition]); + + return this; + } + + getPosition (fromRight) { + let position = 0; + let tab = this.tab; + while ((tab = tab.previousSibling) != null) position++; + + if (fromRight === true) { + position -= this.tabGroup.tabContainer.childElementCount; + } + + if (position === 0) { + position = 1; + } + + return position; + } + activate () { if (this.isClosed) return; let activeTab = this.tabGroup.getActiveTab();