From 35bbcf88eac44907c1003679edc8114a0e3acd9b Mon Sep 17 00:00:00 2001 From: Thomas Brouard Date: Tue, 1 Nov 2016 09:58:28 +0100 Subject: [PATCH] Add newTab button option --- index.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index d1697da..6eab1c8 100644 --- a/index.js +++ b/index.js @@ -26,21 +26,34 @@ if (!document) { })(); class TabGroup { - constructor (args) { + constructor (args = {}) { let options = this.options = { tabContainerSelector: args.tabContainerSelector || ".tabs-tabcontainer", + buttonsContainerSelector: args.buttonsContainerSelector || ".tabs-buttonscontainer", viewContainerSelector: args.viewContainerSelector || ".tabs-viewcontainer", tabClass: args.tabClass || "tabs-tab", viewClass: args.viewClass || "tabs-view", - closeButtonText: args.closeButtonText || "❌" + closeButtonText: args.closeButtonText || "❌", + newTab: args.newTab, + newTabButtonText: args.newTabButton || "+" }; this.tabContainer = document.querySelector(options.tabContainerSelector); this.viewContainer = document.querySelector(options.viewContainerSelector); this.tabs = []; this.newTabId = 0; + this.initNewTabButton(); } - addTab (args) { + initNewTabButton () { + if (!this.options.newTab) return; + let container = document.querySelector(this.options.buttonsContainerSelector); + let button = container.appendChild(document.createElement("button")); + button.classList.add(`${this.options.tabClass}-button-new`); + button.innerHTML = this.options.newTabButtonText; + button.addEventListener("click", this.addTab.bind(this, undefined), false); + } + + addTab (args = this.options.newTab) { let id = this.newTabId; this.newTabId++; let tab = new Tab(this, id, args);