Start extracting navigation calls into an interface
The goal is to render `gr-navigation` obsolete.
We could have resolved the router directly. But the router is so complex
that it seemed worthwhile to have a dedicated interface just for
navigation.
In tests the navigation interface will just do nothing.
Release-Notes: skip
Google-Bug-Id: b/244279450
Change-Id: I1b3585fb649b2c0ce5a4f1dd90e77a78c94eb56f
diff --git a/polygerrit-ui/app/elements/core/gr-router/gr-router.ts b/polygerrit-ui/app/elements/core/gr-router/gr-router.ts
index a9a6389..c1728a2 100644
--- a/polygerrit-ui/app/elements/core/gr-router/gr-router.ts
+++ b/polygerrit-ui/app/elements/core/gr-router/gr-router.ts
@@ -8,7 +8,7 @@
PageContext,
PageNextCallback,
} from '../../../utils/page-wrapper-utils';
-import {GerritNav} from '../gr-navigation/gr-navigation';
+import {GerritNav, NavigationService} from '../gr-navigation/gr-navigation';
import {getAppContext} from '../../../services/app-context';
import {convertToPatchSetNum} from '../../../utils/patch-set-util';
import {assertIsDefined} from '../../../utils/common-util';
@@ -270,7 +270,7 @@
export const routerToken = define<GrRouter>('router');
-export class GrRouter implements Finalizable {
+export class GrRouter implements Finalizable, NavigationService {
readonly _app = app;
_isRedirecting?: boolean;
@@ -448,6 +448,29 @@
);
}
+ /**
+ * This is similar to letting the browser navigate to this URL when the user
+ * clicks it, or to just setting `window.location.href` directly.
+ *
+ * This adds a new entry to the browser location history. Consier using
+ * `replaceUrl()`, if you want to avoid that.
+ *
+ * page.show() eventually just calls `window.history.pushState()`.
+ */
+ setUrl(url: string) {
+ page.show(url);
+ }
+
+ /**
+ * Navigate to this URL, but replace the current URL in the history instead of
+ * adding a new one (which is what `setUrl()` would do).
+ *
+ * page.redirect() eventually just calls `window.history.replaceState()`.
+ */
+ replaceUrl(url: string) {
+ this.redirect(url);
+ }
+
startRouter() {
const base = getBaseUrl();
if (base) {