-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
259 lines (228 loc) · 9.19 KB
/
script.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
$(document).ready(function() {
toastr.options.progressBar = true;
toastr.options.newestOnTop = false;
toastr.options.positionClass = "toast-bottom-right";
/*
* window.pauseTerminal keeps track of whether the terminal should temporarily
* not refresh. This is set to true when a user makes a selection. By default,
* terminal output should refresh automatically.
*/
window.pauseTerminal = false;
$('.navbar-brand').click(function() {
$('.list-group button').removeClass("active");
$('main').css('display', 'none');
$('#information').css('display', 'initial');
window.selectedContainerId = undefined;
$('#serverTitle').text("");
});
$('#button-start').click(function() {
var params = {
id: window.selectedContainerId
};
// toastr.info("Starting…");
$.get('/api/start_container', params, function(text) {
if (text == "ok") {
toastr.success("Container started");
loadConsoleText();
} else if (text == "already started"){
toastr.warning("This container is already started");
} else if (text == "disabled") {
toastr.warning("Buttons are disabled");
} else {
toastr.error("An error occured while starting the container");
console.log("Response: '" + text + "'")
}
}, "text");
});
$('#button-stop').click(function() {
var params = {
id: window.selectedContainerId
};
toastr.info("Stopping…");
$.get('/api/stop_container', params, function(text) {
if (text == "ok") {
toastr.success("Container stopped");
loadConsoleText();
} else if (text == "already stopped"){
toastr.warning("This container is already stopped");
} else if (text == "disabled") {
toastr.warning("Buttons are disabled");
} else {
toastr.error("An error occured while stopping the container");
console.log("Response: '" + text + "'")
}
}, "text");
});
$('#button-restart').click(function() {
var params = {
id: window.selectedContainerId
};
toastr.info("Restarting…");
$.get('/api/restart_container', params, function(text) {
if (text == "ok") {
toastr.success("Container restarted");
loadConsoleText();
} else if (text == "disabled") {
toastr.warning("Buttons are disabled");
} else {
toastr.error("An error occured while restarting the container");
console.log("Response: '" + text + "'")
}
}, "text");
});
$('.terminal-input-form').keypress(function(e) {
if (e.keyCode == 13) {
if (!$('.terminal-input-form').val()){
toastr.warning("Cannot send empty command");
return;
}
sendConsoleCommand($('.terminal-input-form').val());
$('.terminal-input-form').val('');
}
});
$('#command-send-button').click(function(e) {
if (!$('.terminal-input-form').val()){
toastr.warning("Cannot send empty command");
return;
}
sendConsoleCommand($('.terminal-input-form').val());
$('.terminal-input-form').val('');
});
$('.terminal-logs').click(function(){
if (!window.pauseTerminal){
window.pauseTerminal = true;
toastr.info("Clicked in the terminal, log paused.");
}
});
setInterval(loadConsoleText, 1000);
loadConsoleText();
setInterval(loadNav, 3000);
loadNav();
});
function loadNav() {
// Don't waste resources if tab is not in view
if (document.visibilityState === "hidden") {
return;
}
$.getJSON('/api/get_containers', function(containers) {
window.containerData = containers;
var text = "";
/*
* Generate a string of HTML buttons for each container. Each button has a
* click event that triggers setSelectedContainer. This function is called
* on an interval, to periodically refresh the navbar if new containers are
* created. Where possible, the color of the status dot is re-used so that
* the status color dots don't flicker. After adding all the buttons, the
* status dots are updated asynchronously.
*/
// $.each(containers, function(containerId, containerInfo) {
for (var containerId in containers) {
var containerName = containers[containerId].name;
var containerState = containers[containerId].state;
var statusClass = containerState == "running" ? "status-online" : "status-offline";
text += '<button type="button" class="list-group-item list-group-item-action"';
text += 'id="nav-container-' + containerId + '" ';
text += 'onclick="setSelectedContainer("' + containerId + '", "' + containerName + '")">' + containerName;
text += '<span style="float: right;"><div class="circle ' + statusClass + '" ';
text += 'id="nav-container-' + containerId + '-status"></div></span>';
text += '</button>\n'
};
$('#collapseContainerSpoiler > .list-group').html(text);
setNavActive();
});
}
function loadConsoleText() {
// Don't waste resources if tab is not in view
if (document.visibilityState === "hidden") {
return;
}
/*
* If no container is selected, clear the terminal and set the container status
* dot to offline. This means that if a user switches to a different page and
* then decides to view a different container, they won't briefly see the
* terminal output of the first container.
*/
if (!window.selectedContainerId) {
$('#active-status-indicator').removeClass('status-online').addClass('status-offline');
$('.terminal-logs').text('');
return;
}
var statusClass = window.containerData[window.selectedContainerId].state == "running" ? "status-online" : "status-offline";
$('#active-status-indicator').removeClass('status-offline status-online').addClass(statusClass);
/*
* If terminal output is paused, send a message. This message is shown
* indefinitely. When clicked, terminal output will resume.
*/
if (window.pauseTerminal){
toastr.warning('Console output paused. Click to resume', '',
{
onclick: function() {
toastr.success('Console output resumed');
window.pauseTerminal = false;
},
preventDuplicates: true,
timeOut: 0,
}
);
return;
}
/*
* Before loading new text into the terminal, check if the user is scrolled
* to the bottom. If they are, after the text is loaded, scroll to
* the bottom again so div stays scrolled to the bottom like terminals
* should. If the user has intentionally scrolled up to have a look
* at something, the scroll position is not modified.
*/
const term = document.getElementsByClassName("terminal-logs")[0];
const isScrolledToBottom = term.scrollHeight - term.clientHeight <= term.scrollTop + 1;
$('.terminal-logs').load('/api/get_container_logs?id=' + window.selectedContainerId, null, function(){
if (isScrolledToBottom) {
term.scrollTop = term.scrollHeight;
}
});
}
function setSelectedContainer(id, name) {
/*
* Change the selected container. This function is called when a user clicks on
* an entry in the container nav. Sets window.selectedContainerId, updates
* 'active' classes for navbar entries and unpauses the terminal if it was
* paused.
*/
window.selectedContainerId = id;
$('main').css('display', 'initial');
$('#information').css('display', 'none');
$('#serverTitle').text(" - " + name);
if (window.pauseTerminal){
window.pauseTerminal = false;
toastr.clear();
}
setNavActive();
loadConsoleText();
}
function setNavActive(){
$('.list-group button').removeClass("active");
if (window.selectedContainerId){
$('#nav-container-' + window.selectedContainerId).addClass("active");
}
}
function sendConsoleCommand(command){
var params = {
id: window.selectedContainerId,
command: command
};
$.get('/api/send_command', params, function(text) {
if (text == "ok") {
toastr.success("Command sent");
} else if (text == "offline") {
toastr.warning("Cannot send command, the container is offline.");
} else if (text == "disabled") {
toastr.warning("Command sending is disabled");
} else if (text == "timeout") {
toastr.error("Request timed out: did not receive a response from docker in time.");
} else {
toastr.error("Error occured while sending command");
console.log("Response: '" + text + "'")
}
setTimeout(loadConsoleText, 100)
}, "text");
}