2016-11-09 13:33:33 -08:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2017-12-20 09:15:15 -08:00
|
|
|
import {APP_BOOTSTRAP_LISTENER, ComponentRef, InjectionToken} from '@angular/core';
|
2017-03-07 17:27:20 -05:00
|
|
|
import {Router} from '@angular/router';
|
2016-11-09 13:33:33 -08:00
|
|
|
import {UpgradeModule} from '@angular/upgrade/static';
|
|
|
|
|
|
|
|
|
2017-01-03 16:54:46 -08:00
|
|
|
|
2016-11-09 13:33:33 -08:00
|
|
|
/**
|
2017-01-26 22:30:42 -08:00
|
|
|
* @whatItDoes Creates an initializer that in addition to setting up the Angular
|
2016-11-09 13:33:33 -08:00
|
|
|
* router sets up the ngRoute integration.
|
|
|
|
*
|
2018-04-05 11:38:57 +01:00
|
|
|
* @description
|
2016-11-09 13:33:33 -08:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @NgModule({
|
|
|
|
* imports: [
|
|
|
|
* RouterModule.forRoot(SOME_ROUTES),
|
|
|
|
* UpgradeModule
|
|
|
|
* ],
|
|
|
|
* providers: [
|
|
|
|
* RouterUpgradeInitializer
|
|
|
|
* ]
|
|
|
|
* })
|
|
|
|
* export class AppModule {
|
|
|
|
* ngDoBootstrap() {}
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export const RouterUpgradeInitializer = {
|
2017-03-07 17:27:20 -05:00
|
|
|
provide: APP_BOOTSTRAP_LISTENER,
|
|
|
|
multi: true,
|
2018-03-20 13:56:47 -07:00
|
|
|
useFactory: locationSyncBootstrapListener as(ngUpgrade: UpgradeModule) => () => void,
|
2017-03-07 17:27:20 -05:00
|
|
|
deps: [UpgradeModule]
|
2016-11-09 13:33:33 -08:00
|
|
|
};
|
|
|
|
|
2016-12-01 15:46:22 -08:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2017-03-07 17:27:20 -05:00
|
|
|
export function locationSyncBootstrapListener(ngUpgrade: UpgradeModule) {
|
|
|
|
return () => { setUpLocationSync(ngUpgrade); };
|
2016-12-01 15:46:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-20 09:15:15 -08:00
|
|
|
* @whatItDoes Sets up a location synchronization.
|
2016-12-01 15:46:22 -08:00
|
|
|
*
|
2017-03-27 09:44:35 -07:00
|
|
|
* History.pushState does not fire onPopState, so the Angular location
|
2016-12-01 15:46:22 -08:00
|
|
|
* doesn't detect it. The workaround is to attach a location change listener
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
2017-03-07 17:27:20 -05:00
|
|
|
export function setUpLocationSync(ngUpgrade: UpgradeModule) {
|
|
|
|
if (!ngUpgrade.$injector) {
|
|
|
|
throw new Error(`
|
|
|
|
RouterUpgradeInitializer can be used only after UpgradeModule.bootstrap has been called.
|
|
|
|
Remove RouterUpgradeInitializer and call setUpLocationSync after UpgradeModule.bootstrap.
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
2017-12-20 09:15:15 -08:00
|
|
|
const router: Router = ngUpgrade.injector.get(Router);
|
2016-12-01 15:46:22 -08:00
|
|
|
const url = document.createElement('a');
|
|
|
|
|
2017-12-20 09:15:15 -08:00
|
|
|
ngUpgrade.$injector.get('$rootScope')
|
|
|
|
.$on('$locationChangeStart', (_: any, next: string, __: string) => {
|
|
|
|
url.href = next;
|
|
|
|
router.navigateByUrl(url.pathname + url.search + url.hash);
|
|
|
|
});
|
|
|
|
}
|