main.js autoformat

This commit is contained in:
Tomáš Mládek 2019-11-04 18:05:54 +01:00
parent a4f637eda0
commit 3940844fc9

36
main.js
View file

@ -9,15 +9,15 @@ if (document.querySelector('.modal__form-submit')) {
date = new Date(date); date = new Date(date);
const expires = '; expires=' + date.toUTCString(); const expires = '; expires=' + date.toUTCString();
document.cookie = 'outchat_name=' + (value || '') + expires + '; path=/'; document.cookie = 'outchat_name=' + (value || '') + expires + '; path=/';
window.location.href = '' window.location.href = '';
} }
}) });
} }
document.querySelector('.form__input-message').addEventListener('keydown', function (e) { document.querySelector('.form__input-message').addEventListener('keydown', function (e) {
if (!e.ctrlKey && e.keyCode === 13) { if (!e.ctrlKey && e.keyCode === 13) {
e.preventDefault(); e.preventDefault();
document.querySelector('.form__input-submit').click() document.querySelector('.form__input-submit').click();
} }
}); });
@ -34,16 +34,14 @@ document.querySelector('.form__input-submit').addEventListener('click', function
fetch('api.php?action=createMessage', { fetch('api.php?action=createMessage', {
method: 'post', method: 'post',
headers: { headers: {
"Content-Type": "application/x-www-form-urlencoded" "Content-Type": "application/x-www-form-urlencoded",
}, },
body: 'name=' + name + '&text=' + text body: 'name=' + name + '&text=' + text,
}) }).then(() => {
.then(() => { getMessages();
getMessages() }).catch((error) => {
}) console.log(error);
.catch((error) => { });
console.log(error)
})
}); });
function getMessages() { function getMessages() {
@ -51,23 +49,23 @@ function getMessages() {
return fetch('api.php?action=getMessages&timestamp=' + timestamp) return fetch('api.php?action=getMessages&timestamp=' + timestamp)
.then(function (data) { .then(function (data) {
return data.json() return data.json();
}) })
.then(function (data) { .then(function (data) {
if (data.length > 0) { if (data.length > 0) {
if ((window.scrollY + window.innerHeight) === document.body.scrollHeight) { if ((window.scrollY + window.innerHeight) === document.body.scrollHeight) {
setTimeout(function () { setTimeout(function () {
scrollWindowDown() scrollWindowDown();
}, 100) }, 100);
} }
for (message of data) { for (message of data) {
createMessageNode(message.name, message.text_processed, message.timestamp, message.datetime) createMessageNode(message.name, message.text_processed, message.timestamp, message.datetime);
} }
} }
}) })
.catch(function (error) { .catch(function (error) {
console.log(error) console.log(error);
}) });
} }
function createMessageNode(name, text, timestamp, datetime) { function createMessageNode(name, text, timestamp, datetime) {
@ -104,7 +102,7 @@ function createMessageNode(name, text, timestamp, datetime) {
document.querySelector('.chat').appendChild(elementMain); document.querySelector('.chat').appendChild(elementMain);
return true return true;
} }
getMessages().then(() => { getMessages().then(() => {