'update'
[vchess.git] / client / src / components / Chat.vue
1 <template lang="pug">
2 div
3 input#inputChat(type="text" :placeholder="st.tr['Type here']"
4 @keyup.enter="sendChat")
5 button#sendChatBtn(@click="sendChat()") {{ st.tr["Send"] }}
6 p(v-for="chat in chats" :class="classObject(chat)"
7 v-html="chat.name + ': ' + chat.msg")
8 p(v-for="chat in pastChats" :class="classObject(chat)"
9 v-html="chat.name + ': ' + chat.msg")
10 </template>
11
12 <script>
13 import { store } from "@/store";
14 export default {
15 name: "my-chat",
16 // Prop 'pastChats' for corr games where chats are on server
17 props: ["players","pastChats","newChat"],
18 data: function() {
19 return {
20 st: store.state,
21 chats: [], //chat messages after human game
22 };
23 },
24 watch: {
25 newChat: function(chat) {
26 if (chat.msg != "")
27 this.chats.unshift({msg:chat.msg, name:chat.name || "@nonymous"});
28 },
29 },
30 methods: {
31 classObject: function(chat) {
32 return {
33 "my-chatmsg": chat.name == this.st.user.name,
34 "opp-chatmsg": !!this.players && this.players.some(
35 p => p.name == chat.name && p.name != this.st.user.name)
36 };
37 },
38 sendChat: function() {
39 let chatInput = document.getElementById("inputChat");
40 const chatTxt = chatInput.value.trim();
41 if (chatTxt == "")
42 return; //nothing to send
43 chatInput.value = "";
44 const chat = {msg:chatTxt, name: this.st.user.name || "@nonymous"};
45 this.$emit("mychat", chat);
46 this.chats.unshift(chat);
47 },
48 },
49 };
50 </script>
51
52 <style lang="sass" scoped>
53 .my-chatmsg
54 color: grey
55 .opp-chatmsg
56 color: black
57 </style>