diff options
author | Prabhu Ramachandran | 2017-09-15 16:31:50 +0530 |
---|---|---|
committer | GitHub | 2017-09-15 16:31:50 +0530 |
commit | 7419e3b3f4e14f86f21f9464843f9263638fe7a2 (patch) | |
tree | a046617f76879ea5c056ae2c8f39b0a97c8a18a7 /yaksh/static | |
parent | e3a43662d2aae8688039671d3de532e48fbdfda9 (diff) | |
parent | f65102cf4b6a117a3ff86971ad9c1ddd3362c9fd (diff) | |
download | online_test-7419e3b3f4e14f86f21f9464843f9263638fe7a2.tar.gz online_test-7419e3b3f4e14f86f21f9464843f9263638fe7a2.tar.bz2 online_test-7419e3b3f4e14f86f21f9464843f9263638fe7a2.zip |
Merge pull request #326 from FOSSEE/improve-code-server
Improve code server
Diffstat (limited to 'yaksh/static')
-rw-r--r-- | yaksh/static/yaksh/css/ontop.css | 20 | ||||
-rw-r--r-- | yaksh/static/yaksh/js/question.js | 12 | ||||
-rw-r--r-- | yaksh/static/yaksh/js/requesthandler.js | 126 |
3 files changed, 146 insertions, 12 deletions
diff --git a/yaksh/static/yaksh/css/ontop.css b/yaksh/static/yaksh/css/ontop.css new file mode 100644 index 0000000..fb22066 --- /dev/null +++ b/yaksh/static/yaksh/css/ontop.css @@ -0,0 +1,20 @@ +#ontop { + position: fixed; + display: none; + width: 100%; + height: 100%; + top:0; + bottom: 0; + left:0; + right: 0; + background-color: rgba(0,0,0,0.5); + z-index: 1001; /* 1001 coz sidebar is 1000. So will be on top of sidebar*/ +} + +#state { + position: absolute; + top: 50%; + left: 50%; + font-size: 30px; + color: white; +} diff --git a/yaksh/static/yaksh/js/question.js b/yaksh/static/yaksh/js/question.js deleted file mode 100644 index 96ff3de..0000000 --- a/yaksh/static/yaksh/js/question.js +++ /dev/null @@ -1,12 +0,0 @@ -function submitCode() -{ - document.forms["code"].submit(); - var x = document.getElementById("status"); - x.innerHTML = "<strong>Checking answer ...</strong>"; - x = document.getElementById("check"); - x.disabled = true; - x.value = "Checking Answer ..."; - if (document.getElementById("skip")!=null) { - document.getElementById("skip").disabled = true; - } -} diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js new file mode 100644 index 0000000..9fcf5b6 --- /dev/null +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -0,0 +1,126 @@ +request_status = "initial"; +count = 0; +MAX_COUNT = 14 + +function reset_values() { + request_status = "initial"; + count = 0; +} +function check_state(state, uid) { + if ((state == "running" || state == "not started") && count < MAX_COUNT) { + count++; + setTimeout(function() {get_result(uid);}, 2000); + } else if (state == "unknown") { + reset_values(); + notify("Request timeout. Try again later."); + unlock_screen(); + } else { + reset_values() + notify("Please try again."); + unlock_screen(); + } +} + +function notify(text) { + var notice = document.getElementById("notification"); + notice.classList.add("alert"); + notice.classList.add("alert-success"); + notice.innerHTML = text; +} + +function lock_screen() { + document.getElementById("ontop").style.display = "block"; +} + +function unlock_screen() { + document.getElementById("ontop").style.display = "none"; +} + +function get_result(uid){ + var url = "/exam/get_result/" + uid + "/"; + ajax_check_code(url, "GET", "html", null, uid) +} + +function response_handler(method_type, content_type, data, uid){ + if(content_type.indexOf("text/html") !== -1) { + if( method_type === "POST") { + reset_values(); + } + unlock_screen(); + document.open(); + document.write(data); + document.close(); + } else if(content_type.indexOf("application/json") !== -1) { + res = JSON.parse(data); + request_status = res.status; + if(method_type === "POST") { + uid = res.uid; + } + check_state(request_status, uid); + } else { + reset_values(); + unlock_screen(); + } +} + +function ajax_check_code(url, method_type, data_type, data, uid) { + $.ajax({ + method: method_type, + url: url, + data: data, + dataType: data_type, + success: function(data, status, xhr) { + content_type = xhr.getResponseHeader("content-type"); + response_handler(method_type, content_type, data, uid) + }, + error: function(xhr, text_status, error_thrown ) { + reset_values(); + unlock_screen(); + notify("There is some problem. Try later.") + } + }); + +} + +var global_editor = {}; +$(document).ready(function(){ + // Codemirror object, language modes and initial content + // Get the textarea node + var textarea_node = document.querySelector('#answer'); + + var mode_dict = { + 'python': 'python', + 'c': 'text/x-csrc', + 'cpp': 'text/x-c++src', + 'java': 'text/x-java', + 'bash': 'text/x-sh', + 'scilab': 'text/x-csrc' + } + + // Code mirror Options + var options = { + mode: mode_dict[lang], + gutter: true, + lineNumbers: true, + onChange: function (instance, changes) { + render(); + } + }; + + // Initialize the codemirror editor + global_editor.editor = CodeMirror.fromTextArea(textarea_node, options); + + // Setting code editors initial content + global_editor.editor.setValue(init_val); + + function reset_editor() { + global_editor.editor.setValue(init_val); + global_editor.editor.clearHistory(); + } + $('#code').submit(function(e) { + lock_screen(); + var data = $(this).serializeArray(); + ajax_check_code($(this).attr("action"), "POST", "html", data, null) + e.preventDefault(); // To stop the default form submission. + }); +}); |