<template lang="pug">
-.row
- .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
- // TODO: Chat modal sur petit écran, dans la page pour grand écran
- .card.smallpad
- h4 Chat
- p(v-for="chat in chats" :class="classObject(chat)" v-html="chat.msg")
- input#inputChat(type="text" :placeholder="st.tr['Type here']"
- @keyup.enter="sendChat")
- button#sendChatBtn(@click="sendChat") {{ st.tr["Send"] }}
+div
+ button(@click="clearHistory()")
+ | {{ st.tr["Clear history"] }}
+ input#inputChat(
+ type="text"
+ :placeholder="st.tr['Chat here']"
+ @keyup.enter="sendChat()"
+ )
+ button(@click="sendChat()") {{ st.tr["Send"] }}
+ p(v-for="chat in chats.concat(pastChats)")
+ span.name {{ chat.name }} :
+ span(
+ :class="classObject(chat)"
+ v-html="chat.msg"
+ )
</template>
<script>
import { store } from "@/store";
-
export default {
name: "my-chat",
- props: ["players"],
+ // Prop 'pastChats' for corr games where chats are on server
+ props: ["players", "pastChats", "newChat"],
data: function() {
return {
st: store.state,
- chats: [], //chat messages after human game
+ chats: [] //chat messages after human game
};
},
- created: function() {
- const socketMessageListener = msg => {
- const data = JSON.parse(msg.data);
- if (data.code == "newchat") //only event at this level
- {
- this.chats.push({msg:data.msg,
- name:data.name || "@nonymous", sid:data.sid});
- }
- };
- const socketCloseListener = () => {
- store.socketCloseListener(); //reinitialize connexion (in store.js)
- this.st.conn.addEventListener('message', socketMessageListener);
- this.st.conn.addEventListener('close', socketCloseListener);
- };
- this.st.conn.onmessage = socketMessageListener;
- this.st.conn.onclose = socketCloseListener;
+ watch: {
+ newChat: function(chat) {
+ if (chat.msg != "")
+ this.chats.unshift({ msg: chat.msg, name: chat.name || "@nonymous" });
+ }
},
methods: {
classObject: function(chat) {
return {
- "my-chatmsg": chat.sid == this.st.user.sid,
- "opp-chatmsg": this.players.some(
- p => p.sid == chat.sid && p.sid != this.st.user.sid)
+ "my-chatmsg": chat.name == this.st.user.name,
+ "opp-chatmsg":
+ !!this.players &&
+ this.players.some(
+ p => p.name == chat.name && p.name != this.st.user.name
+ )
};
},
sendChat: function() {
let chatInput = document.getElementById("inputChat");
- const chatTxt = chatInput.value;
+ const chatTxt = chatInput.value.trim();
+ if (chatTxt == "") return; //nothing to send
chatInput.value = "";
- const chat = {msg:chatTxt, name: this.st.user.name || "@nonymous",
- sid:this.st.user.sid};
- this.chats.push(chat);
- this.st.conn.send(JSON.stringify({
- code:"newchat", msg:chatTxt, name:chat.name}));
+ const chat = { msg: chatTxt, name: this.st.user.name || "@nonymous" };
+ this.$emit("mychat", chat);
+ this.chats.unshift(chat);
},
- },
+ clearHistory: function() {
+ this.chats = [];
+ this.$emit("chatcleared");
+ }
+ }
};
</script>
-<style lang="sass">
+<style lang="sass" scoped>
+.name
+ color: #abb2b9
+
.my-chatmsg
- color: grey
+ color: #7d3c98
.opp-chatmsg
- color: black
+ color: #2471a3
</style>