Finished. Now some last styling
[vchess.git] / client / src / components / Chat.vue
CommitLineData
cf2343ce 1<template lang="pug">
a1c48034 2div
4f887105
BA
3 input#inputChat(type="text" :placeholder="st.tr['Type here']"
4 @keyup.enter="sendChat")
5 button#sendChatBtn(@click="sendChat") {{ st.tr["Send"] }}
a1c48034
BA
6 p(v-for="chat in pastChats" :class="classObject(chat)"
7 v-html="chat.name + ': ' + chat.msg")
8 p(v-for="chat in chats" :class="classObject(chat)"
9 v-html="chat.name + ': ' + chat.msg")
cf2343ce
BA
10</template>
11
12<script>
5c8e044f
BA
13import { store } from "@/store";
14
cf2343ce
BA
15export default {
16 name: "my-chat",
3837d4f7
BA
17 // Prop 'pastChats' for corr games where chats are on server
18 props: ["players","pastChats"],
cf2343ce
BA
19 data: function() {
20 return {
5c8e044f 21 st: store.state,
cf2343ce
BA
22 chats: [], //chat messages after human game
23 };
24 },
5c8e044f 25 created: function() {
cd0d7743 26 const curMsgListener = this.st.conn.onmessage; //from Game or Hall
5c8e044f 27 const socketMessageListener = msg => {
cd0d7743 28 curMsgListener(msg);
5c8e044f
BA
29 const data = JSON.parse(msg.data);
30 if (data.code == "newchat") //only event at this level
31 {
dcd68c41 32 this.chats.unshift({msg:data.msg, name:data.name || "@nonymous"});
a1c48034 33 this.$emit("newchat-received"); //data not required here
5c8e044f
BA
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;
cf2343ce 43 },
5c8e044f
BA
44 methods: {
45 classObject: function(chat) {
46 return {
dcd68c41 47 "my-chatmsg": chat.name == this.st.user.name,
5c8e044f 48 "opp-chatmsg": this.players.some(
dcd68c41 49 p => p.name == chat.name && p.name != this.st.user.name)
5c8e044f
BA
50 };
51 },
52 sendChat: function() {
53 let chatInput = document.getElementById("inputChat");
54 const chatTxt = chatInput.value;
55 chatInput.value = "";
dcd68c41 56 const chat = {msg:chatTxt, name: this.st.user.name || "@nonymous"};
a1c48034 57 this.$emit("newchat-sent", chat); //useful for corr games
9ca1e26b 58 this.chats.unshift(chat);
5c8e044f
BA
59 this.st.conn.send(JSON.stringify({
60 code:"newchat", msg:chatTxt, name:chat.name}));
61 },
62 },
63};
64</script>
65
4f887105 66<style lang="sass" scoped>
5c8e044f
BA
67.my-chatmsg
68 color: grey
69.opp-chatmsg
70 color: black
4f887105
BA
71#chat
72 max-width: 100%
5c8e044f 73</style>