Skip to content

Commit 2eb363e

Browse files
author
Fabio Picheli
committed
feat: add settings page
1 parent 50f1a35 commit 2eb363e

File tree

7 files changed

+151
-22
lines changed

7 files changed

+151
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

app/app.js

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,25 @@ function start() {
2222

2323
isCommit = location.href.match(urlCommitRegex);
2424

25-
if(location.href.match(urlPullRegex) || location.href.match(urlCommitRegex)) { // show only on PR files page
26-
initialSetup();
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));
2729
}
2830
}
2931
}, 500);
3032
}
3133

32-
function initialSetup() {
34+
function initialSetup(savedItems) {
3335
if ($('.js-diff-progressive-spinner').length || !$('#files').length) {
3436
return;
3537
}
3638

3739
injectCss(isCommit ? 0 : 178, isCommit ? 20 : 0); // style.js
38-
injectHTML();
40+
injectHTML(savedItems);
41+
42+
savedItems.collapsed ? collapseAllDiffBlocks() : expandAllDiffBlocks();
43+
savedItems.closed ? close() : open();
3944

4045
areDiffBlocksCollapsed() ? $('#collapseAll').hide() : $('#expandAll').hide();
4146

@@ -52,25 +57,15 @@ function initialSetup() {
5257
}
5358
});
5459

55-
$('#openAll').click(() => {
56-
$('.gct-folder').addClass('gct-folder-open');
57-
});
58-
59-
$('#closeAll').click(() => {
60-
$('.gct-folder').removeClass('gct-folder-open');
61-
});
60+
$('#openAll').click(() => open());
61+
$('#closeAll').click(() => close());
6262

63-
$('#expandAll').click(() => {
64-
expandAllDiffBlocks();
65-
});
66-
67-
$('#collapseAll').click(() => {
68-
collapseAllDiffBlocks();
69-
});
63+
$('#expandAll').click(() => expandAllDiffBlocks());
64+
$('#collapseAll').click(() => collapseAllDiffBlocks());
7065
}
7166

72-
function injectHTML() {
73-
tree = buildTree();
67+
function injectHTML(savedItems) {
68+
tree = buildTree(savedItems);
7469
$(
7570
`<div class="gct-file-tree">
7671
<div class="gct-header">
@@ -126,7 +121,7 @@ function buildHtmlTree(tree) {
126121
return content;
127122
}
128123

129-
function buildTree() {
124+
function buildTree(savedItems) {
130125
var tree = {};
131126

132127
$('.file-info').map((i, item) => {
@@ -163,9 +158,61 @@ function buildTree() {
163158
tree = mergeObjects(tree, nodeObj);
164159
});
165160

161+
if (savedItems.folders) {
162+
tree = joinEmptyFolders(tree, []);
163+
164+
if (tree.merge) {
165+
let temporaryTree = {};
166+
temporaryTree[tree.key] = tree.obj;
167+
tree = temporaryTree;
168+
}
169+
}
170+
166171
return tree;
167172
}
168173

174+
function joinEmptyFolders(obj, paths) {
175+
let current = obj;
176+
paths.map(path => current = current[path]);
177+
178+
const files = Object.keys(current).filter(key => Array.isArray(current[key]));
179+
180+
Object.keys(current)
181+
.filter(key => !Array.isArray(current[key])).map(key => {
182+
const childPath = [...paths, key];
183+
const folder = joinEmptyFolders(obj, childPath);
184+
if (folder.merge && files.length === 0) {
185+
childPath[childPath.length - 1] = `${key}/${folder.key}`;
186+
current[`${key}/${folder.key}`] = folder.obj;
187+
delete current[key];
188+
189+
return {
190+
merge: true,
191+
obj: { ...current },
192+
key: key,
193+
};
194+
}
195+
});
196+
197+
// yes, I need to extract again the keys from the current, it could be modified
198+
var keys = Object.keys(current).filter(key => !Array.isArray(current[key]));
199+
200+
// has only sub-folder, so should merge the keys
201+
if (keys.length === 1) {
202+
let childKey = keys[0];
203+
204+
if (current[keys[0]].files && current[keys[0]].files.length) {
205+
return {
206+
merge: true,
207+
obj: current[keys[0]],
208+
key: keys[0],
209+
};
210+
}
211+
}
212+
213+
return current;
214+
}
215+
169216
function mergeObjects(og, so) {
170217
for (var key in so) {
171218
if (!og[key]) {
@@ -216,3 +263,11 @@ function collapseAllDiffBlocks() {
216263
}
217264
});
218265
}
266+
267+
function open() {
268+
$('.gct-folder').addClass('gct-folder-open');
269+
}
270+
271+
function close() {
272+
$('.gct-folder').removeClass('gct-folder-open');
273+
}

app/popup/img/github.png

1.32 KB
Loading

app/popup/index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
<head>
3+
<link rel="stylesheet" href="./style.css">
4+
<script src="./script.js"></script>
5+
</head>
6+
<body>
7+
<div class="header text-center">
8+
<img src="img/github.png" alt="GitHub Logo" class="github-logo">
9+
<h2>Github Code Tree</h2>
10+
<h3><a href="https://github.com/picheli20/github-code-tree" target="_blanket">Join us on Github</a></h3>
11+
</div>
12+
<div class="form-item">
13+
<input class="styled-checkbox" id="closed" type="checkbox">
14+
<label for="closed">Start with closed tree</label>
15+
</div>
16+
<div class="form-item">
17+
<input class="styled-checkbox" id="collapsed" type="checkbox">
18+
<label for="collapsed">Start with collapsed files</label>
19+
</div>
20+
<div class="form-item">
21+
<input class="styled-checkbox" id="folders" type="checkbox">
22+
<label for="folders">Join folders (experimental)</label>
23+
</div>
24+
<div class="footer">
25+
<div class="credit"> by <a href="https://github.com/picheli20" target="_blanket">@picheli20</a></div>
26+
</div>
27+
</body>
28+
</html>

app/popup/script.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function debug(what) {
2+
document.getElementById('debug').innerHTML = JSON.stringify(what, null, 2);
3+
}
4+
5+
function init() {
6+
checkBoxInit(document.getElementById('closed'), 'closed');
7+
checkBoxInit(document.getElementById('collapsed'), 'collapsed');
8+
checkBoxInit(document.getElementById('folders'), 'folders');
9+
}
10+
11+
function checkBoxInit(element, storeId) {
12+
chrome.storage.sync.get([storeId], items => element.checked = items[storeId]);
13+
14+
var toStore = {};
15+
element.addEventListener('click', () => {
16+
toStore[storeId] = element.checked;
17+
chrome.storage.sync.set(toStore);
18+
});
19+
}
20+
21+
window.addEventListener("load", init);

app/popup/style.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
body {
2+
width: 200px;
3+
}
4+
5+
.text-center {
6+
text-align: center;
7+
}
8+
9+
.github-logo {
10+
width: 40px;
11+
}
12+
13+
.footer {
14+
margin-top: 10px;
15+
}
16+
17+
.credit {
18+
font-size: 10px;
19+
text-align: center;
20+
font-style: italic;
21+
color: #333;
22+
}

manifest.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
"manifest_version": 2,
66
"permissions": [
77
"activeTab",
8+
"storage",
89
"https://github.com/*"
910
],
1011
"browser_action": {
11-
"default_icon": "icon.png"
12+
"default_icon": "icon.png",
13+
"default_popup": "app/popup/index.html"
1214
},
1315
"optional_permissions": [
1416
"http://*/*",

0 commit comments

Comments
 (0)