forked from ilbonte/rescuetime-again
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.html
117 lines (93 loc) · 3.07 KB
/
merge.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>RescueTime, again</title>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<h1>Merge</h1>
<h2>Which type of file do you want to merge?</h2>
<input type="radio" name="type" value="hour" id="hour" checked>Hour (Productivity/Efficiency)
<br>
<input type="radio" name="type" value="activity" id="activity">Activity
<h3>Upload your files in chronological order!</h3>
<h6>A file at a time </h6>
<input id="file" type="file"/>
<button onClick="choose()">MERGE!</button>
<button onClick="reset()">ResetAll</button>
<ul id="fileList">
</ul>
<script>
var files = [];
(function () {
function onChange(event) {
var reader = new FileReader();
reader.onload = onReaderLoad;
reader.readAsText(event.target.files[0]);
$("#fileList").append("<li>" + document.getElementById('file').value + "</li>");
}
function onReaderLoad(event) {
console.log(event.target.result);
var obj = JSON.parse(event.target.result);
files.push(obj);
}
document.getElementById('file').addEventListener('change', onChange);
}());
function choose() {
if (document.getElementById('hour').checked) {
mergeHours();
} else if (document.getElementById('activity').checked) {
mergeActivity();
}
}
function mergeHours() {
var tot;
tot = files[0];
console.log(files);
for (var i = 1; i < files.length; i++) {
tot.rows = tot.rows.concat(files[i].rows);
}
var JSONString = JSON.stringify(tot);
var data = "text/json;charset=utf-8," + encodeURIComponent(JSONString);
$('<a href="data:' + data + '" download="Hours-Efficency.json">download JSON</a>').appendTo('#fileList');
}
function mergeActivity() {
console.log("mergin'");
var tot;
tot = files[0];
console.log(files);
for (var i = 1; i < files.length; i++) {
tot = merge(tot, files[i]);
}
tot.rows = tot.rows.sort(function (a, b) {
return b[1] - a[1];
});
var JSONString = JSON.stringify(tot);
var data = "text/json;charset=utf-8," + encodeURIComponent(JSONString);
$('<a href="data:' + data + '" download="Activity.json">download JSON</a>').appendTo('#fileList');
}
function merge(full, mese) {
for (var i = 0; i < full.rows.length; i++) {
for (var j = 0; j < mese.rows.length; j++) {
if (full.rows[i][3] === mese.rows[j][3]) {
full.rows[i][1] += mese.rows[j][1];
mese.rows.splice(j, 1);
}
}
}
full.rows = full.rows.concat(mese.rows);
return full;
}
function reset(){
files.clear();
$("#fileList").empty();
}
Array.prototype.clear = function() {
while (this.length) {
this.pop();
}
};
</script>
</body>
</html>