Skip to content

Commit 1aa7029

Browse files
authored
Merge pull request RocketChat#364 from RocketChat/feature/server-config
Load server config from file
2 parents c0a527c + a7f63ee commit 1aa7029

File tree

10 files changed

+418
-373
lines changed

10 files changed

+418
-373
lines changed

src/scripts/menus.js

+61-320
Large diffs are not rendered by default.

src/scripts/menus/app.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { remote } from 'electron';
2+
3+
const APP_NAME = remote.app.getName();
4+
const isMac = process.platform === 'darwin';
5+
6+
const macAppTemplate = [
7+
{
8+
label: 'About ' + APP_NAME,
9+
role: 'about'
10+
},
11+
{
12+
type: 'separator'
13+
},
14+
{
15+
label: 'Hide ' + APP_NAME,
16+
accelerator: 'Command+H',
17+
role: 'hide'
18+
},
19+
{
20+
label: 'Hide Others',
21+
accelerator: 'Command+Alt+H',
22+
role: 'hideothers'
23+
},
24+
{
25+
label: 'Show All',
26+
role: 'unhide'
27+
},
28+
{
29+
type: 'separator'
30+
},
31+
{
32+
label: 'Quit ' + APP_NAME,
33+
accelerator: 'Command+Q',
34+
click: function () {
35+
remote.app.quit();
36+
}
37+
}
38+
];
39+
40+
const appTemplate = [
41+
{
42+
label: 'About ' + APP_NAME,
43+
click: function () {
44+
const win = new remote.BrowserWindow({ width: 310, height: 200, minWidth: 310, minHeight: 200, maxWidth: 310, maxHeight: 200, show: false, maximizable: false, minimizable: false, title: ' ' });
45+
win.loadURL('file://' + __dirname + '/about.html');
46+
win.show();
47+
}
48+
},
49+
{
50+
type: 'separator'
51+
},
52+
{
53+
label: 'Quit',
54+
accelerator: 'Ctrl+Q',
55+
click: function () {
56+
remote.app.quit();
57+
}
58+
}
59+
];
60+
61+
export default isMac ? macAppTemplate : appTemplate;

src/scripts/menus/edit.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
const editTemplate = [
3+
{
4+
label: 'Undo',
5+
accelerator: 'CommandOrControl+Z',
6+
role: 'undo'
7+
},
8+
{
9+
label: 'Redo',
10+
accelerator: 'CommandOrControl+Shift+Z',
11+
role: 'redo'
12+
},
13+
{
14+
type: 'separator'
15+
},
16+
{
17+
label: 'Cut',
18+
accelerator: 'CommandOrControl+X',
19+
role: 'cut'
20+
},
21+
{
22+
label: 'Copy',
23+
accelerator: 'CommandOrControl+C',
24+
role: 'copy'
25+
},
26+
{
27+
label: 'Paste',
28+
accelerator: 'CommandOrControl+V',
29+
role: 'paste'
30+
},
31+
{
32+
label: 'Select All',
33+
accelerator: 'CommandOrControl+A',
34+
role: 'selectall'
35+
}
36+
];
37+
38+
export default editTemplate;

src/scripts/menus/help.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { remote } from 'electron';
2+
const APP_NAME = remote.app.getName();
3+
4+
const helpTemplate = [
5+
{
6+
label: APP_NAME + ' Help',
7+
click: function () {
8+
remote.shell.openExternal('https://rocket.chat/docs');
9+
}
10+
},
11+
{
12+
type: 'separator'
13+
},
14+
{
15+
label: 'Learn More',
16+
click: function () {
17+
remote.shell.openExternal('https://rocket.chat');
18+
}
19+
}
20+
];
21+
22+
export default helpTemplate;

src/scripts/menus/view.js

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { remote } from 'electron';
2+
import webview from '../webview';
3+
import sidebar from '../sidebar';
4+
import tray from '../tray';
5+
6+
const isMac = process.platform === 'darwin';
7+
const certificate = remote.require('./background').certificate;
8+
9+
const viewTemplate = [
10+
{
11+
label: 'Original Zoom',
12+
accelerator: 'CommandOrControl+0',
13+
role: 'resetzoom'
14+
},
15+
{
16+
label: 'Zoom In',
17+
accelerator: 'CommandOrControl+Plus',
18+
role: 'zoomin'
19+
},
20+
{
21+
label: 'Zoom Out',
22+
accelerator: 'CommandOrControl+-',
23+
role: 'zoomout'
24+
},
25+
{
26+
type: 'separator'
27+
},
28+
{
29+
label: 'Current Server - Reload',
30+
accelerator: 'CommandOrControl+R',
31+
click: function () {
32+
const activeWebview = webview.getActive();
33+
if (activeWebview) {
34+
activeWebview.reload();
35+
}
36+
}
37+
},
38+
{
39+
label: 'Current Server - Toggle DevTools',
40+
accelerator: isMac ? 'Command+Alt+I' : 'Ctrl+Shift+I',
41+
click: function () {
42+
const activeWebview = webview.getActive();
43+
if (activeWebview) {
44+
activeWebview.openDevTools();
45+
}
46+
}
47+
},
48+
{
49+
type: 'separator'
50+
},
51+
{
52+
label: 'Application - Reload',
53+
accelerator: 'CommandOrControl+Shift+R',
54+
click: function () {
55+
var mainWindow = remote.getCurrentWindow();
56+
if (mainWindow.destroyTray) {
57+
mainWindow.destroyTray();
58+
}
59+
mainWindow.reload();
60+
}
61+
},
62+
{
63+
label: 'Application - Toggle DevTools',
64+
click: function () {
65+
remote.getCurrentWindow().toggleDevTools();
66+
}
67+
},
68+
{
69+
type: 'separator',
70+
id: 'toggle'
71+
},
72+
{
73+
label: 'Toggle Server List',
74+
click: function () {
75+
sidebar.toggle();
76+
}
77+
},
78+
{
79+
type: 'separator'
80+
},
81+
{
82+
label: 'Clear',
83+
submenu: [
84+
{
85+
label: 'Clear Trusted Certificates',
86+
click: function () {
87+
certificate.clear();
88+
}
89+
}
90+
]
91+
}
92+
];
93+
94+
if (isMac) {
95+
viewTemplate.push({
96+
label: 'Toggle Tray Icon',
97+
click: function () {
98+
tray.toggle();
99+
},
100+
position: 'after=toggle'
101+
});
102+
} else {
103+
viewTemplate.push({
104+
label: 'Toggle Menu Bar',
105+
click: function () {
106+
const current = localStorage.getItem('autohideMenu') === 'true';
107+
remote.getCurrentWindow().setAutoHideMenuBar(!current);
108+
localStorage.setItem('autohideMenu', JSON.stringify(!current));
109+
},
110+
position: 'after=toggle'
111+
});
112+
}
113+
114+
export default viewTemplate;

