-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
314 lines (274 loc) · 10.6 KB
/
Copy pathscripts.js
File metadata and controls
314 lines (274 loc) · 10.6 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
function loadJSON() {
const jsonInput = document.getElementById('jsonInput').value;
let jsonObject;
try {
jsonObject = JSON.parse(jsonInput);
} catch (error) {
alert('Invalid JSON');
return;
}
populateTable(jsonObject.csvTags);
}
function populateTable(csvTags) {
const table = document.getElementById('jsonTable');
const tableHead = document.getElementById('tableHead');
tableHead.style.display = 'table-header-group';
table.style.display = 'table';
const tableBody = table.querySelector('tbody');
tableBody.innerHTML = '';
csvTags.forEach(tag => {
const row = document.createElement('tr');
for (const key of ['order', 'headName', 'code', 'displayName', 'isSubject']) {
const cell = document.createElement('td');
const input = document.createElement('input');
input.type = key === 'isSubject' ? 'checkbox' : 'text';
if (key === 'isSubject') {
input.checked = tag[key];
} else {
input.value = tag[key];
if (key === 'order' || key === 'headName' || key === 'displayName') {
input.addEventListener('input', checkForDuplicates);
}
}
cell.appendChild(input);
row.appendChild(cell);
}
tableBody.appendChild(row);
});
checkForDuplicates();
}
function exportJSON() {
const tableBody = document.getElementById('jsonTable').querySelector('tbody');
const rows = tableBody.querySelectorAll('tr');
const jsonObject = { csvTags: [] };
rows.forEach(row => {
const cells = row.querySelectorAll('td');
const tag = {
order: parseInt(cells[0].querySelector('input').value, 10),
headName: cells[1].querySelector('input').value,
code: cells[2].querySelector('input').value,
displayName: cells[3].querySelector('input').value,
isSubject: cells[4].querySelector('input').checked,
};
jsonObject.csvTags.push(tag);
});
const jsonString = JSON.stringify(jsonObject, null, 2);
const blob = new Blob([jsonString], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'modified_json.json';
a.click();
URL.revokeObjectURL(url);
}
function loadCSV() {
const input = document.getElementById('csvInput');
if (input.files.length === 0) {
alert('Please select a CSV file');
return;
}
const file = input.files[0];
const reader = new FileReader();
reader.onload = function(e) {
const text = e.target.result;
const csvTags = parseCSV(text);
populateTable(csvTags);
document.getElementById('jsonInput').value = JSON.stringify({ csvTags }, null, 2);
};
reader.readAsText(file);
}
function parseCSV(text) {
const lines = text.split('\n');
const headersString = lines[0].replace(/\t/g, ',');
const headers = headersString.split(',').map(header => header.trim());
const csvTags = [];
const codeMap = {
"ORG_NAME": "collegeName",
"ACADEMIC_COURSE_ID": "degreeName",
"COURSE_NAME": "degreeName",
"ADMISSION": "admissionYear",
"STREAM": "batchNameWithoutYear",
"SESSION": "sessionName",
"REGN_NO": "registerNo",
"RROLL": "registerNo",
"CNAME": "studentName",
"GENDER": "genderFirstLetter",
"DOB": "dob",
"FNAME": "fatherName",
"MNAME": "motherName",
"PHOTO": "studentImageRegNo",
"MRKS_REC_STATUS": "omcStatus",
"RESULT": "class",
"YEAR": "examYear",
"MONTH": "examMonthNameFull",
"PERCENT": "semesterPercentage",
"CERT_NO": "slNo",
"SEM": "academicTermNameInRoman",
"TOT": "grandTotalMark",
"TOT_MRKS": "semesterTotalMarksObtained",
"TOT_CREDIT": "earnedCreditWithExcludeTotalCondition",
"TOT_GRADE_POINTS": "totalGradePoint",
"SGPA": "sgpa",
"TOT_GRADE": "semesterGrandTotalGrade"
};
const secondLine = lines[1].replace(/\t/g, ',');
const values = secondLine.split(',').map(value => value.trim());
headers.forEach((header, index) => {
if ((header.startsWith("SUB1") && !header.startsWith("SUB10")) || !header.startsWith("SUB")) {
const isSubject = header.startsWith("SUB");
var code = "";
if(values[index]){
code = isSubject ? header.slice(0, -3).toLowerCase() : (codeMap[header] || header.toLowerCase());
}else{
code = 'blank';
}
const order = index + 1;
let displayName = header;
let headName = header;
if (isSubject) {
headName = header.slice(4);
displayName = header.slice(0, 3) + header.slice(4);
}
csvTags.push({
code,
order,
headName,
isSubject,
displayName
});
}
});
return csvTags;
}
function sortTable() {
const tableBody = document.getElementById('jsonTable').querySelector('tbody');
const rowsArray = Array.from(tableBody.querySelectorAll('tr'));
rowsArray.sort((a, b) => {
const orderA = parseInt(a.querySelector('td:nth-child(1) input').value, 10);
const orderB = parseInt(b.querySelector('td:nth-child(1) input').value, 10);
return orderA - orderB;
});
tableBody.innerHTML = '';
rowsArray.forEach(row => tableBody.appendChild(row));
checkForDuplicates();
}
function correctOrder() {
const tableBody = document.getElementById('jsonTable').querySelector('tbody');
const rows = tableBody.querySelectorAll('tr');
rows.forEach((row, index) => {
const orderCell = row.querySelector('td:nth-child(1) input');
orderCell.value = index + 1;
});
checkForDuplicates();
}
function addRow() {
const table = document.getElementById('jsonTable');
const tableHead = document.getElementById('tableHead');
tableHead.style.display = 'table-header-group';
table.style.display = 'table';
const tableBody = table.querySelector('tbody');
const row = document.createElement('tr');
const orderValue = tableBody.querySelectorAll('tr').length + 1;
for (const key of ['order', 'headName', 'code', 'displayName', 'isSubject']) {
const cell = document.createElement('td');
const input = document.createElement('input');
input.type = key === 'isSubject' ? 'checkbox' : 'text';
if (key === 'order' || key === 'headName' || key === 'displayName') {
input.addEventListener('input', checkForDuplicates);
}
if (key === 'order') {
input.value = orderValue;
}
cell.appendChild(input);
row.appendChild(cell);
}
tableBody.appendChild(row);
checkForDuplicates();
}
function checkForDuplicates() {
const tableBody = document.getElementById('jsonTable').querySelector('tbody');
const rows = Array.from(tableBody.querySelectorAll('tr'));
const orderValues = rows.map(row => row.querySelector('td:nth-child(1) input').value);
const headNameValues = rows.map(row => row.querySelector('td:nth-child(2) input').value);
const displayNameValues = rows.map(row => row.querySelector('td:nth-child(4) input').value);
const isSubjectValues = rows.map(row => row.querySelector('td:nth-child(5) input').checked);
// Reset all inputs
rows.forEach(row => {
row.querySelector('td:nth-child(1) input').classList.remove('error');
row.querySelector('td:nth-child(2) input').classList.remove('error');
row.querySelector('td:nth-child(4) input').classList.remove('error');
});
// Find duplicates in orders
const duplicateOrders = orderValues.filter((value, index, self) => self.indexOf(value) !== index && value !== "");
// Highlight duplicate orders
duplicateOrders.forEach(duplicate => {
rows.forEach(row => {
const orderInput = row.querySelector('td:nth-child(1) input');
if (orderInput.value === duplicate) {
orderInput.classList.add('error');
}
});
});
// Find duplicates in headName and isSubject combination
const uniqueCombinations = new Set();
const duplicateCombinations = new Set();
rows.forEach((row, index) => {
const headName = headNameValues[index];
const isSubject = isSubjectValues[index];
const key = `${headName}-${isSubject}`;
if (uniqueCombinations.has(key)) {
duplicateCombinations.add(key);
} else {
uniqueCombinations.add(key);
}
});
// Highlight duplicate headName and isSubject combinations
duplicateCombinations.forEach(duplicate => {
rows.forEach(row => {
const headNameInput = row.querySelector('td:nth-child(2) input');
const isSubjectInput = row.querySelector('td:nth-child(5) input');
const key = `${headNameInput.value}-${isSubjectInput.checked}`;
if (key === duplicate) {
headNameInput.classList.add('error');
}
});
});
// Find duplicates in displayName
const duplicateDisplayNames = displayNameValues.filter((value, index, self) => self.indexOf(value) !== index && value !== "");
// Highlight duplicate displayNames
duplicateDisplayNames.forEach(duplicate => {
rows.forEach(row => {
const displayNameInput = row.querySelector('td:nth-child(4) input');
if (displayNameInput.value === duplicate) {
displayNameInput.classList.add('error');
}
});
});
}
function resetJSON() {
document.getElementById('jsonInput').value = '';
const table = document.getElementById('jsonTable');
const tableBody = table.querySelector('tbody');
tableBody.innerHTML = '';
table.style.display = 'none';
}
function toggleTextareaSize() {
const textarea = document.getElementById('jsonInput');
const expandBtn = document.querySelector('.expand-btn i');
if (textarea.style.height === '400px') {
textarea.style.height = '150px';
expandBtn.className = 'fas fa-expand';
} else {
textarea.style.height = '400px';
expandBtn.className = 'fas fa-compress';
}
}
function formatJSON() {
const jsonInput = document.getElementById('jsonInput');
try {
const jsonObject = JSON.parse(jsonInput.value);
jsonInput.value = JSON.stringify(jsonObject, null, 2);
} catch (error) {
alert('Invalid JSON');
}
}