electron-tabs/index.js

203 lines
5.5 KiB
JavaScript
Raw Normal View History

2016-10-31 14:53:50 +00:00
if (!document) {
throw Error("electron-tabs module must be called in renderer process");
}
// Inject styles
(function () {
const styles = `
webview {
display: flex;
flex: 0 1;
width: 0px;
height: 0px;
}
webview.visible {
width: 100%;
height: 100%;
top: 0;
right: 0;
bottom: 0;
left: 0;
2016-10-27 20:38:20 +00:00
}
2016-10-31 14:53:50 +00:00
`;
let styleTag = document.createElement("style");
styleTag.innerHTML = styles;
document.getElementsByTagName("head")[0].appendChild(styleTag);
})();
class TabGroup {
constructor (args) {
2016-10-31 15:41:25 +00:00
let options = this.options = {
tabContainerSelector: args.tabContainerSelector || ".tabs-tabcontainer",
viewContainerSelector: args.viewContainerSelector || ".tabs-viewcontainer",
tabClass: args.tabClass || "tabs-tab",
viewClass: args.viewClass || "tabs-view",
closeButtonText: args.closeButtonText || "❌"
2016-10-31 15:41:25 +00:00
};
this.tabContainer = document.querySelector(options.tabContainerSelector);
this.viewContainer = document.querySelector(options.viewContainerSelector);
2016-10-31 14:53:50 +00:00
this.tabs = [];
this.newTabId = 0;
}
addTab (args) {
let id = this.newTabId;
this.newTabId++;
let tab = new Tab(this, id, args);
this.pushTab(tab);
return tab;
}
pushTab (tab) {
this.tabs.push(tab);
}
2016-10-27 20:38:20 +00:00
2016-10-31 14:53:50 +00:00
removeTab (tab) {
let id = tab.id;
for (let i in this.tabs) {
if (this.tabs[i].id === id) {
this.tabs.splice(i, 1);
break;
2016-10-27 20:38:20 +00:00
}
}
2016-10-31 14:53:50 +00:00
}
2016-10-27 20:38:20 +00:00
2016-10-31 14:53:50 +00:00
setActiveTab (tab) {
this.removeTab(tab);
this.pushTab(tab);
}
2016-10-27 20:38:20 +00:00
2016-10-31 14:53:50 +00:00
getActiveTab () {
if (this.tabs.length < 1) return null;
return this.tabs[this.tabs.length - 1];
}
activateRecentTab (tab) {
let recentTab = this.tabs[this.tabs.length - 1];
if (!recentTab) return;
recentTab.activate();
}
}
class Tab {
constructor (tabGroup, id, args) {
this.tabGroup = tabGroup;
this.id = id;
this.title = args.title;
this.iconURL = args.iconURL;
2016-10-31 16:45:03 +00:00
this.closable = args.closable === false ? false : true;
2016-10-31 14:53:50 +00:00
this.webviewAttributes = args.webviewAttributes || {};
this.webviewAttributes.src = args.src;
2016-10-31 16:24:50 +00:00
this.tabElements = {};
2016-10-31 14:53:50 +00:00
this.initTab();
this.initWebview();
}
initTab () {
2016-10-31 16:24:50 +00:00
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"]) {
let span = tab.appendChild(document.createElement("span"));
span.classList.add(`${tabClass}-${el}`);
this.tabElements[el] = span;
}
2016-10-31 14:53:50 +00:00
this.setTitle(this.title);
2016-10-31 16:24:50 +00:00
this.setIcon(this.iconURL);
2016-10-31 16:45:03 +00:00
this.setButtons();
2016-10-31 16:24:50 +00:00
2016-10-31 16:59:13 +00:00
tab.addEventListener("click", this.tabClickHandler.bind(this), false);
2016-10-31 14:53:50 +00:00
this.tabGroup.tabContainer.appendChild(this.tab);
}
2016-10-27 20:38:20 +00:00
2016-10-31 14:53:50 +00:00
initWebview () {
this.webview = document.createElement("webview");
2016-10-31 15:41:25 +00:00
this.webview.classList.add(this.tabGroup.options.viewClass);
2016-10-31 14:53:50 +00:00
if (this.webviewAttributes) {
let attrs = this.webviewAttributes;
for (let key in attrs) {
this.webview.setAttribute(key, attrs[key]);
}
2016-10-27 20:38:20 +00:00
}
2016-10-31 14:53:50 +00:00
this.tabGroup.viewContainer.appendChild(this.webview);
}
setTitle (title) {
2016-10-31 16:24:50 +00:00
let span = this.tabElements.title;
span.innerHTML = title;
2016-10-31 14:53:50 +00:00
this.title = title;
}
getTitle () {
2016-10-31 16:24:50 +00:00
return this.title;
2016-10-31 14:53:50 +00:00
}
setIcon (iconURL) {
this.iconURL = iconURL;
2016-10-31 16:24:50 +00:00
let span = this.tabElements.icon;
if (iconURL) {
span.innerHTML = `<img src="${iconURL}" />`;
}
}
getIcon () {
return this.iconURL;
2016-10-31 14:53:50 +00:00
}
2016-10-27 20:38:20 +00:00
2016-10-31 16:45:03 +00:00
setButtons () {
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;
2016-10-31 16:45:03 +00:00
button.addEventListener("click", this.close.bind(this), false);
}
}
2016-10-31 16:59:13 +00:00
tabClickHandler (e) {
if (e.which === 1) {
this.activate();
} else if (e.which === 2) {
this.close();
}
}
2016-10-31 14:53:50 +00:00
activate () {
let activeTab = this.tabGroup.getActiveTab();
if (activeTab) {
activeTab.tab.classList.remove("active");
activeTab.webview.classList.remove("visible");
2016-10-27 20:38:20 +00:00
}
2016-10-31 14:53:50 +00:00
this.tabGroup.setActiveTab(this);
this.tab.classList.add("active");
this.webview.classList.add("visible");
}
2016-10-27 20:38:20 +00:00
2016-10-31 14:53:50 +00:00
flash (flag) {
2016-10-31 15:45:11 +00:00
if (flag !== false) {
2016-10-31 14:53:50 +00:00
this.tab.classList.add("flash");
} else {
this.tab.classList.remove("flash");
2016-10-27 20:38:20 +00:00
}
}
2016-10-31 14:53:50 +00:00
move (index) {
// TODO: move
}
close (force) {
if (!this.closable && !force) return;
2016-10-31 15:49:15 +00:00
let tabGroup = this.tabGroup;
tabGroup.tabContainer.removeChild(this.tab);
tabGroup.viewContainer.removeChild(this.webview);
tabGroup.removeTab(this);
tabGroup.activateRecentTab();
2016-10-31 14:53:50 +00:00
}
}
module.exports = TabGroup;