From 6c2ead8edd5bbafeb9908ed133e7748561504a5e Mon Sep 17 00:00:00 2001 From: W Etheredge Date: Sun, 5 Nov 2017 21:38:01 -0600 Subject: [PATCH] Add the ability to get tabs by a relative position. Made 0 be an invalid position. --- README.md | 38 ++++++++++++++++++++++---------------- index.js | 22 +++++++++++++++++++--- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 508280f..2726634 100644 --- a/README.md +++ b/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)` diff --git a/index.js b/index.js index 7e29ab2..c862a2f 100644 --- a/index.js +++ b/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;