diff --git a/src/app.ts b/src/app.ts index aa2341c..4a79be1 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,37 +1,28 @@ -// angular1 imports -import * as angular from 'angular' -import 'angular-route' - -// angular2 imports -import {NgModule, Component} from '@angular/core'; +import {NgModule} from '@angular/core'; import {Router, RouterModule, UrlHandlingStrategy} from '@angular/router'; import {BrowserModule} from '@angular/platform-browser'; import {UpgradeAdapter} from '@angular/upgrade'; // modules import {MessagesModule, MessagesNgModule} from './messages'; -import {MenuModule} from './menu'; +import {MenuModule, MenuNgModule} from './menu'; import {SettingsNgModule} from './settings'; +import {Ng2RouterRoot, createRootModule} from './upgrade_utils'; -import {Repository} from './repository'; - - - -@Component({selector : 'ng2-router-root', template : ``}) -export class Ng2RouterRoot { -} - - - +// This URL handling strategy is custom and application-specific. class Ng1Ng2UrlHandlingStrategy implements UrlHandlingStrategy { shouldProcessUrl(url) { return url.toString().startsWith("/settings"); } extract(url) { return url; } merge(url, whole) { return url; } } -// // angular2 setup @NgModule({ - imports: [BrowserModule, MessagesNgModule, SettingsNgModule, RouterModule.forRoot([], {useHash: true})], + imports: [ + BrowserModule, + MessagesNgModule, + SettingsNgModule, + RouterModule.forRoot([], {useHash: true}) + ], providers: [ { provide: UrlHandlingStrategy, useClass: Ng1Ng2UrlHandlingStrategy } ], @@ -39,28 +30,15 @@ class Ng1Ng2UrlHandlingStrategy implements UrlHandlingStrategy { }) class AppModule {} const adapter = new UpgradeAdapter(AppModule); - +MenuNgModule.setAdapter(adapter); MessagesNgModule.setAdapter(adapter); - - - -// angular1 setup -const RootModule = angular.module('rootModule', ['ngRoute', MessagesModule.name, MenuModule.name]); -RootModule.component('rootCmp', {template : '
'}); -RootModule.directive('ng2RouterRoot', adapter.downgradeNg2Component(Ng2RouterRoot)); -RootModule.config(($routeProvider) => { - $routeProvider - .otherwise({template : '', reloadOnSearch: false}); -}); - -RootModule.service('repository', Repository); -adapter.upgradeNg1Provider("repository", {asToken: Repository}); - +createRootModule(adapter, ['ngRoute', MessagesModule.name, MenuModule.name]); export function bootstrap(el) { - const ref = adapter.bootstrap(el, [ 'rootModule' ]); + const ref = adapter.bootstrap(el, ['rootModule']); + // this is required because of a bug in NgUpgrade setTimeout(() => { ref.ng2Injector.get(Router).initialNavigation(); }, 0); diff --git a/src/menu/index.ts b/src/menu/index.ts index 3032690..daa938a 100644 --- a/src/menu/index.ts +++ b/src/menu/index.ts @@ -1,3 +1,4 @@ +// This module is purely written in Angular 1. import * as angular from 'angular'; import {NgModule} from '@angular/core'; import {UpgradeAdapter} from '@angular/upgrade'; @@ -5,16 +6,13 @@ import {UpgradeAdapter} from '@angular/upgrade'; import {MenuCmp} from './menu_cmp'; export const MenuModule = angular.module('MenuModule', ['ngRoute']); - MenuModule.component('menu', MenuCmp); - MenuModule.config(($routeProvider) => { $routeProvider.when('/', {template : ''}); }); +// techncially, this is not required, but it is nice to have it for consistency @NgModule({}) export class MenuNgModule { - static setAdapter(adapter: UpgradeAdapter) { - //no components are migrated to angular2 - } + static setAdapter(adapter: UpgradeAdapter) {} } \ No newline at end of file diff --git a/src/menu/menu_cmp.ts b/src/menu/menu_cmp.ts index f84f680..0ba82b3 100644 --- a/src/menu/menu_cmp.ts +++ b/src/menu/menu_cmp.ts @@ -3,6 +3,7 @@ export const MenuCmp = {

Messages

