-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.js
84 lines (67 loc) · 2.35 KB
/
console.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
class Console extends Window {
constructor() {
super();
this.history = [];
this.list = document.createElement("div");
this.list.style.position = "absolute";
this.list.style.overflowY = "auto";
this.list.style.left = "0";
this.list.style.right = "0";
this.list.style.top = "0";
this.list.style.bottom = "40px";
this.list.className = "no-results";
this.content.appendChild(this.list);
this.txtInput = document.createElement("input");
this.txtInput.type = "text";
this.txtInput.placeholder = "hostname or ip";
this.txtInput.style.position = "absolute";
this.txtInput.style.left = "40px";
this.txtInput.style.bottom = "40px";
this.txtInput.style.width = "calc(100% - 80px)";
this.txtInput.style.margin = "0";
this.txtInput.style.border = "0";
this.txtInput.style.boxSizing = "border-box";
this.content.appendChild(this.txtInput);
let historyIndex = -1;
this.txtInput.onkeydown = event=> {
if (event.key === "Enter") {
if (this.txtInput.value.length == 0) return;
this.Push(this.txtInput.value.trim().toLocaleLowerCase());
this.list.scrollTop = this.list.scrollHeight;
this.txtInput.value = "";
event.preventDefault();
}
if (event.key === "ArrowUp" || event.key === "ArrowDown") {
if (this.history.length == 0) return;
if (event.key === "ArrowUp") historyIndex--;
if (event.key === "ArrowDown") historyIndex++;
if (historyIndex < 0) historyIndex = this.history.length - 1;
historyIndex %= this.history.length;
this.txtInput.value = this.history[historyIndex];
event.preventDefault();
}
else if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") {
historyIndex = -1;
}
};
this.defaultElement = this.txtInput;
this.txtInput.focus();
this.txtInput.onfocus = ()=> this.BringToFront();
this.escAction = ()=> { this.txtInput.value = ""; };
}
Push(command) { //overridable
if (command === "") return;
if (command === "!!" && this.history.length === 0) return false;
if (command === "!!") {
this.Push(this.history[this.history.length - 1]);
return false;
}
this.txtInput.style.left = "8px";
this.txtInput.style.bottom = "8px";
this.txtInput.style.width = "calc(100% - 16px)";
if (this.history.includes(command))
this.history.splice(this.history.indexOf(command), 1);
this.history.push(command);
return true;
}
}