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