summaryrefslogtreecommitdiff
path: root/client-web/source/chat.ts
blob: d1165eed82d7d9aa4f1f09863653a65ee520b17e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { ediv, espan } from "./helper.ts";
import { CHAT } from "./index.ts";
import { Room } from "./room.ts";
import { User } from "./user/mod.ts";

export class Chat {
    private _shown = false;

    messages = ediv({ class: "messages" })
    controls = ediv({ class: "controls" })

    get shown() { return this._shown }
    set shown(value: boolean) {
        if (value && !this._shown) document.body.prepend(CHAT)
        if (!value && this._shown) document.body.removeChild(CHAT)
        this._shown = value
    }

    constructor(public room: Room) {
        const send = document.createElement("input")
        send.type = "text"
        send.onkeydown = (ev) => {
            if (ev.code == "Enter") {
                room.local_user.chat(send.value)
                this.send_message(room.local_user, send.value)
                send.value = ""
            }
        }
        this.controls.append(send)
        this.messages.append(document.createElement("hr"))
        CHAT.append(this.messages, this.controls)
    }

    send_message(sender: User, message: string) {
        this.messages.append(ediv({ class: "message" },
            espan(sender.display_name, { class: "author" }),
            ": ",
            espan(message, { class: "content" })
        ))
    }
}