Commit | Line | Data |
---|---|---|
cf2343ce | 1 | <template lang="pug"> |
9ca1e26b | 2 | .card |
4f887105 BA |
3 | input#inputChat(type="text" :placeholder="st.tr['Type here']" |
4 | @keyup.enter="sendChat") | |
5 | button#sendChatBtn(@click="sendChat") {{ st.tr["Send"] }} | |
3837d4f7 BA |
6 | p(v-for="chat in pastChats" :class="classObject(chat)" v-html="chat.name + ': ' + chat.msg") |
7 | p(v-for="chat in chats" :class="classObject(chat)" v-html="chat.name + ': ' + chat.msg") | |
cf2343ce BA |
8 | </template> |
9 | ||
10 | <script> | |
5c8e044f BA |
11 | import { store } from "@/store"; |
12 | ||
cf2343ce BA |
13 | export default { |
14 | name: "my-chat", | |
3837d4f7 BA |
15 | // Prop 'pastChats' for corr games where chats are on server |
16 | props: ["players","pastChats"], | |
cf2343ce BA |
17 | data: function() { |
18 | return { | |
5c8e044f | 19 | st: store.state, |
cf2343ce BA |
20 | chats: [], //chat messages after human game |
21 | }; | |
22 | }, | |
5c8e044f | 23 | created: function() { |
cd0d7743 | 24 | const curMsgListener = this.st.conn.onmessage; //from Game or Hall |
5c8e044f | 25 | const socketMessageListener = msg => { |
cd0d7743 | 26 | curMsgListener(msg); |
5c8e044f BA |
27 | const data = JSON.parse(msg.data); |
28 | if (data.code == "newchat") //only event at this level | |
29 | { | |
9ca1e26b | 30 | this.chats.unshift({msg:data.msg, |
c6788ecf | 31 | name:data.name || "@nonymous", sid:data.from}); |
5c8e044f BA |
32 | } |
33 | }; | |
34 | const socketCloseListener = () => { | |
35 | store.socketCloseListener(); //reinitialize connexion (in store.js) | |
36 | this.st.conn.addEventListener('message', socketMessageListener); | |
37 | this.st.conn.addEventListener('close', socketCloseListener); | |
38 | }; | |
39 | this.st.conn.onmessage = socketMessageListener; | |
40 | this.st.conn.onclose = socketCloseListener; | |
cf2343ce | 41 | }, |
5c8e044f BA |
42 | methods: { |
43 | classObject: function(chat) { | |
44 | return { | |
45 | "my-chatmsg": chat.sid == this.st.user.sid, | |
46 | "opp-chatmsg": this.players.some( | |
47 | p => p.sid == chat.sid && p.sid != this.st.user.sid) | |
48 | }; | |
49 | }, | |
50 | sendChat: function() { | |
51 | let chatInput = document.getElementById("inputChat"); | |
52 | const chatTxt = chatInput.value; | |
53 | chatInput.value = ""; | |
54 | const chat = {msg:chatTxt, name: this.st.user.name || "@nonymous", | |
55 | sid:this.st.user.sid}; | |
63ca2b89 | 56 | this.$emit("newchat", chat); //useful for corr games |
9ca1e26b | 57 | this.chats.unshift(chat); |
5c8e044f BA |
58 | this.st.conn.send(JSON.stringify({ |
59 | code:"newchat", msg:chatTxt, name:chat.name})); | |
60 | }, | |
61 | }, | |
62 | }; | |
63 | </script> | |
64 | ||
4f887105 | 65 | <style lang="sass" scoped> |
5c8e044f BA |
66 | .my-chatmsg |
67 | color: grey | |
68 | .opp-chatmsg | |
69 | color: black | |
4f887105 BA |
70 | #chat |
71 | max-width: 100% | |
5c8e044f | 72 | </style> |