-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserverGroup.js
More file actions
426 lines (396 loc) · 14.2 KB
/
serverGroup.js
File metadata and controls
426 lines (396 loc) · 14.2 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"use strict";
import Gtk from "gi://Gtk";
import Adw from "gi://Adw";
import { ServerSetting } from "./serverSetting.js";
/**
* A new group is displayed when _Add_ is clicked in the preferences dialog.
* It displays controls for the settings of a server.
*/
export class ServerGroup {
/**
* Constructor.
*
* @param {ServerStatusPreferences} preferences
* @param {ServerSetting} settings, may be null in which case the fields remain empty, expander is automatically opened and name field focused.
*/
constructor(preferences, settings) {
this.id = this.createUID();
this.preferences = preferences;
this.serverSettingGroup = new Adw.PreferencesGroup({});
// expander
this.expander = this.getExpanderRow(settings);
// move up/down row
const moveRow = new Adw.ActionRow({
title: "Move Up/Down",
});
this.moveUpButton = Gtk.Button.new_from_icon_name("go-up-symbolic");
this.moveUpHandlerId = this.moveUpButton.connect("clicked", () => {
// does a move actually happen?
if (this.moveUp(preferences.serverGroups)) {
preferences.reorder();
preferences.save();
}
});
this.moveDownButton =
Gtk.Button.new_from_icon_name("go-down-symbolic");
this.moveDownHandlerId = this.moveDownButton.connect("clicked", () => {
// does a move actually happen?
if (this.moveDown(preferences.serverGroups)) {
preferences.reorder();
preferences.save();
}
});
const moveButtonBox = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 10,
});
moveButtonBox.append(this.moveUpButton);
moveButtonBox.append(this.moveDownButton);
moveRow.add_suffix(moveButtonBox);
this.serverSettingGroup.add(moveRow);
// visibility row - show/hide server without deleting
this.visible = settings?.visible ?? true;
const visibilityRow = new Adw.ActionRow({
title: "Show in menu",
subtitle: "Hidden servers are not displayed and not checked",
});
const visibilityIcon = this.visible ? "view-reveal-symbolic" : "view-conceal-symbolic";
this.visibilityButton = Gtk.Button.new_from_icon_name(visibilityIcon);
this.visibilityHandlerId = this.visibilityButton.connect("clicked", () => {
this.visible = !this.visible;
const newIcon = this.visible ? "view-reveal-symbolic" : "view-conceal-symbolic";
this.visibilityButton.set_icon_name(newIcon);
this.update();
});
visibilityRow.add_suffix(this.visibilityButton);
this.serverSettingGroup.add(visibilityRow);
// delete button
const deleteRow = new Adw.ActionRow({
title: "Delete this server",
});
this.deleteButton = Gtk.Button.new_from_icon_name(
"edit-delete-symbolic",
);
this.deleteButton.set_css_classes(["destructive-action"]);
deleteRow.add_suffix(this.deleteButton);
this.serverSettingGroup.add(deleteRow);
this.deleteHandlerId = this.deleteButton.connect("clicked", () => {
const messageDialog = new Adw.MessageDialog({
transient_for: preferences.window,
destroy_with_parent: true,
modal: true,
heading: "Confirm Delete",
body: "Are you sure you want to delete this server?",
});
messageDialog.add_response("cancel", "_Cancel");
messageDialog.add_response("delete", "_Delete");
messageDialog.set_response_appearance(
"delete",
Adw.ResponseAppearance.ADW_RESPONSE_DESTRUCTIVE,
);
messageDialog.set_default_response("cancel");
messageDialog.set_close_response("cancel");
messageDialog.connect("response", (_, response) => {
if (response === "delete") {
this.createServerSettings();
this.removeGroup(this.id, preferences.serverGroups);
preferences.page.remove(this.serverSettingGroup);
preferences.save();
this.destroy();
}
messageDialog.destroy();
});
messageDialog.present();
});
this.createServerSettings();
if (settings === null) {
this.expander.set_expanded(true);
this.nameRow.grab_focus();
}
}
getExpanderRow(settings) {
this.expander = new Adw.ExpanderRow();
// disable pango as it fails on & in url query strings
this.expander.set_use_markup(false);
const title = settings?.name ?? "";
this.expander.set_title(title);
const subtitle = settings
? `${settings.isGet ? "GET" : "HEAD"} ${settings.url} @ ${settings.frequency}s with ${settings.timeout}s timeout ${settings.notifies ? "🔔" : ""}`
: "";
this.expander.set_subtitle(subtitle);
this.serverSettingGroup.add(this.expander);
// name text field
this.nameRow = new Adw.EntryRow({
title: "Name",
text: settings?.name ?? "",
show_apply_button: true,
});
this.nameHandlerId = this.nameRow.connect("apply", () => {
this.update();
});
this.expander.add_row(this.nameRow);
// url text field
this.urlRow = new Adw.EntryRow({
title: "URL",
text: settings?.url ?? "",
show_apply_button: true,
});
this.urlHandlerId = this.urlRow.connect("apply", () => {
this.update();
});
this.expander.add_row(this.urlRow);
// frequency spinner
this.frequencyRow = Adw.SpinRow.new_with_range(10, 300, 10);
this.frequencyRow.set_value(settings?.frequency ?? 120);
this.frequencyRow.set_title("Frequency (secs.)");
this.frequencyHandlerId = this.frequencyRow.connect("notify::value", () => {
this.update();
});
this.expander.add_row(this.frequencyRow);
// timeout spinner
this.timeoutRow = Adw.SpinRow.new_with_range(1, 300, 1);
this.timeoutRow.set_value(settings?.timeout ?? 10);
this.timeoutRow.set_title("Timeout (secs.)");
this.timeoutHandlerId = this.timeoutRow.connect("notify::value", () => {
this.update();
});
this.expander.add_row(this.timeoutRow);
// 'use GET' switch
this.useGetSwitchRow = new Adw.SwitchRow({
title: "Use GET rather than HEAD",
});
const isGet = settings?.isGet ?? false;
this.useGetSwitchRow.set_active(isGet);
this.useGetHandlerId = this.useGetSwitchRow.connect("notify::active", () => {
this.update();
});
this.expander.add_row(this.useGetSwitchRow);
// 'use notifications' switch
this.useNotificationsSwitchRow = new Adw.SwitchRow({
title: "Notify when down",
});
const notifies = settings?.notifies ?? false;
this.useNotificationsSwitchRow.set_active(notifies);
this.useNotificationsHandlerId = this.useNotificationsSwitchRow.connect("notify::active", () => {
this.update();
});
this.expander.add_row(this.useNotificationsSwitchRow);
return this.expander;
}
/**
* Renew #serverSettings, save them and update UI.
*/
update() {
this.createServerSettings();
this.preferences.save();
this.updateExpander();
}
/**
* Get the title based on user input.
*
* @returns {String}
*/
getTitle() {
return this.nameRow.text;
}
/**
* Get the subtitle based on user input.
*
* @returns {String}
*/
getSubtitle() {
const url = this.urlRow.text;
const freq = this.frequencyRow.text;
const timeout = this.timeoutRow.text;
const httpMethod = this.useGetSwitchRow.active ? "GET" : "HEAD";
return `${httpMethod} ${url} @ ${freq}s with ${timeout}s timeout ${this.useNotificationsSwitchRow.active ? "🔔" : ""}`;
}
/**
* Update the expander title & subtitle.
*/
updateExpander() {
this.expander.set_title(this.getTitle());
this.expander.set_subtitle(this.getSubtitle());
}
/**
* Move this `Adw.PreferenceGroup` down by one in the list.
*
* @param {ServerGroup} array of `ServerGroup`s
* @returns true if a move occurred.
*/
moveDown(serverGroups) {
const index = this.getPosition(serverGroups);
if (index !== -1 && index < serverGroups.length - 1) {
this.move(index, index + 1, serverGroups);
return true;
}
return false; // no move was made
}
/**
* Move this `Adw.PreferenceGroup` up by one in the list.
*
* @param {ServerGroup} array of `ServerGroup`s
* @returns true if a move occurred.
*/
moveUp(serverGroups) {
const index = this.getPosition(serverGroups);
if (index > 0) {
this.move(index, index - 1, serverGroups);
return true;
}
return false; // no move was made
}
/**
* Find the index of `this` in the provided array.
*
* @param {ServerGroup} array of `ServerGroup`s
* @returns int index of `this` in provided array, -1 if not found
*/
getPosition(serverGroups) {
for (let i = 0; i < serverGroups.length; i++) {
const serverGroup = serverGroups[i];
if (serverGroup.id === this.id) {
return i;
}
}
return -1;
}
/**
* Move `this` in provided array using provided 'from' index and 'to' index.
*
* @param {int} fromIndex the position being moved from
* @param {int} toIndex the move destination
* @param {ServerGroup} array of `ServerGroup`s
*/
move(fromIndex, toIndex, serverGroups) {
const serverGroup = serverGroups[fromIndex];
serverGroups.splice(fromIndex, 1);
serverGroups.splice(toIndex, 0, serverGroup);
}
/**
* Return this group's server settings, creating it if null.
*
* @returns {ServerSetting}
*/
getSettings() {
if (!this.settings) {
this.createServerSettings();
}
return this.settings;
}
/**
* Return this preference group.
*
* @returns {Adw.PreferencesGroup}
*/
getGroup() {
return this.serverSettingGroup;
}
/**
* Returns the _Name_ `EntryRow`.
*
* @returns {Adw.EntryRow}
*/
getNameInput() {
return this.nameRow;
}
/**
* Create a `ServerSetting` based on control values.
*/
createServerSettings() {
this.settings = new ServerSetting(
this.nameRow.text,
this.urlRow.text,
this.frequencyRow.text,
this.timeoutRow.text,
this.useGetSwitchRow.active,
this.useNotificationsSwitchRow.active,
this.visible,
);
}
/**
* Create a unique ID for this group.
*
* @returns {String}
*/
createUID() {
const buffer = [];
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charlen = chars.length;
for (let i = 0; i < 32; i++) {
buffer[i] = chars.charAt(Math.floor(Math.random() * charlen));
}
return buffer.join("");
}
/**
* Remove the group with supplied id from the provided set of groups.
*
* @param {String} id the id of the group to remove
* @param {ServerGroup} array of `ServerGroup`s without group with supplied id
*/
removeGroup(id, serverGroups) {
for (let i = 0; i < serverGroups.length; i++) {
const candidate = serverGroups[i];
if (candidate.id === id) {
serverGroups.splice(i, 1);
break;
}
}
}
/**
* Disconnect listeners.
*/
destroy() {
if (this.moveUpButton && this.moveUpHandlerId) {
this.moveUpButton.disconnect(this.moveUpHandlerId);
this.moveUpHandlerId = null;
this.moveUpButton = null;
}
if (this.moveDownButton && this.moveDownHandlerId) {
this.moveDownButton.disconnect(this.moveDownHandlerId);
this.moveDownHandlerId = null;
this.moveDownButton = null;
}
if (this.visibilityButton && this.visibilityHandlerId) {
this.visibilityButton.disconnect(this.visibilityHandlerId);
this.visibilityHandlerId = null;
this.visibilityButton = null;
}
if (this.deleteButton && this.deleteHandlerId) {
this.deleteButton.disconnect(this.deleteHandlerId);
this.deleteHandlerId = null;
this.deleteButton = null;
}
if (this.nameRow && this.nameHandlerId) {
this.nameRow.disconnect(this.nameHandlerId);
this.nameHandlerId = null;
this.nameRow = null;
}
if (this.urlRow && this.urlHandlerId) {
this.urlRow.disconnect(this.urlHandlerId);
this.urlHandlerId = null;
this.urlRow = null;
}
if (this.frequencyHandlerId) {
this.frequencyRow.disconnect(this.frequencyHandlerId);
this.frequencyHandlerId = null;
this.frequencyRow = null;
}
if (this.useGetHandlerId) {
this.useGetSwitchRow.disconnect(this.useGetHandlerId);
this.useGetHandlerId = null;
this.useGetSwitchRow = null;
}
if (this.useNotificationsHandlerId) {
this.useNotificationsSwitchRow.disconnect(this.useNotificationsHandlerId);
this.useNotificationsHandlerId = null;
this.useNotificationsSwitchRow = null;
}
if (this.timeoutHandlerId) {
this.timeoutRow.disconnect(this.timeoutHandlerId);
this.timeoutHandlerId = null;
this.timeoutRow = null;
}
}
}