-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
374 lines (288 loc) · 11.2 KB
/
content.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* Globals: configuration, notifications */
;(function () {
'use strict'
var config = {}
var urlObserver = new window.MutationObserver(function(mutations, observer) {
setTimeout(main)
})
urlObserver.observe(document.getElementById('js-pjax-loader-bar'), {
attributes: true,
attributeFilter: ['class']
})
function main() {
if (document.querySelector('.diff-progressive-loader')) {
setTimeout(main, 100)
} else {
for(var prop in config) {
if (config[prop] && features[prop]) {
features[prop]()
}
}
}
}
configuration.get(function (items) {
config = items
main()
})
insertStyles()
// -----------------------------------------------------------------------------
// Features
var features = {
showOutdatedComments: function() {
var outdatedDiffs = document.getElementsByClassName('outdated-diff-comment-container')
for(var i = 0; i < outdatedDiffs.length; i++) {
outdatedDiffs[i].classList.add('open')
}
},
highlightOutdatedDiffIcons: function() {
var octicons = document.querySelectorAll('.discussion-timeline .discussion-item-icon .octicon-x')
for(var i = 0; i < octicons.length; i++) {
var discussionItemIcon = octicons[i].parentElement
discussionItemIcon.style.backgroundColor = '#ffefc6'
discussionItemIcon.style.color = '#4c4a42'
}
},
showCurrentDiffFileName: function() {
var prtoolbar = document.querySelector('.pr-toolbar.js-sticky')
if (! prtoolbar) return
var diffbar = prtoolbar.querySelector('.diffbar')
var headers = document.getElementsByClassName('file-header')
var blobs = document.getElementsByClassName('blob-wrapper')
var diffbarItem = document.getElementById('__ghcape-current-file')
if (! diffbarItem) {
diffbarItem = createDiffItem()
diffbar.insertBefore(diffbarItem, diffbar.querySelector('.diffbar-item.diffstat').nextElementSibling)
}
document.addEventListener('scroll', onScroll, false)
onScroll()
function onScroll() {
var index = firstIndexInViewport(blobs)
var currentHeader = headers[index]
var toolbarPosition = prtoolbar.style.position
diffbarItem.style.display = toolbarPosition === 'fixed' || toolbarPosition === 'sticky' ? 'block' : 'none'
if (currentHeader) {
diffbarItem.setAttribute('aria-label', currentHeader.dataset.path)
diffbarItem.firstChild.innerHTML = currentHeader.dataset.path.split('/').slice(-1)
}
}
function createDiffItem() {
var diffbarItem = document.createElement('div')
var path = document.createElement('div')
diffbarItem.id = '__ghcape-current-file'
diffbarItem.className = 'diffbar-item'
diffbarItem.classList.add('tooltipped', 'tooltipped-s')
path.style.marginRight = '0'
path.style.whiteSpace = 'nowrap'
path.style.textOverflow = 'ellipsis'
path.style.overflow = 'hidden'
if (! inSplitView() && viewingSubsetChanges()) {
path.style.maxWidth = '65px'
} else {
path.style.maxWidth = config.showHideAllButtons ? '160px' : '290px'
}
diffbarItem.appendChild(path)
return diffbarItem
}
},
collapsableDiffs: function() {
makeCollapsable({
trigger: 'file-header',
targets: ['file-header', 'file-info'],
toggleableSibling: 'js-file-content'
})
},
showHideAllButtons: function() {
var actions = document.querySelector('.pr-toolbar.js-sticky .toc-select ~ .float-right') // Ugh
if (actions && actions.getElementsByClassName('__ghcape-show-hide-all').length === 0) {
var headers = Array.prototype.slice.call(document.getElementsByClassName('file-header'))
var className = 'btn-link muted-link __ghcape-show-hide-all'
if(! inSplitView() && viewingSubsetChanges()){
className += ' __ghcape-hidden'
}
var showAll = document.createElement('button')
showAll.innerHTML = 'Show all'
showAll.className = className
showAll.onclick = function() { changeHadersVisibillity('remove') }
var hideAll = document.createElement('button')
hideAll.innerHTML = 'Hide all'
hideAll.className = className
hideAll.onclick = function() { changeHadersVisibillity('add') } // This will potentially break the filename on the sticky header
actions.appendChild(showAll)
actions.appendChild(hideAll)
}
function changeHadersVisibillity(method) {
headers.forEach(function(header) {
var code = nextByClass(header, 'js-file-content')
if (code) code.classList[method]('__ghcape-hidden')
})
}
},
collapsableCommits: function() {
makeCollapsable({
trigger: 'commit-group-title',
targets: ['commit-group-title', 'octicon-git-commit'],
toggleableSibling: 'commit-group'
})
},
toggleContributions: function() {
var actionClasses = [
{ trigger: '.text-green', code: 'blob-code-addition' },
{ trigger: '.text-red', code: 'blob-code-deletion' }
]
actionClasses.forEach(function(classes) {
var trigger = document.querySelector('.diffbar-item.diffstat > ' + classes.trigger)
if (! trigger) return
var tooltipText = { 'Hide': 'Show', 'Show': 'Hide' }
trigger.style.cursor = 'pointer'
trigger.classList.add('tooltipped', 'tooltipped-s')
trigger.setAttribute('aria-label', 'Hide')
trigger.addEventListener('click', function() {
var code = document.getElementsByClassName('blob-code ' + classes.code)
var newTooltipText = tooltipText[trigger.getAttribute('aria-label')]
trigger.setAttribute('aria-label', 'Loading')
setTimeout(function () {
for(var i = 0; i < code.length; i++) {
code[i].parentNode.classList.toggle('__ghcape-hidden')
}
trigger.setAttribute('aria-label', newTooltipText)
})
}, true)
})
},
resizeableSplittedDiffs: function() {
if(! inSplitView()) return
var datas = Array.prototype.slice.call(document.querySelectorAll('.data:not(.__ghcape-draggable)'))
var codeWidth = null
var draggableBarWidth = null
datas.forEach(function(data) {
var table = data.firstElementChild
if (! table) return
var code = findFirstTd(table)
if(! draggableBarWidth) {
var tr = table.querySelector('tr:not(.js-expandable-line)')
draggableBarWidth = (tr.clientWidth / 2) + 'px'
}
if(! codeWidth) {
codeWidth = getComputedStyle(code, null).getPropertyValue('width')
codeWidth = parseInt(codeWidth, 10) + 'px'
}
var draggableBar = document.createElement('div')
draggableBar.className = '__ghcape-draggable-bar'
draggableBar.style.top = 0
draggableBar.style.left = draggableBarWidth
code.style.width = codeWidth
data.classList.add('__ghcape-draggable')
data.appendChild(draggableBar)
setTimeout(function() { attachEvents(draggableBar) }, 0)
})
function attachEvents(draggableBar) {
var table = draggableBar.previousElementSibling
var maxWidth = 970
var minWidth = 370
var offset = 0
var mouseX = 0
var td = null
var move = function(event) {
td = td || findFirstTd(table)
var direction = mouseX === 0 ? 1 : (event.pageX - mouseX)
var width = (parseInt(td.style.width, 10) + direction)
if (width >= minWidth && width < maxWidth) {
td.style.width = width + 'px'
draggableBar.style.left = (event.clientX - offset) + 'px'
mouseX = event.pageX
}
}
draggableBar.addEventListener('mousedown', function(e) {
offset = e.clientX - parseInt(draggableBar.style.left, 10)
table.parentElement.style.userSelect = 'none'
document.addEventListener('mousemove', move, false)
}, false)
document.addEventListener('mouseup', function() {
table.parentElement.style.userSelect = 'inherit'
document.removeEventListener('mousemove', move, false)
}, false)
}
function findFirstTd(table) {
return table.querySelector('.blob-num:not(.blob-num-expandable):not(.blob-num-hunk) ~ td')
}
},
notifications: function() {
notifications.load()
}
}
// -----------------------------------------------------------------------------
// Utils
function makeCollapsable(classes) {
var triggers = document.getElementsByClassName(classes.trigger)
var targets = classes.targets || [ classes.trigger ]
for(var i = 0; i < triggers.length; i++) {
triggers[i].addEventListener('click', togglePanel)
triggers[i].style.cursor = 'pointer'
}
function togglePanel(event) {
if (! containsAnyClass(event.target, targets)) return
var code = nextByClass(this, classes.toggleableSibling)
if (code) {
code.classList.toggle('__ghcape-hidden')
}
}
}
function prevByClass(node, className) {
return findSibling(node, 'previous', className)
}
function nextByClass(node, className) {
return findSibling(node, 'next', className)
}
function findSibling(node, direction, className) {
while (node = node[direction + 'Sibling']) {
if (node.classList && node.classList.contains(className)) {
return node
}
}
}
function containsAnyClass(el, classes) {
for(var i = 0; i < classes.length; i++) {
if (el.classList.contains(classes[i])) {
return true
}
}
return false
}
function firstIndexInViewport(els) {
for(var i = 0; i < els.length; i++) {
if (inViewport(els[i])) {
return i
}
}
}
function inViewport(el) {
var rect = el.getBoundingClientRect()
var windowHeight = window.innerHeight || document.documentElement.clientHeight
return rect.height && rect.top <= windowHeight && (rect.top + rect.height) >= 0
}
function inSplitView() {
return !! document.querySelector('.BtnGroup [aria-label="Viewing diff in split mode"]')
}
function viewingSubsetChanges() {
return document.getElementsByClassName('subset-files-tab').length
}
function insertStyles() {
var style = document.createElement('style')
var styles = [
'.__ghcape-hidden { display: none !important; }',
'.__ghcape-show-hide-all { margin-left: 8px; line-height: 28px; }',
'.__ghcape-draggable { position: relative; }',
'.__ghcape-draggable .diff-table.file-diff-split { table-layout: auto; }',
'.__ghcape-draggable td.blob-num { width: inherit; min-width: inherit; }',
'.__ghcape-draggable-bar {',
'height: 100%; width: 8px;',
'top: 0; position: absolute;',
'border-left: 2px solid #d8d8d8; border-right: 2px solid #d8d8d8;',
'background: #F7F7F7;',
'cursor: col-resize;',
'}'
]
style.innerHTML = styles.join(' ')
document.body.appendChild(style)
}
})()