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