-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
chat_class.js
128 lines (111 loc) · 4.19 KB
/
chat_class.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class ChatClass {
constructor(window, rootElement, canvas) {
this.rootElement = rootElement;
this.canvas = canvas;
this.chatRootElement = document.createElement("div");
this.chatRootElement.style.position = "absolute";
this.chatRootElement.style.background = "#313131A0";
this.chatRootElement.style.width =
Math.floor(canvas.offsetWidth * 0.3) + "px";
this.chatRootElement.style.height =
Math.floor(canvas.offsetHeight * 0.3) + "px";
this.chatRootElement.style.padding = "0px";
this.chatRootElement.style.margin = "0px";
this.chatRootElement.style.display = "flex";
this.chatRootElement.style.flexDirection = "column";
this.messageElement = document.createElement("div");
this.messageElement.setAttribute("id", "messageArea");
this.messageElement.style.position = "relative";
this.messageElement.style.background = "#00000000";
this.messageElement.style.flex = "1";
this.messageElement.style.margin = "2% 2% 0% 2%";
this.messageElement.style.fontSize = "12px";
this.messageElement.style.fontWeight = "normal";
this.messageElement.style.color = "white";
this.messageElement.style.resize = "none";
this.messageElement.style.overflowY = "scroll";
this.messageElement.style.overflow = "auto";
this.messageElement.style.padding = "5px";
this.chatRootElement.appendChild(this.messageElement);
this.chatElement = document.createElement("input");
this.chatElement.setAttribute("type", "text");
this.chatElement.style.position = "relative";
this.chatElement.style.background = "#000000A0";
this.chatElement.style.width = "calc(96% - 10px)";
this.chatElement.style.height = "Auto";
this.chatElement.style.flex = "0";
this.chatElement.style.margin = "2% 0% 2% 2%";
this.chatElement.style.border = "0 none black";
this.chatElement.style.color = "white";
this.chatElement.style.fontSize = "12px";
this.chatElement.style.paddingLeft = "5px";
this.chatElement.style.paddingRight = "5px";
this.chatRootElement.appendChild(this.chatElement);
rootElement.appendChild(this.chatRootElement);
var self = this;
this.chatElement.addEventListener("blur", (e) => {
self.chatElement.value = "";
});
window.addEventListener(
"keydown",
(e) => {
switch (e.keyCode) {
case KEYCODE_RETURN:
if (self.chatElement === document.activeElement) {
if (self.chatElement.value && self.chatElement.value.length > 0) {
if (self.chat) {
self.chat(self.chatElement.value);
}
}
self.chatElement.value = "";
self.chatElement.blur();
} else {
self.chatElement.focus();
}
break;
}
},
false
);
window.onresize = function () {
self.updateSize();
};
window.onorientationchange = function () {
self.updateSize();
};
this.updateSize();
this.setVisible(false);
}
setVisible(visible) {
const value = visible ? "visible" : "hidden";
if (this.chatRootElement.style.visibility !== value) {
this.chatRootElement.style.visibility = value;
}
}
isInputActive() {
return this.chatElement === document.activeElement;
}
writeToMessage(text) {
this.messageElement.innerHTML += text;
this.messageElement.scrollTop = this.messageElement.scrollHeight;
}
updateSize() {
var chatWidth = 400;
var chatHeight = 200;
var margin = 5;
var scaleWidth = this.canvas.offsetWidth / this.canvas.width;
var scaleHeight = this.canvas.offsetHeight / this.canvas.height;
chatWidth *= scaleWidth;
chatHeight *= scaleHeight;
this.chatRootElement.style.width = chatWidth + "px";
this.chatRootElement.style.height = chatHeight + "px";
this.chatRootElement.style.left =
this.canvas.offsetLeft + margin * scaleWidth + "px";
this.chatRootElement.style.top =
this.canvas.offsetTop +
this.canvas.offsetHeight -
chatHeight -
margin * scaleHeight +
"px";
}
}