Some graphical improvements (first attempt)
[vchess.git] / client / src / components / Chat.vue
1 <template lang="pug">
2 div
3 .card.smallpad
4 h4 Chat
5 p(v-for="chat in chats" :class="classObject(chat)" v-html="chat.msg")
6 input#inputChat(type="text" :placeholder="st.tr['Type here']"
7 @keyup.enter="sendChat")
8 button#sendChatBtn(@click="sendChat") {{ st.tr["Send"] }}
9 </template>
10
11 <script>
12 import { store } from "@/store";
13
14 export default {
15 name: "my-chat",
16 props: ["players"],
17 data: function() {
18 return {
19 st: store.state,
20 chats: [], //chat messages after human game
21 };
22 },
23 created: function() {
24 const curMsgListener = this.st.conn.onmessage; //from Game or Hall
25 const socketMessageListener = msg => {
26 curMsgListener(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.from});
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;
41 },
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>