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