2016-11-01 09:56:07 +00:00
|
|
|
const EventEmitter = require("events");
|
|
|
|
|
2016-10-31 14:53:50 +00:00
|
|
|
if (!document) {
|
2020-02-04 10:13:57 +00:00
|
|
|
throw Error("electron-tabs module must be called in renderer process");
|
2016-10-31 14:53:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inject styles
|
|
|
|
(function () {
|
2020-02-04 10:13:57 +00:00
|
|
|
const styles = `
|
|
|
|
webview {
|
|
|
|
position: absolute;
|
|
|
|
visibility: hidden;
|
2016-10-31 14:53:50 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
webview.visible {
|
2020-02-04 10:57:48 +00:00
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
2020-02-04 10:13:57 +00:00
|
|
|
visibility: visible;
|
2016-10-31 14:53:50 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
`;
|
|
|
|
let styleTag = document.createElement("style");
|
|
|
|
styleTag.innerHTML = styles;
|
|
|
|
document.getElementsByTagName("head")[0].appendChild(styleTag);
|
|
|
|
})();
|
2016-10-27 20:38:20 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
class TabGroup extends EventEmitter {
|
|
|
|
constructor (args = {}) {
|
|
|
|
super();
|
|
|
|
let options = this.options = {
|
|
|
|
tabContainerSelector: args.tabContainerSelector || ".etabs-tabs",
|
|
|
|
buttonsContainerSelector: args.buttonsContainerSelector || ".etabs-buttons",
|
|
|
|
viewContainerSelector: args.viewContainerSelector || ".etabs-views",
|
|
|
|
tabClass: args.tabClass || "etabs-tab",
|
|
|
|
viewClass: args.viewClass || "etabs-view",
|
2020-02-04 11:00:41 +00:00
|
|
|
closeButtonText: args.closeButtonText || "×",
|
2020-02-04 10:13:57 +00:00
|
|
|
newTab: args.newTab,
|
|
|
|
newTabButtonText: args.newTabButtonText || "+",
|
|
|
|
ready: args.ready
|
|
|
|
};
|
|
|
|
this.tabContainer = document.querySelector(options.tabContainerSelector);
|
|
|
|
this.viewContainer = document.querySelector(options.viewContainerSelector);
|
|
|
|
this.tabs = [];
|
|
|
|
this.newTabId = 0;
|
|
|
|
TabGroupPrivate.initNewTabButton.bind(this)();
|
|
|
|
if (typeof this.options.ready === "function") {
|
|
|
|
this.options.ready(this);
|
2016-11-01 13:43:42 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
}
|
2016-11-01 13:43:42 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
addTab (args = this.options.newTab) {
|
|
|
|
if (typeof args === "function") {
|
|
|
|
args = args(this);
|
2017-10-28 00:55:11 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
let id = this.newTabId;
|
|
|
|
this.newTabId++;
|
|
|
|
let tab = new Tab(this, id, args);
|
|
|
|
this.tabs.push(tab);
|
|
|
|
// Don't call tab.activate() before a tab is referenced in this.tabs
|
|
|
|
if (args.active === true) {
|
|
|
|
tab.activate();
|
2017-11-06 03:38:01 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
this.emit("tab-added", tab, this);
|
|
|
|
return tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTab (id) {
|
|
|
|
for (let i in this.tabs) {
|
|
|
|
if (this.tabs[i].id === id) {
|
|
|
|
return this.tabs[i];
|
|
|
|
}
|
2017-11-06 03:38:01 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTabByPosition (position) {
|
|
|
|
let fromRight = position < 0;
|
|
|
|
for (let i in this.tabs) {
|
|
|
|
if (this.tabs[i].getPosition(fromRight) === position) {
|
|
|
|
return this.tabs[i];
|
|
|
|
}
|
2017-11-06 03:38:01 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
2017-11-06 03:38:01 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
getTabByRelPosition (position) {
|
|
|
|
position = this.getActiveTab().getPosition() + position;
|
|
|
|
if (position <= 0) {
|
|
|
|
return null;
|
2016-11-01 13:40:03 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
return this.getTabByPosition(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
getNextTab () {
|
|
|
|
return this.getTabByRelPosition(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
getPreviousTab () {
|
|
|
|
return this.getTabByRelPosition(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
getTabs () {
|
|
|
|
return this.tabs.slice();
|
|
|
|
}
|
|
|
|
|
|
|
|
eachTab (fn) {
|
|
|
|
this.getTabs().forEach(fn);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
getActiveTab () {
|
|
|
|
if (this.tabs.length === 0) return null;
|
|
|
|
return this.tabs[0];
|
|
|
|
}
|
2016-11-01 13:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const TabGroupPrivate = {
|
2020-02-04 10:13:57 +00:00
|
|
|
initNewTabButton: function () {
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeTab: function (tab, triggerEvent) {
|
|
|
|
let id = tab.id;
|
|
|
|
for (let i in this.tabs) {
|
|
|
|
if (this.tabs[i].id === id) {
|
|
|
|
this.tabs.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (triggerEvent) {
|
|
|
|
this.emit("tab-removed", tab, this);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setActiveTab: function (tab) {
|
|
|
|
TabGroupPrivate.removeTab.bind(this)(tab);
|
|
|
|
this.tabs.unshift(tab);
|
|
|
|
this.emit("tab-active", tab, this);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
activateRecentTab: function (tab) {
|
|
|
|
if (this.tabs.length > 0) {
|
|
|
|
this.tabs[0].activate();
|
2016-10-31 14:53:50 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
return this;
|
|
|
|
}
|
2016-11-01 13:40:03 +00:00
|
|
|
};
|
2016-10-31 14:53:50 +00:00
|
|
|
|
2016-11-01 09:56:07 +00:00
|
|
|
class Tab extends EventEmitter {
|
2020-02-04 10:13:57 +00:00
|
|
|
constructor (tabGroup, id, args) {
|
|
|
|
super();
|
|
|
|
this.tabGroup = tabGroup;
|
|
|
|
this.id = id;
|
|
|
|
this.title = args.title;
|
|
|
|
this.badge = args.badge;
|
|
|
|
this.iconURL = args.iconURL;
|
|
|
|
this.icon = args.icon;
|
|
|
|
this.closable = args.closable === false ? false : true;
|
|
|
|
this.webviewAttributes = args.webviewAttributes || {};
|
|
|
|
this.webviewAttributes.src = args.src;
|
|
|
|
this.tabElements = {};
|
|
|
|
TabPrivate.initTab.bind(this)();
|
|
|
|
TabPrivate.initWebview.bind(this)();
|
|
|
|
if (args.visible !== false) {
|
|
|
|
this.show();
|
2016-10-31 14:53:50 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
if (typeof args.ready === "function") {
|
|
|
|
args.ready(this);
|
2016-10-31 14:53:50 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setTitle (title) {
|
|
|
|
if (this.isClosed) return;
|
|
|
|
let span = this.tabElements.title;
|
|
|
|
span.innerHTML = title;
|
|
|
|
span.title = title;
|
|
|
|
this.title = title;
|
|
|
|
this.emit("title-changed", title, this);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTitle () {
|
|
|
|
if (this.isClosed) return;
|
|
|
|
return this.title;
|
|
|
|
}
|
|
|
|
|
|
|
|
setBadge (badge) {
|
|
|
|
if (this.isClosed) return;
|
|
|
|
let span = this.tabElements.badge;
|
|
|
|
this.badge = badge;
|
|
|
|
|
|
|
|
if (badge) {
|
|
|
|
span.innerHTML = badge;
|
|
|
|
span.classList.remove('hidden');
|
|
|
|
} else {
|
|
|
|
span.classList.add('hidden');
|
2016-10-31 14:53:50 +00:00
|
|
|
}
|
2017-09-14 15:23:49 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
this.emit("badge-changed", badge, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
getBadge () {
|
|
|
|
if (this.isClosed) return;
|
|
|
|
return this.badge;
|
|
|
|
}
|
|
|
|
|
|
|
|
setIcon (iconURL, icon) {
|
|
|
|
if (this.isClosed) return;
|
|
|
|
this.iconURL = iconURL;
|
|
|
|
this.icon = icon;
|
|
|
|
let span = this.tabElements.icon;
|
|
|
|
if (iconURL) {
|
|
|
|
span.innerHTML = `<img src="${iconURL}" />`;
|
|
|
|
this.emit("icon-changed", iconURL, this);
|
|
|
|
} else if (icon) {
|
|
|
|
span.innerHTML = `<i class="${icon}"></i>`;
|
|
|
|
this.emit("icon-changed", icon, this);
|
2017-06-03 13:01:10 +00:00
|
|
|
}
|
2017-09-14 15:23:49 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
getIcon () {
|
|
|
|
if (this.isClosed) return;
|
|
|
|
if (this.iconURL) return this.iconURL;
|
|
|
|
return this.icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
setPosition (newPosition) {
|
|
|
|
let tabContainer = this.tabGroup.tabContainer;
|
|
|
|
let tabs = tabContainer.children;
|
|
|
|
let oldPosition = this.getPosition() - 1;
|
|
|
|
|
|
|
|
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--;
|
2016-10-31 16:24:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
if (newPosition > oldPosition) {
|
|
|
|
newPosition++;
|
2016-10-31 14:53:50 +00:00
|
|
|
}
|
2016-10-27 20:38:20 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
tabContainer.insertBefore(tabs[oldPosition], tabs[newPosition]);
|
2017-10-27 01:55:12 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
return this;
|
|
|
|
}
|
2017-10-27 01:55:12 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
getPosition (fromRight) {
|
|
|
|
let position = 0;
|
|
|
|
let tab = this.tab;
|
|
|
|
while ((tab = tab.previousSibling) != null) position++;
|
2017-10-27 01:55:12 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
if (fromRight === true) {
|
|
|
|
position -= this.tabGroup.tabContainer.childElementCount;
|
2017-10-27 01:55:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
if (position >= 0) {
|
|
|
|
position++;
|
|
|
|
}
|
2017-10-27 01:55:12 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
return position;
|
|
|
|
}
|
2017-10-27 01:55:12 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
activate () {
|
|
|
|
if (this.isClosed) return;
|
|
|
|
let activeTab = this.tabGroup.getActiveTab();
|
|
|
|
if (activeTab) {
|
|
|
|
activeTab.tab.classList.remove("active");
|
|
|
|
activeTab.webview.classList.remove("visible");
|
|
|
|
activeTab.emit("inactive", activeTab);
|
2017-10-27 01:55:12 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
TabGroupPrivate.setActiveTab.bind(this.tabGroup)(this);
|
|
|
|
this.tab.classList.add("active");
|
|
|
|
this.webview.classList.add("visible");
|
|
|
|
this.webview.focus();
|
|
|
|
this.emit("active", this);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
show (flag) {
|
|
|
|
if (this.isClosed) return;
|
|
|
|
if (flag !== false) {
|
|
|
|
this.tab.classList.add("visible");
|
|
|
|
this.emit("visible", this);
|
|
|
|
} else {
|
|
|
|
this.tab.classList.remove("visible");
|
|
|
|
this.emit("hidden", this);
|
2016-10-31 14:53:50 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
hide () {
|
|
|
|
return this.show(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
flash (flag) {
|
|
|
|
if (this.isClosed) return;
|
|
|
|
if (flag !== false) {
|
|
|
|
this.tab.classList.add("flash");
|
|
|
|
this.emit("flash", this);
|
|
|
|
} else {
|
|
|
|
this.tab.classList.remove("flash");
|
|
|
|
this.emit("unflash", this);
|
2016-11-01 10:13:00 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
unflash () {
|
|
|
|
return this.flash(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
close (force) {
|
|
|
|
this.emit("closing", this);
|
|
|
|
if (this.isClosed || (!this.closable && !force)) return;
|
|
|
|
this.isClosed = true;
|
|
|
|
let tabGroup = this.tabGroup;
|
|
|
|
tabGroup.tabContainer.removeChild(this.tab);
|
|
|
|
tabGroup.viewContainer.removeChild(this.webview);
|
|
|
|
let activeTab = this.tabGroup.getActiveTab();
|
|
|
|
TabGroupPrivate.removeTab.bind(tabGroup)(this, true);
|
|
|
|
this.emit("close", this);
|
|
|
|
|
|
|
|
if (activeTab.id === this.id) {
|
|
|
|
TabGroupPrivate.activateRecentTab.bind(tabGroup)();
|
2016-11-01 10:13:00 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-01 10:13:00 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
const TabPrivate = {
|
|
|
|
initTab: function () {
|
|
|
|
let tabClass = this.tabGroup.options.tabClass;
|
|
|
|
|
|
|
|
// Create tab element
|
|
|
|
let tab = this.tab = document.createElement("div");
|
|
|
|
tab.classList.add(tabClass);
|
|
|
|
for (let el of ["icon", "title", "buttons", "badge"]) {
|
|
|
|
let span = tab.appendChild(document.createElement("span"));
|
|
|
|
span.classList.add(`${tabClass}-${el}`);
|
|
|
|
this.tabElements[el] = span;
|
2016-10-27 20:38:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
this.setTitle(this.title);
|
|
|
|
this.setBadge(this.badge);
|
|
|
|
this.setIcon(this.iconURL, this.icon);
|
|
|
|
TabPrivate.initTabButtons.bind(this)();
|
|
|
|
TabPrivate.initTabClickHandler.bind(this)();
|
|
|
|
|
|
|
|
this.tabGroup.tabContainer.appendChild(this.tab);
|
|
|
|
},
|
|
|
|
|
|
|
|
initTabButtons: function () {
|
|
|
|
let container = this.tabElements.buttons;
|
|
|
|
let tabClass = this.tabGroup.options.tabClass;
|
|
|
|
if (this.closable) {
|
|
|
|
let button = container.appendChild(document.createElement("button"));
|
|
|
|
button.classList.add(`${tabClass}-button-close`);
|
|
|
|
button.innerHTML = this.tabGroup.options.closeButtonText;
|
|
|
|
button.addEventListener("click", this.close.bind(this, false), false);
|
2016-11-01 15:05:09 +00:00
|
|
|
}
|
2020-02-04 10:13:57 +00:00
|
|
|
},
|
2016-11-01 15:05:09 +00:00
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
initTabClickHandler: function () {
|
|
|
|
// Mouse up
|
|
|
|
const tabClickHandler = function (e) {
|
|
|
|
if (this.isClosed) return;
|
|
|
|
if (e.which === 2) {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.tab.addEventListener("mouseup", tabClickHandler.bind(this), false);
|
|
|
|
// Mouse down
|
|
|
|
const tabMouseDownHandler = function (e) {
|
|
|
|
if (this.isClosed) return;
|
|
|
|
if (e.which === 1) {
|
|
|
|
if (e.target.matches("button")) return;
|
|
|
|
this.activate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.tab.addEventListener("mousedown", tabMouseDownHandler.bind(this), false);
|
|
|
|
},
|
|
|
|
|
|
|
|
initWebview: function () {
|
|
|
|
this.webview = document.createElement("webview");
|
|
|
|
|
|
|
|
const tabWebviewDidFinishLoadHandler = function (e) {
|
|
|
|
this.emit("webview-ready", this);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.webview.addEventListener("did-finish-load", tabWebviewDidFinishLoadHandler.bind(this), false);
|
|
|
|
|
2020-02-04 11:07:21 +00:00
|
|
|
this.webview.addEventListener("dom-ready", function () {
|
|
|
|
// Remove this once https://github.com/electron/electron/issues/14474 is fixed
|
|
|
|
tab.webview.blur();
|
|
|
|
tab.webview.focus();
|
|
|
|
});
|
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
this.webview.classList.add(this.tabGroup.options.viewClass);
|
|
|
|
if (this.webviewAttributes) {
|
|
|
|
let attrs = this.webviewAttributes;
|
|
|
|
for (let key in attrs) {
|
|
|
|
this.webview.setAttribute(key, attrs[key]);
|
|
|
|
}
|
2016-10-31 14:53:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 10:13:57 +00:00
|
|
|
this.tabGroup.viewContainer.appendChild(this.webview);
|
|
|
|
}
|
2016-11-01 13:40:03 +00:00
|
|
|
};
|
|
|
|
|
2017-04-04 12:33:57 +00:00
|
|
|
module.exports = TabGroup;
|
2019-09-26 05:51:50 +00:00
|
|
|
|