Files
codeocean/app/assets/javascripts/editor/prompt.js
2018-11-06 16:46:01 +01:00

45 lines
1.3 KiB
JavaScript

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();
}
}
};