diff --git a/README.md b/README.md index 9ad9e82..c724ea2 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,31 @@ Add a new tab to the tab group and returns a `Tab` instance. #### `tabGroup.getTab(id)` -Retrieve an instance of `Tab` from its `id` (return `null` if not found). +Retrieve an instance of `Tab` from this `id` (return `null` if not found). + +#### `tabGroup.getTabByPosition(position)` + +Retrieve an instance of `Tab` from this `position` (return `null` if not found). A negative value is an offset from the right. + +To get the tab in the leftmost position: + +```javascript +tabGroup.getTabByPosition(1); +``` + +To get the tab in the rightmost position: + +```javascript +tabGroup.getTabByPosition(-1); +``` + +> Note: Position 0 does not contain a tab. + +#### `tabGroup.getTabByRelPosition(position)` + +Retrieve an instance of `Tab` from this `position` relative to the active tab (return `null` if not found). +`tabGroup.getNextTab()` is an alias to `tabGroup.getTabByRelPosition(1)`. +`tabGroup.getPreviousTab()` is an alias to `tabGroup.getTabByRelPosition(-1)`. #### `tabGroup.getActiveTab()` @@ -138,21 +162,7 @@ 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); -``` +Move tab to the specified position. If `position` is 0 then `null` is returned and nothing happens. See [`tabGroup.getTabByPosition`](#tabgroupgettabbypositionposition) for information about positions. #### `tab.getPosition(fromRight)` diff --git a/index.js b/index.js index 10e9991..495ec6a 100644 --- a/index.js +++ b/index.js @@ -76,6 +76,32 @@ class TabGroup extends EventEmitter { return null; } + getTabByPosition (position) { + let fromRight = position < 0; + for (let i in this.tabs) { + if (this.tabs[i].getPosition(fromRight) === position) { + return this.tabs[i]; + } + } + return null; + } + + getTabByRelPosition (position) { + position = this.getActiveTab().getPosition() + position; + if (position <= 0) { + return null; + } + return this.getTabByPosition(position); + } + + getNextTab () { + return this.getTabByRelPosition(1); + } + + getPreviousTab () { + return this.getTabByRelPosition(-1); + } + getTabs () { return this.tabs; } @@ -212,7 +238,7 @@ class Tab extends EventEmitter { setPosition (newPosition) { let tabContainer = this.tabGroup.tabContainer; let tabs = tabContainer.children; - let oldPosition = this.getPosition(); + let oldPosition = this.getPosition() - 1; if (newPosition < 0) { newPosition += tabContainer.childElementCount; @@ -247,8 +273,8 @@ class Tab extends EventEmitter { position -= this.tabGroup.tabContainer.childElementCount; } - if (position === 0) { - position = 1; + if (position >= 0) { + position++; } return position;