` diff --git a/src/messages/index.ts b/src/messages/index.ts index eba0da0..ad08e68 100644 --- a/src/messages/index.ts +++ b/src/messages/index.ts @@ -1,7 +1,10 @@ +// This module is in the midst of transitiong from Angular 1 to Angular 2. +// We migrated MessageTextCmp to Angular2. import * as angular from 'angular'; import {NgModule} from '@angular/core'; import {UpgradeAdapter} from '@angular/upgrade'; +import {Repository} from './repository'; import {MessageTextCmp} from './message_text_cmp'; import {MessagesCmp} from './messages_cmp'; import {MessageCmp} from './message_cmp'; @@ -10,7 +13,7 @@ export const MessagesModule = angular.module('MessagesModule', ['ngRoute']); MessagesModule.component('messages', MessagesCmp); MessagesModule.component('message', MessageCmp); - +MessagesModule.service('repository', Repository); MessagesModule.config(($routeProvider) => { $routeProvider .when('/messages/:folder', {template : ''}) @@ -22,5 +25,6 @@ export class MessagesNgModule { static setAdapter(adapter: UpgradeAdapter) { // all components migrated to angular 2 can be downgraded here MessagesModule.directive('messageText', adapter.downgradeNg2Component(MessageTextCmp)); + adapter.upgradeNg1Provider("repository", {asToken: Repository}); } } \ No newline at end of file diff --git a/src/messages/message_cmp.ts b/src/messages/message_cmp.ts index d5651d4..aa23661 100644 --- a/src/messages/message_cmp.ts +++ b/src/messages/message_cmp.ts @@ -1,4 +1,4 @@ -import {Message, Repository} from '../repository'; +import {Message, Repository} from './repository'; export class MessageController { folder: string; diff --git a/src/messages/messages_cmp.ts b/src/messages/messages_cmp.ts index f091407..4316236 100644 --- a/src/messages/messages_cmp.ts +++ b/src/messages/messages_cmp.ts @@ -1,4 +1,4 @@ -import {Message, Repository} from '../repository'; +import {Message, Repository} from './repository'; export class MessagesController { folder: string; diff --git a/src/repository.ts b/src/messages/repository.ts similarity index 100% rename from src/repository.ts rename to src/messages/repository.ts diff --git a/src/settings/index.ts b/src/settings/index.ts index 76d292d..bff7788 100644 --- a/src/settings/index.ts +++ b/src/settings/index.ts @@ -1,3 +1,4 @@ +// This module was fully migrated to Angular 2 import {NgModule} from '@angular/core'; import {CommonModule} from '@angular/common'; import {RouterModule} from '@angular/router'; diff --git a/src/settings/page_size_cmp.ts b/src/settings/page_size_cmp.ts index b2843db..8156a1e 100644 --- a/src/settings/page_size_cmp.ts +++ b/src/settings/page_size_cmp.ts @@ -1,5 +1,5 @@ import {Component} from '@angular/core'; -import {Repository} from '../repository' +import {Repository} from '../messages/repository' @Component({ selector: 'page-settings', diff --git a/src/upgrade_utils.ts b/src/upgrade_utils.ts new file mode 100644 index 0000000..825880c --- /dev/null +++ b/src/upgrade_utils.ts @@ -0,0 +1,31 @@ +// angular1 imports +import * as angular from 'angular' +import 'angular-route' + +// angular2 imports +import {NgModule, Component} from '@angular/core'; +import {Router, RouterModule, UrlHandlingStrategy} from '@angular/router'; +import {BrowserModule} from '@angular/platform-browser'; +import {UpgradeAdapter} from '@angular/upgrade'; + +// modules +import {MessagesModule, MessagesNgModule} from './messages'; +import {MenuModule, MenuNgModule} from './menu'; +import {SettingsNgModule} from './settings'; + + +@Component({selector : 'ng2-router-root', template: ``}) +export class Ng2RouterRoot {} + +export function createRootModule(adapter: UpgradeAdapter, moduleNames: string[]) { + const RootModule = angular.module('rootModule', moduleNames); + + RootModule.component('rootCmp', {template : '
'}); + RootModule.directive('ng2RouterRoot', adapter.downgradeNg2Component(Ng2RouterRoot)); + RootModule.config(($routeProvider) => { + $routeProvider + .otherwise({template : '', reloadOnSearch: false}); + }); + + return RootModule; +}