From 8d3f6183a03e09633f8238e373e036ce2a53ccd6 Mon Sep 17 00:00:00 2001 From: W Etheredge Date: Thu, 26 Oct 2017 18:36:19 -0500 Subject: [PATCH 1/4] Add basic methods for tab position --- index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/index.js b/index.js index 0092357..89f1113 100644 --- a/index.js +++ b/index.js @@ -271,6 +271,27 @@ class Tab extends EventEmitter { TabGroupPrivate.activateRecentTab.bind(tabGroup)(); } } + + getPosition () { + let i = 0; + let tab = this.tab; + while ((tab = tab.previousSibling) != null) i++; + return i; + } + + setPosition (newPosition) { + let tabContainer = this.tabGroup.tabContainer; + let tabs = tabContainer.children; + let oldPosition = this.getPosition(); + + if (newPosition > oldPosition) { + newPosition++; + } + + tabContainer.insertBefore(tabs[oldPosition], tabs[newPosition]); + + return this; + } } const TabPrivate = { From f84908d4cbd3add6e08a3cbd45f262ec8d4670a8 Mon Sep 17 00:00:00 2001 From: W Etheredge Date: Thu, 26 Oct 2017 19:03:37 -0500 Subject: [PATCH 2/4] Documentation update (tab positioning) --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 1a9432c..ca38ccc 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,14 @@ Set tab icon (a iconURL or an icon must be given). Get current tab icon URL / icon. +#### `tab.setPosition(newPosition)` + +Move tab to the specified position. + +#### `tab.getPosition()` + +Get the tab position. + #### `tab.activate()` Activate this tab. The class "active" is added to the active tab. From 53dcb198f133f7569bf194cdf5dd7e8cbde495a6 Mon Sep 17 00:00:00 2001 From: W Etheredge Date: Thu, 26 Oct 2017 20:55:12 -0500 Subject: [PATCH 3/4] Add support for negative positions --- README.md | 20 ++++++++++++++--- index.js | 66 +++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 62 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index ca38ccc..9f8474a 100644 --- a/README.md +++ b/README.md @@ -138,11 +138,25 @@ Get current tab icon URL / icon. #### `tab.setPosition(newPosition)` -Move tab to the specified position. +Move tab to the specified position. A negative value is an offset from the right. -#### `tab.getPosition()` +To move a tab to the leftmost position: -Get the tab 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); +``` + +#### `tab.getPosition(fromRight)` + +Get the tab position. If `fromRight` is true, then the index returned is negative and is the offset from the right. #### `tab.activate()` diff --git a/index.js b/index.js index 89f1113..10e9991 100644 --- a/index.js +++ b/index.js @@ -209,6 +209,51 @@ class Tab extends EventEmitter { return this.icon; } + setPosition (newPosition) { + let tabContainer = this.tabGroup.tabContainer; + let tabs = tabContainer.children; + let oldPosition = this.getPosition(); + + if (newPosition < 0) { + newPosition += tabContainer.childElementCount; + + if (newPosition < 0) { + newPosition = 0; + } + } else { + if (newPosition > tabContainer.childElementCount) { + newPosition = tabContainer.childElementCount; + } + + // Make 1 be leftmost position + newPosition--; + } + + if (newPosition > oldPosition) { + newPosition++; + } + + tabContainer.insertBefore(tabs[oldPosition], tabs[newPosition]); + + return this; + } + + getPosition (fromRight) { + let position = 0; + let tab = this.tab; + while ((tab = tab.previousSibling) != null) position++; + + if (fromRight === true) { + position -= this.tabGroup.tabContainer.childElementCount; + } + + if (position === 0) { + position = 1; + } + + return position; + } + activate () { if (this.isClosed) return; let activeTab = this.tabGroup.getActiveTab(); @@ -271,27 +316,6 @@ class Tab extends EventEmitter { TabGroupPrivate.activateRecentTab.bind(tabGroup)(); } } - - getPosition () { - let i = 0; - let tab = this.tab; - while ((tab = tab.previousSibling) != null) i++; - return i; - } - - setPosition (newPosition) { - let tabContainer = this.tabGroup.tabContainer; - let tabs = tabContainer.children; - let oldPosition = this.getPosition(); - - if (newPosition > oldPosition) { - newPosition++; - } - - tabContainer.insertBefore(tabs[oldPosition], tabs[newPosition]); - - return this; - } } const TabPrivate = { From fda4fbd6129ecdf5b188080d455641e87ef1ad6c Mon Sep 17 00:00:00 2001 From: wetheredge <33011447+wetheredge@users.noreply.github.com> Date: Thu, 26 Oct 2017 21:13:19 -0500 Subject: [PATCH 4/4] Documentation update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9f8474a..9ad9e82 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ tab.setPosition(-1); #### `tab.getPosition(fromRight)` -Get the tab position. If `fromRight` is true, then the index returned is negative and is the offset from the right. +Get the tab position. If `fromRight` is true the index returned is negative and is the offset from the right. #### `tab.activate()`