Add support for downloading binary files

This change uses a dropdown to be able to download either
the patch, left side content or right side content.

Screenshot: https://imgur.com/a/D2ffRX7

Bug: Issue 6029
Bug: Issue 7867
Change-Id: I4e05dba0dcfaf8699e20f151a6949ffb08701bbe
This commit is contained in:
Paladox none
2019-09-25 20:15:42 +00:00
parent b77e26cae3
commit 45913eb5a5
3 changed files with 83 additions and 25 deletions

View File

@@ -26,6 +26,7 @@ limitations under the License.
<link rel="import" href="../../core/gr-reporting/gr-reporting.html">
<link rel="import" href="../../shared/gr-button/gr-button.html">
<link rel="import" href="../../shared/gr-count-string-formatter/gr-count-string-formatter.html">
<link rel="import" href="../../shared/gr-dropdown/gr-dropdown.html">
<link rel="import" href="../../shared/gr-dropdown-list/gr-dropdown-list.html">
<link rel="import" href="../../shared/gr-fixed-panel/gr-fixed-panel.html">
<link rel="import" href="../../shared/gr-icons/gr-icons.html">
@@ -75,8 +76,7 @@ limitations under the License.
align-items: center;
display: flex;
}
.navLink:not([href]),
.downloadLink:not([href]) {
.navLink:not([href]) {
color: var(--deemphasized-text-color);
}
.navLinks {
@@ -266,12 +266,15 @@ limitations under the License.
</gr-patch-range-select>
<span class="download desktop">
<span class="separator"></span>
<a
class="downloadLink"
download
href$="[[_computeDownloadLink(_change.project, _changeNum, _patchRange, _path)]]">
Download
</a>
<gr-dropdown
link
down-arrow
items="[[_computeDownloadDropdownLinks(_change.project, _changeNum, _patchRange, _path)]]"
horizontal-align="left">
<span class="downloadTitle">
Download
</span>
</gr-dropdown>
</span>
</div>
<div class="rightControls">

View File

@@ -880,7 +880,36 @@
history.replaceState(null, '', url);
},
_computeDownloadLink(project, changeNum, patchRange, path) {
_computeDownloadDropdownLinks(project, changeNum, patchRange, path) {
if (!patchRange || !patchRange.patchNum) { return []; }
return [
{
url: this._computeDownloadPatchLink(
project, changeNum, patchRange, path),
name: 'Patch',
},
{
// We pass 1 here to indicate this is parent 1.
url: this._computeDownloadFileLink(
project, changeNum, patchRange, path, 1),
name: 'Left Content',
},
{
// We pass 0 here to indicate this is parent 0.
url: this._computeDownloadFileLink(
project, changeNum, patchRange, path, 0),
name: 'Right Content',
},
];
},
_computeDownloadFileLink(project, changeNum, patchRange, path, parent) {
return this.changeBaseURL(project, changeNum, patchRange.patchNum) +
`/files/${encodeURIComponent(path)}/download?parent=${parent}`;
},
_computeDownloadPatchLink(project, changeNum, patchRange, path) {
let url = this.changeBaseURL(project, changeNum, patchRange.patchNum);
url += '/patch?zip&path=' + encodeURIComponent(path);
return url;

View File

@@ -556,22 +556,6 @@ limitations under the License.
element._path, '1', 'PARENT'));
});
test('download link', () => {
element._change = {project: 'test'},
element._changeNum = '42';
element._patchRange = {
basePatchNum: PARENT,
patchNum: '10',
};
element._fileList = ['chell.go', 'glados.txt', 'wheatley.md'];
element._path = 'glados.txt';
flushAsynchronousOperations();
const link = element.$$('.downloadLink');
assert.equal(link.getAttribute('href'),
'/changes/test~42/revisions/10/patch?zip&path=glados.txt');
assert.isTrue(link.hasAttribute('download'));
});
test('_prefs.manual_review is respected', () => {
const saveReviewedStub = sandbox.stub(element, '_saveReviewedState',
() => Promise.resolve());
@@ -1124,5 +1108,47 @@ limitations under the License.
1,
]);
});
test('_computeDownloadDropdownLinks', () => {
const downloadLinks = [
{
url: '/changes/test~12/revisions/1/patch?zip&path=index.php',
name: 'Patch',
},
{
url: '/changes/test~12/revisions/1' +
'/files/index.php/download?parent=1',
name: 'Left Content',
},
{
url: '/changes/test~12/revisions/1' +
'/files/index.php/download?parent=0',
name: 'Right Content',
},
];
assert.deepEqual(
element._computeDownloadDropdownLinks(
'test', 12, {patchNum: 1}, 'index.php'),
downloadLinks);
});
test('_computeDownloadFileLink', () => {
assert.equal(
element._computeDownloadFileLink(
'test', 12, {patchNum: 1}, 'index.php', 1),
'/changes/test~12/revisions/1/files/index.php/download?parent=1');
assert.equal(
element._computeDownloadFileLink(
'test', 12, {patchNum: 1}, 'index.php', 0),
'/changes/test~12/revisions/1/files/index.php/download?parent=0');
});
test('_computeDownloadPatchLink', () => {
assert.equal(
element._computeDownloadPatchLink(
'test', 12, {patchNum: 1}, 'index.php'),
'/changes/test~12/revisions/1/patch?zip&path=index.php');
});
});
</script>