b4970f707052fe1a8449bc7b4955fc0db0d5df20
[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 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;
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>