-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchSystem.js
160 lines (131 loc) · 5.23 KB
/
searchSystem.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
const settings = {
maxPrintMatchesLength: 25,
};
function searchByInfo(authorName, testCaseInput, testCaseOutput) {
if (authorName + testCaseInput + testCaseOutput === "") return masterFileRead;
return masterFileRead.filter((e) => {
return (
e.author === authorName ||
e.testCases.some((t) => {
return (
(testCaseInput === "" || t[0] === testCaseInput) &&
(testCaseOutput === "" || t[1] === testCaseOutput) &&
testCaseInput + testCaseOutput !== ""
);
})
);
});
}
function clearSearchResults() {
try {
var div = document.getElementById("results");
while (div.firstChild) {
div.removeChild(div.firstChild);
}
return;
} catch (err) {
console.log(err);
return;
}
}
function clearSearchQuery() {
document.getElementById("authorInput").value = "";
document.getElementById("testCaseInputInput").value = "";
document.getElementById("testCaseOutputInput").value = "";
return;
}
function createBoldAndText(boldedText, regularText, paramsIn = {}) {
var params = {
afterBoldText: paramsIn.afterBoldText ?? "",
mainClass: paramsIn.mainClass ?? "resultInfoText",
boldClass: paramsIn.boldClass ?? "boldedLabelText",
textClass: paramsIn.textClass ?? "textLabelText",
};
//Main holder
var showText = document.createElement("pre");
showText.classList.add(params.mainClass);
//Info
if (boldedText !== "") {
var boldedLabel = document.createElement("span");
boldedLabel.classList.add(params.boldClass);
boldedLabel.innerHTML = `<strong>${boldedText}: </strong>${params.afterBoldText}`;
showText.appendChild(boldedLabel);
}
if (regularText !== "") {
var textLabel = document.createElement("span");
textLabel.classList.add(params.textClass);
textLabel.innerText = regularText;
showText.appendChild(textLabel);
}
return showText;
}
function printResults(matchArray) {
if (matchArray.length > settings.maxPrintMatchesLength) {
document.getElementById(
"resultAmount"
).innerText = `Found ${matchArray.length} results, only showing first ${settings.maxPrintMatchesLength}`;
matchArray = matchArray.slice(0, settings.maxPrintMatchesLength);
} else {
document.getElementById("resultAmount").innerText = `Found ${matchArray.length} results`;
}
var div = document.getElementById("results");
matchArray.forEach((data) => {
var showAll = document.createElement("div");
showAll.classList.add("resultCard");
//Title info
showAll.appendChild(createBoldAndText("TITLE", data.title));
//Author info
showAll.appendChild(createBoldAndText("AUTHOR", data.author));
//Solution label
var cocSolutions = data.solutions;
//Solution info
if (cocSolutions.length === 0) {
showAll.appendChild(createBoldAndText("SOLUTIONS", "No solution avalible"));
} else {
var showSolution = document.createElement("div");
showSolution.classList.add("resultAllSolutions");
//For each solution
cocSolutions.forEach((sol, indexSol) => {
//Create solution card
var solutionInfoCard = document.createElement("div");
solutionInfoCard.classList.add("resultInfoSolution");
//Make clickable
solutionInfoCard.setAttribute("onclick", `copySolution(${masterFileRead.indexOf(data)}, ${indexSol})`);
//Add info
solutionInfoCard.appendChild(createBoldAndText("Language", sol.lang));
solutionInfoCard.appendChild(createBoldAndText("Characters", sol.solution.replace(/\\/g, "").length));
solutionInfoCard.appendChild(createBoldAndText("Solution", sol.solution, { afterBoldText: "<br>" }));
showSolution.appendChild(solutionInfoCard);
});
showAll.appendChild(createBoldAndText("SOLUTIONS", ""));
showAll.appendChild(showSolution);
}
div.appendChild(showAll);
});
}
async function submitSearch() {
await clearSearchResults();
var searchAuthor = document.getElementById("authorInput").value;
var searchTestCaseInput = document.getElementById("testCaseInputInput").value;
var searchTestCaseOutput = document.getElementById("testCaseOutputInput").value;
var currentMatches = searchByInfo(searchAuthor, searchTestCaseInput, searchTestCaseOutput);
printResults(currentMatches);
await clearSearchQuery();
}
function solutionInfoCount() {
var hasSolution = 0;
var cocAmount = 0;
masterFileRead.forEach((e) => {
if (e.solutions.length !== 0) hasSolution++;
cocAmount++;
});
document.getElementById(
"answerCountInfo"
).innerText = `There are currently ${hasSolution} answers outs of ${cocAmount} (${(
(hasSolution / cocAmount) *
100
).toFixed(2)}%)`;
}
function copySolution(masterIndex, solutionIndex) {
navigator.clipboard.writeText(masterFileRead[masterIndex].solutions[solutionIndex].solution);
}