From ce995e06e3509a1340061c51dfa08a65c69eef66 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 14 Aug 2017 12:10:42 +0530 Subject: Front-end modification for improved code server Added JQuery to handle request. Sends ajax request and gets json as response. json contains token i.e uid which is answer id. Using uid, an ajax request is sent after every 2 secs till the server gives the desire result. If the code result has error then html is written on the document. If the result has correct answer then next question is displayed. *includes function for string will not work for older browers. Will substitute with a different function in next commit. --- yaksh/static/yaksh/js/question.js | 12 ----- yaksh/static/yaksh/js/requesthandler.js | 90 +++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 12 deletions(-) delete mode 100644 yaksh/static/yaksh/js/question.js create mode 100644 yaksh/static/yaksh/js/requesthandler.js (limited to 'yaksh/static') 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 = "Checking answer ..."; - 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..9d023cc --- /dev/null +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -0,0 +1,90 @@ +request_status = "initial" +function submitRequest(){ + document.forms["code"].submit(); +} + +function check_state(state, uid, success) { + if (state == "running") { + setTimeout(get_result(uid), 2000); + } +} + +function get_result(uid){ + $.ajax({ + method: "GET", + url: "/exam/get_results/"+uid+"/", + dataType: "html", // Your server can response html, json, xml format. + success: function(data, status, xhr) { + content_type = xhr.getResponseHeader("content-type"); + if(content_type.includes("text/html")) { + request_status = "initial"; + document.open(); + document.write(data); + document.close(); + } else if(content_type.includes("application/json")) { + res = JSON.parse(data); + request_status = res.status; + check_state(request_status, uid, res.success); + } + } + }); +} + +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) { + $.ajax({ + type: 'POST', + url: $(this).attr("action"), + data: $(this).serializeArray(), + dataType: "html", // Your server can response html, json, xml format. + success: function(data, status, xhr) { + content_type = xhr.getResponseHeader("content-type"); + if(content_type.includes("text/html")) { + document.open(); + document.write(data); + document.close(); + } else if(content_type.includes("application/json")) { + res = JSON.parse(data); + var uid = res.uid; + request_status = res.state; + check_state(request_status, uid, false); + } + } + }); + e.preventDefault(); // To stop the default form submission. + }); +}); -- cgit From 49615e5a24ecfdd0b22bae080e7f9bb2507bbfd7 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 14 Aug 2017 14:51:39 +0530 Subject: To handle unknown status response from code server --- yaksh/static/yaksh/js/requesthandler.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'yaksh/static') diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index 9d023cc..b637928 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -3,9 +3,15 @@ function submitRequest(){ document.forms["code"].submit(); } -function check_state(state, uid, success) { - if (state == "running") { +function check_state(state, uid) { + if (state == "running" || state == "not started") { setTimeout(get_result(uid), 2000); + } else if (state == "unknown") { + request_status = "initial"; + var $notice = document.getElementById("notification"); + $notice.classList.add("alert"); + $notice.classList.add("alert-success"); + $notice.innerHTML = "Your are requesting for a wrong data"; } } @@ -24,7 +30,7 @@ function get_result(uid){ } else if(content_type.includes("application/json")) { res = JSON.parse(data); request_status = res.status; - check_state(request_status, uid, res.success); + check_state(request_status, uid); } } }); @@ -74,6 +80,7 @@ $(document).ready(function(){ success: function(data, status, xhr) { content_type = xhr.getResponseHeader("content-type"); if(content_type.includes("text/html")) { + request_status = "initial" document.open(); document.write(data); document.close(); @@ -81,7 +88,7 @@ $(document).ready(function(){ res = JSON.parse(data); var uid = res.uid; request_status = res.state; - check_state(request_status, uid, false); + check_state(request_status, uid); } } }); -- cgit From f730a531cf041620209abb812792080cb2d63b4d Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 14 Aug 2017 22:33:02 +0530 Subject: Changes related to front-end Removed snippet append in the check view as snippet is not posted in request. Added an overlay when an user submits a code, with a status text on it. This is to block user from triggering any other event when JS is running. Overlay disappears when JS complete its execution. On time out a request is posted via JS, it receives a JSON response but cannot display user the error as time is over. So in such case, the django itself handles the result and does not return JSONv response. --- yaksh/static/yaksh/css/ontop.css | 20 +++++++++++++++ yaksh/static/yaksh/js/requesthandler.js | 44 +++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 yaksh/static/yaksh/css/ontop.css (limited to 'yaksh/static') 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/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index b637928..d463434 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -8,13 +8,29 @@ function check_state(state, uid) { setTimeout(get_result(uid), 2000); } else if (state == "unknown") { request_status = "initial"; - var $notice = document.getElementById("notification"); - $notice.classList.add("alert"); - $notice.classList.add("alert-success"); - $notice.innerHTML = "Your are requesting for a wrong data"; + notify("You are requesting for a wrong data"); + unlock_screen(); + } else { + request_status = "initial"; + 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){ $.ajax({ method: "GET", @@ -23,7 +39,7 @@ function get_result(uid){ success: function(data, status, xhr) { content_type = xhr.getResponseHeader("content-type"); if(content_type.includes("text/html")) { - request_status = "initial"; + unlock_screen(); document.open(); document.write(data); document.close(); @@ -31,7 +47,15 @@ function get_result(uid){ res = JSON.parse(data); request_status = res.status; check_state(request_status, uid); + } else { + request_status = "initial"; + unlock_screen(); } + }, + error: function(xhr, text_status, error_thrown ) { + request_status = "initial"; + unlock_screen(); + notify("There is some problem. Try later.") } }); } @@ -72,6 +96,7 @@ $(document).ready(function(){ global_editor.editor.clearHistory(); } $('#code').submit(function(e) { + lock_screen(); $.ajax({ type: 'POST', url: $(this).attr("action"), @@ -81,6 +106,7 @@ $(document).ready(function(){ content_type = xhr.getResponseHeader("content-type"); if(content_type.includes("text/html")) { request_status = "initial" + unlock_screen(); document.open(); document.write(data); document.close(); @@ -89,7 +115,15 @@ $(document).ready(function(){ var uid = res.uid; request_status = res.state; check_state(request_status, uid); + } else { + request_status = "initial"; + unlock_screen(); } + }, + error: function(xhr, text_status, error_thrown ) { + request_status = "initial"; + unlock_screen(); + notify("There is some problem. Try later.") } }); e.preventDefault(); // To stop the default form submission. -- cgit From d108cd03e446dcac2b4eeeaec1ea17c47b205c78 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Thu, 17 Aug 2017 10:52:25 +0530 Subject: Fixed a bug related to GET request after every 2 seconds Also added a count to limit GET request So now for a given question, maximum GET request is 15. After 15 requests, if server still does not give the desired response then the request loop will break. --- yaksh/static/yaksh/js/requesthandler.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'yaksh/static') diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index d463434..f653da1 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -1,17 +1,22 @@ request_status = "initial" +count = 0; function submitRequest(){ document.forms["code"].submit(); } function check_state(state, uid) { - if (state == "running" || state == "not started") { - setTimeout(get_result(uid), 2000); + if ((state == "running" || state == "not started") && count < 15) { + count++; + setTimeout(function() {get_result(uid);}, 2000); } else if (state == "unknown") { request_status = "initial"; + count = 0; notify("You are requesting for a wrong data"); unlock_screen(); } else { request_status = "initial"; + count = 0; + notify("Please try after few minutes"); unlock_screen(); } } @@ -49,11 +54,13 @@ function get_result(uid){ check_state(request_status, uid); } else { request_status = "initial"; + count = 0; unlock_screen(); } }, error: function(xhr, text_status, error_thrown ) { request_status = "initial"; + count = 0; unlock_screen(); notify("There is some problem. Try later.") } @@ -106,6 +113,7 @@ $(document).ready(function(){ content_type = xhr.getResponseHeader("content-type"); if(content_type.includes("text/html")) { request_status = "initial" + count = 0; unlock_screen(); document.open(); document.write(data); @@ -117,11 +125,13 @@ $(document).ready(function(){ check_state(request_status, uid); } else { request_status = "initial"; + count = 0; unlock_screen(); } }, error: function(xhr, text_status, error_thrown ) { request_status = "initial"; + count = 0; unlock_screen(); notify("There is some problem. Try later.") } -- cgit From 055d0d1c51f90b4496a096a13bab5e4978fe9b92 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Tue, 22 Aug 2017 14:58:50 +0530 Subject: Added custom expected condition class To check if the ontop div display is none. If so then the selenium will proceed ahead else wait for the div property to change. Currently I have not specified the actual exception in try except, will add later Modified JS to handle ontop div --- yaksh/static/yaksh/js/requesthandler.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'yaksh/static') diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index f653da1..c5629ab 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -1,22 +1,25 @@ request_status = "initial" count = 0; +checker = null function submitRequest(){ document.forms["code"].submit(); } function check_state(state, uid) { - if ((state == "running" || state == "not started") && count < 15) { + if ((state == "running" || state == "not started") && count < 7) { count++; setTimeout(function() {get_result(uid);}, 2000); } else if (state == "unknown") { request_status = "initial"; count = 0; notify("You are requesting for a wrong data"); + clearInterval(checker); unlock_screen(); } else { request_status = "initial"; count = 0; notify("Please try after few minutes"); + clearInterval(checker); unlock_screen(); } } @@ -36,6 +39,13 @@ function unlock_screen() { document.getElementById("ontop").style.display = "none"; } +function check_lock_screen() { + var $ontop_div = document.getElementById("ontop"); + if ($ontop_div.style.display == "block") { + $ontop_div.style.display = "none"; + } +} + function get_result(uid){ $.ajax({ method: "GET", @@ -44,6 +54,7 @@ function get_result(uid){ success: function(data, status, xhr) { content_type = xhr.getResponseHeader("content-type"); if(content_type.includes("text/html")) { + clearInterval(checker); unlock_screen(); document.open(); document.write(data); @@ -55,12 +66,14 @@ function get_result(uid){ } else { request_status = "initial"; count = 0; + clearInterval(checker); unlock_screen(); } }, error: function(xhr, text_status, error_thrown ) { request_status = "initial"; count = 0; + clearInterval(checker); unlock_screen(); notify("There is some problem. Try later.") } @@ -103,6 +116,7 @@ $(document).ready(function(){ global_editor.editor.clearHistory(); } $('#code').submit(function(e) { + checker = setInterval(check_lock_screen, 30000); lock_screen(); $.ajax({ type: 'POST', @@ -114,6 +128,7 @@ $(document).ready(function(){ if(content_type.includes("text/html")) { request_status = "initial" count = 0; + clearInterval(checker); unlock_screen(); document.open(); document.write(data); @@ -126,12 +141,14 @@ $(document).ready(function(){ } else { request_status = "initial"; count = 0; + clearInterval(checker); unlock_screen(); } }, error: function(xhr, text_status, error_thrown ) { request_status = "initial"; count = 0; + clearInterval(checker); unlock_screen(); notify("There is some problem. Try later.") } -- cgit From 3d9bce5471d04bf50e03155d3cd67bdae44a4fcc Mon Sep 17 00:00:00 2001 From: prathamesh Date: Thu, 31 Aug 2017 16:28:57 +0530 Subject: updated travis config and added print to debug on travis --- yaksh/static/yaksh/js/requesthandler.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'yaksh/static') diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index c5629ab..39114e7 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -53,13 +53,13 @@ function get_result(uid){ dataType: "html", // Your server can response html, json, xml format. success: function(data, status, xhr) { content_type = xhr.getResponseHeader("content-type"); - if(content_type.includes("text/html")) { + if(content_type.indexOf("text/html") !== -1) { clearInterval(checker); unlock_screen(); document.open(); document.write(data); document.close(); - } else if(content_type.includes("application/json")) { + } else if(content_type.indexOf("application/json") !== -1) { res = JSON.parse(data); request_status = res.status; check_state(request_status, uid); @@ -116,7 +116,7 @@ $(document).ready(function(){ global_editor.editor.clearHistory(); } $('#code').submit(function(e) { - checker = setInterval(check_lock_screen, 30000); + checker = setInterval(check_lock_screen, 20000); lock_screen(); $.ajax({ type: 'POST', @@ -125,7 +125,7 @@ $(document).ready(function(){ dataType: "html", // Your server can response html, json, xml format. success: function(data, status, xhr) { content_type = xhr.getResponseHeader("content-type"); - if(content_type.includes("text/html")) { + if(content_type.indexOf("text/html") !== -1) { request_status = "initial" count = 0; clearInterval(checker); @@ -133,7 +133,7 @@ $(document).ready(function(){ document.open(); document.write(data); document.close(); - } else if(content_type.includes("application/json")) { + } else if(content_type.indexOf("application/json") !== -1) { res = JSON.parse(data); var uid = res.uid; request_status = res.state; -- cgit From e2c65655dcdc5558cfb4ab668c3012024d27ac75 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Fri, 1 Sep 2017 16:51:37 +0530 Subject: Removed all the checkpoints set for selenium tests on travis javascript strings includes method changed to indexOf, as includes belongs to ES6. This was the main reason for failure of selenium tests on travis. --- yaksh/static/yaksh/js/requesthandler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'yaksh/static') diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index 39114e7..9890b54 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -116,7 +116,7 @@ $(document).ready(function(){ global_editor.editor.clearHistory(); } $('#code').submit(function(e) { - checker = setInterval(check_lock_screen, 20000); + checker = setInterval(check_lock_screen, 30000); lock_screen(); $.ajax({ type: 'POST', -- cgit From 7aac7a18e4e393b7e267d3687be73a7fdfb7b400 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Tue, 5 Sep 2017 19:27:15 +0530 Subject: Removed redundancy from request handler JS --- yaksh/static/yaksh/js/requesthandler.js | 59 +++++++++++---------------------- 1 file changed, 20 insertions(+), 39 deletions(-) (limited to 'yaksh/static') diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index 9890b54..9e2c2e5 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -12,7 +12,7 @@ function check_state(state, uid) { } else if (state == "unknown") { request_status = "initial"; count = 0; - notify("You are requesting for a wrong data"); + notify("Request timeout. Try again later"); clearInterval(checker); unlock_screen(); } else { @@ -47,13 +47,23 @@ function check_lock_screen() { } function get_result(uid){ + var url = "/exam/get_results/"+uid+"/"; + ajax_call(url, "GET", "html", null, uid) +} + +function ajax_call(url, method_type, data_type, data, uid) { $.ajax({ - method: "GET", - url: "/exam/get_results/"+uid+"/", - dataType: "html", // Your server can response html, json, xml format. + method: method_type, + url: url, + data: data, + dataType: data_type, // Your server can response html, json, xml format. success: function(data, status, xhr) { content_type = xhr.getResponseHeader("content-type"); if(content_type.indexOf("text/html") !== -1) { + if( method_type === "POST") { + request_status = "initial"; + count = 0; + } clearInterval(checker); unlock_screen(); document.open(); @@ -62,6 +72,9 @@ function get_result(uid){ } 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 { request_status = "initial"; @@ -78,6 +91,7 @@ function get_result(uid){ notify("There is some problem. Try later.") } }); + } var global_editor = {}; @@ -118,41 +132,8 @@ $(document).ready(function(){ $('#code').submit(function(e) { checker = setInterval(check_lock_screen, 30000); lock_screen(); - $.ajax({ - type: 'POST', - url: $(this).attr("action"), - data: $(this).serializeArray(), - dataType: "html", // Your server can response html, json, xml format. - success: function(data, status, xhr) { - content_type = xhr.getResponseHeader("content-type"); - if(content_type.indexOf("text/html") !== -1) { - request_status = "initial" - count = 0; - clearInterval(checker); - unlock_screen(); - document.open(); - document.write(data); - document.close(); - } else if(content_type.indexOf("application/json") !== -1) { - res = JSON.parse(data); - var uid = res.uid; - request_status = res.state; - check_state(request_status, uid); - } else { - request_status = "initial"; - count = 0; - clearInterval(checker); - unlock_screen(); - } - }, - error: function(xhr, text_status, error_thrown ) { - request_status = "initial"; - count = 0; - clearInterval(checker); - unlock_screen(); - notify("There is some problem. Try later.") - } - }); + var data = $(this).serializeArray(); + ajax_call($(this).attr("action"), "POST", "html", data, null) e.preventDefault(); // To stop the default form submission. }); }); -- cgit From ad020f9039e33fc6c29e9f0e1de8fd56c2f78406 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Fri, 8 Sep 2017 13:41:40 +0530 Subject: Modification as per comments on the PR --- yaksh/static/yaksh/js/requesthandler.js | 92 +++++++++++++++------------------ 1 file changed, 41 insertions(+), 51 deletions(-) (limited to 'yaksh/static') diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index 9e2c2e5..73d0405 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -1,34 +1,34 @@ -request_status = "initial" +request_status = "initial"; count = 0; -checker = null + function submitRequest(){ document.forms["code"].submit(); } +function reset_values() { + request_status = "initial"; + count = 0; +} function check_state(state, uid) { if ((state == "running" || state == "not started") && count < 7) { count++; setTimeout(function() {get_result(uid);}, 2000); } else if (state == "unknown") { - request_status = "initial"; - count = 0; + reset_values(); notify("Request timeout. Try again later"); - clearInterval(checker); unlock_screen(); } else { - request_status = "initial"; - count = 0; - notify("Please try after few minutes"); - clearInterval(checker); + 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; + var notice = document.getElementById("notification"); + notice.classList.add("alert"); + notice.classList.add("alert-success"); + notice.innerHTML = text; } function lock_screen() { @@ -39,54 +39,45 @@ function unlock_screen() { document.getElementById("ontop").style.display = "none"; } -function check_lock_screen() { - var $ontop_div = document.getElementById("ontop"); - if ($ontop_div.style.display == "block") { - $ontop_div.style.display = "none"; - } +function get_result(uid){ + var url = "/exam/get_result/"+uid+"/"; + ajax_check_code(url, "GET", "html", null, uid) } -function get_result(uid){ - var url = "/exam/get_results/"+uid+"/"; - ajax_call(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_call(url, method_type, data_type, data, uid) { +function ajax_check_code(url, method_type, data_type, data, uid) { $.ajax({ method: method_type, url: url, data: data, - dataType: data_type, // Your server can response html, json, xml format. + dataType: data_type, success: function(data, status, xhr) { content_type = xhr.getResponseHeader("content-type"); - if(content_type.indexOf("text/html") !== -1) { - if( method_type === "POST") { - request_status = "initial"; - count = 0; - } - clearInterval(checker); - 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 { - request_status = "initial"; - count = 0; - clearInterval(checker); - unlock_screen(); - } + response_handler(method_type, content_type, data, uid) }, error: function(xhr, text_status, error_thrown ) { - request_status = "initial"; - count = 0; - clearInterval(checker); + reset_values(); unlock_screen(); notify("There is some problem. Try later.") } @@ -130,10 +121,9 @@ $(document).ready(function(){ global_editor.editor.clearHistory(); } $('#code').submit(function(e) { - checker = setInterval(check_lock_screen, 30000); lock_screen(); var data = $(this).serializeArray(); - ajax_call($(this).attr("action"), "POST", "html", data, null) + ajax_check_code($(this).attr("action"), "POST", "html", data, null) e.preventDefault(); // To stop the default form submission. }); }); -- cgit From cc7fce05eaf48e6662b2f37f088591627f12131a Mon Sep 17 00:00:00 2001 From: prathamesh Date: Fri, 8 Sep 2017 17:41:14 +0530 Subject: Modifications as per comments on PR - II --- yaksh/static/yaksh/js/requesthandler.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'yaksh/static') diff --git a/yaksh/static/yaksh/js/requesthandler.js b/yaksh/static/yaksh/js/requesthandler.js index 73d0405..9fcf5b6 100644 --- a/yaksh/static/yaksh/js/requesthandler.js +++ b/yaksh/static/yaksh/js/requesthandler.js @@ -1,25 +1,22 @@ request_status = "initial"; count = 0; - -function submitRequest(){ - document.forms["code"].submit(); -} +MAX_COUNT = 14 function reset_values() { request_status = "initial"; count = 0; } function check_state(state, uid) { - if ((state == "running" || state == "not started") && count < 7) { + 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"); + notify("Request timeout. Try again later."); unlock_screen(); } else { reset_values() - notify("Please try again"); + notify("Please try again."); unlock_screen(); } } @@ -40,7 +37,7 @@ function unlock_screen() { } function get_result(uid){ - var url = "/exam/get_result/"+uid+"/"; + var url = "/exam/get_result/" + uid + "/"; ajax_check_code(url, "GET", "html", null, uid) } -- cgit