From 4e36e14679aefaaa26e8b4324a986255621bc4c2 Mon Sep 17 00:00:00 2001 From: W Etheredge Date: Sat, 28 Oct 2017 01:55:11 +0100 Subject: [PATCH] Add ability to get tabs by position as well as by id --- index.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 10e9991..7e29ab2 100644 --- a/index.js +++ b/index.js @@ -67,7 +67,15 @@ class TabGroup extends EventEmitter { return tab; } - getTab (id) { + getTab (id, idIsPosition) { + if (idIsPosition !== true) { + return this.getTabById(id); + } else { + return this.getTabByPosition(id); + } + } + + getTabById (id) { for (let i in this.tabs) { if (this.tabs[i].id === id) { return this.tabs[i]; @@ -76,6 +84,19 @@ class TabGroup extends EventEmitter { return null; } + 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]; + } + } + return null; + } + getTabs () { return this.tabs; } @@ -212,7 +233,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 +268,8 @@ class Tab extends EventEmitter { position -= this.tabGroup.tabContainer.childElementCount; } - if (position === 0) { - position = 1; + if (position >= 0) { + position++; } return position;