Preserve URL line numbers specified by @
Some URLs (for example, comment links in Gerrit emails) specify the line
number following an @-sign rather than in the hash using an octothorp
because the URL is already mostly inside a hash (for backward
compatibility with the GWT UI). When these URLs do not include the
project name (as they currently do not in Gerrit emails) the project is
loaded and the parsed route is "upgraded" to include the project.
However, when generating the upgrade URL, the `_generateUrl` method
looks for the `lineNum` and `leftSide` properties to generate the
address. While the address specified by the @-sign is properly converted
to an octothorp-hash before this point, it appears on the properties
object as `hash`, and is thus ignored by `_generateUrl`.
With this change, instead of passing the raw hash through the app params
and parsing it in the `gr-diff-view`, the hash is parsed into its
`leftSide` and `lineNum` values and attached to the `app.params` by the
router. In this way, the location is preserved through URL upgrade, the
param format used in navigation matches that used in URL generation and
the diff view no longer parses the route.
Bug: Issue 7087
Change-Id: Idb2e3cccf2884fae742247cf1ebbde1ad97e53ab
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.js b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.js
index 409fd09..56be27d 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.js
@@ -26,8 +26,6 @@
RIGHT: 'right',
};
- const HASH_PATTERN = /^[ab]?\d+$/;
-
Polymer({
is: 'gr-diff-view',
@@ -467,7 +465,7 @@
_paramsChanged(value) {
if (value.view !== Gerrit.Nav.View.DIFF) { return; }
- this._loadHash(this.params.hash);
+ this._initCursor(this.params);
this._changeNum = value.changeNum;
this._patchRange = {
@@ -539,18 +537,16 @@
},
/**
- * If the URL hash is a diff address then configure the diff cursor.
+ * If the params specify a diff address then configure the diff cursor.
*/
- _loadHash(hash) {
- hash = (hash || '').replace(/^#/, '');
- if (!HASH_PATTERN.test(hash)) { return; }
- if (hash[0] === 'a' || hash[0] === 'b') {
+ _initCursor(params) {
+ if (params.lineNum === undefined) { return; }
+ if (params.leftSide) {
this.$.cursor.side = DiffSides.LEFT;
- hash = hash.substring(1);
} else {
this.$.cursor.side = DiffSides.RIGHT;
}
- this.$.cursor.initialLineNumber = parseInt(hash, 10);
+ this.$.cursor.initialLineNumber = params.lineNum;
},
_pathChanged(path) {