4 * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 dragonblocks.Chat = class{
27 this.lines = dragonblocks.settings.chat.lines;
30 dragonblocks.keyHandler.down("t", _ => {
31 dragonblocks.chat.open();
33 dragonblocks.keyHandler.down("/", event => {
34 dragonblocks.chat.open();
35 document.getElementById("dragonblocks.chat.input").value = "/";
39 let display = document.createElement("div");
40 display.id = "dragonblocks.chat";
41 display.style.position = "fixed";
42 display.style.top = "0px";
43 display.style.left = "0px";
44 display.style.backgroundColor = "black";
45 display.style.opacity = "0.7";
46 display.style.height = 23 * this.lines + "px";
47 display.style.fontSize = "20px";
48 display.style.color = "white";
49 display.style.width = "100%";
50 display.style.fontFamily = "monospace";
51 display.style.overflowY = "scroll";
52 display.style.scrollbarWidth = "none";
53 display.style.visibility = "hidden";
54 document.body.appendChild(display);
55 let input = document.createElement("input");
56 input.id = "dragonblocks.chat.input";
57 input.style.position = "fixed";
58 input.style.top = 23 * this.lines + "px";
59 input.style.left = "0px";
60 input.style.backgroundColor = "black";
61 input.style.border = "none";
62 input.style.opacity = "0.7";
63 input.style.fontSize = "20px";
64 input.style.color = "white";
65 input.style.height = "23px";
66 input.style.width = "100%";
67 input.style.caretWidth = "30px";
68 input.style.caretHeight = "20px";
69 input.style.fontFamily = "monospace";
70 input.style.visibility = "hidden";
71 input.addEventListener("keydown", event => {
74 if(event.srcElement.value == "")
76 dragonblocks.chat.input[dragonblocks.chat.input.length - 1] = event.srcElement.value;
77 dragonblocks.chat.send(event.srcElement.value);
78 event.srcElement.value = "";
79 dragonblocks.chat.input.push("");
80 dragonblocks.chat.history = -1;
83 dragonblocks.chat.close();
86 event.srcElement.value = dragonblocks.chat.historyUp();
89 event.srcElement.value = dragonblocks.chat.historyDown();
93 input.addEventListener("input", _ => { dragonblocks.chat.input[dragonblocks.chat.input.length - 1] = event.srcElement.value });
94 document.body.appendChild(input);
97 dragonblocks.keyHandler.lockAll();
98 document.getElementById("dragonblocks.chat").style.visibility = "visible";
99 document.getElementById("dragonblocks.chat.input").style.visibility = "visible";
100 document.getElementById("dragonblocks.chat.input").focus();
103 setTimeout(_ => {dragonblocks.keyHandler.unlockAll();});
104 document.getElementById("dragonblocks.chat").style.visibility = "hidden";
105 document.getElementById("dragonblocks.chat.input").style.visibility = "hidden";
106 document.getElementById("dragonblocks.chat.input").blur();
110 if(text.startsWith("!HTML"))
111 text = text.replace("!HTML", "");
113 text = dblib.htmlEntities(text);
115 document.getElementById("dragonblocks.chat").innerHTML += text;
116 document.getElementById("dragonblocks.chat").lastChild.scrollIntoView();
119 for(let func of dragonblocks.onChatMessageFunctions)
120 if(func(input) == false)
126 if(this.input[this.input.length + this.history] == undefined)
128 return this.input[this.input.length + this.history];
132 if(this.input[this.input.length + this.history] == undefined)
134 return this.input[this.input.length + this.history];
137 document.getElementById("dragonblocks.chat").innerHTML = "<br>".repeat(this.lines);
140 dragonblocks.chat = new dragonblocks.Chat();
141 dragonblocks.chatMessage = function(msg){
142 dragonblocks.chat.write(msg);
144 dragonblocks.onChatMessageFunctions = [];
145 dragonblocks.registerOnChatMessage = function(func){
146 dragonblocks.onChatMessageFunctions.push(func);