Add the ability to get tabs by a relative position.
Made 0 be an invalid position.
This commit is contained in:
parent
f618ab0c4d
commit
6c2ead8edd
2 changed files with 41 additions and 19 deletions
38
README.md
38
README.md
|
@ -96,7 +96,27 @@ 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).
|
||||
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()`
|
||||
|
||||
|
@ -146,21 +166,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)`
|
||||
|
||||
|
|
22
index.js
22
index.js
|
@ -86,9 +86,6 @@ class TabGroup extends EventEmitter {
|
|||
|
||||
getTabByPosition (position) {
|
||||
let fromRight = position < 0;
|
||||
if (position === 0) {
|
||||
position = 1;
|
||||
}
|
||||
for (let i in this.tabs) {
|
||||
if (this.tabs[i].getPosition(fromRight) === position) {
|
||||
return this.tabs[i];
|
||||
|
@ -97,6 +94,22 @@ class TabGroup extends EventEmitter {
|
|||
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;
|
||||
}
|
||||
|
@ -231,6 +244,9 @@ class Tab extends EventEmitter {
|
|||
}
|
||||
|
||||
setPosition (newPosition) {
|
||||
if (newPosition === 0) {
|
||||
return null;
|
||||
}
|
||||
let tabContainer = this.tabGroup.tabContainer;
|
||||
let tabs = tabContainer.children;
|
||||
let oldPosition = this.getPosition() - 1;
|
||||
|
|
Loading…
Reference in a new issue