-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
211 lines (186 loc) · 5.5 KB
/
script.js
File metadata and controls
211 lines (186 loc) · 5.5 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
let tokens = [];
let sequenceBegin = -1,
sequenceEnd = -1,
flag = 0;
let currentTokenIndex = 0;
let submitButton = document.querySelector(".submit");
submitButton.addEventListener("click", function () {
extractMsg();
});
function extractMsg() {
let textarea = document.querySelector("textarea");
let tokenMsg = textarea.value;
if (tokenMsg == "") {
toastMsg();
return [];
} else {
tokens = extractTokens(tokenMsg);
extractSequence(tokenMsg);
currentTokenIndex = 0;
if (tokens.length < 1) {
toastMsg();
return [];
}
textarea.remove();
submitButton.remove();
changeMarginTop();
createResultDiv();
displayToken();
}
}
function extractTokens(tokenMsg) {
let match = tokenMsg.match(/(?:\b|\d{4}-?)((?:\d{4}-?){4})(?:\d{4}-?)\b/g);
if (!match) return [];
//replace hyphens with double space
for (let i = 0; i < match.length; i++) {
match[i] = match[i].replace(/-/g, " ");
}
tokens = match;
//it will add space after each four digits if space is missing
tokens.forEach((element, index) => {
tokens[index] = element.replace(/(\d{4})(?=\d)/g, "$1 ");
});
return tokens;
}
function extractSequence(tokenMsg) {
//pattern with or without space ex: SquNo:- 7~11 SquNo:- 7 Sequence: 0~2 SeqNo: 5 SeqNo: 5~8
//sequence separator (= or ~)
let match = tokenMsg.match(
/(?<=(?:Sq(?:u)?No|Sequence|SeqNo):\s*-?\s*)\d+(?:(?:[~=]\d+)?)/g
);
try {
match.forEach((matched) => {
let numbers = matched.split(/[~=]/).map((num) => parseInt(num, 10));
if (numbers.length == 2) {
[sequenceBegin, sequenceEnd] = numbers;
} else {
sequenceEnd = numbers[0];
}
});
} catch (error) {
console.log(error);
}
}
function createResultDiv() {
let tokenHtml = `
<div class="token" style="white-space: pre-wrap;"></div>
<div class="forward_buttons">
<button class="arrow-previous">Previous Token</button>
<button class="arrow-next">Next Token</button>
</div>
`;
let resultDiv = document.querySelector(".results");
resultDiv.innerHTML = tokenHtml;
resultDiv.classList.add("animate");
tokenTable();
forwardButtons();
}
function tokenTable() {
let tableData = `
<thead>
<tr>
<th>S/N</th>
<th>Token</th>
<th>Seq.</th>
</tr>
</thead>
<tbody>
`;
//token length difference
let difference = 0;
if (sequenceEnd != -1) {
if (sequenceBegin != -1) {
difference = tokens.length - (sequenceEnd - sequenceBegin + 1);
} else difference = tokens.length - 1;
}
for (let i = 0; i < tokens.length; i++) {
let sequenceCol = "";
// Check if token length is more than the sequence
if (difference) {
sequenceCol = 0;
difference--;
}
//check if sequence start and ending index is available
else if (sequenceBegin != -1 && sequenceEnd != -1) {
console.log(difference);
if (sequenceBegin <= sequenceEnd) {
sequenceCol = sequenceBegin++;
}
} else if (sequenceEnd !== -1 && !flag) {
sequenceCol = sequenceEnd;
flag++;
}
tableData += `
<tr>
<td>${i + 1}</td>
<td>${tokens[i].replace(/ /g, " - ")}</td>
<td>${sequenceCol}</td>
</tr>
`;
}
tableData += "</tbody>";
let table = document.querySelector("table");
table.innerHTML = tableData;
table.classList.add("animate");
}
function displayToken() {
let toksequenceEndiv = document.querySelector(".token");
if (currentTokenIndex < tokens.length) {
let token = tokens[currentTokenIndex];
toksequenceEndiv.textContent = token;
} else {
toksequenceEndiv.textContent = "That's all";
}
updateNthChildValue(currentTokenIndex + 1);
}
function changeMarginTop() {
var section = document.querySelector("section");
var totalToken = tokens.length * 10;
section.style.marginTop = Math.min(150 - Math.abs((totalToken -= 50))) + "px";
}
function updateNthChildValue(nthChildValue) {
let styleTag = document.querySelector("style");
let cssText = `tbody tr:nth-child(${nthChildValue}){
color:white;
background-color: darkcyan;
}
tbody tr:nth-child(${nthChildValue}) td{
border:1px solid white;
}`;
styleTag.innerHTML = cssText;
}
function forwardButtons() {
let previousButton = document.querySelector(".arrow-previous");
previousButton.addEventListener("click", function () {
currentTokenIndex = Math.max(0, currentTokenIndex - 1);
displayToken();
});
let nextButton = document.querySelector(".arrow-next");
nextButton.addEventListener("click", function () {
currentTokenIndex = Math.min(tokens.length, currentTokenIndex + 1);
displayToken();
});
}
//Toast alert message
let notifications = document.querySelector(".notifications");
function createToast(type, icon, title, text) {
let newToast = document.createElement("div");
newToast.innerHTML = `
<div class="toast ${type}">
<i class="${icon}"></i>
<div class="content">
<div class="title">${title}</div>
<span>${text}</span>
</div>
<i class="fa-solid fa-xmark" onclick="(this.parentElement).remove()"></i>
</div>`;
notifications.appendChild(newToast);
newToast.timeOut = setTimeout(() => newToast.remove(), 5000);
}
function toastMsg() {
let type = "warning";
let icon = "fa-solid fa-triangle-exclamation";
let title = "Warning";
let text = "Enter valid message!";
createToast(type, icon, title, text);
}