-
Notifications
You must be signed in to change notification settings - Fork 4
/
js.js
171 lines (149 loc) · 5.44 KB
/
js.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
let checkboxes = [];
let flagValueInput = null;
let flagTypeInput = null;
let checkboxesTable = null;
let flagTableInput = null;
let selectedName = null;
let selectedDescription = null;
let init = false;
let fetchTries = 5;
let flagTables = {};
const columnCount = 4;
function clearSelectedNameAndDesc() {
selectedName.innerText = "";
selectedDescription.innerText = "";
}
function _newCheckbox(td, currentFlag, val) {
let unknown = false;
var flagName = currentFlag.name;
if (flagName.length === 0 || flagName.includes("_UNKNOWN_")) {
flagName = "[unknown]";
unknown = true;
}
td.className = "flag";
let newCheckbox = document.createElement("input");
let valStr = val.toString();
newCheckbox.type = "checkbox";
newCheckbox.name = flagName;
newCheckbox.value = valStr;
newCheckbox.id = "FLAG_" + valStr;
let newLabel = document.createElement("label");
newLabel.appendChild(newCheckbox);
newLabel.onmouseenter = () => {
if (unknown) selectedName.classList.add("dimmedColor");
else selectedName.classList.remove("dimmedColor");
selectedName.innerText = flagName;
selectedDescription.innerText = currentFlag.description;
};
newLabel.onmouseleave = () => {
clearSelectedNameAndDesc();
};
newLabel.onchange = () => {
updateFlagValue();
};
newLabel.innerHTML += `${(unknown ? "<div class=\"dimmedColor flagCellPadding\">" : "<div class=\"flagCellPadding\">")}${flagName}<br><div class=\"flag_value\">(1 << ${val})</div></div>`;
td.appendChild(newLabel);
return newCheckbox;
}
function redrawCheckboxes() {
checkboxes = [];
checkboxesTable.innerHTML = "";
let flagVal = 0;
let currentTable = flagTableInput.value;
let currentType = flagTypeInput.value;
for (let i = 0; i < Math.ceil(32 / columnCount); i++) { // Rows
let newTr = document.createElement("tr");
for (let j = 0; j < columnCount; j++, flagVal++) { // Columns
let newTd = document.createElement("td");
let currentFlag = flagTables[currentTable]["flags"][currentType][flagVal];
checkboxes.push(_newCheckbox(
newTd,
currentFlag,
flagVal
));
newTr.appendChild(newTd);
}
checkboxesTable.appendChild(newTr);
}
}
function updateAllCheckboxes(flagsValueNum) {
checkboxes.forEach((checkbox) => {
let flagBit = ((0x1 << parseInt(checkbox.value)));
if ((BigInt(flagBit) & BigInt(flagsValueNum)) > 0) {
$("#" + checkbox.id).prop("checked", true);
}
else {
$("#" + checkbox.id).prop("checked", false);
}
});
}
function updateFlagValue() {
let flagsNumber = 0;
checkboxes.forEach((checkbox) => {
if ($("#" + checkbox.id).prop("checked")) {
flagsNumber |= (0x1 << parseInt(checkbox.value));
}
});
flagValueInput.value = (flagsNumber>>>0).toString(16).toUpperCase();
}
function processFlagValue() {
if (!init) return;
flagValueInput.value = flagValueInput.value.toUpperCase().substr(0, 8);
let input = (flagValueInput.value.length > 0) ? flagValueInput.value : 0;
if (!(/^[0-9a-f]+$/i.test(input))) {
flagValueInput.classList.add("badInput");
updateAllCheckboxes(0);
return;
} else {
flagValueInput.classList.remove("badInput");
let inputNumber = parseInt(input, 16);
updateAllCheckboxes(inputNumber);
}
}
function processFlagType() {
if (!init) return;
clearSelectedNameAndDesc();
redrawCheckboxes();
processFlagValue();
}
function processFlagTable() {
processFlagType();
}
async function fetchFlagTable(key, url) {
let response = await fetch(url);
if (response?.status === 200) {
let obj = await response.json();
flagTables[key] = obj;
}
}
async function fetchAllFlagTables() {
if (!flagTables["default"]) await fetchFlagTable("default", "https://raw.githubusercontent.com/adam10603/GTAVFlags/main/flags.json");
if (!flagTables["default"]) throw new Error("Failed to load a flag lookup table. Retrying ...");
if (!flagTables["ikt"]) await fetchFlagTable("ikt", "https://raw.githubusercontent.com/E66666666/GTAVHandlingInfo/master/flags.json");
if (!flagTables["ikt"]) throw new Error("Failed to load a flag lookup table. Retrying ...");
}
function postFetchTables() {
flagTableInput.options[0].innerText += ` (v${flagTables["default"]["version"]})`;
flagTableInput.options[1].innerText += ` (v${flagTables["ikt"]["version"]})`;
redrawCheckboxes();
init = true;
}
function initTables() {
fetchAllFlagTables().then(postFetchTables).catch((err) => {
if (fetchTries-- > 0) {
console.warn(err);
setTimeout(initTables, 500);
}
else alert("Failed to load flag lookup tables. Try refreshing the page!");
});
}
window.onload = () => {
flagValueInput = document.getElementById("flagvalue");
flagTypeInput = document.getElementById("flagtype");
checkboxesTable = document.getElementById("checkboxes_table");
flagTableInput = document.getElementById("flagtable");
selectedName = document.getElementById("selected_name");
selectedDescription = document.getElementById("selected_description");
flagValueInput.value = "0";
initTables();
};