-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·202 lines (194 loc) · 6.12 KB
/
Copy pathindex.js
File metadata and controls
executable file
·202 lines (194 loc) · 6.12 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
var token = "c0a29d1039808ba12ba6600e355267c73449beb9";
var commit_count = 10;
var cur_commits = [];
var new_commits = [];
//newest date is a week ago
var newest_date = new Date();
newest_date.setDate(newest_date.getDate() - 7);
$(window).load(function(){
//github links go in here
var urls = [
"https://github.com/dudeofea/github-feed-viewer"
];
//remove github link and just leave /:author/:repo:
for (var i = 0; i < urls.length; i++) {
urls[i] = urls[i].substring(urls[i].indexOf("github.com/")+11);
};
update_commits(urls);
setInterval(function(){
update_commits(urls);
}, 20000);
});
//get newest commits
function update_commits(urls){
//get commit info from repos
var d = new Dep({ bundleArgs: true });
for (var k = 0; k < urls.length; k++) {
var url = "https://api.github.com/repos/"+urls[k]+"/commits?access_token="+token;
$.get(url, function(commits){
for (var i = 0; i < commits.length; i++) {
var commit_date = new Date(commits[i]['commit']['author']['date']);
if(commit_date > newest_date){
commits[i]['date'] = commit_date;
$.get(commits[i]['url']+"?access_token="+token, d.addDep());
new_commits.push(commits[i]);
}
};
});
};
d.calc(function(data){
//add stats & diffs
for (var i = 0; i < data.length / 3; i++) {
new_commits[i]['stats'] = data[i*3]['stats'];
new_commits[i]['files'] = data[i*3]['files'];
};
//sort cur_commits by date
new_commits.sort(function(a, b){
if(a['date'] > b['date'])
return -1;
if(a['date'] < b['date'])
return 1;
return 0;
});
//slice
new_commits = new_commits.slice(0, Math.min(commit_count, data.length / 3));
//get newest date and add elements
var now = new Date();
for (var i = new_commits.length -1; i >= 0; i--) {
//fuck off negative commits
if(new_commits[i]['date'] < now && new_commits[i]['date'] > newest_date){
newest_date = new_commits[i]['date'];
}
if(typeof new_commits[i] != "undefined"){
$('#commits').prepend('<div class="commit-wrapper"></div>');
var sel = $('#commits > div:first-child');
print_commit(sel, new_commits[i]);
}
};
//cleanup
$('#commits > div:gt('+commit_count+')').remove();
//update array
cur_commits = new_commits.concat(cur_commits);
cur_commits.slice(0, commit_count);
new_commits = [];
//click to select commit
$(".commit-wrapper").off();
$(".commit-wrapper").click(function(){
show_diff($(this).index());
});
//show first commit in list
commit_slideshow_i = 0;
show_diff(0);
});
}
//show a diff patch in the detail window
function show_diff(i){
if(cur_commits.length == 0)
return;
var total_length = 0;
var diffs = $('#diff-view');
var diff_i = 0;
diffs.html('');
for (var j = 0; j < cur_commits[i]['files'].length; j++) {
if(cur_commits[i]['files'][j]['additions'] == 0 && cur_commits[i]['files'][j]['deletions'] == 0){
continue;
}
if(total_length > 100){
break;
}
//get patch
var html = cur_commits[i]['files'][j]['patch'];
if(typeof html == "undefined"){
html = cur_commits[i]['files'][j]['status'];
}
var newlines = html.split("\n").length;
total_length += newlines;
//add the patch block
diffs.append('<div class="diff"><p class="title"></p><pre><code class="diff"></code></pre></div>');
//select it
var sel = $('#diff-view .diff:nth-child('+(diff_i+1)+') pre code');
$('#diff-view .diff:nth-child('+(diff_i+1)+') .title').html(cur_commits[i]['files'][j]['filename']);
//replace < and > with < and >
html = html.replace(/</g, '<');
html = html.replace(/>/g, '>');
sel.html(html);
sel.each(function(i, block){
hljs.highlightBlock(block);
//_highlight_diff(block);
});
diff_i++;
};
//add selected class
$("#commits > div").removeClass('selected');
$("#commits > div:nth-child("+(i+1)+")").addClass('selected');
}
function _highlight_diff(block){
var html = $(block).html();
//highlight deletions / additions
html = html.replace(/\n(-.*)/g, '\n<span class="hljs-deletion">$1</span>');
html = html.replace(/\n(\+.*)/g, '\n<span class="hljs-addition">$1</span>');
$(block).html(html);
}
//update commit times
function update_times(){
var now = new Date();
$('#commits > div').each(function(i){
if(typeof cur_commits[i] != "undefined"){
var sel = $("#commits > div:nth-child("+(i+1)+") .time");
var date = new Date(cur_commits[i]['commit']['author']['date']);
sel.html(print_time(now, date));
}
});
}
setInterval(update_times, 1000);
//slideshow of commits
var commit_slideshow;
var commit_slideshow_i = 0;
setInterval(function(){
show_diff(commit_slideshow_i++);
if(commit_slideshow_i >= commit_count || commit_slideshow_i >= cur_commits.length){
commit_slideshow_i = 0;
}
}, 5000);
//create commit in commit-wrapper element
function print_commit(sel, commit){
var html = "";
var insert_o = Math.min(0.4 + Math.sqrt(commit['stats']['additions'])/20, 1.0);
var delete_o = Math.min(0.4 + Math.sqrt(commit['stats']['deletions'])/20, 1.0);
if(commit['author'] == null){
commit['author'] = {
avatar_url: 'blacktocat.png',
login: commit['commit']['author']['name']
};
}
var message = commit['commit']['message'];
message = message.replace(/</g, "<");
message = message.replace(/>/g, ">");
html += '<div class="commit"> \
<img src="'+commit['author']['avatar_url']+'"> \
<div class="username">'+commit['author']['login']+'</div> \
<div class="stats"> \
<div class="insert-count" style="opacity:'+insert_o+'">'+commit['stats']['additions']+'+</div> \
<div class="delete-count" style="opacity:'+delete_o+'">'+commit['stats']['deletions']+'-</div> \
</div> \
<div class="message">'+message+'</div> \
<div class="time"></div> \
</div>';
sel.html(html);
}
//format a time difference
function print_time(now, date){
var sec = parseInt((now.getTime() - date.getTime()) / 1000);
if(sec < 0)
return 'The Future';
if(sec < 60) //show seconds
return sec + "s";
var min = parseInt(sec/60);
if(min < 60) //show minutes
return parseInt(min)+"m "+parseInt(sec%60)+"s";
var hr = parseInt(min/60);
if(hr < 24) //show hours
return parseInt(hr)+"h "+parseInt(min%60)+"m";
//show days
return parseInt(hr/24)+"d "+parseInt(hr%24)+"h";
}