Skip to content

Commit 03c27a2

Browse files
author
Fabio Picheli
committed
fix: position of fixed tree
1 parent 25424f8 commit 03c27a2

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

app/background/index.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ var isCommit = false;
33
const open = () => $('.gct-folder').addClass('gct-folder-open');
44
const close = () => $('.gct-folder').removeClass('gct-folder-open');
55

6+
const isDiffOpen = (el) => $(el).hasClass('Details--on') && $(el).hasClass('open');
7+
8+
const openDiff = (el) => $(el).addClass('Details--on open');
9+
const closeDiff = (el) => $(el).removeClass('Details--on open');
610

711
const expandAllDiffBlocks = () => {
812
$('#collapseAll').show();
913
$('#expandAll').hide();
10-
$('#files .file').each(() => !$(this).hasClass('Details--on open') && $(this).addClass('Details--on open'));
14+
$('#files .file').each((i, el) => openDiff(el));
1115
}
1216

1317
const collapseAllDiffBlocks = () => {
1418
$('#collapseAll').hide();
1519
$('#expandAll').show();
16-
$('#files .file').each(() => $(this).hasClass('Details--on open') && $(this).removeClass('Details--on open'));
20+
$('#files .file').each((i, el) => closeDiff(el));
1721
}
1822

19-
2023
const injectHTML = (savedItems) => $(
2124
`<div class="gct-file-tree">
2225
<div class="gct-header">
@@ -48,28 +51,43 @@ const mergeObjects = (og, so) => {
4851
}
4952

5053

51-
const initialSetup = (savedItems) => {
54+
const init = (savedItems) => {
5255
if ($('.js-diff-progressive-spinner').length || !$('#files').length) {
5356
return;
5457
}
5558

56-
injectCss(isCommit ? 0 : 178, isCommit ? 20 : 0); // style.js
59+
injectCss(isCommit); // style.js
5760
injectHTML(savedItems);
5861

5962
savedItems.collapsed ? collapseAllDiffBlocks() : expandAllDiffBlocks();
6063
savedItems.closed ? close() : open();
6164

65+
$(window).scroll(() => {
66+
let topOffset;
67+
68+
if (isCommit) {
69+
const { top } = $(`.toc-diff-stats`).offset();
70+
topOffset = top + 50;
71+
} else {
72+
const { top } = $(`.tabnav-tabs`).offset();
73+
topOffset = top + 120;
74+
}
75+
76+
window.pageYOffset > topOffset
77+
? $('.gct-file-tree').addClass('gct-file-tree-fixed')
78+
: $('.gct-file-tree').removeClass('gct-file-tree-fixed');
79+
});
80+
6281
// Click Functions
6382
$('.gct-folder-name').click(obj => {
6483
$($($(obj.currentTarget).parent()[0])[0]).toggleClass('gct-folder-open');
6584
});
6685

86+
// On file name click
6787
$('.gct-file-name').click(obj => {
6888
var href = $(obj.currentTarget)[0].getAttribute("href");
6989
var file = $(`.file-info > a[href="${href}"]`).parent().parent().parent();
70-
if ($(file).hasClass('Details--on open')) {
71-
$(file).removeClass('Details--on open');
72-
}
90+
openDiff(file);
7391
});
7492

7593
$('#openAll').click(() => open());
@@ -79,17 +97,8 @@ const initialSetup = (savedItems) => {
7997
$('#collapseAll').click(() => collapseAllDiffBlocks());
8098
}
8199

82-
$(window).scroll(() => {
83-
if (isCommit) return;
84-
if($('.is-stuck').length) {
85-
$('.gct-file-tree').addClass('gct-file-tree-fixed');
86-
}else {
87-
$('.gct-file-tree').removeClass('gct-file-tree-fixed');
88-
}
89-
});
90-
91100
const start = () => setInterval(() => {
92-
if(!$('.gct-folder-name').length) {
101+
if(!$('.gct-file-tree').length) {
93102
urlPullRegex = /(http|https):\/\/(www\.)?github\.com\/[-a-zA-Z0-9]*\/[-a-zA-Z0-9]*\/pull\/[0-9]*\/(files|commits)/;
94103
urlCommitRegex = /(http|https):\/\/(www\.)?github\.com\/[-a-zA-Z0-9]*\/[-a-zA-Z0-9]*\/commit/;
95104

@@ -98,7 +107,7 @@ const start = () => setInterval(() => {
98107
if(
99108
(location.href.match(urlPullRegex) || location.href.match(urlCommitRegex)) // show only on PR files page
100109
) {
101-
chrome.storage.sync.get(['closed', 'collapsed', 'folders'], items => initialSetup(items));
110+
chrome.storage.sync.get(['closed', 'collapsed', 'folders'], items => init(items));
102111
}
103112
}
104113
}, 500);

app/background/style.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const injectCss = (offsetTop, offsetLeft, isCommit) => {
1+
const injectCss = (isCommit) => {
22
$('#gct-style').remove();
33
$(`<style type='text/css' id="gct-style">
44
body.full-width #files {
@@ -17,9 +17,9 @@ const injectCss = (offsetTop, offsetLeft, isCommit) => {
1717
1818
.gct-file-tree {
1919
position: absolute;
20-
top: ${$('#files').offset().top - offsetTop}px;
20+
top: ${$('#files').offset().top - (isCommit ? 0 : 178)}px;
2121
background-color: transparent;
22-
left: ${offsetLeft}px;
22+
left: ${isCommit ? 20 : 0}px;
2323
z-index: 28;
2424
width: 280px;
2525
border: 1px solid #ddd;
@@ -77,9 +77,9 @@ const injectCss = (offsetTop, offsetLeft, isCommit) => {
7777
body:not(.full-width) .gct-file-tree,
7878
.gct-file-tree-fixed {
7979
position: fixed;
80-
top: 70px;
80+
top: ${isCommit ? 20 : 70}px;
8181
left: 20px;
82-
height: calc(100% - 80px);
82+
height: calc(100% - ${isCommit ? 40 : 80}px);
8383
}
8484
</style>`)
8585
.appendTo("head");

0 commit comments

Comments
 (0)