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