ChatServer/pages/js/message.js

44 lines
1.5 KiB
JavaScript
Raw Normal View History

var sendmsg = function sendmsg(msginputta) {
window.jsonobj = JSON.stringify({"message": msginputta.value, "conversation": window.convid});
console.log(window.jsonobj);
$.ajax({
url: "/sendmessage", data: window.jsonobj, method: "POST", success: respfunc, error: respfunc
});
};
var respfunc = function respfunc(result) {
if (result != "Success") { //on success result is string
var msginput = document.getElementById("msginput");
if (result.responseText == "JSONERROR") {
console.log("Got JSON error. Retrying...");
sendmsg(msginput);
}
else {
var errormsg = document.getElementById("errormsg");
errormsg.innerHTML = result.responseText;
2016-08-04 13:10:46 +00:00
errormsg.style = "display: block"; //TODO: Hide errormsg after a while (index.js)
msginput.disabled = false;
}
2016-07-28 13:04:24 +00:00
}
else
2016-08-04 13:10:46 +00:00
location.reload(true); //TODO: Don't referesh on message send
};
2016-07-28 13:04:24 +00:00
var sendmsgonenter = function sendmsgonenter(e) {
2016-07-28 13:04:24 +00:00
var code = e.keyCode || e.which;
2016-08-01 10:16:54 +00:00
if (code != 13 || e.shiftKey) { //Enter keycode
2016-07-28 13:04:24 +00:00
return;
}
2016-08-01 10:16:54 +00:00
e.preventDefault();
2016-07-28 13:04:24 +00:00
var textarea = event.target;
2016-08-01 10:16:54 +00:00
if (textarea.value.trim().length == 0)
return;
textarea.disabled = true; //msginput
window.convid = document.getElementById("convidp").innerText * 1;
sendmsg(textarea);
2016-07-28 13:04:24 +00:00
};
$(document).ready(function () {
2016-08-01 10:16:54 +00:00
$('#msginput').on("keydown", sendmsgonenter);
2016-07-28 13:04:24 +00:00
});