Chat is working
[vchess.git] / client / src / components / Chat.vue
CommitLineData
cf2343ce 1<template lang="pug">
5c8e044f
BA
2.row
3 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
4 // TODO: Chat modal sur petit écran, dans la page pour grand écran
cf2343ce 5 .card.smallpad
f21cd6d9 6 h4 Chat
5c8e044f
BA
7 p(v-for="chat in chats" :class="classObject(chat)" v-html="chat.msg")
8 input#inputChat(type="text" :placeholder="st.tr['Type here']"
cf2343ce
BA
9 @keyup.enter="sendChat")
10 button#sendChatBtn(@click="sendChat") {{ st.tr["Send"] }}
11</template>
12
13<script>
5c8e044f
BA
14import { store } from "@/store";
15
cf2343ce
BA
16export default {
17 name: "my-chat",
5c8e044f 18 props: ["players"],
cf2343ce
BA
19 data: function() {
20 return {
5c8e044f 21 st: store.state,
cf2343ce
BA
22 chats: [], //chat messages after human game
23 };
24 },
5c8e044f
BA
25 created: function() {
26 const socketMessageListener = msg => {
27 const data = JSON.parse(msg.data);
28 if (data.code == "newchat") //only event at this level
29 {
30 this.chats.push({msg:data.msg,
31 name:data.name || "@nonymous", sid:data.sid});
32 }
33 };
34 const socketCloseListener = () => {
35 store.socketCloseListener(); //reinitialize connexion (in store.js)
36 this.st.conn.addEventListener('message', socketMessageListener);
37 this.st.conn.addEventListener('close', socketCloseListener);
38 };
39 this.st.conn.onmessage = socketMessageListener;
40 this.st.conn.onclose = socketCloseListener;
cf2343ce 41 },
5c8e044f
BA
42 methods: {
43 classObject: function(chat) {
44 return {
45 "my-chatmsg": chat.sid == this.st.user.sid,
46 "opp-chatmsg": this.players.some(
47 p => p.sid == chat.sid && p.sid != this.st.user.sid)
48 };
49 },
50 sendChat: function() {
51 let chatInput = document.getElementById("inputChat");
52 const chatTxt = chatInput.value;
53 chatInput.value = "";
54 const chat = {msg:chatTxt, name: this.st.user.name || "@nonymous",
55 sid:this.st.user.sid};
56 this.chats.push(chat);
57 this.st.conn.send(JSON.stringify({
58 code:"newchat", msg:chatTxt, name:chat.name}));
59 },
60 },
61};
62</script>
63
64<style lang="sass">
65.my-chatmsg
66 color: grey
67.opp-chatmsg
68 color: black
69</style>