From 75d0d28e8bb1ec730298af1d48bd2b29f3595075 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Sat, 6 Nov 2021 01:05:45 +0100 Subject: [PATCH] Shell: Refactor usage of methods and const --- app/assets/javascripts/shell.js | 48 ++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/app/assets/javascripts/shell.js b/app/assets/javascripts/shell.js index d30e800a..6b875e18 100644 --- a/app/assets/javascripts/shell.js +++ b/app/assets/javascripts/shell.js @@ -1,11 +1,11 @@ $(document).on('turbolinks:load', function () { - var ENTER_KEY_CODE = 13; + const ENTER_KEY_CODE = 13; - var clearOutput = function () { + const clearOutput = function () { $('#output').html(''); }; - var executeCommand = function (command) { + const executeCommand = function (command) { $.ajax({ data: { command: command @@ -15,9 +15,9 @@ $(document).on('turbolinks:load', function () { }).done(handleResponse); }; - var handleKeyPress = function (event) { + const handleKeyPress = function (event) { if (event.which === ENTER_KEY_CODE) { - var command = $(this).val(); + const command = $(this).val(); if (command === 'clear') { clearOutput(); } else { @@ -28,7 +28,7 @@ $(document).on('turbolinks:load', function () { } }; - var handleResponse = function (response) { + const handleResponse = function (response) { if (response.status === 'timeout') { printTimeout(response); } else { @@ -36,45 +36,51 @@ $(document).on('turbolinks:load', function () { } }; - var printCommand = function (command) { - $('#output').append('

' + command + '

'); + const printCommand = function (command) { + const em = $(''); + em.text(command); + const p = $('

'); + p.append(em) + $('#output').append(p); }; - var printOutput = function (output) { + const printOutput = function (output) { if (output) { if (output.stdout) { - var element = $('

'); + const element = $('

'); element.addClass('text-success'); - element.html(output.stdout); + element.text(output.stdout); $('#output').append(element); } if (output.stderr) { - var element = $('

'); + const element = $('

'); element.addClass('text-warning'); - element.html(output.stderr); + element.text(output.stderr); $('#output').append(element); } if (!output.stdout && !output.stderr) { - var element = $('

'); + const element = $('

'); element.addClass('text-muted'); - element.html($('#output').data('message-no-output')); - $('#output').append(element); + const output = $('#output'); + element.text(output.data('message-no-output')); + output.append(element); } } }; - var printTimeout = function (output) { - var element = $.append('

'); + const printTimeout = function (output) { + const element = $.append('

'); element.addClass('text-danger'); - element.html($('#shell').data('message-timeout')); + element.text($('#shell').data('message-timeout')); $('#output').append(element); }; if ($('#shell').isPresent()) { - $('#command').focus(); - $('#command').on('keypress', handleKeyPress); + const command = $('#command') + command.focus(); + command.on('keypress', handleKeyPress); } }) ;