Skip to content

Commit 25424f8

Browse files
author
Fabio Picheli
committed
refactor: make the code more readable
1 parent 2661ac1 commit 25424f8

File tree

5 files changed

+75
-111
lines changed

5 files changed

+75
-111
lines changed

app/background/icons.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/app.js renamed to app/background/index.js

Lines changed: 69 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,54 @@
1-
$(document).ready(start);
2-
31
var isCommit = false;
42

5-
$(window).scroll(() => {
6-
if (isCommit) return;
7-
if($('.is-stuck').length) {
8-
$('.gct-file-tree').addClass('gct-file-tree-fixed');
9-
}else {
10-
$('.gct-file-tree').removeClass('gct-file-tree-fixed');
11-
}
12-
});
3+
const open = () => $('.gct-folder').addClass('gct-folder-open');
4+
const close = () => $('.gct-folder').removeClass('gct-folder-open');
5+
136

14-
function start() {
15-
var oldLocation;
16-
setInterval(() => {
17-
if(!$('.gct-folder-name').length) {
18-
oldLocation = location.origin + location.pathname;
7+
const expandAllDiffBlocks = () => {
8+
$('#collapseAll').show();
9+
$('#expandAll').hide();
10+
$('#files .file').each(() => !$(this).hasClass('Details--on open') && $(this).addClass('Details--on open'));
11+
}
1912

20-
urlPullRegex = /(http|https):\/\/(www\.)?github\.com\/[-a-zA-Z0-9]*\/[-a-zA-Z0-9]*\/pull\/[0-9]*\/(files|commits)/;
21-
urlCommitRegex = /(http|https):\/\/(www\.)?github\.com\/[-a-zA-Z0-9]*\/[-a-zA-Z0-9]*\/commit/;
13+
const collapseAllDiffBlocks = () => {
14+
$('#collapseAll').hide();
15+
$('#expandAll').show();
16+
$('#files .file').each(() => $(this).hasClass('Details--on open') && $(this).removeClass('Details--on open'));
17+
}
2218

23-
isCommit = location.href.match(urlCommitRegex);
2419

25-
if(
26-
(location.href.match(urlPullRegex) || location.href.match(urlCommitRegex)) // show only on PR files page
27-
) {
28-
chrome.storage.sync.get(['closed', 'collapsed', 'folders'], items => initialSetup(items));
29-
}
20+
const injectHTML = (savedItems) => $(
21+
`<div class="gct-file-tree">
22+
<div class="gct-header">
23+
<div id="openAll">Open All</div>
24+
<div id="closeAll">Close All</div>
25+
<div id="expandAll">Expand All</div>
26+
<div id="collapseAll">Collapse All</div>
27+
</div>
28+
${buildHtmlTree(buildTree(savedItems))}
29+
</div>`
30+
).appendTo('#files');
31+
32+
const mergeObjects = (og, so) => {
33+
for (var key in so) {
34+
if (!og[key]) {
35+
og[key] = {};
3036
}
31-
}, 500);
37+
38+
if (so[key].hasOwnProperty('length')) {
39+
og[key] = og[key].hasOwnProperty('length') ? og[key] : [];
40+
og[key].push(so[key][0]);
41+
}
42+
43+
if(typeof so[key] === 'object' && !so[key].hasOwnProperty('length')) {
44+
mergeObjects(og[key], so[key]);
45+
}
46+
}
47+
return og;
3248
}
3349

34-
function initialSetup(savedItems) {
50+
51+
const initialSetup = (savedItems) => {
3552
if ($('.js-diff-progressive-spinner').length || !$('#files').length) {
3653
return;
3754
}
@@ -62,22 +79,31 @@ function initialSetup(savedItems) {
6279
$('#collapseAll').click(() => collapseAllDiffBlocks());
6380
}
6481

65-
function injectHTML(savedItems) {
66-
tree = buildTree(savedItems);
67-
$(
68-
`<div class="gct-file-tree">
69-
<div class="gct-header">
70-
<div id="openAll">Open All</div>
71-
<div id="closeAll">Close All</div>
72-
<div id="expandAll">Expand All</div>
73-
<div id="collapseAll">Collapse All</div>
74-
</div>
75-
${buildHtmlTree(tree)}
76-
</div>`
77-
).appendTo('#files');
78-
}
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+
91+
const start = () => setInterval(() => {
92+
if(!$('.gct-folder-name').length) {
93+
urlPullRegex = /(http|https):\/\/(www\.)?github\.com\/[-a-zA-Z0-9]*\/[-a-zA-Z0-9]*\/pull\/[0-9]*\/(files|commits)/;
94+
urlCommitRegex = /(http|https):\/\/(www\.)?github\.com\/[-a-zA-Z0-9]*\/[-a-zA-Z0-9]*\/commit/;
7995

80-
function buildHtmlTree(tree) {
96+
isCommit = location.href.match(urlCommitRegex);
97+
98+
if(
99+
(location.href.match(urlPullRegex) || location.href.match(urlCommitRegex)) // show only on PR files page
100+
) {
101+
chrome.storage.sync.get(['closed', 'collapsed', 'folders'], items => initialSetup(items));
102+
}
103+
}
104+
}, 500);
105+
106+
const buildHtmlTree = (tree) => {
81107
var content = '<ul>';
82108

83109
let unorderedList = [];
@@ -119,7 +145,7 @@ function buildHtmlTree(tree) {
119145
return content;
120146
}
121147

122-
function buildTree(savedItems) {
148+
const buildTree = (savedItems) => {
123149
var tree = {};
124150

125151
$('.file-info').map((i, item) => {
@@ -169,7 +195,7 @@ function buildTree(savedItems) {
169195
return tree;
170196
}
171197

172-
function joinEmptyFolders(obj, paths) {
198+
const joinEmptyFolders = (obj, paths) => {
173199
let current = obj;
174200
paths.map(path => current = current[path]);
175201

@@ -211,61 +237,4 @@ function joinEmptyFolders(obj, paths) {
211237
return current;
212238
}
213239

214-
function mergeObjects(og, so) {
215-
for (var key in so) {
216-
if (!og[key]) {
217-
og[key] = {};
218-
}
219-
220-
if (so[key].hasOwnProperty('length')) {
221-
og[key] = og[key].hasOwnProperty('length') ? og[key] : [];
222-
og[key].push(so[key][0]);
223-
}
224-
225-
if(typeof so[key] === 'object' && !so[key].hasOwnProperty('length')) {
226-
mergeObjects(og[key], so[key]);
227-
}
228-
}
229-
return og;
230-
}
231-
232-
function areDiffBlocksCollapsed() {
233-
var numberOfDiffBlocksCollapsed = 0;
234-
var numberOfDiffBlocks = $('#files .file').length;
235-
236-
$('#files .file').each(function(){
237-
if ($(this).hasClass('Details--on open')) {
238-
numberOfDiffBlocksCollapsed++;
239-
}
240-
});
241-
242-
return numberOfDiffBlocksCollapsed === numberOfDiffBlocks;
243-
}
244-
245-
function expandAllDiffBlocks() {
246-
$('#collapseAll').show();
247-
$('#expandAll').hide();
248-
$('#files .file').each(function(){
249-
if (!$(this).hasClass('Details--on open')) {
250-
$(this).addClass('Details--on open')
251-
}
252-
});
253-
}
254-
255-
function collapseAllDiffBlocks() {
256-
$('#collapseAll').hide();
257-
$('#expandAll').show();
258-
$('#files .file').each(function(){
259-
if ($(this).hasClass('Details--on open')) {
260-
$(this).removeClass('Details--on open')
261-
}
262-
});
263-
}
264-
265-
function open() {
266-
$('.gct-folder').addClass('gct-folder-open');
267-
}
268-
269-
function close() {
270-
$('.gct-folder').removeClass('gct-folder-open');
271-
}
240+
$(document).ready(start);

app/style.js renamed to app/background/style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function injectCss(offsetTop, offsetLeft) {
1+
const injectCss = (offsetTop, offsetLeft, isCommit) => {
22
$('#gct-style').remove();
33
$(`<style type='text/css' id="gct-style">
44
body.full-width #files {

app/icons.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

manifest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
],
2424
"js": [
2525
"lib/jquery-1.11.1.min.js",
26-
"app/icons.js",
27-
"app/style.js",
28-
"app/app.js"
26+
"app/background/icons.js",
27+
"app/background/style.js",
28+
"app/background/index.js"
2929
]
3030
}
3131
]

0 commit comments

Comments
 (0)