summaryrefslogtreecommitdiff
path: root/lfpe.js
diff options
context:
space:
mode:
Diffstat (limited to 'lfpe.js')
-rw-r--r--lfpe.js146
1 files changed, 109 insertions, 37 deletions
diff --git a/lfpe.js b/lfpe.js
index c6cd97f..b10a265 100644
--- a/lfpe.js
+++ b/lfpe.js
@@ -1,20 +1,17 @@
-function CreateCORSRequest(method, url) {
+function CreateCORSRequest(method, url)
+{
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
- // XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
- } else if (typeof XDomainRequest != "undefined") {
- // XDomainRequest for IE.
- xhr = new XDomainRequest();
- xhr.open(method, url);
} else {
- // CORS not supported.
xhr = null;
}
+
return xhr;
}
-function submit(e) {
+function catch_return(e)
+{
if (e.keyCode == 13) {
e.preventDefault();
Next();
@@ -23,49 +20,124 @@ function submit(e) {
return false;
}
+function pause()
+{
+ var paused = document.getElementById("paused");
+ var button = document.getElementById("pause_button");
+ var next_button = document.getElementById("next");
+ if (paused.value == 0) {
+ button.innerHTML = "Unpause";
+ paused.value = 1;
+ next.setAttribute("disabled", "disabled");
+ } else {
+ button.innerHTML = "Pause";
+ paused.value = 0;
+ next.removeAttribute("disabled");
+ }
+}
+
function Next()
{
- url = "http://coltrane.cl.uni-heidelberg.de:60666/next";
- var pe = document.getElementById("trgt").value;
- if (pe != "") {
- var src = document.getElementById("src_pp").value;
- url += "?example="+src+" %7C%7C%7C "+pe;
+ // elements
+ var button = document.getElementById("next");
+ var target_textarea = document.getElementById("target_textarea")
+ var raw_source_textarea = document.getElementById("raw_source_textarea");
+ var current_seg_id = document.getElementById("current_seg_id");
+ var source = document.getElementById("source");
+ var status = document.getElementById("status");
+
+ // disable button and textarea
+ button.setAttribute("disabled", "disabled");
+ target_textarea.setAttribute("disabled", "disabled");
+
+ var base_url = "http://coltrane.cl.uni-heidelberg.de:60666"; // FIXME: variable
+
+ var key = document.getElementById("key").value;
+ next_url = base_url+"/next?key="+key;
+ var post_edit = target_textarea.value;
+ if (post_edit != "") {
+ // compose request
+ next_url += "&example="+source.value+" %7C%7C%7C "+post_edit;
+ // update document overview
+ document.getElementById("seg_"+(current_seg_id.value)+"_t").innerHTML=post_edit;
+ } else {
+ if (source.value != "") {
+ alert("Error: 1");
+ }
+ // FIXME: do something reasonable
}
- document.getElementById("translating_status").style.display = "block";
- var xhr = CreateCORSRequest('get', url);
+
+ // show 'working' message
+ status.style.display = "block";
+
+ // build request
+ var xhr = CreateCORSRequest('get', next_url);
if (!xhr) {
- alert('CORS not supported');
- return;
+ alert("Error: 2");
+ // FIXME: do something reasonable
}
+ // 'next' request's callbacks
xhr.onload = function() {
+ /*
+ * translation system is currently handling another request
+ * FIXME: maybe poll server for result?
+ *
+ */
+ if (xhr.responseText == "locked") {
+ alert("Translation system is locked, try again in a moment (reload page and click 'Start/Continue' again).");
+ document.getElementById("status").style.display = "none";
+
+ return;
+ }
+ // got response: seg id\tsource\ttranslation\traw source
+ // 0 1 2 3
var x = xhr.responseText.split("\t");
- if (x == "fi") {
- document.getElementById("src").style.display = "none";
- document.getElementById("trgt").style.display = "none";
- document.getElementById("translating_status").style.display = "none";
- document.getElementById("next").innerHTML = "Thank you!";
- document.getElementById("next").disabled = true;
+ if (x == "fi") { // done, hide/disable functional elements
+ raw_source_textarea.style.display = "none";
+ target_textarea.style.display = "none";
+ status.style.display = "none";
+ button.innerHTML = "Session finished, thank you!";
+ button.setAttribute("disabled", "disabled");
+ document.getElementById("pause_button").setAttribute("disabled", "disabled");
+ document.getElementById("seg_"+current_seg_id.value).className = "";
} else {
- document.getElementById("src_pp").value = x[0];
- document.getElementById("src").value = x[2];
- document.getElementById("src").rows = Math.round(x[2].length/80)+1;
- var firstLetter = x[1][0].toUpperCase();
- var rest = x[1].substring(1);
- var t = firstLetter + rest;
- document.getElementById("trgt").value = t;
- document.getElementById("trgt").rows = Math.round(x[1].length/80)+1;
- document.getElementById("translating_status").style.display = "none";
- document.getElementById("trgt").focus();
- document.getElementById("trgt").selectionStart = 0;
- document.getElementById("trgt").selectionEnd = 0;
+ var id = x[0];
+ var src = x[1];
+ var translation = x[2];
+ var raw_source = x[3];
+
+ // update interface
+ status.style.display = "none";
+ target_textarea.value = translation;
+ raw_source_textarea.value = raw_source;
+ button.innerHTML = "Next";
+ button.removeAttribute("disabled");
+ target_textarea.removeAttribute("disabled", "disabled");
+ document.getElementById("seg_"+id).className = "bold";
+ if (x[0] > 0) {
+ document.getElementById("seg_"+(id-1)).className = "";
+ }
+ target_textarea.rows = Math.round(translation.length/80)+1;
+ raw_source_textarea.rows = Math.round(raw_source.length/80)+1;
+ target_textarea.focus();
+ target_textarea.selectionStart = 0;
+ target_textarea.selectionEnd = 0;
+
+ // remember aux data in DOM
+ current_seg_id.value = id;
+ source.value = src;
+
+ // confirm to server
+ var xhr_confirm = CreateCORSRequest('get', "http://coltrane.cl.uni-heidelberg.de:60666/confirm");
+ xhr_confirm.send(); // FIXME: handle errors
}
};
xhr.onerror = function() {
- alert('Error');
+ // FIXME: do something reasonable
};
- xhr.send();
+ xhr.send(); // send 'next' request
}