src/scripts/menus/window.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { remote } from 'electron';
2+
import servers from '../servers';
3+
const isMac = process.platform === 'darwin';
4+
5+
const macWindowTemplate = [
6+
{
7+
label: 'Minimize',
8+
accelerator: 'Command+M',
9+
role: 'minimize'
10+
},
11+
{
12+
label: 'Close',
13+
accelerator: 'Command+W',
14+
role: 'close'
15+
},
16+
{
17+
type: 'separator'
18+
},
19+
{
20+
type: 'separator',
21+
id: 'server-list-separator',
22+
visible: false
23+
},
24+
{
25+
label: 'Add new server',
26+
accelerator: 'Command+N',
27+
click: function () {
28+
var mainWindow = remote.getCurrentWindow();
29+
mainWindow.show();
30+
servers.clearActive();
31+
}
32+
},
33+
{
34+
type: 'separator'
35+
},
36+
{
37+
label: 'Bring All to Front',
38+
click: function () {
39+
var mainWindow = remote.getCurrentWindow();
40+
mainWindow.show();
41+
}
42+
}
43+
];
44+
45+
const windowTemplate = [
46+
{
47+
type: 'separator',
48+
id: 'server-list-separator',
49+
visible: false
50+
},
51+
{
52+
label: 'Add new server',
53+
accelerator: 'Ctrl+N',
54+
click: function () {
55+
servers.clearActive();
56+
}
57+
},
58+
{
59+
type: 'separator'
60+
},
61+
{
62+
label: 'Close',
63+
accelerator: 'Ctrl+W',
64+
click: function () {
65+
remote.getCurrentWindow().close();
66+
}
67+
}
68+
];
69+
70+
export default isMac ? macWindowTemplate : windowTemplate;

src/scripts/servers.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* globals $ */
22

3+
import jetpack from 'fs-jetpack';
34
import { EventEmitter } from 'events';
45
import { remote } from 'electron';
56
const remoteServers = remote.require('./background').remoteServers;
@@ -62,6 +63,30 @@ class Servers extends EventEmitter {
6263
localStorage.setItem(this.hostsKey, JSON.stringify(hosts));
6364
}
6465

66+
// Load server info from server config file
67+
if (Object.keys(hosts).length === 0) {
68+
const serverFileName = 'servers.json';
69+
const userDataDir = jetpack.cwd(remote.app.getPath('userData'));
70+
try {
71+
const result = userDataDir.read(serverFileName, 'json');
72+
if (result) {
73+
hosts = {};
74+
Object.keys(result).forEach((title) => {
75+
const url = result[title];
76+
hosts[url] = { title, url };
77+
});
78+
localStorage.setItem(this.hostsKey, JSON.stringify(hosts));
79+
// Assume user doesn't want sidebar if they only have one server
80+
if (Object.keys(hosts).length === 1) {
81+
localStorage.setItem('sidebar-closed', 'true');
82+
}
83+
}
84+
85+
} catch (e) {
86+
console.log('Server file invalid');
87+
}
88+
}
89+
6590
this._hosts = hosts;
6691
remoteServers.loadServers(this._hosts);
6792
this.emit('loaded');
@@ -185,9 +210,16 @@ class Servers extends EventEmitter {
185210
}
186211

187212
setActive (hostUrl) {
213+
let url;
188214
if (this.hostExists(hostUrl)) {
215+
url = hostUrl;
216+
} else if (Object.keys(this._hosts).length > 0) {
217+
url = Object.keys(this._hosts)[0];
218+
}
219+
220+
if (url) {
189221
localStorage.setItem(this.activeKey, hostUrl);
190-
this.emit('active-setted', hostUrl);
222+
this.emit('active-setted', url);
191223
return true;
192224
}
193225
this.emit('loaded');
@@ -215,4 +247,4 @@ class Servers extends EventEmitter {
215247
}
216248
}
217249

218-
export var servers = new Servers();
250+
export default new Servers();

0 commit comments

Comments
 (0)