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