diff options
author | Vitaly Takmazov | 2018-10-02 11:30:54 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-10-02 11:30:54 +0300 |
commit | e04371500a9dd469f02024f63ef39117f8a4d649 (patch) | |
tree | 3f92df084a714aa4f7a828e8070167f86d4d63e1 /juick-server/src/main/assets | |
parent | 6ae7c42af78568dba6678ac01301e8a782da8967 (diff) |
js cleanup
Diffstat (limited to 'juick-server/src/main/assets')
-rw-r--r-- | juick-server/src/main/assets/scripts.js | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/juick-server/src/main/assets/scripts.js b/juick-server/src/main/assets/scripts.js index af099674..8060bd71 100644 --- a/juick-server/src/main/assets/scripts.js +++ b/juick-server/src/main/assets/scripts.js @@ -175,7 +175,11 @@ function initWS() { } } }; - setInterval(wsSendKeepAlive, 90000); + var keepAlive = setInterval(wsSendKeepAlive, 90000); + window.addEventListener('beforeunload', () => { + clearInterval(keepAlive); + ws.close(); + }) } function wsSendKeepAlive() { @@ -277,7 +281,7 @@ function postformListener(formEl, ev) { if (ev.ctrlKey && (ev.keyCode == 10 || ev.keyCode == 13)) { let form = formEl.closest('form'); if (!form.onsubmit || form.onsubmit()) { - form.dispatchEvent(new Event('submit')); + form.querySelector('input[type="submit"]').click(); } } } @@ -300,12 +304,19 @@ function newMessage(evt) { } } +function handleErrors(response) { + if (!response.ok) { + throw Error(response.statusText); + } + return response; +} + function showCommentForm(mid, rid) { let reply = document.getElementById(rid); let formTarget = reply.querySelector('div.msg-cont .msg-comment-target'); if (formTarget) { let formHtml = ` - <form action="/comment" method="POST" enctype="multipart/form-data"> + <form> <input type="hidden" name="mid" value="${mid}"> <input type="hidden" name="rid" value="${rid}"> <div class="msg-comment"> @@ -342,13 +353,13 @@ function showCommentForm(mid, rid) { return false; } submitButton.disabled = true; - e.preventDefault(); let formData = new FormData(form); fetch('/api/comment' + '?hash=' + document.getElementById('body').getAttribute('data-hash'), { method: 'POST', body: formData, credentials: 'omit' - }).then(response => { + }).then(handleErrors) + .then(response => { if (response.ok) { response.json().then(result => { if (result.newMessage) { @@ -362,6 +373,7 @@ function showCommentForm(mid, rid) { }).catch(error => { alert(error.message); }); + e.preventDefault(); }); } reply.querySelector('.msg-comment textarea').focus(); @@ -505,7 +517,8 @@ function openSocialWindow(a) { function checkUsername() { var uname = document.querySelector('#username').textContent, style = document.querySelector('#username').style; - fetch('//api.juick.com/users?uname=' + uname) + fetch('/api/users?uname=' + uname) + .then(handleErrors) .then(function () { style.background = '#FFCCCC'; }) @@ -547,9 +560,9 @@ function likeMessage(e, mid) { fetch('/api/like?mid=' + mid + '&hash=' + document.getElementById('body').getAttribute('data-hash'), { method: 'POST', - mode: 'cors', credentials: 'omit' }) + .then(handleErrors) .then(function (response) { if (response.ok) { e.closest('article').appendChild(resultMessage('OK!')); @@ -566,9 +579,9 @@ function subscribeMessage(e, mid) { fetch('/api/subscribe?mid=' + mid + '&hash=' + document.getElementById('body').getAttribute('data-hash'), { method: 'POST', - mode: 'cors', credentials: 'omit' }) + .then(handleErrors) .then(function (response) { if (response.ok) { window.location.reload(true); @@ -590,6 +603,7 @@ function setPopular(e, mid, popular) { + '&hash=' + document.getElementById('body').getAttribute('data-hash'), { credentials: 'same-origin' }) + .then(handleErrors) .then(function () { e.closest('article').append(resultMessage('OK!')); }); @@ -601,6 +615,7 @@ function setPrivacy(e, mid) { + '&hash=' + document.getElementById('body').getAttribute('data-hash'), { credentials: 'same-origin' }) + .then(handleErrors) .then(function () { e.closest('article').append(resultMessage('OK!')); }); @@ -609,8 +624,9 @@ function setPrivacy(e, mid) { function getTags() { fetch('/api/tags?hash=' + document.getElementById('body').getAttribute('data-hash'), { - credentials: 'same-origin' + credentials: 'omit' }) + .then(handleErrors) .then(response => { return response.json(); }) @@ -698,13 +714,13 @@ ready(function () { }); }); postmsg.addEventListener('submit', e => { - e.preventDefault(); let formData = new FormData(postmsg); fetch('/api/post' + '?hash=' + document.getElementById('body').getAttribute('data-hash'), { method: 'POST', body: formData, credentials: 'omit' - }).then(response => { + }).then(handleErrors) + .then(response => { if (response.ok) { response.json().then(result => { if (result.newMessage) { @@ -719,17 +735,18 @@ ready(function () { }).catch(error => { alert(error.message); }); + e.preventDefault(); }); } document.querySelectorAll('.pmmsg').forEach(pmmsg => { pmmsg.addEventListener('submit', e => { - e.preventDefault(); let formData = new FormData(pmmsg); fetch('/api/pm' + '?hash=' + document.getElementById('body').getAttribute('data-hash'), { method: 'POST', body: formData, credentials: 'omit' - }).then(response => { + }).then(handleErrors) + .then(response => { if (response.ok) { response.json().then(result => { if (result.to) { @@ -744,6 +761,7 @@ ready(function () { }).catch(error => { alert(error.message); }); + e.preventDefault(); }); }); |