code style

This commit is contained in:
Tomáš Mládek 2018-10-18 13:11:22 +02:00
parent 84e715d480
commit 18a1358ef2

92
main.js
View file

@ -1,34 +1,34 @@
// modal form handle
if (document.querySelector('.modal__form-submit')) {
document.querySelector('.modal__form-submit').addEventListener('click', function (e) {
e.preventDefault()
const value = document.querySelector('.modal__form-input').value
e.preventDefault();
const value = document.querySelector('.modal__form-input').value;
if (value.length >= 3) {
var date = new Date(Date.now())
date = date.setTime(date.getTime() + (90 * 24 * 60 * 60 * 1000))
date = new Date(date)
const expires = '; expires=' + date.toUTCString()
document.cookie = 'outchat_name=' + (value || '') + expires + '; path=/'
let date = new Date(Date.now());
date = date.setTime(date.getTime() + (90 * 24 * 60 * 60 * 1000));
date = new Date(date);
const expires = '; expires=' + date.toUTCString();
document.cookie = 'outchat_name=' + (value || '') + expires + '; path=/';
window.location.href = ''
}
})
}
document.querySelector('.form__input-message').addEventListener('keydown', function (e) {
if (e.ctrlKey && e.keyCode == 13) {
if (e.ctrlKey && e.keyCode === 13) {
document.querySelector('.form__input-submit').click()
}
})
});
document.querySelector('.form__input-submit').addEventListener('click', function (e) {
e.preventDefault()
e.preventDefault();
const name = getCookie('outchat_name')
const text = document.querySelector('.form__input-message').value
document.querySelector('.form__input-message').value = ''
const name = getCookie('outchat_name');
const text = document.querySelector('.form__input-message').value;
document.querySelector('.form__input-message').value = '';
var formData = new FormData()
formData.append(name, text)
let formData = new FormData();
formData.append(name, text);
fetch('api.php?action=createMessage', {
method: 'post',
@ -37,16 +37,16 @@ document.querySelector('.form__input-submit').addEventListener('click', function
},
body: 'name=' + name + '&text=' + text
})
.then(function (data) {
.then(() => {
getMessages()
})
.catch(function (error) {
.catch((error) => {
console.log(error)
})
})
});
function getMessages() {
const timestamp = (document.querySelector('.chat').lastElementChild) ? document.querySelector('.chat').lastElementChild.getAttribute('data-timestamp') : 0
const timestamp = (document.querySelector('.chat').lastElementChild) ? document.querySelector('.chat').lastElementChild.getAttribute('data-timestamp') : 0;
return fetch('api.php?action=getMessages&timestamp=' + timestamp)
.then(function (data) {
@ -54,7 +54,7 @@ function getMessages() {
})
.then(function (data) {
if (data.length > 0) {
if ((window.scrollY + window.innerHeight) == document.body.scrollHeight) {
if ((window.scrollY + window.innerHeight) === document.body.scrollHeight) {
setTimeout(function () {
scrollWindowDown()
}, 100)
@ -70,49 +70,49 @@ function getMessages() {
}
function createMessageNode(name, text, timestamp, datetime) {
const elementMain = document.createElement('div', {class: 'message'})
const elementText = document.createElement('div', {class: 'message__text'})
const elementInfo = document.createElement('div', {class: 'message__info'})
const elementName = document.createElement('div', {id: 'message__info-name'})
const elementDatetime = document.createElement('div', {id: 'message__info-datetime'})
const contentName = document.createTextNode(name)
const contentDatetime = document.createTextNode('[' + datetime + ']')
const elementMain = document.createElement('div', {class: 'message'});
const elementText = document.createElement('div', {class: 'message__text'});
const elementInfo = document.createElement('div', {class: 'message__info'});
const elementName = document.createElement('div', {id: 'message__info-name'});
const elementDatetime = document.createElement('div', {id: 'message__info-datetime'});
const contentName = document.createTextNode(name);
const contentDatetime = document.createTextNode('[' + datetime + ']');
//const contentText = document.createTextNode(text)
// element creation
elementMain.classList.add('message')
elementText.classList.add('message__text')
elementInfo.classList.add('message__info')
elementDatetime.classList.add('message__info-datetime')
elementName.classList.add('message__info-name')
elementMain.classList.add('message');
elementText.classList.add('message__text');
elementInfo.classList.add('message__info');
elementDatetime.classList.add('message__info-datetime');
elementName.classList.add('message__info-name');
// "hash" name color
elementName.style.color = "#" + intToRGB(hashCode(name))
elementName.style.color = "#" + intToRGB(hashCode(name));
// append everything to chat
elementName.appendChild(contentName)
elementDatetime.appendChild(contentDatetime)
elementText.innerHTML = text
elementName.appendChild(contentName);
elementDatetime.appendChild(contentDatetime);
elementText.innerHTML = text;
elementInfo.appendChild(elementName)
elementInfo.appendChild(elementDatetime)
elementInfo.appendChild(elementName);
elementInfo.appendChild(elementDatetime);
elementMain.appendChild(elementInfo)
elementMain.appendChild(elementText)
elementMain.setAttribute('data-timestamp', timestamp)
elementMain.appendChild(elementInfo);
elementMain.appendChild(elementText);
elementMain.setAttribute('data-timestamp', timestamp);
document.querySelector('.chat').appendChild(elementMain)
document.querySelector('.chat').appendChild(elementMain);
return true
}
getMessages().then(() => {
scrollWindowDown()
})
scrollWindowDown();
});
setInterval(function () {
getMessages()
}, 2000)
getMessages();
}, 2000);
if (inIframe()) {
document.querySelector('.modal__info_url').style.visibility = 'visible';