-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-loading.js
114 lines (105 loc) · 3 KB
/
data-loading.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
let fileHandle;
var classes;
var dependencies;
/**
* Load classes event listener
*/
document.getElementById('load-classes-button').addEventListener('click', async () => {
[fileHandle] = await window.showOpenFilePicker();
const file = await fileHandle.getFile();
var contents = await file.text();
classes = await parseCSV(contents);
//console.log(classes[0]);
checkIfBothLoaded();
numClassesPerRole();
});
/**
* Load dependencies event listener
*/
document.getElementById('load-dependencies-button').addEventListener('click', async () => {
[fileHandle] = await window.showOpenFilePicker();
const file = await fileHandle.getFile();
var contents = await file.text();
dependencies = await parseDependencyCSV(contents);
//console.log(dependencies[0]);
checkIfBothLoaded();
});
/**
* Parse classses CSV
* @param {String} contents - content of the csv
* @returns {Array} classes
*/
async function parseCSV(contents) {
contents = contents.replace('\r\n', '\n');
if (contents.endsWith('\n')) {
contents = contents.slice(0, -2);
}
var lines = contents.split("\n");
console.log(lines.length - 1);
var result = [];
var headers = lines[0].split(",");
//console.log(headers);
for (var i = 1; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split(",");
for (var j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
return result;
}
/**
* Parse dependencies CSV
* @param {String} contents - content of the csv
* @returns {Array} dependecies
*/
async function parseDependencyCSV(contents) {
contents = contents.replace('\r\n', '\n');
if (contents.endsWith('\n')) {
contents = contents.slice(0, -2);
}
var lines = contents.split("\n");
console.log(lines.length);
var result = [];
for (var i=0; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split(",");
for (var j=0; j < currentline.length; j++) {
if (currentline[j] == 0) {
} else if (currentline[j] == 1) {
obj[j] = 1;
} else if (currentline[j] == 2) {
obj[j] = 2;
} else if (currentline[j] == 3) {
obj[j] = 3;
} else {
console.log('unknown value in dependencie matrix');
}
}
result.push(obj);
}
return result;
}
/**
* Check if both classes and dependencies are loaded
*/
function checkIfBothLoaded() {
if (classes != null && classes.length > 0 && dependencies != null && dependencies.length > 0) {
buildPackageTree();
draw();
}
}
/**
* Load files on page load
*/
document.addEventListener('load', loadFiles());
/**
* Load default data set
*/
async function loadFiles() {
classes = await parseCSV(classes_file.text);
numClassesPerRole();
dependencies = await parseDependencyCSV(dependencies_file.text);
checkIfBothLoaded();
}