Skip to content

Commit 96f5d15

Browse files
committed
switch console javascript code to revealing module pattern to avoid global variables
1 parent bf7b584 commit 96f5d15

File tree

1 file changed

+190
-169
lines changed

1 file changed

+190
-169
lines changed

public/themes/pterodactyl/js/frontend/console.js

Lines changed: 190 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -20,187 +20,208 @@
2020
var CONSOLE_PUSH_COUNT = 50;
2121
var CONSOLE_PUSH_FREQ = 200;
2222

23-
(function initConsole() {
24-
window.TerminalQueue = [];
25-
window.Terminal = $('#terminal').terminal(function (command, term) {
26-
Socket.emit('send command', command);
27-
}, {
28-
greetings: '',
29-
name: Pterodactyl.server.uuid,
30-
height: 450,
31-
exit: false,
32-
prompt: Pterodactyl.server.username + ':~$ ',
33-
scrollOnEcho: false,
34-
scrollBottomOffset: 5,
35-
onBlur: function (terminal) {
36-
return false;
37-
}
38-
});
23+
const Console = (function () {
3924

40-
Socket.on('initial status', function (data) {
41-
Terminal.clear();
42-
if (data.status === 1 || data.status === 2) {
43-
Socket.emit('send server log');
44-
}
45-
});
46-
})();
25+
var terminalQueue;
26+
var terminal;
4727

48-
(function pushOutputQueue() {
49-
if (TerminalQueue.length > CONSOLE_PUSH_COUNT) {
50-
// console throttled warning show
28+
function initConsole() {
29+
termianlQueue = [];
30+
terminal = $('#terminal').terminal(function (command, term) {
31+
Socket.emit('send command', command);
32+
}, {
33+
greetings: '',
34+
name: Pterodactyl.server.uuid,
35+
height: 450,
36+
exit: false,
37+
prompt: Pterodactyl.server.username + ':~$ ',
38+
scrollOnEcho: false,
39+
scrollBottomOffset: 5,
40+
onBlur: function (terminal) {
41+
return false;
42+
}
43+
});
44+
45+
Socket.on('initial status', function (data) {
46+
terminal.clear();
47+
if (data.status === 1 || data.status === 2) {
48+
Socket.emit('send server log');
49+
}
50+
});
5151
}
5252

53-
if (TerminalQueue.length > 0) {
54-
for (var i = 0; i < CONSOLE_PUSH_COUNT && TerminalQueue.length > 0; i++) {
55-
Terminal.echo(TerminalQueue[0]);
56-
TerminalQueue.shift();
53+
function pushOutputQueue() {
54+
if (termianlQueue.length > CONSOLE_PUSH_COUNT) {
55+
// console throttled warning show
5756
}
57+
58+
if (termianlQueue.length > 0) {
59+
for (var i = 0; i < CONSOLE_PUSH_COUNT && termianlQueue.length > 0; i++) {
60+
terminal.echo(termianlQueue[0]);
61+
termianlQueue.shift();
62+
}
63+
}
64+
65+
window.setTimeout(pushOutputQueue, CONSOLE_PUSH_FREQ);
5866
}
5967

60-
window.setTimeout(pushOutputQueue, CONSOLE_PUSH_FREQ);
61-
})();
68+
return {
69+
init: function () {
70+
$('[data-attr="power"]').click(function (event) {
71+
if (! $(this).hasClass('disabled')) {
72+
Socket.emit('set status', $(this).data('action'));
73+
}
74+
});
6275

63-
$(document).ready(function () {
64-
$('[data-attr="power"]').click(function (event) {
65-
if (! $(this).hasClass('disabled')) {
66-
Socket.emit('set status', $(this).data('action'));
67-
}
68-
});
69-
70-
var ctc = $('#chart_cpu');
71-
var timeLabels = [];
72-
var cpuData = [];
73-
var CPUChart = new Chart(ctc, {
74-
type: 'line',
75-
data: {
76-
labels: timeLabels,
77-
datasets: [
78-
{
79-
label: "Percent Use",
80-
fill: false,
81-
lineTension: 0.03,
82-
backgroundColor: "#00A1CB",
83-
borderColor: "#00A1CB",
84-
borderCapStyle: 'butt',
85-
borderDash: [],
86-
borderDashOffset: 0.0,
87-
borderJoinStyle: 'miter',
88-
pointBorderColor: "rgba(75,192,192,1)",
89-
pointBackgroundColor: "#fff",
90-
pointBorderWidth: 1,
91-
pointHoverRadius: 5,
92-
pointHoverBackgroundColor: "rgba(75,192,192,1)",
93-
pointHoverBorderColor: "rgba(220,220,220,1)",
94-
pointHoverBorderWidth: 2,
95-
pointRadius: 1,
96-
pointHitRadius: 10,
97-
data: cpuData,
98-
spanGaps: false,
76+
var ctc = $('#chart_cpu');
77+
var timeLabels = [];
78+
var cpuData = [];
79+
var CPUChart = new Chart(ctc, {
80+
type: 'line',
81+
data: {
82+
labels: timeLabels,
83+
datasets: [
84+
{
85+
label: "Percent Use",
86+
fill: false,
87+
lineTension: 0.03,
88+
backgroundColor: "#00A1CB",
89+
borderColor: "#00A1CB",
90+
borderCapStyle: 'butt',
91+
borderDash: [],
92+
borderDashOffset: 0.0,
93+
borderJoinStyle: 'miter',
94+
pointBorderColor: "rgba(75,192,192,1)",
95+
pointBackgroundColor: "#fff",
96+
pointBorderWidth: 1,
97+
pointHoverRadius: 5,
98+
pointHoverBackgroundColor: "rgba(75,192,192,1)",
99+
pointHoverBorderColor: "rgba(220,220,220,1)",
100+
pointHoverBorderWidth: 2,
101+
pointRadius: 1,
102+
pointHitRadius: 10,
103+
data: cpuData,
104+
spanGaps: false,
105+
}
106+
]
107+
},
108+
options: {
109+
title: {
110+
display: true,
111+
text: 'CPU Usage (as Percent Total)'
112+
},
113+
legend: {
114+
display: false,
115+
},
116+
animation: {
117+
duration: 1,
118+
}
99119
}
100-
]
101-
},
102-
options: {
103-
title: {
104-
display: true,
105-
text: 'CPU Usage (as Percent Total)'
106-
},
107-
legend: {
108-
display: false,
109-
},
110-
animation: {
111-
duration: 1,
112-
}
113-
}
114-
});
115-
116-
var ctm = $('#chart_memory');
117-
var memoryData = [];
118-
var MemoryChart = new Chart(ctm, {
119-
type: 'line',
120-
data: {
121-
labels: timeLabels,
122-
datasets: [
123-
{
124-
label: "Memory Use",
125-
fill: false,
126-
lineTension: 0.03,
127-
backgroundColor: "#01A4A4",
128-
borderColor: "#01A4A4",
129-
borderCapStyle: 'butt',
130-
borderDash: [],
131-
borderDashOffset: 0.0,
132-
borderJoinStyle: 'miter',
133-
pointBorderColor: "rgba(75,192,192,1)",
134-
pointBackgroundColor: "#fff",
135-
pointBorderWidth: 1,
136-
pointHoverRadius: 5,
137-
pointHoverBackgroundColor: "rgba(75,192,192,1)",
138-
pointHoverBorderColor: "rgba(220,220,220,1)",
139-
pointHoverBorderWidth: 2,
140-
pointRadius: 1,
141-
pointHitRadius: 10,
142-
data: memoryData,
143-
spanGaps: false,
120+
});
121+
122+
var ctm = $('#chart_memory');
123+
var memoryData = [];
124+
var MemoryChart = new Chart(ctm, {
125+
type: 'line',
126+
data: {
127+
labels: timeLabels,
128+
datasets: [
129+
{
130+
label: "Memory Use",
131+
fill: false,
132+
lineTension: 0.03,
133+
backgroundColor: "#01A4A4",
134+
borderColor: "#01A4A4",
135+
borderCapStyle: 'butt',
136+
borderDash: [],
137+
borderDashOffset: 0.0,
138+
borderJoinStyle: 'miter',
139+
pointBorderColor: "rgba(75,192,192,1)",
140+
pointBackgroundColor: "#fff",
141+
pointBorderWidth: 1,
142+
pointHoverRadius: 5,
143+
pointHoverBackgroundColor: "rgba(75,192,192,1)",
144+
pointHoverBorderColor: "rgba(220,220,220,1)",
145+
pointHoverBorderWidth: 2,
146+
pointRadius: 1,
147+
pointHitRadius: 10,
148+
data: memoryData,
149+
spanGaps: false,
150+
}
151+
]
152+
},
153+
options: {
154+
title: {
155+
display: true,
156+
text: 'Memory Usage (in Megabytes)'
157+
},
158+
legend: {
159+
display: false,
160+
},
161+
animation: {
162+
duration: 1,
163+
}
144164
}
145-
]
146-
},
147-
options: {
148-
title: {
149-
display: true,
150-
text: 'Memory Usage (in Megabytes)'
151-
},
152-
legend: {
153-
display: false,
154-
},
155-
animation: {
156-
duration: 1,
157-
}
158-
}
159-
});
160-
Socket.on('proc', function (proc) {
161-
if (cpuData.length > 10) {
162-
cpuData.shift();
163-
memoryData.shift();
164-
timeLabels.shift();
165-
}
165+
});
166+
Socket.on('proc', function (proc) {
167+
if (cpuData.length > 10) {
168+
cpuData.shift();
169+
memoryData.shift();
170+
timeLabels.shift();
171+
}
172+
173+
var cpuUse = (Pterodactyl.server.cpu > 0) ? parseFloat(((proc.data.cpu.total / Pterodactyl.server.cpu) * 100).toFixed(3).toString()) : proc.data.cpu.total;
174+
cpuData.push(cpuUse);
175+
memoryData.push(parseInt(proc.data.memory.total / (1024 * 1024)));
176+
177+
var m = new Date();
178+
timeLabels.push($.format.date(new Date(), 'HH:mm:ss'));
179+
180+
CPUChart.update();
181+
MemoryChart.update();
182+
});
183+
184+
// Update Listings on Initial Status
185+
Socket.on('initial status', function (data) {
186+
updateServerPowerControls(data.status);
187+
});
166188

167-
var cpuUse = (Pterodactyl.server.cpu > 0) ? parseFloat(((proc.data.cpu.total / Pterodactyl.server.cpu) * 100).toFixed(3).toString()) : proc.data.cpu.total;
168-
cpuData.push(cpuUse);
169-
memoryData.push(parseInt(proc.data.memory.total / (1024 * 1024)));
170-
171-
var m = new Date();
172-
timeLabels.push($.format.date(new Date(), 'HH:mm:ss'));
173-
174-
CPUChart.update();
175-
MemoryChart.update();
176-
});
177-
178-
// Update Listings on Initial Status
179-
Socket.on('initial status', function (data) {
180-
updateServerPowerControls(data.status);
181-
});
182-
183-
// Update Listings on Status
184-
Socket.on('status', function (data) {
185-
updateServerPowerControls(data.status);
186-
});
187-
188-
function updateServerPowerControls (data) {
189-
// Server is On or Starting
190-
if(data == 1 || data == 2) {
191-
$('[data-attr="power"][data-action="start"]').addClass('disabled');
192-
$('[data-attr="power"][data-action="stop"], [data-attr="power"][data-action="restart"]').removeClass('disabled');
193-
} else {
194-
if (data == 0) {
195-
$('[data-attr="power"][data-action="start"]').removeClass('disabled');
189+
// Update Listings on Status
190+
Socket.on('status', function (data) {
191+
updateServerPowerControls(data.status);
192+
});
193+
194+
function updateServerPowerControls (data) {
195+
// Server is On or Starting
196+
if(data == 1 || data == 2) {
197+
$('[data-attr="power"][data-action="start"]').addClass('disabled');
198+
$('[data-attr="power"][data-action="stop"], [data-attr="power"][data-action="restart"]').removeClass('disabled');
199+
} else {
200+
if (data == 0) {
201+
$('[data-attr="power"][data-action="start"]').removeClass('disabled');
202+
}
203+
$('[data-attr="power"][data-action="stop"], [data-attr="power"][data-action="restart"]').addClass('disabled');
204+
}
205+
206+
if(data !== 0) {
207+
$('[data-attr="power"][data-action="kill"]').removeClass('disabled');
208+
} else {
209+
$('[data-attr="power"][data-action="kill"]').addClass('disabled');
210+
}
196211
}
197-
$('[data-attr="power"][data-action="stop"], [data-attr="power"][data-action="restart"]').addClass('disabled');
198-
}
212+
},
199213

200-
if(data !== 0) {
201-
$('[data-attr="power"][data-action="kill"]').removeClass('disabled');
202-
} else {
203-
$('[data-attr="power"][data-action="kill"]').addClass('disabled');
204-
}
214+
getTerminal: function() {
215+
return terminal
216+
},
217+
218+
getTerminalQueue: function() {
219+
return terminalQueue
220+
},
205221
}
222+
223+
});
224+
225+
$(document).ready(function () {
226+
Console.init();
206227
});

0 commit comments

Comments
 (0)