-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
releases-tab.js
69 lines (58 loc) · 2.03 KB
/
releases-tab.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
/*
Access a repository’s releases using the Releases tab or by pressing g r.
https://cloud.githubusercontent.com/assets/170270/13136797/16d3f0ea-d64f-11e5-8a45-d771c903038f.png
The tab isn’t shown if there are no releases.
*/
import {h} from 'dom-chef';
import select from 'select-dom';
import features from '../libs/features';
import * as icons from '../libs/icons';
import * as cache from '../libs/cache';
import * as pageDetect from '../libs/page-detect';
import {safeElementReady} from '../libs/utils';
import {registerShortcut} from './improve-shortcut-help';
const repoUrl = pageDetect.getRepoURL();
const repoKey = `releases-count:${repoUrl}`;
// Get as soon as possible, to have it ready before the first paint
const cached = cache.get(repoKey);
function updateReleasesCount() {
if (pageDetect.isRepoRoot()) {
const releasesCountEl = select('.numbers-summary a[href$="/releases"] .num');
const releasesCount = Number(releasesCountEl ? releasesCountEl.textContent.replace(/,/g, '') : 0);
cache.set(repoKey, releasesCount, 3);
return releasesCount;
}
return cached;
}
async function init() {
await safeElementReady('.pagehead + *'); // Wait for the tab bar to be loaded
const count = await updateReleasesCount();
if (count === 0) {
return false;
}
const releasesTab = (
<a href={`/${repoUrl}/releases`} class="reponav-item" data-hotkey="g r">
{icons.tag()}
<span> Releases </span>
{count === undefined ? '' : <span class="Counter">{count}</span>}
</a>
);
select('.reponav-dropdown').before(releasesTab);
registerShortcut('repos', 'g r', 'Go to Releases');
if (pageDetect.isReleasesOrTags()) {
const selected = select('.reponav-item.selected');
if (selected) {
selected.classList.remove('js-selected-navigation-item', 'selected');
}
releasesTab.classList.add('js-selected-navigation-item', 'selected');
releasesTab.setAttribute('data-selected-links', 'repo_releases'); // Required for ajaxLoad
}
}
features.add({
id: 'releases-tab',
include: [
features.isRepo
],
load: features.onAjaxedPages,
init
});