|
43 | 43 | checkboxEnable.type = 'checkbox'; |
44 | 44 | checkboxEnable.addEventListener('change', |
45 | 45 | function () { |
46 | | - const compareRadioGroup = document.querySelectorAll('.GitHubCommitCompareButtonAB'); |
47 | | - if (this.checked) { |
48 | | - // add or show radios |
49 | | - if (compareRadioGroup.length === 0) { |
50 | | - addCompareRadios(); |
51 | | - } else { |
52 | | - $('.GitHubCommitCompareToggle').show(); |
53 | | - } |
54 | | - } else { |
55 | | - // hide radios |
56 | | - $('.GitHubCommitCompareToggle').hide(); |
57 | | - } |
58 | | - const compareButton = document.getElementById('GitHubCommitCompareButton'); |
59 | | - if (compareButton) compareButton.classList.remove('disabled'); |
60 | | - }); |
| 46 | + toggleVisibility(this.checked); |
| 47 | + }); |
61 | 48 |
|
62 | 49 | const label = document.createElement('label'); |
63 | 50 | label.classList.add('tooltipped', 'tooltipped-n'); |
|
117 | 104 | } |
118 | 105 | } |
119 | 106 |
|
| 107 | + function toggleVisibility(visible) { |
| 108 | + if (visible) { |
| 109 | + const compareRadioGroup = document.querySelectorAll('.GitHubCommitCompareButtonAB'); |
| 110 | + // add or show things |
| 111 | + if (compareRadioGroup.length === 0) { |
| 112 | + addCompareRadios(); |
| 113 | + addStackLinks(); |
| 114 | + } else { |
| 115 | + $('.GitHubCommitCompareToggle').show(); |
| 116 | + } |
| 117 | + } else { |
| 118 | + // hide things |
| 119 | + $('.GitHubCommitCompareToggle').hide(); |
| 120 | + } |
| 121 | + const compareButton = document.getElementById('GitHubCommitCompareButton'); |
| 122 | + if (compareButton) compareButton.classList.remove('disabled'); |
| 123 | + } |
| 124 | + |
120 | 125 | function updateRadioButtons() { |
121 | 126 | let compareAdisabled = true; |
122 | 127 | let compareBdisabled = false; |
|
143 | 148 | updateCompareButton(); |
144 | 149 | } |
145 | 150 |
|
146 | | - function updateCompareButton() { |
| 151 | + function rangeHref(hashBase, hashHead) { |
147 | 152 | const repo = document.querySelector('meta[property="og:url"]').content; |
| 153 | + return `${repo}/files/` + (hashBase ? `${hashBase}..` : '') + hashHead; |
| 154 | + } |
148 | 155 |
|
| 156 | + function updateCompareButton() { |
149 | 157 | const compareA = document.querySelector('.GitHubCommitCompareButtonAB [name="GitHubCommitCompareButtonA"]:checked'); |
150 | 158 | const hashA = compareA.parentNode.parentNode.parentNode.querySelector('clipboard-copy').value; |
151 | 159 | const compareB = document.querySelector('.GitHubCommitCompareButtonAB [name="GitHubCommitCompareButtonB"]:checked'); |
152 | 160 | const clipboardCopyB = compareB.parentNode.parentNode.parentNode.querySelector('clipboard-copy'); |
153 | 161 | const hashB = clipboardCopyB == null ? null : clipboardCopyB.value; |
154 | 162 |
|
155 | 163 | const a = document.getElementById('GitHubCommitCompareButton'); |
156 | | - const href = `${repo}/files/` + (hashB ? `${hashB}..` : '') + hashA; |
| 164 | + const href = rangeHref(hashB, hashA); |
157 | 165 | a.setAttribute('href', href); |
158 | 166 | a.querySelector('span').textContent = `${hashB ? hashB.substring(0, 7) : 'base'}..${hashA.substring(0, 7)}`; |
159 | 167 | } |
160 | 168 |
|
| 169 | + function addStackLinks() { |
| 170 | + const commits = []; |
| 171 | + $('#commits_bucket .commits-list-item .commit-title>a').each((index, commitTitleA) => { |
| 172 | + const title = $(commitTitleA).text(); |
| 173 | + |
| 174 | + const stackMatch = /^(\d+):.*$/.exec(title); |
| 175 | + const stackId = stackMatch ? stackMatch[1] : null; |
| 176 | + |
| 177 | + const hrefSplit = $(commitTitleA).prop("href").split("/"); |
| 178 | + const sha = hrefSplit[hrefSplit.length - 1]; |
| 179 | + |
| 180 | + commits.push({ |
| 181 | + title: title, |
| 182 | + sha: sha, |
| 183 | + stackId: stackId, |
| 184 | + titleA: commitTitleA |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + let hashBase = null; |
| 189 | + let stackTitleA = commits[0].titleA; |
| 190 | + for (let i =0; i<commits.length; i++) { |
| 191 | + const commit = commits[i]; |
| 192 | + if (i === commits.length - 1 || commits[i+1].stackId) { |
| 193 | + const href = rangeHref(hashBase, commit.sha); |
| 194 | + $(stackTitleA).after(`<a class="GitHubCommitCompareToggle" href="${href}"> (stack)</a>`); |
| 195 | + console.log($(stackTitleA).text()); |
| 196 | + console.log(href); |
| 197 | + hashBase = commit.sha; |
| 198 | + if (i<commits.length -1) { |
| 199 | + stackTitleA = commits[i+1].titleA; |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
161 | 205 | function addCompareRadios() { |
162 | 206 | $('#commits_bucket .commits-listing').prepend( |
163 | 207 | `<ol class="commit-group table-list table-list-bordered GitHubCommitCompareToggle"> |
|
0 commit comments