diff --git a/app/assets/javascripts/editor.js.erb b/app/assets/javascripts/editor.js.erb index e4f66c4c..87a39fb2 100644 --- a/app/assets/javascripts/editor.js.erb +++ b/app/assets/javascripts/editor.js.erb @@ -11,7 +11,8 @@ $(function() { CodeOceanEditorFlowr, CodeOceanEditorSubmissions, CodeOceanEditorTurtle, - CodeOceanEditorWebsocket + CodeOceanEditorWebsocket, + CodeOceanEditorPrompt ); if ($('#editor').isPresent() && CodeOceanEditor) { diff --git a/app/assets/javascripts/editor/editor.js.erb b/app/assets/javascripts/editor/editor.js.erb index 3d1c7257..c6ed46c2 100644 --- a/app/assets/javascripts/editor/editor.js.erb +++ b/app/assets/javascripts/editor/editor.js.erb @@ -28,7 +28,6 @@ var CodeOceanEditor = { output_mode_is_streaming: true, numMessages: 0, - prompt: '#prompt', lastCopyText: null, autosaveTimer: null, diff --git a/app/assets/javascripts/editor/evaluation.js.erb b/app/assets/javascripts/editor/evaluation.js.erb index fdc3d552..1c457b36 100644 --- a/app/assets/javascripts/editor/evaluation.js.erb +++ b/app/assets/javascripts/editor/evaluation.js.erb @@ -111,82 +111,27 @@ CodeOceanEditorEvaluation = { stopCode: function (event) { event.preventDefault(); if (this.isActiveFileStoppable()) { - this.killWebsocketAndContainer(); + this.websocket.send(JSON.stringify({'cmd': 'client_kill'})); + this.killWebsocket(); + this.cleanUpUI(); } }, - stopCodeServerSendEvent: function (event) { - var jqxhr = this.ajax({ - data: { - container_id: $('#stop').data('container').id - }, - url: $('#stop').data('url') - }); - jqxhr.always(function () { - this.hideSpinner(); - this.running = false; - this.toggleButtonStates(); - }.bind(this)); - jqxhr.fail(ajaxError); - }, - - killWebsocketAndContainer: function () { - if (this.websocket.getReadyState() != WebSocket.OPEN) { + killWebsocket: function () { + if (this.websocket != null && this.websocket.getReadyState() != WebSocket.OPEN) { return; } - this.websocket.send(JSON.stringify({cmd: 'exit'})); this.websocket.killWebSocket(); - - this.hideSpinner(); this.running = false; + }, + + cleanUpUI: function() { + this.hideSpinner(); this.toggleButtonStates(); this.hidePrompt(); }, - //TODO: Move Prompt Part in own component - showPrompt: function(msg) { - var label = $('#prompt .input-group-addon'); - var prompt = $(this.prompt); - label.text(msg.data || label.data('prompt')); - if (prompt.isPresent() && prompt.hasClass('hidden')) { - prompt.removeClass('hidden'); - } - $('#prompt input').focus(); - }, - - hidePrompt: function() { - var prompt = $(this.prompt); - if (prompt.isPresent() && !prompt.hasClass('hidden')) { - prompt.addClass('hidden'); - } - }, - - initPrompt: function() { - if ($('#run').isPresent()) { - $('#run').bind('click', this.hidePrompt.bind(this)); - } - if ($('#prompt').isPresent()) { - $('#prompt').on('keypress', this.handlePromptKeyPress.bind(this)); - $('#prompt-submit').on('click', this.submitPromptInput.bind(this)); - } - }, - - submitPromptInput: function() { - var input = $('#prompt-input'); - var message = input.val(); - this.websocket.send(JSON.stringify({cmd: 'result', 'data': message})); - this.websocket.flush(); - input.val(''); - this.hidePrompt(); - }, - - handlePromptKeyPress: function(evt) { - if (evt.which === this.ENTER_KEY_CODE) { - this.submitPromptInput(); - } - }, - renderWebsocketOutput: function(msg){ var element = this.findOrCreateRenderElement(0); element.append(msg.data); diff --git a/app/assets/javascripts/editor/execution.js.erb b/app/assets/javascripts/editor/execution.js.erb index c28c47a4..acc7eb55 100644 --- a/app/assets/javascripts/editor/execution.js.erb +++ b/app/assets/javascripts/editor/execution.js.erb @@ -39,10 +39,11 @@ CodeOceanEditorWebsocket = { }, handleExitCommand: function() { - this.killWebsocketAndContainer(); + this.killWebsocket(); this.handleQaApiOutput(); this.handleStderrOutputForFlowr(); this.augmentStacktraceInOutput(); this.cleanUpTurtle(); + this.cleanUpUI(); } }; \ No newline at end of file diff --git a/app/assets/javascripts/editor/prompt.js b/app/assets/javascripts/editor/prompt.js new file mode 100644 index 00000000..1cbdfc8f --- /dev/null +++ b/app/assets/javascripts/editor/prompt.js @@ -0,0 +1,45 @@ +CodeOceanEditorPrompt = { + prompt: '#prompt', + + showPrompt: function(msg) { + var label = $('#prompt .input-group-addon'); + var prompt = $(this.prompt); + label.text(msg.data || label.data('prompt')); + if (prompt.isPresent() && prompt.hasClass('hidden')) { + prompt.removeClass('hidden'); + } + $('#prompt input').focus(); + }, + + hidePrompt: function() { + var prompt = $(this.prompt); + if (prompt.isPresent() && !prompt.hasClass('hidden')) { + prompt.addClass('hidden'); + } + }, + + initPrompt: function() { + if ($('#run').isPresent()) { + $('#run').bind('click', this.hidePrompt.bind(this)); + } + if ($('#prompt').isPresent()) { + $('#prompt').on('keypress', this.handlePromptKeyPress.bind(this)); + $('#prompt-submit').on('click', this.submitPromptInput.bind(this)); + } + }, + + submitPromptInput: function() { + var input = $('#prompt-input'); + var message = input.val(); + this.websocket.send(JSON.stringify({cmd: 'result', 'data': message})); + this.websocket.flush(); + input.val(''); + this.hidePrompt(); + }, + + handlePromptKeyPress: function(evt) { + if (evt.which === this.ENTER_KEY_CODE) { + this.submitPromptInput(); + } + } +}; \ No newline at end of file diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index 865ff747..7ff99437 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -139,12 +139,12 @@ class SubmissionsController < ApplicationController tubesock.onmessage do |data| Rails.logger.info(Time.now.getutc.to_s + ": Client sending: " + data) # Check whether the client send a JSON command and kill container - # if the command is 'exit', send it to docker otherwise. + # if the command is 'client_exit', send it to docker otherwise. begin parsed = JSON.parse(data) - if parsed['cmd'] == 'exit' + if parsed['cmd'] == 'client_kill' Rails.logger.debug("Client exited container.") - @docker_client.exit_container(result[:container]) + @docker_client.kill_container(result[:container]) else socket.send data Rails.logger.debug('Sent the received client data to docker:' + data)