Add newTab button option

This commit is contained in:
Thomas Brouard 2016-11-01 09:58:28 +01:00
parent 6b644b3b18
commit 35bbcf88ea

View file

@ -26,21 +26,34 @@ if (!document) {
})(); })();
class TabGroup { class TabGroup {
constructor (args) { constructor (args = {}) {
let options = this.options = { let options = this.options = {
tabContainerSelector: args.tabContainerSelector || ".tabs-tabcontainer", tabContainerSelector: args.tabContainerSelector || ".tabs-tabcontainer",
buttonsContainerSelector: args.buttonsContainerSelector || ".tabs-buttonscontainer",
viewContainerSelector: args.viewContainerSelector || ".tabs-viewcontainer", viewContainerSelector: args.viewContainerSelector || ".tabs-viewcontainer",
tabClass: args.tabClass || "tabs-tab", tabClass: args.tabClass || "tabs-tab",
viewClass: args.viewClass || "tabs-view", viewClass: args.viewClass || "tabs-view",
closeButtonText: args.closeButtonText || "❌" closeButtonText: args.closeButtonText || "❌",
newTab: args.newTab,
newTabButtonText: args.newTabButton || "+"
}; };
this.tabContainer = document.querySelector(options.tabContainerSelector); this.tabContainer = document.querySelector(options.tabContainerSelector);
this.viewContainer = document.querySelector(options.viewContainerSelector); this.viewContainer = document.querySelector(options.viewContainerSelector);
this.tabs = []; this.tabs = [];
this.newTabId = 0; 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; let id = this.newTabId;
this.newTabId++; this.newTabId++;
let tab = new Tab(this, id, args); let tab = new Tab(this, id, args);