switch console javascript code to revealing module pattern to avoid global variables

This commit is contained in:
Jakob Schrettenbrunner 2017-01-26 22:12:33 +01:00
parent bf7b58470a
commit 96f5d15811

View file

@ -20,9 +20,14 @@
var CONSOLE_PUSH_COUNT = 50; var CONSOLE_PUSH_COUNT = 50;
var CONSOLE_PUSH_FREQ = 200; var CONSOLE_PUSH_FREQ = 200;
(function initConsole() { const Console = (function () {
window.TerminalQueue = [];
window.Terminal = $('#terminal').terminal(function (command, term) { var terminalQueue;
var terminal;
function initConsole() {
termianlQueue = [];
terminal = $('#terminal').terminal(function (command, term) {
Socket.emit('send command', command); Socket.emit('send command', command);
}, { }, {
greetings: '', greetings: '',
@ -38,29 +43,30 @@ var CONSOLE_PUSH_FREQ = 200;
}); });
Socket.on('initial status', function (data) { Socket.on('initial status', function (data) {
Terminal.clear(); terminal.clear();
if (data.status === 1 || data.status === 2) { if (data.status === 1 || data.status === 2) {
Socket.emit('send server log'); Socket.emit('send server log');
} }
}); });
})(); }
(function pushOutputQueue() { function pushOutputQueue() {
if (TerminalQueue.length > CONSOLE_PUSH_COUNT) { if (termianlQueue.length > CONSOLE_PUSH_COUNT) {
// console throttled warning show // console throttled warning show
} }
if (TerminalQueue.length > 0) { if (termianlQueue.length > 0) {
for (var i = 0; i < CONSOLE_PUSH_COUNT && TerminalQueue.length > 0; i++) { for (var i = 0; i < CONSOLE_PUSH_COUNT && termianlQueue.length > 0; i++) {
Terminal.echo(TerminalQueue[0]); terminal.echo(termianlQueue[0]);
TerminalQueue.shift(); termianlQueue.shift();
} }
} }
window.setTimeout(pushOutputQueue, CONSOLE_PUSH_FREQ); window.setTimeout(pushOutputQueue, CONSOLE_PUSH_FREQ);
})(); }
$(document).ready(function () { return {
init: function () {
$('[data-attr="power"]').click(function (event) { $('[data-attr="power"]').click(function (event) {
if (! $(this).hasClass('disabled')) { if (! $(this).hasClass('disabled')) {
Socket.emit('set status', $(this).data('action')); Socket.emit('set status', $(this).data('action'));
@ -203,4 +209,19 @@ $(document).ready(function () {
$('[data-attr="power"][data-action="kill"]').addClass('disabled'); $('[data-attr="power"][data-action="kill"]').addClass('disabled');
} }
} }
},
getTerminal: function() {
return terminal
},
getTerminalQueue: function() {
return terminalQueue
},
}
});
$(document).ready(function () {
Console.init();
}); });