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