-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathhandlersUtil.js
More file actions
118 lines (95 loc) · 5.03 KB
/
handlersUtil.js
File metadata and controls
118 lines (95 loc) · 5.03 KB
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
const commonUtil = require('./commonUtil');
const handlersUtil = {
onPathContentFetchedForBtns: data => {
const formattedFileSize = commonUtil.getFileSizeAndUnit(data);
commonUtil.removePrevInstancesOf('.js-file-clipboard');
commonUtil.removePrevInstancesOf('.js-file-download');
const btnGroupHtml = `
<button aria-label="Copy file contents to clipboard" class="js-file-clipboard btn btn-sm BtnGroup-item file-clipboard-button tooltipped tooltipped-s js-enhanced-github-copy-btn" data-copied-hint="Copied!" type="button" click="selectText()" data-clipboard-target="tbody">
Copy File
</button>
<a href="${data.download_url}" download="${encodeURIComponent(data.name)}"
aria-label="(Alt/Option/Ctrl + Click) to download File" class="js-file-download btn btn-sm BtnGroup-item file-download-button tooltipped tooltipped-s">
<span style="margin-right: 5px;">${formattedFileSize}</span>
<svg class="octicon octicon-cloud-download" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16">
<path d="M9 12h2l-3 3-3-3h2V7h2v5zm3-8c0-.44-.91-3-4.5-3C5.08 1 3 2.92 3 5 1.02 5 0 6.52 0 8c0 1.53 1 3 3 3h3V9.7H3C1.38 9.7 1.3 8.28 1.3 8c0-.17.05-1.7 1.7-1.7h1.3V5c0-1.39 1.56-2.7 3.2-2.7 2.55 0 3.13 1.55 3.2 1.8v1.2H12c.81 0 2.7.22 2.7 2.2 0 2.09-2.25 2.2-2.7 2.2h-2V11h2c2.08 0 4-1.16 4-3.5C16 5.06 14.08 4 12 4z"></path>
</svg>
</a>`;
const btnGroup = document.querySelectorAll('.BtnGroup:not(.d-md-none)')[0];
btnGroup.insertAdjacentHTML('beforeend', btnGroupHtml);
},
onPathContentFetched: (data = []) => {
data = commonUtil.sortFileStructureAsOnSite(data);
if (!data) {
return;
}
let isAnyFilePresent = false;
for (let i = 0; i < data.length; i++) {
if (data[i].type === 'file') {
isAnyFilePresent = true;
break;
}
}
if (!isAnyFilePresent) {
return;
}
setTimeout(function() {
commonUtil.removePrevInstancesOf('.eg-download'); // remove before adding new ones
const containerItems = document.querySelectorAll(
'.js-navigation-container.js-active-navigation-container .js-navigation-item'
);
let actualDataIndex = 0;
let startIndex = 0;
if (
window.location.pathname &&
window.location.pathname.indexOf(`tree/${commonUtil.getBranch()}`) > -1 &&
!window.location.pathname.endsWith(`tree/${commonUtil.getBranch()}`) &&
!window.location.pathname.endsWith(`tree/${commonUtil.getBranch()}/`)
) {
startIndex = 1;
}
const repoPath = commonUtil.getUsernameWithReponameFromGithubURL();
if (
window.location.pathname !== `/${repoPath.user}/${repoPath.repo}` &&
window.location.href.indexOf('tree/' + commonUtil.getBranch()) === -1
) {
return;
}
for (var i = startIndex; i < containerItems.length; i++) {
const commitElem = containerItems[i].querySelector('div:nth-of-type(3)');
const fileMetadata = data[actualDataIndex];
const isValidFile = fileMetadata.type === 'file' && fileMetadata.size !== 0;
if (commitElem) {
containerItems[i].querySelector('div:nth-of-type(2)').classList.remove('col-md-2', 'mr-3');
containerItems[i].querySelector('div:nth-of-type(2)').classList.add('col-md-1', 'mr-2');
if (isValidFile || fileMetadata.type === 'symlink') {
const formattedFileSize = commonUtil.getFileSizeAndUnit(fileMetadata);
const html = `
<div role="gridcell" class="mr-1 text-gray-light eg-download" style="width: 95px;">
<span class="css-truncate css-truncate-target d-block">
<span style="margin-right: 5px;">
${formattedFileSize}
</span>
<a style="float: right" href="${fileMetadata.download_url}" title="(Alt/Option/Ctrl + Click) to download File" aria-label="(Alt/Option/Ctrl + Click) to download File" class="tooltipped tooltipped-s"
download="${encodeURIComponent(fileMetadata.name)}">
<svg class="octicon octicon-cloud-download" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16">
<path d="M9 12h2l-3 3-3-3h2V7h2v5zm3-8c0-.44-.91-3-4.5-3C5.08 1 3 2.92 3 5 1.02 5 0 6.52 0 8c0 1.53 1 3 3 3h3V9.7H3C1.38 9.7 1.3 8.28 1.3 8c0-.17.05-1.7 1.7-1.7h1.3V5c0-1.39 1.56-2.7 3.2-2.7 2.55 0 3.13 1.55 3.2 1.8v1.2H12c.81 0 2.7.22 2.7 2.2 0 2.09-2.25 2.2-2.7 2.2h-2V11h2c2.08 0 4-1.16 4-3.5C16 5.06 14.08 4 12 4z"></path>
</svg>
</a>
</span>
</div>
`;
commitElem.insertAdjacentHTML('afterend', html);
} else {
const defaultHtml = `
<div role="gridcell" class="mr-1 eg-download" style="width: 90px;"></div>
`;
commitElem.insertAdjacentHTML('afterend', defaultHtml);
}
actualDataIndex++;
}
}
}, 1000);
}
};
module.exports = handlersUtil;