From 4e36e14679aefaaa26e8b4324a986255621bc4c2 Mon Sep 17 00:00:00 2001 From: W Etheredge Date: Sat, 28 Oct 2017 01:55:11 +0100 Subject: [PATCH 1/7] 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; From 88c5a4c2fadfcfc4f254aeb6050603cfa54b203b Mon Sep 17 00:00:00 2001 From: W Etheredge Date: Sat, 28 Oct 2017 02:11:46 +0100 Subject: [PATCH 2/7] Documentation update (getting tabs by position) --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9ad9e82..508280f 100644 --- a/README.md +++ b/README.md @@ -86,9 +86,17 @@ Add a new tab to the tab group and returns a `Tab` instance. * `active` (default: `false`): set this to `true` if you want to activate the tab once it is loaded. Otherwise you will need to call `tab.activate()`. * `ready`: a callback function to call once the tab is ready. The `Tab` instance is passed as the only parameter. -#### `tabGroup.getTab(id)` +#### `tabGroup.getTab(id, idIsPosition)` -Retrieve an instance of `Tab` from its `id` (return `null` if not found). +If `idIsPosition` is true `id` is passed to `tabGroup.getTabByPosition`. Otherwise, `id` is passed to `tabGroup.getTabById`. + +#### `tabGroup.getTabById(id)` + +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). #### `tabGroup.getActiveTab()` From 6c2ead8edd5bbafeb9908ed133e7748561504a5e Mon Sep 17 00:00:00 2001 From: W Etheredge Date: Sun, 5 Nov 2017 21:38:01 -0600 Subject: [PATCH 3/7] 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; From ed9e63e92ffcfb04399023ac854dd8bb15246476 Mon Sep 17 00:00:00 2001 From: W Etheredge Date: Tue, 7 Nov 2017 14:20:40 +0000 Subject: [PATCH 4/7] Make tab.setPosition not do anything if nothing is passed --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index c862a2f..6093ef2 100644 --- a/index.js +++ b/index.js @@ -244,7 +244,7 @@ class Tab extends EventEmitter { } setPosition (newPosition) { - if (newPosition === 0) { + if (newPosition === 0 || newPosition === undefined) { return null; } let tabContainer = this.tabGroup.tabContainer; From b0534b9fc7280ee0ca5b76c10df26b9f3b422907 Mon Sep 17 00:00:00 2001 From: W Etheredge Date: Thu, 16 Nov 2017 14:13:23 +0000 Subject: [PATCH 5/7] Change tabGroup.getTabById to tabGroup.getTab --- index.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/index.js b/index.js index 6093ef2..4870497 100644 --- a/index.js +++ b/index.js @@ -67,15 +67,7 @@ class TabGroup extends EventEmitter { return tab; } - getTab (id, idIsPosition) { - if (idIsPosition !== true) { - return this.getTabById(id); - } else { - return this.getTabByPosition(id); - } - } - - getTabById (id) { + getTab (id) { for (let i in this.tabs) { if (this.tabs[i].id === id) { return this.tabs[i]; From f1ef5f790c46303ccd025cd7d2216a25afa228b7 Mon Sep 17 00:00:00 2001 From: W Etheredge Date: Sun, 19 Nov 2017 03:58:07 +0000 Subject: [PATCH 6/7] Remove parameter check from tab.setPosition --- index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/index.js b/index.js index 4870497..495ec6a 100644 --- a/index.js +++ b/index.js @@ -236,9 +236,6 @@ class Tab extends EventEmitter { } setPosition (newPosition) { - if (newPosition === 0 || newPosition === undefined) { - return null; - } let tabContainer = this.tabGroup.tabContainer; let tabs = tabContainer.children; let oldPosition = this.getPosition() - 1; From 083d70cb3cec233f4f02148d38343e276761904f Mon Sep 17 00:00:00 2001 From: W Etheredge Date: Sun, 19 Nov 2017 09:31:26 -0600 Subject: [PATCH 7/7] Documentation update (getTabById -> getTab) --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index 2726634..c724ea2 100644 --- a/README.md +++ b/README.md @@ -86,11 +86,7 @@ Add a new tab to the tab group and returns a `Tab` instance. * `active` (default: `false`): set this to `true` if you want to activate the tab once it is loaded. Otherwise you will need to call `tab.activate()`. * `ready`: a callback function to call once the tab is ready. The `Tab` instance is passed as the only parameter. -#### `tabGroup.getTab(id, idIsPosition)` - -If `idIsPosition` is true `id` is passed to `tabGroup.getTabByPosition`. Otherwise, `id` is passed to `tabGroup.getTabById`. - -#### `tabGroup.getTabById(id)` +#### `tabGroup.getTab(id)` Retrieve an instance of `Tab` from this `id` (return `null` if not found).