Add events

This commit is contained in:
Thomas Brouard 2016-11-01 10:56:07 +01:00
parent 51777efe21
commit 0f69a9f441

View file

@ -1,3 +1,5 @@
const EventEmitter = require("events");
if (!document) { if (!document) {
throw Error("electron-tabs module must be called in renderer process"); throw Error("electron-tabs module must be called in renderer process");
} }
@ -25,8 +27,9 @@ if (!document) {
document.getElementsByTagName("head")[0].appendChild(styleTag); document.getElementsByTagName("head")[0].appendChild(styleTag);
})(); })();
class TabGroup { class TabGroup extends EventEmitter {
constructor (args = {}) { constructor (args = {}) {
super();
let options = this.options = { let options = this.options = {
tabContainerSelector: args.tabContainerSelector || ".tabs-tabcontainer", tabContainerSelector: args.tabContainerSelector || ".tabs-tabcontainer",
buttonsContainerSelector: args.buttonsContainerSelector || ".tabs-buttonscontainer", buttonsContainerSelector: args.buttonsContainerSelector || ".tabs-buttonscontainer",
@ -58,10 +61,11 @@ class TabGroup {
this.newTabId++; this.newTabId++;
let tab = new Tab(this, id, args); let tab = new Tab(this, id, args);
this.tabs.push(tab); this.tabs.push(tab);
this.emit("tab-added", tab, this);
return tab; return tab;
} }
removeTab (tab) { removeTab (tab, triggerEvent) {
let id = tab.id; let id = tab.id;
for (let i in this.tabs) { for (let i in this.tabs) {
if (this.tabs[i].id === id) { if (this.tabs[i].id === id) {
@ -69,11 +73,15 @@ class TabGroup {
break; break;
} }
} }
if (triggerEvent) {
this.emit("tab-removed", tab, this);
}
} }
setActiveTab (tab) { setActiveTab (tab) {
this.removeTab(tab); this.removeTab(tab);
this.tabs.unshift(tab); this.tabs.unshift(tab);
this.emit("tab-active", tab, this);
} }
getActiveTab () { getActiveTab () {
@ -87,8 +95,9 @@ class TabGroup {
} }
} }
class Tab { class Tab extends EventEmitter {
constructor (tabGroup, id, args) { constructor (tabGroup, id, args) {
super();
this.tabGroup = tabGroup; this.tabGroup = tabGroup;
this.id = id; this.id = id;
this.title = args.title; this.title = args.title;
@ -137,6 +146,7 @@ class Tab {
let span = this.tabElements.title; let span = this.tabElements.title;
span.innerHTML = title; span.innerHTML = title;
this.title = title; this.title = title;
this.emit("title-changed", title, this);
} }
getTitle () { getTitle () {
@ -149,6 +159,7 @@ class Tab {
if (iconURL) { if (iconURL) {
span.innerHTML = `<img src="${iconURL}" />`; span.innerHTML = `<img src="${iconURL}" />`;
} }
this.emit("icon-changed", iconURL, this);
} }
getIcon () { getIcon () {
@ -183,13 +194,16 @@ class Tab {
this.tabGroup.setActiveTab(this); this.tabGroup.setActiveTab(this);
this.tab.classList.add("active"); this.tab.classList.add("active");
this.webview.classList.add("visible"); this.webview.classList.add("visible");
this.emit("active", this);
} }
flash (flag) { flash (flag) {
if (flag !== false) { if (flag !== false) {
this.tab.classList.add("flash"); this.tab.classList.add("flash");
this.emit("flash-start", this);
} else { } else {
this.tab.classList.remove("flash"); this.tab.classList.remove("flash");
this.emit("flash-end", this);
} }
} }
@ -202,7 +216,8 @@ class Tab {
let tabGroup = this.tabGroup; let tabGroup = this.tabGroup;
tabGroup.tabContainer.removeChild(this.tab); tabGroup.tabContainer.removeChild(this.tab);
tabGroup.viewContainer.removeChild(this.webview); tabGroup.viewContainer.removeChild(this.webview);
tabGroup.removeTab(this); tabGroup.removeTab(this, true);
this.emit("close", this);
tabGroup.activateRecentTab(); tabGroup.activateRecentTab();
} }
} }