| 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 | |
| 15 | export default { |
| 16 | name: "my-chat", |
| 17 | // Prop 'pastChats' for corr games where chats are on server |
| 18 | props: ["players","pastChats","newChat"], |
| 19 | data: function() { |
| 20 | return { |
| 21 | st: store.state, |
| 22 | chats: [], //chat messages after human game |
| 23 | }; |
| 24 | }, |
| 25 | watch: { |
| 26 | newChat: function(chat) { |
| 27 | if (chat.msg != "") |
| 28 | this.chats.unshift({msg:chat.msg, name:chat.name || "@nonymous"}); |
| 29 | }, |
| 30 | }, |
| 31 | methods: { |
| 32 | classObject: function(chat) { |
| 33 | return { |
| 34 | "my-chatmsg": chat.name == this.st.user.name, |
| 35 | "opp-chatmsg": !!this.players && this.players.some( |
| 36 | p => p.name == chat.name && p.name != this.st.user.name) |
| 37 | }; |
| 38 | }, |
| 39 | sendChat: function() { |
| 40 | let chatInput = document.getElementById("inputChat"); |
| 41 | const chatTxt = chatInput.value.trim(); |
| 42 | if (chatTxt == "") |
| 43 | return; //nothing to send |
| 44 | chatInput.value = ""; |
| 45 | const chat = {msg:chatTxt, name: this.st.user.name || "@nonymous"}; |
| 46 | this.$emit("mychat", chat); |
| 47 | this.chats.unshift(chat); |
| 48 | }, |
| 49 | }, |
| 50 | }; |
| 51 | </script> |
| 52 | |
| 53 | <style lang="sass" scoped> |
| 54 | .my-chatmsg |
| 55 | color: grey |
| 56 | .opp-chatmsg |
| 57 | color: black |
| 58 | </style> |