Rollback last (bad) improving attempt
[vchess.git] / client / src / components / Chat.vue
1 <template lang="pug">
2 div
3 button(@click="clearHistory()")
4 | {{ st.tr["Clear history"] }}
5 input#inputChat(
6 type="text"
7 :placeholder="st.tr['Chat here']"
8 @keyup.enter="sendChat()"
9 )
10 button(@click="sendChat()") {{ st.tr["Send"] }}
11 p(v-for="chat in chats.concat(pastChats)")
12 span.name {{ chat.name || "@nonymous" }} :&nbsp;
13 span(
14 :class="classObject(chat)"
15 v-html="chat.msg"
16 )
17 </template>
18
19 <script>
20 import { store } from "@/store";
21 export default {
22 name: "my-chat",
23 // Prop 'pastChats' for corr games where chats are on server
24 props: ["players", "pastChats"],
25 data: function() {
26 return {
27 st: store.state,
28 chats: [] //chat messages after human game
29 };
30 },
31 methods: {
32 classObject: function(chat) {
33 return {
34 "my-chatmsg": (
35 !!chat.name && chat.name == this.st.user.name ||
36 !!chat.sid && chat.sid == this.st.user.sid
37 ),
38 "opp-chatmsg":
39 !!this.players &&
40 this.players.some(
41 p => {
42 return (
43 (
44 !!p.name &&
45 p.name == chat.name &&
46 p.name != this.st.user.name
47 )
48 ||
49 (
50 !!p.sid &&
51 p.sid == chat.sid &&
52 p.sid != this.st.user.sid
53 )
54 );
55 }
56 )
57 };
58 },
59 sendChat: function() {
60 let chatInput = document.getElementById("inputChat");
61 const chatTxt = chatInput.value.trim();
62 chatInput.focus(); //required on smartphones
63 if (chatTxt == "") return; //nothing to send
64 chatInput.value = "";
65 const chat = {
66 msg: chatTxt,
67 name: this.st.user.name,
68 // SID is required only for anonymous users (in live games)
69 sid: this.st.user.id == 0 ? this.st.user.sid : null
70 };
71 this.$emit("mychat", chat);
72 this.chats.unshift(chat);
73 },
74 newChat: function(chat) {
75 if (chat.msg != "") this.chats.unshift(chat);
76 },
77 clearHistory: function() {
78 this.chats = [];
79 this.$emit("chatcleared");
80 }
81 }
82 };
83 </script>
84
85 <style lang="sass" scoped>
86 .name
87 color: #839192
88
89 .my-chatmsg
90 color: #6c3483
91 .opp-chatmsg
92 color: #1f618d
93 </style>