Ver código fonte

HWJS13 (chat) done

DimaBondarenko 3 anos atrás
pai
commit
60a108fed0

Diferenças do arquivo suprimidas por serem muito extensas
+ 2 - 0
HWJS13/css/style.min.css


Diferenças do arquivo suprimidas por serem muito extensas
+ 9 - 0
HWJS13/css/style.min.css.map


BIN
HWJS13/img/1626160774_64.jpg


BIN
HWJS13/img/icons8-телеграмма-app-48.png


+ 24 - 0
HWJS13/index.html

@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+    <link rel="stylesheet" href="css/style.min.css">
+</head>
+<body>
+    <div class="container">
+        <div class="wrapper-messages">
+            
+        </div>
+        <div class="wrapper-form">
+            <input class="nick-inp" type="text" placeholder="your nick" max="5">
+            <div class="hr"></div>
+            <input class="message-inp" type="text" placeholder="your message">
+            <img src="img/icons8-телеграмма-app-48.png" alt="img">
+        </div>
+    </div>
+    <script src="js/script.js"></script>
+</body>
+</html>

+ 102 - 0
HWJS13/js/script.js

@@ -0,0 +1,102 @@
+//////////////////////////////////Chat Homework
+let nextMessageId = 0;
+let arrMonthName=['Января','Февраля','Марта','Апреля','Мая','Июня','Июля','Августа','Сентября','Октября','Ноября','Декабря'];
+let currentDate;
+
+let wrapperMessages = document.querySelector(".wrapper-messages");
+let inputNick = document.querySelector(".nick-inp");
+let inputMes = document.querySelector(".message-inp");
+let btnImg = document.querySelector("img");
+let url = "http://students.a-level.com.ua:10012";
+
+const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms));
+
+function createMessage (parent,{nick, message, timestamp}){
+    let hours = new Date(timestamp).getHours();
+    let minutes = new Date(timestamp).getMinutes();
+    let date = new Date(timestamp).getDate();
+    let month = new Date(timestamp).getMonth();
+    let year = new Date(timestamp).getFullYear();
+    let fullDate = `${date} ${arrMonthName[month]} ${year}`
+
+    if(currentDate !== fullDate){
+        let divDate = document.createElement('div');
+        divDate.classList.add("mes-date");
+        parent.prepend(divDate);
+        divDate.innerHTML = fullDate;
+        currentDate = fullDate;
+    }
+
+    let divWrap = document.createElement('div');
+    divWrap.classList.add("wrap-message");
+    parent.prepend(divWrap);
+
+    let divNick = document.createElement('div');
+    divNick.classList.add("nick");
+    divWrap.append(divNick);
+    divNick.innerHTML = nick;
+
+    let divMes = document.createElement('div');
+    divMes.classList.add("message");
+    divWrap.append(divMes);
+    divMes.innerHTML = message;
+
+    let divTime = document.createElement('div');
+    divTime.classList.add("mes-time");
+    divMes.append(divTime);
+    divTime.innerHTML = `${hours < 10 ? '0' + hours : hours}:${minutes < 10 ? '0' + minutes : minutes}`;
+}
+
+async function jsonPost(url,data){
+    let response = await fetch(url, {
+        method : 'POST',
+        body : JSON.stringify(data),
+        headers : {
+            // 'Content-Type' : 'application/json',
+            // 'Accept' : 'application/json',
+        }
+    })
+    if(response.status === 200){
+        let json = await response.json();
+        return json
+    }
+}
+
+async function sendMessage(nick, message){
+    if (nick.length >= 3 && message.length >= 3){
+        await jsonPost(url, {func: "addMessage", nick: nick, message: message});
+    }
+}
+async function getMessages(){
+    let data = await jsonPost(url, {func: "getMessages", messageId: nextMessageId});
+    nextMessageId = data.nextMessageId;
+    data.data.forEach(elem=> {
+        createMessage(wrapperMessages, elem)
+    })
+}
+
+async function sendAndCheck(){
+    await sendMessage(inputNick.value, inputMes.value);
+    inputMes.value = ''               /// оставляем только ник
+    await getMessages();
+    btnImg.onclick = onClick;       //// снова вешаем клик 
+}
+
+getMessages();
+
+async function checkLoop(){
+    while(true){
+        await delay(3000);
+        await getMessages();
+        // console.log('3000')
+    }
+}
+
+checkLoop()
+
+function onClick(){
+    btnImg.onclick = null           //// для исключения повторного клика 
+    sendAndCheck();
+}
+
+btnImg.onclick = onClick;

+ 102 - 0
HWJS13/sass/style.scss

@@ -0,0 +1,102 @@
+*{
+    box-sizing: border-box;
+    margin: 0;
+}
+body{
+    padding-top: 20px;
+}
+.container{
+    position: relative;
+    padding: 20px;
+    border-radius: 5px;
+    margin: 0 auto;
+    // height: 660px;
+    width: 600px;
+    background: url("../img/1626160774_64.jpg") center center/cover no-repeat;
+    .wrapper-messages{
+        display: flex;
+        flex-direction: column-reverse;
+        height: 500px;
+        width: 100%;
+        margin-bottom: 20px;
+        overflow-y: scroll;
+        &::-webkit-scrollbar { width: 0 !important }
+        .wrap-message{
+            display: flex;
+            justify-content: space-between;
+            align-items: flex-start;
+            margin: 10px 0;
+            
+            .nick{
+                padding: 5px 10px;
+                text-align: center;
+                background-color: white;
+                min-height: 30px;
+                border-radius: 3px;
+                font-size: 16px;
+                margin-right: 30px;
+            }
+            .message{
+                position: relative;
+                padding: 5px 10px;
+                border-radius:  0px 3px 3px 3px;
+                font-size: 16px;
+                text-align: center;
+                background-color: white;
+                position: relative;
+                min-height: 30px;
+                
+                &:before{
+                    content: "";
+                    position: absolute;
+                    width: 20px;
+                    height: 20px;
+                    background-color: rgb(255, 255, 255);
+                    left: -19px;
+                    top: 0px;
+                    clip-path: polygon(75% 0, 100% 0, 100% 100%, 98% 82%, 97% 71%, 96% 61%, 94% 50%, 92% 40%, 90% 31%, 87% 21%, 84% 13%, 80% 6%);
+                }
+                .mes-time{
+                    text-align: right;
+                    font-size: 10px;
+                    color: cadetblue;
+                }
+            }
+        }
+    }
+    .wrapper-form{
+        position: relative;
+        width: 100%;
+        min-height: 50px;
+        background-color: rgb(255, 255, 255);
+        border-radius: 10px;
+        .nick-inp, .message-inp{
+            margin-left: 20px;
+            font-size: 16px;
+            display: block;
+            border: none;
+            width: 400px;
+            height: 40px;
+            outline: none;
+        }
+        img{
+            position: absolute;
+            width: 30px;
+            height: 30px;
+            right: 10px;
+            bottom: 10px;
+        }
+    }
+}
+
+.hr{
+    background-color: rgb(226, 226, 226);
+    width: 85%;
+    margin-left: 20px;
+    height: 1px;
+}
+
+.mes-date{
+    text-align: center;
+    color: rgb(219, 219, 219);
+}