Add top-menu documentation links to PolyGerrit

Bug: Issue 5692
Change-Id: Ieeaad992c2421b893a36c866ddecf9ae618cdeb0
This commit is contained in:
Luca Milanesio 2017-04-24 20:04:14 +02:00 committed by Logan Hanks
parent b95cafed36
commit f4a0eeb648
5 changed files with 124 additions and 4 deletions

View File

@ -58,6 +58,33 @@
],
}];
var DOCUMENTATION_LINKS = [
{
url : '/index.html',
name : 'Table of Contents',
},
{
url : '/user-search.html',
name : 'Searching',
},
{
url : '/user-upload.html',
name : 'Uploading',
},
{
url : '/access-control.html',
name : 'Access Control',
},
{
url : '/rest-api.html',
name : 'REST API',
},
{
url : '/intro-project-owner.html',
name : 'Project Owner Guide',
}
];
Polymer({
is: 'gr-main-header',
@ -82,9 +109,14 @@
return DEFAULT_LINKS;
},
},
_docBaseUrl: {
type: String,
value: null,
},
_links: {
type: Array,
computed: '_computeLinks(_defaultLinks, _userLinks, _adminLinks)',
computed: '_computeLinks(_defaultLinks, _userLinks, _adminLinks,' +
'_docBaseUrl)',
},
_loginURL: {
type: String,
@ -106,6 +138,7 @@
attached: function() {
this._loadAccount();
this._loadConfig();
this.listen(window, 'location-change', '_handleLocationChange');
},
@ -137,7 +170,7 @@
return '//' + window.location.host + this.getBaseUrl() + path;
},
_computeLinks: function(defaultLinks, userLinks, adminLinks) {
_computeLinks: function(defaultLinks, userLinks, adminLinks, docBaseUrl) {
var links = defaultLinks.slice();
if (userLinks && userLinks.length > 0) {
links.push({
@ -151,9 +184,33 @@
links: adminLinks,
});
}
var docLinks = this._getDocLinks(docBaseUrl, DOCUMENTATION_LINKS);
if (docLinks.length) {
links.push({
title: 'Documentation',
links: docLinks,
});
}
return links;
},
_getDocLinks: function(docBaseUrl, docLinks) {
if (!docBaseUrl || !docLinks) {
return [];
}
return docLinks.map(function(link) {
var url = docBaseUrl;
if (url && url[url.length - 1] === '/') {
url = url.substring(0, url.length - 1);
}
return {
url: url + link.url,
name: link.name,
target: '_blank',
};
});
},
_loadAccount: function() {
this.$.restAPI.getAccount().then(function(account) {
this._account = account;
@ -162,6 +219,14 @@
}.bind(this));
},
_loadConfig: function() {
return this.$.restAPI.getConfig().then(function(config) {
if (config && config.gerrit) {
this._docBaseUrl = config.gerrit.doc_url;
}
}.bind(this));
},
_accountLoaded: function(account) {
if (!account) { return; }

View File

@ -130,5 +130,31 @@ limitations under the License.
links: adminLinks,
}));
});
test('documentation links', function() {
var docLinks = [
{
name: 'Table of Contents',
url: '/Documentation/index.html',
},
];
assert.deepEqual(element._getDocLinks(null, docLinks), []);
assert.deepEqual(element._getDocLinks('', docLinks), []);
assert.deepEqual(element._getDocLinks('base', null), []);
assert.deepEqual(element._getDocLinks('base', []), []);
assert.deepEqual(element._getDocLinks('base', docLinks), [{
name: 'Table of Contents',
target: '_blank',
url: 'base/Documentation/index.html',
}]);
assert.deepEqual(element._getDocLinks('base/', docLinks), [{
name: 'Table of Contents',
target: '_blank',
url: 'base/Documentation/index.html',
}]);
});
});
</script>

View File

@ -138,7 +138,9 @@ limitations under the License.
hidden$="[[link.url]]">[[link.name]]</span>
<a
class="itemAction"
href$="[[_computeRelativeURL(link.url)]]"
href$="[[_computeLinkURL(link)]]"
rel$="[[_computeLinkRel(link)]]"
target$="[[link.target]]"
hidden$="[[!link.url]]">[[link.name]]</a>
</li>
</template>

View File

@ -87,6 +87,17 @@
return this._computeURLHelper(host, path);
},
_computeLinkURL: function(link) {
if (link.target) {
return link.url;
}
return this._computeRelativeURL(link.url);
},
_computeLinkRel: function(link) {
return link.target ? 'noopener' : null;
},
_handleItemTap: function(e) {
var id = e.target.getAttribute('data-id');
if (id && this.disabledIds.indexOf(id) === -1) {

View File

@ -49,13 +49,29 @@ limitations under the License.
assert.isTrue(element.$.dropdown.opened);
});
test('_computeRelativeURL', function() {
test('_computeURLHelper', function() {
var path = '/test';
var host = 'http://www.testsite.com';
var computedPath = element._computeURLHelper(host, path);
assert.equal(computedPath, '//http://www.testsite.com/test');
});
test('link URLs', function() {
assert.equal(
element._computeLinkURL({url: '/test'}),
'//' + window.location.host + '/test');
assert.equal(
element._computeLinkURL({url: '/test', target: '_blank'}),
'/test');
});
test('link rel', function() {
assert.isNull(element._computeLinkRel({url: '/test'}));
assert.equal(
element._computeLinkRel({url: '/test', target: '_blank'}),
'noopener');
});
test('_getClassIfBold', function() {
var bold = true;
assert.equal(element._getClassIfBold(bold), 'bold-text');