-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.tamper.js
246 lines (242 loc) · 9.43 KB
/
files.tamper.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
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
// ==UserScript==
// @name GitHub Files View Enhancement
// @namespace jpi
// @version 0.8
// @description
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @match https://github.etsycorp.com/*/pull/*
// @match https://github.etsycorp.com/*/commit/*
// @match https://github.etsycorp.com/*/compare/*
// @match https://*github.com/*/pull/*
// @match https://*github.com/*/commit/*
// @match https://*github.com/*/compare/*
// @copyright 2012
// ==/UserScript==
(function($) {
var filelist = new Array(),
diff_data = new Array(),
viewed_comments = {
comments: new Array(),
counts: new Array()
},
current_index = 0;
files_url_regex = /(files|commit[^s]|compare)/,
created_reload_container = false,
reload_container_style = {
'position': 'fixed',
'bottom' : '5px',
'right' : '5px',
'padding' : '10px',
'border' : '1px solid black',
'width' : '300px',
'display' : 'none',
'z-index' : '100',
'background-color' : 'white'
};
// constants
var POLLING_TIME = 5000;
function parseFiles() {
$('#toc li a').filter(function () {
return ! $(this).parent().hasClass('diffstat');
}).each(function (index) {
filelist[index] = {
href: $(this).attr('href'),
text: $(this).html(),
elem: $(this)
};
});
}
function moveFiles() {
$('#toc').wrapInner('<div style="position:fixed; width: 38%; top: 60px; left: 0px; z-index: 100; height: 90%; overflow: scroll;" />');
$('#toc').addClass('open');
$('p .minibutton.js-details-target').hide();
}
function adjustPageMargins() {
if (String(window.location).match(files_url_regex)) {
$('.container').css('margin-right', '0px');
} else {
$('.container').css('margin-right', 'auto');
}
var sheet = document.styleSheets[0];
var rules = sheet.cssRules;
sheet.insertRule('.line-comments .clipper { width: 99% }', rules.length);
}
function adjustPageMarginsClick(event) {
if ($(event.target).data('container-id') == 'files_bucket') {
$('.container').css('margin-right', '0px');
} else {
$('.container').css('margin-right', 'auto');
}
var sheet = document.styleSheets[0];
var rules = sheet.cssRules;
sheet.insertRule('.line-comments .clipper { width: 99% }', rules.length);
}
function showFile(index) {
var old_file_link = $("a[href=" + filelist[current_index].href + "]"),
new_file_link = $("a[href=" + filelist[index].href + "]"),
old_file = $(filelist[current_index].href),
new_file = $(filelist[index].href);
old_file.hide();
old_file_link.css('font-weight', 'normal');
new_file.show();
new_file_link.css('font-weight', 'bold');
current_index = index;
window.scrollTo(0, new_file.offset().top);
}
function iterateFiles(func) {
$.each(filelist, function (index, value) {
func(index, value);
});
}
function hideAllFiles() {
iterateFiles(function (index, value) {
$(value.href).hide();
});
}
function updateCommentCount() {
iterateFiles(function (index, value) {
var comment_count = $(value.href + " .commit-comment-header").length;
if (comment_count > 0) {
value.elem.html(
value.text.replace(/\//,'/­') + " (" + getReadCommentCount(index) + '/' + comment_count + ")"
);
}
});
}
function getReadCommentCount(index) {
initReadComment(index);
return viewed_comments.counts[index];
}
function initReadComment(index) {
if (! viewed_comments.counts[index]) {
viewed_comments.counts[index] = 0;
}
if (! viewed_comments.comments[index]) {
viewed_comments.comments[index] = new Array();
}
}
function setReadComment(index, comment_id) {
initReadComment(index);
if (! viewed_comments.comments[index][comment_id]) {
viewed_comments.comments[index][comment_id] = true;
viewed_comments.counts[index]++;
}
}
function updateReadCommentCounts() {
var docViewTop = $(window).scrollTop(),
docViewBottom = docViewTop + $(window).height();
iterateFiles(function (index, value) {
$(value.href + ":visible div.js-comment.comment").filter(function () {
var thisTop = $(this).offset().top,
thisBottom = thisTop + $(this).height();
return (
thisTop >= docViewTop &&
thisBottom <= docViewBottom
);
}).each(function () {
var comment_id = parseInt($(this).attr('id').substr(1));
if (comment_id > 0) {
setReadComment(index, comment_id);
}
});
});
updateCommentCount();
}
function renderFilesMod() {
adjustPageMargins();
moveFiles();
hideAllFiles();
showFile(current_index);
updateReadCommentCounts();
}
function attachEvents() {
iterateFiles(function (index, value) {
value.elem.click(function () {
showFile(index);
return false;
});
});
$('ul.js-hard-tabs li a').click(adjustPageMarginsClick);
$(window).scroll(updateReadCommentCounts);
setTimeout(pollForNewComments, POLLING_TIME);
console.log('Set polling time');
}
function pollForNewComments() {
console.log('Polling...');
$.ajax({
url: window.location,
success: function(data) {
var $data = $(data),
comment_count = $('table.diff-table .commit-comment-header').length,
new_comment_count = $data.find('div#diff-comment-data .commit-comment-header').length;
if (comment_count != new_comment_count) {
console.log('found new comments' + comment_count + ' ' + new_comment_count);
storeDiffData($data);
alertForReload(new_comment_count - comment_count);
}
setTimeout(pollForNewComments, POLLING_TIME);
},
error: function() {
setTimeout(pollForNewComments, POLLING_TIME);
}
});
}
function alertForReload(new_comment_count) {
prepareReloadContainer();
var reload_container = $('#reload');
reload_container.html('Comments have been updated.<br/>');
reload_container.append('<a href="#" id="reload-link">Reload</a>');
$('#reload-link').click(reloadDiffData);
$('#reload').css('display', 'block');
}
function prepareReloadContainer() {
if (!created_reload_container) {
$('body').append('<div id="reload"></div>');
var reload_container = $('#reload');
reload_container.css(reload_container_style);
created_reload_container = true;
}
}
function storeDiffData(elem) {
diff_data = new Array();
var diff_comment_data = elem.find("#diff-comment-data");
$('#diff-comment-data').html(diff_comment_data.html());
elem.find('div[id^=diff-]').each(function (index) {
diff_data[index] = {
id: $(this).attr('id'),
html: $(this).html(),
};
});
}
function reloadDiffData() {
$.each(diff_data, function (index, value) {
$('#' + value.id).html(value.html);
});
// This next blip is copied from github since they didn't functionalize it.
$(function() {
var a;
if (!document.getElementById("diff-comment-data"))
return;
return a = {}, $("#files.diff-view > .file > .meta").each(function() {
return a[$(this).attr("data-path")] = this
}), $("#diff-comment-data > table").each(function() {
var b, c, d, e;
return c = $(this).attr("data-path"), d = $(this).attr("data-position"), b = $(a[c]).closest(".file"), e = b.find(".data table tr[data-position='" + d + "']"), e.after($(this).find("tr").detach()), b.addClass("has-inline-notes show-inline-notes")
}), $("#diff-comment-data > div").each(function() {
var b;
return b = $(this).attr("data-path"), $(a[b]).closest(".file").find(".file-comments-place-holder").replaceWith($(this).detach())
})
}), $(document).on("change", ".js-show-inline-comments-toggle", function() {
return $(this).closest(".file").toggleClass("show-inline-notes", this.checked)
}), $(document).on("change", "#js-inline-comments-toggle", function() {
return $("#comments").toggleClass("only-commit-comments", !this.checked)
});
// end copy from github source
// TODO: Find a better way to do this
updateReadCommentCounts();
$('#reload').css('display', 'none');
}
parseFiles();
renderFilesMod();
attachEvents();
})(jQuery);