-// // TODO: Chat modal sur petit écran, dans la page pour grand écran
-// created: function() {
-// const socketMessageListener = msg => {
-// const data = JSON.parse(msg.data);
-// switch (data.code)
-// {
-// case "newchat":
-// // TODO: new chat just arrived: data contain all informations
-// // (uid, name, message; no need for timestamp, we can use local time here)
-// this.chats.push({msg:data.msg, author:this.oppid});
-// break;
-// // TODO: distinguish these (dis)connect events from their analogs in game.js
-// // TODO: implement and harmonize: opponents and people are arrays, not objects ?!
-// case "connect":
-// this.players.push({name:data.name, id:data.uid});
-// break;
-// case "disconnect":
-// const pIdx = this.players.findIndex(p => p.id == data.uid);
-// this.players.splice(pIdx);
-// break;
-// }
-// };
-// const socketCloseListener = () => {
-// this.conn.addEventListener('message', socketMessageListener);
-// this.conn.addEventListener('close', socketCloseListener);
-// };
-// this.conn.onmessage = socketMessageListener;
-// this.conn.onclose = socketCloseListener;
-// },
-// methods: {
-// // TODO: complete this component
-// sendChat: function() {
-// let chatInput = document.getElementById("input-chat");
-// const chatTxt = chatInput.value;
-// chatInput.value = "";
-// this.chats.push({msg:chatTxt, author:this.myid});
-// this.conn.send(JSON.stringify({
-// code:"newchat", oppid: this.oppid, msg: chatTxt}));
-// },
-//// startChat: function(e) {
-//// document.getElementById("modal-chat").checked = true;
-//// },
- },
-});
+ methods: {
+ classObject: function(chat) {
+ return {
+ "my-chatmsg": (
+ !!chat.name && chat.name == this.st.user.name ||
+ !!chat.sid && chat.sid == this.st.user.sid
+ ),
+ "opp-chatmsg":
+ !!this.players &&
+ this.players.some(
+ p => {
+ return (
+ (
+ !!p.name &&
+ p.name == chat.name &&
+ p.name != this.st.user.name
+ )
+ ||
+ (
+ !!p.sid &&
+ p.sid == chat.sid &&
+ p.sid != this.st.user.sid
+ )
+ );
+ }
+ )
+ };
+ },
+ sendChat: function() {
+ let chatInput = document.getElementById("inputChat");
+ const chatTxt = chatInput.value.trim();
+ chatInput.focus(); //required on smartphones
+ if (chatTxt == "") return; //nothing to send
+ chatInput.value = "";
+ const chat = {
+ msg: chatTxt,
+ name: this.st.user.name,
+ // SID is required only for anonymous users (in live games)
+ sid: this.st.user.id == 0 ? this.st.user.sid : null
+ };
+ this.$emit("mychat", chat);
+ this.chats.unshift(chat);
+ },
+ newChat: function(chat) {
+ if (chat.msg != "") this.chats.unshift(chat);
+ },
+ clearHistory: function() {
+ this.chats = [];
+ this.$emit("chatcleared");
+ }
+ }
+};
+</script>
+
+<style lang="sass" scoped>
+.name
+ color: #839192
+
+.my-chatmsg
+ color: #6c3483
+.opp-chatmsg
+ color: #1f618d
+</style>