Some graphical improvements (first attempt)
[vchess.git] / client / src / components / Chat.vue
CommitLineData
cf2343ce 1<template lang="pug">
7aa548e7
BA
2div
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"] }}
cf2343ce
BA
9</template>
10
11<script>
5c8e044f
BA
12import { store } from "@/store";
13
cf2343ce
BA
14export default {
15 name: "my-chat",
5c8e044f 16 props: ["players"],
cf2343ce
BA
17 data: function() {
18 return {
5c8e044f 19 st: store.state,
cf2343ce
BA
20 chats: [], //chat messages after human game
21 };
22 },
5c8e044f 23 created: function() {
cd0d7743 24 const curMsgListener = this.st.conn.onmessage; //from Game or Hall
5c8e044f 25 const socketMessageListener = msg => {
cd0d7743 26 curMsgListener(msg);
5c8e044f
BA
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,
c6788ecf 31 name:data.name || "@nonymous", sid:data.from});
5c8e044f
BA
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>