diff --git a/polygerrit-ui/app/behaviors/gr-path-list-behavior/gr-path-list-behavior.html b/polygerrit-ui/app/behaviors/gr-path-list-behavior/gr-path-list-behavior.html index f3db039b9c..d3491c79a4 100644 --- a/polygerrit-ui/app/behaviors/gr-path-list-behavior/gr-path-list-behavior.html +++ b/polygerrit-ui/app/behaviors/gr-path-list-behavior/gr-path-list-behavior.html @@ -93,16 +93,20 @@ limitations under the License. * Example * // returns 'text.html' * util.truncatePath.('text.html'); + * + * @param {string} path + * @param {number=} opt_threshold * @return {string} Returns the truncated value of a URL. */ - truncatePath(path) { + truncatePath(path, opt_threshold) { + const threshold = opt_threshold || 1; const pathPieces = path.split('/'); - if (pathPieces.length < 2) { - return path; - } + if (pathPieces.length <= threshold) { return path; } + + const index = pathPieces.length - threshold; // Character is an ellipsis. - return '\u2026/' + pathPieces[pathPieces.length - 1]; + return `\u2026/${pathPieces.slice(index).join('/')}`; }, }; })(window); diff --git a/polygerrit-ui/app/behaviors/gr-path-list-behavior/gr-path-list-behavior_test.html b/polygerrit-ui/app/behaviors/gr-path-list-behavior/gr-path-list-behavior_test.html index e0b1b7ec1a..f48fb989b3 100644 --- a/polygerrit-ui/app/behaviors/gr-path-list-behavior/gr-path-list-behavior_test.html +++ b/polygerrit-ui/app/behaviors/gr-path-list-behavior/gr-path-list-behavior_test.html @@ -66,6 +66,19 @@ limitations under the License. assert.equal(shortenedPath, expectedPath); }); + test('truncatePath with opt_threshold', () => { + const truncatePath = Gerrit.PathListBehavior.truncatePath; + let path = 'level1/level2/level3/level4/file.js'; + let shortenedPath = truncatePath(path, 2); + // The expected path is truncated with an ellipsis. + const expectedPath = '\u2026/level4/file.js'; + assert.equal(shortenedPath, expectedPath); + + path = 'level2/file.js'; + shortenedPath = truncatePath(path, 2); + assert.equal(shortenedPath, path); + }); + test('truncatePath with short path should not add ellipsis', () => { const truncatePath = Gerrit.PathListBehavior.truncatePath; const path = 'file.js'; diff --git a/polygerrit-ui/app/elements/change-list/gr-change-list-item/gr-change-list-item.html b/polygerrit-ui/app/elements/change-list/gr-change-list-item/gr-change-list-item.html index 7fb5fe4239..9613637a43 100644 --- a/polygerrit-ui/app/elements/change-list/gr-change-list-item/gr-change-list-item.html +++ b/polygerrit-ui/app/elements/change-list/gr-change-list-item/gr-change-list-item.html @@ -15,6 +15,7 @@ limitations under the License. --> + @@ -148,7 +149,12 @@ limitations under the License.