Update index.js
beautified code
This commit is contained in:
parent
a861267b46
commit
daff9b637f
1 changed files with 36 additions and 36 deletions
72
index.js
72
index.js
|
@ -5,7 +5,7 @@ if (!document) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inject styles
|
// Inject styles
|
||||||
(function () {
|
(function() {
|
||||||
const styles = `
|
const styles = `
|
||||||
webview {
|
webview {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
@ -28,7 +28,7 @@ if (!document) {
|
||||||
})();
|
})();
|
||||||
|
|
||||||
class TabGroup extends EventEmitter {
|
class TabGroup extends EventEmitter {
|
||||||
constructor (args = {}) {
|
constructor(args = {}) {
|
||||||
super();
|
super();
|
||||||
let options = this.options = {
|
let options = this.options = {
|
||||||
tabContainerSelector: args.tabContainerSelector || ".etabs-tabs",
|
tabContainerSelector: args.tabContainerSelector || ".etabs-tabs",
|
||||||
|
@ -51,7 +51,7 @@ class TabGroup extends EventEmitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addTab (args = this.options.newTab) {
|
addTab(args = this.options.newTab) {
|
||||||
if (typeof args === "function") {
|
if (typeof args === "function") {
|
||||||
args = args(this);
|
args = args(this);
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ class TabGroup extends EventEmitter {
|
||||||
return tab;
|
return tab;
|
||||||
}
|
}
|
||||||
|
|
||||||
getTab (id) {
|
getTab(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) {
|
||||||
return this.tabs[i];
|
return this.tabs[i];
|
||||||
|
@ -72,14 +72,14 @@ class TabGroup extends EventEmitter {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
getActiveTab () {
|
getActiveTab() {
|
||||||
if (this.tabs.length === 0) return null;
|
if (this.tabs.length === 0) return null;
|
||||||
return this.tabs[0];
|
return this.tabs[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const TabGroupPrivate = {
|
const TabGroupPrivate = {
|
||||||
initNewTabButton: function () {
|
initNewTabButton: function() {
|
||||||
if (!this.options.newTab) return;
|
if (!this.options.newTab) return;
|
||||||
let container = document.querySelector(this.options.buttonsContainerSelector);
|
let container = document.querySelector(this.options.buttonsContainerSelector);
|
||||||
let button = container.appendChild(document.createElement("button"));
|
let button = container.appendChild(document.createElement("button"));
|
||||||
|
@ -88,7 +88,7 @@ const TabGroupPrivate = {
|
||||||
button.addEventListener("click", this.addTab.bind(this, undefined), false);
|
button.addEventListener("click", this.addTab.bind(this, undefined), false);
|
||||||
},
|
},
|
||||||
|
|
||||||
removeTab: function (tab, triggerEvent) {
|
removeTab: function(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) {
|
||||||
|
@ -102,14 +102,14 @@ const TabGroupPrivate = {
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
setActiveTab: function (tab) {
|
setActiveTab: function(tab) {
|
||||||
TabGroupPrivate.removeTab.bind(this)(tab);
|
TabGroupPrivate.removeTab.bind(this)(tab);
|
||||||
this.tabs.unshift(tab);
|
this.tabs.unshift(tab);
|
||||||
this.emit("tab-active", tab, this);
|
this.emit("tab-active", tab, this);
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
activateRecentTab: function (tab) {
|
activateRecentTab: function(tab) {
|
||||||
if (this.tabs.length > 0) {
|
if (this.tabs.length > 0) {
|
||||||
this.tabs[0].activate();
|
this.tabs[0].activate();
|
||||||
}
|
}
|
||||||
|
@ -118,13 +118,13 @@ const TabGroupPrivate = {
|
||||||
};
|
};
|
||||||
|
|
||||||
class Tab extends EventEmitter {
|
class Tab extends EventEmitter {
|
||||||
constructor (tabGroup, id, args) {
|
constructor(tabGroup, id, args) {
|
||||||
super();
|
super();
|
||||||
this.tabGroup = tabGroup;
|
this.tabGroup = tabGroup;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.title = args.title;
|
this.title = args.title;
|
||||||
this.iconURL = args.iconURL;
|
this.iconURL = args.iconURL;
|
||||||
this.icon = args.icon;
|
this.icon = args.icon;
|
||||||
this.closable = args.closable === false ? false : true;
|
this.closable = args.closable === false ? false : true;
|
||||||
this.webviewAttributes = args.webviewAttributes || {};
|
this.webviewAttributes = args.webviewAttributes || {};
|
||||||
this.webviewAttributes.src = args.src;
|
this.webviewAttributes.src = args.src;
|
||||||
|
@ -142,7 +142,7 @@ class Tab extends EventEmitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setTitle (title) {
|
setTitle(title) {
|
||||||
if (this.isClosed) return;
|
if (this.isClosed) return;
|
||||||
let span = this.tabElements.title;
|
let span = this.tabElements.title;
|
||||||
span.innerHTML = title;
|
span.innerHTML = title;
|
||||||
|
@ -151,34 +151,34 @@ class Tab extends EventEmitter {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
getTitle () {
|
getTitle() {
|
||||||
if (this.isClosed) return;
|
if (this.isClosed) return;
|
||||||
return this.title;
|
return this.title;
|
||||||
}
|
}
|
||||||
|
|
||||||
setIcon (iconURL, icon) {
|
setIcon(iconURL, icon) {
|
||||||
if (this.isClosed) return;
|
if (this.isClosed) return;
|
||||||
this.iconURL = iconURL;
|
this.iconURL = iconURL;
|
||||||
this.icon = icon;
|
this.icon = icon;
|
||||||
let span = this.tabElements.icon;
|
let span = this.tabElements.icon;
|
||||||
if (iconURL) {
|
if (iconURL) {
|
||||||
span.innerHTML = `<img src="${iconURL}" />`;
|
span.innerHTML = `<img src="${iconURL}" />`;
|
||||||
this.emit("icon-changed", iconURL, this);
|
this.emit("icon-changed", iconURL, this);
|
||||||
} else if (icon) {
|
} else if (icon) {
|
||||||
span.innerHTML = `<i class="${icon}"></i>`;
|
span.innerHTML = `<i class="${icon}"></i>`;
|
||||||
this.emit("icon-changed", icon, this);
|
this.emit("icon-changed", icon, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
getIcon () {
|
getIcon() {
|
||||||
if (this.isClosed) return;
|
if (this.isClosed) return;
|
||||||
if(this.iconURL) return this.iconURL;
|
if (this.iconURL) return this.iconURL;
|
||||||
return this.icon;
|
return this.icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
activate () {
|
activate() {
|
||||||
if (this.isClosed) return;
|
if (this.isClosed) return;
|
||||||
let activeTab = this.tabGroup.getActiveTab();
|
let activeTab = this.tabGroup.getActiveTab();
|
||||||
if (activeTab) {
|
if (activeTab) {
|
||||||
|
@ -192,7 +192,7 @@ class Tab extends EventEmitter {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
show (flag) {
|
show(flag) {
|
||||||
if (this.isClosed) return;
|
if (this.isClosed) return;
|
||||||
if (flag !== false) {
|
if (flag !== false) {
|
||||||
this.tab.classList.add("visible");
|
this.tab.classList.add("visible");
|
||||||
|
@ -204,11 +204,11 @@ class Tab extends EventEmitter {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
hide () {
|
hide() {
|
||||||
return this.show(false);
|
return this.show(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
flash (flag) {
|
flash(flag) {
|
||||||
if (this.isClosed) return;
|
if (this.isClosed) return;
|
||||||
if (flag !== false) {
|
if (flag !== false) {
|
||||||
this.tab.classList.add("flash");
|
this.tab.classList.add("flash");
|
||||||
|
@ -220,11 +220,11 @@ class Tab extends EventEmitter {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
unflash () {
|
unflash() {
|
||||||
return this.flash(false);
|
return this.flash(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
close (force) {
|
close(force) {
|
||||||
if (this.isClosed || (!this.closable && !force)) return;
|
if (this.isClosed || (!this.closable && !force)) return;
|
||||||
this.isClosed = true;
|
this.isClosed = true;
|
||||||
let tabGroup = this.tabGroup;
|
let tabGroup = this.tabGroup;
|
||||||
|
@ -241,7 +241,7 @@ class Tab extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
const TabPrivate = {
|
const TabPrivate = {
|
||||||
initTab: function () {
|
initTab: function() {
|
||||||
let tabClass = this.tabGroup.options.tabClass;
|
let tabClass = this.tabGroup.options.tabClass;
|
||||||
|
|
||||||
// Create tab element
|
// Create tab element
|
||||||
|
@ -261,7 +261,7 @@ const TabPrivate = {
|
||||||
this.tabGroup.tabContainer.appendChild(this.tab);
|
this.tabGroup.tabContainer.appendChild(this.tab);
|
||||||
},
|
},
|
||||||
|
|
||||||
initTabButtons: function () {
|
initTabButtons: function() {
|
||||||
let container = this.tabElements.buttons;
|
let container = this.tabElements.buttons;
|
||||||
let tabClass = this.tabGroup.options.tabClass;
|
let tabClass = this.tabGroup.options.tabClass;
|
||||||
if (this.closable) {
|
if (this.closable) {
|
||||||
|
@ -272,9 +272,9 @@ const TabPrivate = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
initTabClickHandler: function () {
|
initTabClickHandler: function() {
|
||||||
// Click
|
// Click
|
||||||
const tabClickHandler = function (e) {
|
const tabClickHandler = function(e) {
|
||||||
if (this.isClosed) return;
|
if (this.isClosed) return;
|
||||||
if (e.which === 2) {
|
if (e.which === 2) {
|
||||||
this.close();
|
this.close();
|
||||||
|
@ -282,7 +282,7 @@ const TabPrivate = {
|
||||||
};
|
};
|
||||||
this.tab.addEventListener("click", tabClickHandler.bind(this), false);
|
this.tab.addEventListener("click", tabClickHandler.bind(this), false);
|
||||||
// Mouse down
|
// Mouse down
|
||||||
const tabMouseDownHandler = function (e) {
|
const tabMouseDownHandler = function(e) {
|
||||||
if (this.isClosed) return;
|
if (this.isClosed) return;
|
||||||
if (e.which === 1) {
|
if (e.which === 1) {
|
||||||
if (e.target.matches("button")) return;
|
if (e.target.matches("button")) return;
|
||||||
|
@ -292,7 +292,7 @@ const TabPrivate = {
|
||||||
this.tab.addEventListener("mousedown", tabMouseDownHandler.bind(this), false);
|
this.tab.addEventListener("mousedown", tabMouseDownHandler.bind(this), false);
|
||||||
},
|
},
|
||||||
|
|
||||||
initWebview: function () {
|
initWebview: function() {
|
||||||
this.webview = document.createElement("webview");
|
this.webview = document.createElement("webview");
|
||||||
this.webview.classList.add(this.tabGroup.options.viewClass);
|
this.webview.classList.add(this.tabGroup.options.viewClass);
|
||||||
if (this.webviewAttributes) {
|
if (this.webviewAttributes) {
|
||||||
|
|
Loading…
Reference in a new issue