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