diff options
author | Kyle Gunger <kgunger12@gmail.com> | 2022-07-27 00:30:42 -0400 |
---|---|---|
committer | Kyle Gunger <kgunger12@gmail.com> | 2022-07-27 00:30:42 -0400 |
commit | d0ec435399ff3308be1b79ad67e37321b9a97b8e (patch) | |
tree | 9298f875c7cb6328294427d8e543c9024a08798d /scripts/gui/chat.js | |
parent | a97fcacb0ce4d2f30fbe989ec1f62f6bd95c7e9a (diff) |
[chat] Use an object instead of a list
Diffstat (limited to 'scripts/gui/chat.js')
-rw-r--r-- | scripts/gui/chat.js | 65 |
1 files changed, 26 insertions, 39 deletions
diff --git a/scripts/gui/chat.js b/scripts/gui/chat.js index 90259e2..31cc460 100644 --- a/scripts/gui/chat.js +++ b/scripts/gui/chat.js @@ -5,7 +5,7 @@ const CHAT_RPC = ["addChannel", "recieveMessage", "deleteChannel"] class Chat { constructor(e, soc) { - this.chats = []; + this.chats = {}; this.active = null; this.root = e; this.socket = soc; @@ -17,19 +17,6 @@ class Chat { cin.getElementsByTagName("button")[0].addEventListener("click", this.sendMessage.bind(this)); } - getChannel (name) - { - for(let i in this.chats) - { - if (this.chats[i].name == name) - { - return this.chats[i]; - } - } - - return null; - } - isActive (name) { if(this.active !== null) @@ -38,9 +25,12 @@ class Chat { return false; } - addChannel (name, follow = true) + addChannel (dat) { - if(this.getChannel(name) != null) + if(typeof dat.id !== "string") + return; + + if(this.chats[dat.id] != null) return; let d = document.createElement("div"); @@ -50,16 +40,16 @@ class Chat { b.setAttribute("active", false); - b.onclick = this.switchChannel.bind(this, name); + b.onclick = this.switchChannel.bind(this, dat.id); - b.innerText = name; + b.innerText = dat.name; d.className = "chat-text"; - this.chats.push({name: name, e: d, btn: b}); + this.chats[dat.id] = {name: dat.name, e: d, btn: b}; - if(follow) - this.switchChannel(name) + if(dat.follow) + this.switchChannel(dat.id) } getActiveChannel () @@ -67,10 +57,9 @@ class Chat { return this.active; } - switchChannel (name) + switchChannel (id) { - let c = this.getChannel(name); - this.active = c; + let c = this.chats[id]; if(c == null) return; @@ -85,6 +74,8 @@ class Chat { c.e.scroll({ top: c.e.scrollTopMax }); + + this.active = c; } checkEnter(e) @@ -100,12 +91,12 @@ class Chat { if(str == "") return; this.chatInput.value = ""; - this.socket.send("chat", {str}); + this.socket.send("chat", {channel: this.getActiveChannel().name, text: str}); } - recieveMessage (channel, msg) + recieveMessage (msg) { - let c = this.getChannel(channel); + let c = this.chats[msg.channel]; if(c == null) return; @@ -136,9 +127,9 @@ class Chat { c.e.scroll({top: c.e.scrollTopMax}); } - clearChannel (name) + clearChannel (id) { - let c = this.getChannel(name); + let c = this.chats[id]; if(c == null) return; @@ -146,18 +137,14 @@ class Chat { c.e.firstElementChild.remove(); } - deleteChannel (name) + deleteChannel (id) { - let c = this.getChannel(name); - if(c == null) + let c = this.chats[id]; + if(c == null || id === "global") return; - let id = this.chats.indexOf(c); - if(this.isActive(name)) { - if(id == 0 && this.chats.length > 1) - this.switchChannel(this.chats[1].name); - else - this.switchChannel(this.chats[id - 1].name); + if(this.isActive(id)) { + this.switchChannel("global"); } @@ -166,7 +153,7 @@ class Chat { c.btn.remove(); - this.chats.splice(this.chats.indexOf(c), 1); + delete this.chats[id]; } toggle () { |