-
Notifications
You must be signed in to change notification settings - Fork 37
/
enterprise.js
254 lines (214 loc) · 8.22 KB
/
enterprise.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
247
248
249
250
251
252
253
254
var isGitHub = $("meta[property='og:site_name']").attr('content') === 'GitHub';
var useLocalStorage = true;
var lsNamespace = 'ppr'; // Prepend to entries in localStorage for some namespacing to make deletion easier.
var pullRequestNumber;
var commitHash;
var repositoryName;
var repositoryAuthor;
var autoCollapseExpressions;
function onFilesPage() {
return window.location.href.indexOf('files') !== -1;
}
function htmlIsInjected() {
return $('.pretty-pull-requests-inserted').length > 0;
}
function injectHtml() {
if (!onFilesPage()) return;
$('<span class="pretty-pull-requests collapse-lines">' +
'<label><input type="checkbox" class="js-collapse-additions" checked="yes">+</label>' +
'<label><input type="checkbox" class="js-collapse-deletions" checked="yes">-</label>' +
'</span>').insertAfter('.actions, .file-actions');
$('<div class="pretty-pull-requests bottom-collapse">Click to Collapse</div>').insertAfter('.data.highlight.blob-wrapper');
$('<div class="pretty-pull-requests-inserted" style="display: none"></div>').appendTo('body');
}
function collapseAdditions() {
if (isGitHub) {
$(this).closest('[id^=diff-]').find('.blob-code-addition').parent('tr').slideToggle();
} else {
$(this).closest('[id^=diff-]').find('.gi').slideToggle();
}
}
function collapseDeletions() {
if (isGitHub) {
$(this).closest('[id^=diff-]').find('.blob-code-deletion').parent('tr').slideToggle();
} else {
$(this).closest('[id^=diff-]').find('.gd').slideToggle();
}
}
function getDiffSpans(path) {
return $('.user-select-contain, .js-selectable-text').filter(function () {
return this.innerHTML.trim().match(path);
});
}
function getIds(path) {
var $spans = getDiffSpans(path).closest('[id^=diff-]');
var $as = $spans.prev('a[name^=diff-]');
var $ids = $as.map(function(index, a) {
return $(a).attr('name');
});
return $ids;
}
function getId(path) {
var $span = $('a[title="' + path + '"]').closest('[id^=diff-]');
var $a = $span.prev('a[name^=diff-]');
var id = $a.attr('name');
return id;
}
function uniquify(diffId) {
var diffViewId = pullRequestNumber || commitHash;
return lsNamespace + '|' + repositoryAuthor + '|' + repositoryName + '|' + diffViewId + '|' + diffId;
}
function collectUniquePageInfo() {
repositoryAuthor = $('[itemprop="author"]').find('a').text();
repositoryName = $('strong[itemprop="name"]').find('a').text();
pullRequestNumber = $('.gh-header-number').text();
commitHash = $('.sha.user-select-contain').text();
}
function toggleDiff(id, duration, display) {
var $a = $('a[name^=' + id + ']');
duration = !isNaN(duration) ? duration : 200;
if ($.inArray(display, ['expand', 'collapse', 'toggle']) < 0) {
if (!useLocalStorage) {
display = 'toggle';
} else {
display = (localStorage.getItem(uniquify(id)) === 'collapse') ? 'expand' : 'collapse';
}
}
if ($a) {
var $span = $a.next('div[id^=diff-]');
var $data = $span.children('.js-file-content');
var $bottom = $span.children('.bottom-collapse');
switch (display) {
case 'toggle':
$data.toggle(duration);
$bottom.toggle();
return true;
case 'expand':
$data.slideDown(duration);
$bottom.show();
return useLocalStorage ? localStorage.removeItem(uniquify(id)) : true;
default:
$data.slideUp(duration);
$bottom.hide();
return useLocalStorage ? localStorage.setItem(uniquify(id), display) : true;
}
}
return false;
}
function toggleDiffs(path, display) {
var $ids = getIds(path);
$ids.each(function(index, id) {
toggleDiff(id, 200, display);
});
}
function moveToNextTab($pullRequestTabs, selectedTabIndex) {
selectedTabIndex += 1;
if (selectedTabIndex >= $pullRequestTabs.length) {
selectedTabIndex = 0;
}
$pullRequestTabs[selectedTabIndex].click();
}
function moveToPreviousTab($pullRequestTabs, selectedTabIndex) {
selectedTabIndex -= 1;
if (selectedTabIndex < 0) {
selectedTabIndex = $pullRequestTabs.length - 1;
}
$pullRequestTabs[selectedTabIndex].click();
}
function initDiffs() {
if (useLocalStorage) {
$('a[name^=diff-]').each(function(index, item) {
var id = $(item).attr('name');
if (localStorage.getItem(uniquify(id)) === 'collapse') {
toggleDiff(id, 0, 'collapse');
}
});
}
autoCollapse();
}
function clickTitle(e) {
e.preventDefault();
var path = $(this).attr('title') || this.innerText;
var id = getId(path);
return toggleDiff(id);
}
function clickCollapse() {
var $span = $(this).closest('.js-file-content').prev('.file-header');
var path = $span.attr('data-path');
var id = getId(path);
return toggleDiff(id, '200', 'collapse');
}
function autoCollapse() {
autoCollapseExpressions.forEach(item => {
toggleDiffs(item, 'collapse');
});
}
chrome.storage.sync.get({url: '', saveCollapsedDiffs: true, tabSwitchingEnabled: false, autoCollapseExpressions: []}, function(items) {
if (items.url === window.location.origin ||
"https://github.com" === window.location.origin) {
autoCollapseExpressions = items.autoCollapseExpressions;
var interval = null;
var injectHtmlIfNecessary = function () {
if (!htmlIsInjected()) {
if (onFilesPage()) {
collectUniquePageInfo();
injectHtml();
initDiffs();
$body.on('click', '.user-select-contain, .js-selectable-text, .file-info .link-gray-dark', clickTitle);
$body.on('click', '.bottom-collapse', clickCollapse);
$body.on('click', '.js-collapse-additions', collapseAdditions);
$body.on('click', '.js-collapse-deletions', collapseDeletions);
}
} else {
clearInterval(interval);
}
};
interval = setInterval(injectHtmlIfNecessary, 1000);
var $body = $('body');
useLocalStorage = items.saveCollapsedDiffs;
injectHtmlIfNecessary();
if (items.tabSwitchingEnabled) {
$body.on('keydown', function (e) {
if (e.keyCode !== 192 || e.target.nodeName === 'TEXTAREA') {
return;
}
var $pullRequestTabs = $('nav.tabnav-tabs a.tabnav-tab');
var $selectedTab = $('nav.tabnav-tabs a.tabnav-tab.selected');
var selectedTabIndex = $pullRequestTabs.index( $selectedTab );
if (e.shiftKey) {
// Making this work like it would in other apps, where the shift
// key makes the cmd+tilde go backwards through the list.
moveToPreviousTab($pullRequestTabs, selectedTabIndex);
} else {
moveToNextTab($pullRequestTabs, selectedTabIndex);
}
});
}
// Actions per changed file
chrome.runtime.onConnect.addListener(function (port) {
console.assert(port.name == "pullrequest");
port.onMessage.addListener(function (msg) {
if (msg.collapse !== undefined) {
toggleDiffs(msg.collapse, 'collapse');
}
if (msg.expand !== undefined) {
toggleDiffs(msg.expand, 'expand');
}
if (msg.goto !== undefined) {
getDiffSpans(msg.goto)[0].scrollIntoViewIfNeeded();
}
});
});
// Create the tree with the changed files after pressing the octocat button
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.getPaths) {
var paths = $.map($('.file .user-select-contain, .file .js-selectable-text'), function (item) {
return $.trim(item.innerHTML);
});
if (paths.length > 0) {
sendResponse({paths: paths});
}
}
});
}
});