This repository has been archived by the owner on Feb 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
sidebar.js
150 lines (124 loc) · 4.93 KB
/
sidebar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { ContextMenuHandler } from './context-menu-handler';
const { ipcRenderer } = require('electron');
const open = require('open');
export class Sidebar {
constructor(page) {
const TabGroup = require('electron-tabs');
const dragula = require('dragula');
this.tabGroup = new TabGroup({
ready: function(tabGroup) {
dragula([tabGroup.tabContainer], {
direction: "vertical",
mirrorContainer: document.querySelector(".etabs-tabs")
});
}
})
.on('tab-removed', this.onTabRemoved.bind(null));
this.addEventListenerForAddAccount();
this.initiateTabs();
}
initiateTabs() {
/* This is an ugly way to do it, but as electron-tabs does not allow
setting the ID, in order to have the same tabs ID on the saved json and
the generated we need to resave once we reload the saved tabs. */
const config = require('./config');
const tabSettingsArray = config.get('SavedTabs');
const postSettingsArray = [];
tabSettingsArray.map((savedtab, index) => {
this.tabGroup.addTab({
title: savedtab.title.substr(0, 1),
src: process.env.PROTONMAIL_LOGIN_URL,
visible: true,
active: !index,
ready: (tab) => {
postSettingsArray.push({ id: tab.id, title: savedtab.title, active: savedtab.active });
this.onTabReady(tab, savedtab.title);
}
});
});
settings.set("SavedTabs", postSettingsArray);
}
onTabRemoved(tab) {
const tabSettingsArray = settings.get('SavedTabs', []);
for (let i = 0; i < tabSettingsArray.length; i++)
if (tabSettingsArray[i].id === tab.id) {
tabSettingsArray.splice(i, 1);
break;
}
settings.set("SavedTabs", tabSettingsArray);
}
addEventListenerForAddAccount() {
document.querySelector("[action-add-account]").addEventListener("click", () => this.addAccount());
}
addAccount() {
const swal = require('sweetalert');
const options = {
title: "Account name",
text: "Enter the name of your ProtonMail account",
type: "input",
confirmButtonText: "Add account",
showCancelButton: true,
allowOutsideClick: true,
};
const onConfirmCallback = (name) => !!name ? this.createTab(name) : null;
swal(options, onConfirmCallback);
}
createTab(name, active = false) {
const config = require('./config');
this.tabGroup.addTab({
title: name.substr(0, 1),
src: process.env.PROTONMAIL_LOGIN_URL,
visible: true,
active: true,
ready: (tab) => {
this.onTabReady(tab, name);
const tabSettingsArray = [{ id: tab.id, title: name, active: tab.active }];
config.set("SavedTabs", config.get("SavedTabs").concat(tabSettingsArray));
}
});
}
onTabReady(tab, name) {
const domReadyEvent = () => {
this.prefillUsernameInLoginForm(tab.webview.getWebContents(), name);
tab.webview.removeEventListener("dom-ready", domReadyEvent);
};
this.prepareContextMenu(tab);
tab.tabElements.title.setAttribute('tab-id', tab.id);
tab.webview.addEventListener("dom-ready", domReadyEvent);
tab.webview.addEventListener("page-title-updated", () => this.onTabTitleUpdate());
tab.webview.addEventListener('new-window', (e) => {
e.preventDefault();
open(e.url);
});
}
prepareContextMenu(tab) {
const functionName = 'sidebarItemContextMenu';
tab.tabElements.title.setAttribute('prepend-context-menu', functionName);
tab.tabElements.title.setAttribute('tab-id', tab.id);
const tabs = this.tabGroup.tabs;
ContextMenuHandler.addContextMenu(functionName, (params, browserWindow, targetElement) => {
return [{
label: 'Remove tab',
visible: true,
click: () => {
const tabId = parseInt(targetElement.getAttribute('tab-id'));
const tab = tabs.find(_tab => _tab.id === tabId);
tab.close();
},
}];
});
}
prefillUsernameInLoginForm(webContents, username) {
webContents.executeJavaScript(`document.querySelector('[name=username]').value = '${username}'`);
}
onTabTitleUpdate() {
let totalCount = 0;
for (let _tab of this.tabGroup.tabs) {
let extractedTitle = (/\(([0-9]+)\)/).exec(_tab.webview.getTitle());
let unreadCount = extractedTitle ? parseInt(extractedTitle[1]) : 0;
totalCount += unreadCount;
_tab.setBadge(unreadCount ? unreadCount : '');
}
ipcRenderer.send('set-badge', totalCount);
}
}