-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnassh_extension_popup.js
334 lines (306 loc) · 9.38 KB
/
nassh_extension_popup.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* CSP means that we can't kick off the initialization from the html file,
* so we do it like this instead.
*/
window.addEventListener('DOMContentLoaded', (event) => {
nassh.loadWebFonts(document);
lib.init().then(() => {
// Save a handle for debugging.
window.popup_ = new popup();
});
});
/**
* Manage the browser extension popup.
*
* @constructor
*/
function popup() {
// The nassh global preference managers.
this.prefs_ = new nassh.PreferenceManager();
this.localPrefs_ = new nassh.LocalPreferenceManager();
// The hterm pref manager. We use the 'default' profile to theme.
this.htermPrefs_ = new hterm.PreferenceManager('default');
// Load the theme first so the style doesn't flicker.
this.htermPrefs_.readStorage(() => {
this.updateTheme_();
this.prefs_.readStorage(() => {
this.populateList_();
this.localPrefs_.readStorage(() => {
this.localPrefs_.syncProfiles(this.prefs_);
});
});
});
}
/**
* Open a specific connection.
*
* @param {string} id The profile id to open.
* @param {boolean=} newWindow Whether to open in a window or tab.
*/
popup.prototype.openLink_ = function(id, newWindow = true) {
let profile;
let url = lib.f.getURL('/html/nassh.html');
switch (id) {
case 'connect-dialog':
break;
case 'options':
nassh.openOptionsPage();
return;
case 'feedback':
nassh.sendFeedback();
return;
case 'mosh':
url = lib.f.getURL('/plugin/mosh/mosh_client.html');
default: {
if (id != 'mosh') {
profile = this.localPrefs_.getProfile(id);
}
let openas = '';
if (newWindow) {
const state = profile ? profile.get('win/state') : '';
if (state !== 'normal') {
openas = `openas=${state}`;
}
}
url += `?promptOnReload=yes&${openas}#profile-id:${id}`;
break;
}
}
// Launch it.
if (!newWindow) {
// Should we offer a way to open tabs in the background?
chrome.tabs.create({url: url, active: true});
} else {
let top = 0;
let left = 0;
let height = 600;
let width = 900;
let profile;
try {
profile = this.localPrefs_.getProfile(id);
} catch (e) {
// Ignore errors here to make the UI more robust. And this ignores the
// saved settings for connect-dialog which we don't track currently.
}
if (profile) {
const parseDim = (value, fallback) => {
const ret = parseInt(value, 10);
return isNaN(ret) ? fallback : ret;
};
top = parseDim(profile.get('win/top'), top);
left = parseDim(profile.get('win/left'), left);
height = parseDim(profile.get('win/height'), height);
width = parseDim(profile.get('win/width'), width);
}
lib.f.openWindow(url, '',
'chrome=no,close=yes,resize=yes,scrollbars=yes,' +
`minimizable=yes,top=${top},left=${left},` +
`height=${height},width=${width}`);
}
// Close the popup. It happens automatically on some systems (e.g. Linux),
// but not all (e.g. Chrome OS).
window.close();
};
/**
* Open a specific connection via mouse clicks.
*
* @param {!MouseEvent} e The event triggering this.
*/
popup.prototype.mouseClickLink_ = function(e) {
// We route multiple event types here.
if (e.type === 'auxclick') {
// Only consume middle mouse. Leave other buttons for future use.
if (e.button != 1) {
return;
}
}
// Figure out whether to open a window or a tab.
let newWindow;
if (e.ctrlKey || e.type === 'auxclick') {
newWindow = false;
} else if (e.shiftKey) {
newWindow = true;
} else {
// TODO: Get default from prefs.
newWindow = true;
}
this.openLink_(e.target.id, newWindow);
};
/**
* Open a specific connection via the keyboard.
*
* @param {!KeyboardEvent} e The event triggering this.
*/
popup.prototype.keyupLink_ = function(e) {
switch (e.key) {
case 'Enter': {
// Figure out whether to open a window or a tab.
let newWindow;
if (e.ctrlKey) {
newWindow = false;
} else if (e.shiftKey) {
newWindow = true;
} else {
// TODO: Get default from prefs.
newWindow = true;
}
this.openLink_(e.target.id, newWindow);
e.preventDefault();
break;
}
}
};
/**
* When a key is pressed down.
*
* @param {!KeyboardEvent} e The event triggering this.
*/
popup.prototype.keydownWindow_ = function(e) {
// Helper to find the last focusable element.
const findLastFocusElement = () => {
let ret;
document.querySelectorAll('[tabIndex]').forEach((ele) => {
if (!ret || ret.tabIndex < ele.tabIndex) {
ret = ele;
}
});
return ret;
};
// Helper to find the first focusable element.
const findFirstFocusElement = () => {
let ret;
document.querySelectorAll('[tabIndex]').forEach((ele) => {
if (!ret || ret.tabIndex > ele.tabIndex) {
ret = ele;
}
});
return ret;
};
switch (e.key) {
case 'PageUp':
case 'ArrowUp':
case 'ArrowLeft': {
// Move focus to the previous entry.
const tabIndex = e.target.tabIndex - 1;
let ele = document.querySelector(`[tabIndex="${tabIndex}"]`);
if (ele === null) {
ele = findLastFocusElement();
}
ele.focus();
e.preventDefault();
break;
}
case 'PageDown':
case 'ArrowDown':
case 'ArrowRight': {
// Move focus to the next entry.
const tabIndex = e.target.tabIndex + 1;
let ele = document.querySelector(`[tabIndex="${tabIndex}"]`);
if (ele === null) {
ele = findFirstFocusElement();
}
ele.focus();
e.preventDefault();
break;
}
case 'Home':
findFirstFocusElement().focus();
e.preventDefault();
break;
case 'End':
findLastFocusElement().focus();
e.preventDefault();
break;
}
};
/**
* Fill the popup with all the connections.
*/
popup.prototype.populateList_ = function() {
// Create a copy since we're going to modify it in place below.
const ids = this.prefs_.get('profile-ids').slice();
ids.unshift('connect-dialog');
ids.push('mosh');
ids.push('options');
ids.push('feedback');
for (let i = 0; i < ids.length; i++) {
const id = ids[i];
const link = document.createElement('div');
link.title = nassh.msg('POPUP_CONNECT_TOOLTIP');
link.id = id;
link.tabIndex = i + 1;
link.className = 'links';
const mouseClick = /** @type {!EventListener} */ (
this.mouseClickLink_.bind(this));
link.addEventListener('click', mouseClick);
link.addEventListener('auxclick', mouseClick);
link.addEventListener('keyup', /** @type {!EventListener} */ (
this.keyupLink_.bind(this)));
switch (id) {
case 'connect-dialog':
link.textContent = nassh.msg('CONNECTION_DIALOG_NAME');
link.style.textAlign = 'center';
break;
case 'mosh':
link.textContent = '⸘m🍪sh‽';
link.style.textAlign = 'center';
break;
case 'options':
link.textContent = nassh.msg('HTERM_OPTIONS_BUTTON_LABEL');
link.style.textAlign = 'center';
break;
case 'feedback':
link.textContent = nassh.msg('SEND_FEEDBACK_LABEL');
link.style.textAlign = 'center';
break;
default: {
const profile = this.prefs_.getProfile(id);
const desc = profile.get('description');
link.textContent = desc;
break;
}
}
document.body.appendChild(link);
}
window.addEventListener('keydown', /** @type {!EventListener} */ (
this.keydownWindow_.bind(this)));
// Workaround bugs on Chrome on macOS where the popup renders as a small box
// due to the body dimenions being unset. https://crbug.com/428044
if (hterm.os == 'mac') {
// This height calculation is excessive due to padding, but it's not worth
// the extra coding effort to get it pixel-perfect (e.g. getComputedStyle).
const height = document.body.clientHeight;
// Set it slightly bigger immediately so hopefully this will lead to less
// flashing/refreshing. It's not clear whether this always worksaround the
// bug though :(.
document.body.style.height = `${height + 1}px`;
// Schedule an update in case the window is still too small. Hopefully
// this will catch the rest. If we already workedaround it, we'll only
// make the window slightly bigger so the user won't notice.
setTimeout(() => document.body.style.height = `${height + 2}px`, 50);
}
};
/**
* Style the popup with the right colors.
*/
popup.prototype.updateTheme_ = function() {
let style = document.body.style;
style.color = this.htermPrefs_.getString('foreground-color');
style.backgroundColor = this.htermPrefs_.getString('background-color');
style.fontSize = this.htermPrefs_.getNumber('font-size') + 'px';
style.fontFamily = this.htermPrefs_.getString('font-family');
if (style.webkitFontSmoothing !== undefined) {
style.webkitFontSmoothing = this.htermPrefs_.getString('font-smoothing');
}
style = document.createElement('style');
style.textContent = (
'div.links:hover {' +
` background-color: ${this.htermPrefs_.getString('cursor-color')};` +
'}'
);
document.head.appendChild(style);
};