Skip to content

Commit

Permalink
refactor(static): Clean up imports and switch let to const
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed May 15, 2017
1 parent 27fa6dc commit 4a0dcaf
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions src/angular-hybrid.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import * as angular from 'angular';

import { $InjectorLike, Ng1ViewConfig, StateObject, StateProvider } from '@uirouter/angularjs';

import { Component, ElementRef, Inject, Injector, Input, NgModule } from '@angular/core';
import { downgradeComponent, UpgradeModule } from '@angular/upgrade/static';

import {} from '@angular/upgrade';

import { StateObject, forEach, PathNode, Resolvable, StateRegistry, UIRouter, ViewConfig, ViewService } from '@uirouter/core';
import {
_UIROUTER_SERVICE_PROVIDERS, applyModuleConfig, forEach, NATIVE_INJECTOR_TOKEN, ng2LazyLoadBuilder, Ng2ViewConfig,
Ng2ViewDeclaration, ParentUIViewInject, PathNode, Resolvable, StateRegistry, StatesModule, UIRouter,
UIROUTER_MODULE_TOKEN, UIROUTER_ROOT_MODULE, UIRouterModule, UIView, ViewConfig, ViewService
_UIROUTER_SERVICE_PROVIDERS, applyModuleConfig, NATIVE_INJECTOR_TOKEN, ng2LazyLoadBuilder, Ng2ViewConfig, UIView,
Ng2ViewDeclaration, ParentUIViewInject, StatesModule, UIROUTER_MODULE_TOKEN, UIROUTER_ROOT_MODULE, UIRouterModule
} from '@uirouter/angular';

import { $InjectorLike, Ng1ViewConfig, StateProvider } from '@uirouter/angularjs';
import { UIRouterRx } from '@uirouter/rx';

/**
* Create a ng1 module for the ng1 half of the hybrid application to depend on.
*
* Example:
* let myApp = angular.module('myApp', ['ui.router.upgrade']);
* const myApp = angular.module('myApp', ['ui.router.upgrade']);
*/
export let upgradeModule = angular.module('ui.router.upgrade', ['ui.router']);
export let ng1InitModule = angular.module('ui.router.init');
export const upgradeModule = angular.module('ui.router.upgrade', ['ui.router']);
export const ng1InitModule = angular.module('ui.router.init');

/**
* UIViewNgUpgrade is a component bridge from ng1 ui-view to ng2 ui-view
Expand Down Expand Up @@ -111,21 +110,21 @@ export class UIViewNgUpgrade {

// The ng2 ui-view component is inside this ui-view-ng-upgrade directive, which is inside the ng1 "host" ui-view.
// Both ui-views share the same "view context" information (the view's fqn and created-by-state context information)
let ng1elem = angular.element(ref.nativeElement).parent().parent();
const ng1elem = angular.element(ref.nativeElement).parent().parent();

// Expose getters on PARENT_INJECT for context (creation state) and fqn (view address)
// These will be used by further nested UIView
Object.defineProperty(parent, "context", {
get: function() {
let data = ng1elem['inheritedData']('$uiView');
const data = ng1elem['inheritedData']('$uiView');
return (data && data.$cfg) ? data.$cfg.viewDecl.$context : registry.root();
},
enumerable: true
});

Object.defineProperty(parent, "fqn", {
get: function() {
let data = ng1elem['inheritedData']('$uiView');
const data = ng1elem['inheritedData']('$uiView');
return (data && data.$uiView) ? data.$uiView.fqn : null;
},
enumerable: true
Expand All @@ -139,7 +138,7 @@ export class UIViewNgUpgrade {

// Register the ng1 DI '$uiRouter' object as an ng2 Provider.
function uiRouterUpgradeFactory(router: UIRouter, injector: Injector) {
let modules: StatesModule[] = injector.get(UIROUTER_MODULE_TOKEN, []);
const modules: StatesModule[] = injector.get(UIROUTER_MODULE_TOKEN, []);
modules.forEach(module => applyModuleConfig(router, injector, module));
return router;
}
Expand Down Expand Up @@ -182,20 +181,20 @@ upgradeModule.directive("uiViewNgUpgrade", <any> downgradeComponent({
}));

upgradeModule.run(['$injector', (ng1Injector: $InjectorLike) => {
let $uiRouter: UIRouter = ng1Injector.get('$uiRouter');
const $uiRouter: UIRouter = ng1Injector.get('$uiRouter');
new UIRouterRx($uiRouter);

// Expose a merged ng1/ng2 injector as a Resolvable (on the root state).
// This mimics how ui-router-ng2 exposes the root ng2 Injector, but
// it retrieves from ng1 injector first, then ng2 injector if the token isn't found.
const mergedInjector = {
get: function(token: any, ng2NotFoundValue?: any) {
let ng2Injector = ng1Injector.get('$$angularInjector');
const ng2Injector = ng1Injector.get('$$angularInjector');
return (ng1Injector.has(token) && ng1Injector.get(token)) || ng2Injector.get(token, ng2NotFoundValue)
}
};

let ng2InjectorResolvable = Resolvable.fromData(NATIVE_INJECTOR_TOKEN, mergedInjector);
const ng2InjectorResolvable = Resolvable.fromData(NATIVE_INJECTOR_TOKEN, mergedInjector);
$uiRouter.stateRegistry.root().resolvables.push(ng2InjectorResolvable);
}]);

Expand Down Expand Up @@ -223,7 +222,7 @@ function applyHybridAdapter(ng2Injector: Injector) {
}));

upgradeModule.run(['$injector', (ng1Injector: $InjectorLike) => {
let $uiRouter: UIRouter = ng1Injector.get('$uiRouter');
const $uiRouter: UIRouter = ng1Injector.get('$uiRouter');

// Expose a merged ng1/ng2 injector as a Resolvable (on the root state).
// This mimics how @uirouter/angular exposes the root ng2 Injector, but
Expand All @@ -234,7 +233,7 @@ function applyHybridAdapter(ng2Injector: Injector) {
}
};

let ng2InjectorResolvable = Resolvable.fromData(NATIVE_INJECTOR_TOKEN, mergedInjector);
const ng2InjectorResolvable = Resolvable.fromData(NATIVE_INJECTOR_TOKEN, mergedInjector);
$uiRouter.stateRegistry.root().resolvables.push(ng2InjectorResolvable);
}]);

Expand All @@ -243,7 +242,7 @@ function applyHybridAdapter(ng2Injector: Injector) {
}]);

upgradeModule.config(['$uiRouterProvider', ($uiRouterProvider: UIRouter) => {
let registry = $uiRouterProvider.stateRegistry;
const registry = $uiRouterProvider.stateRegistry;

/** Applies the `UIRouterRx` plugin for observable states/params */
$uiRouterProvider.plugin(UIRouterRx);
Expand All @@ -265,7 +264,7 @@ function applyHybridAdapter(ng2Injector: Injector) {
* which that provides a ng1 -> ng2 boundary in the component tree.
*/
registry.decorator('views', function(state: StateObject, parentFn: Function) {
let views = parentFn(state);
const views = parentFn(state);

forEach(views, (viewDecl: any, viewName: string) => {
if (viewDecl.$type === 'ng1-to-ng2' || isNg2ComponentClass(viewDecl.component)) {
Expand All @@ -290,8 +289,8 @@ function applyHybridAdapter(ng2Injector: Injector) {
// Register a ViewConfig factory for views of type `ng1-to-ng2`.
// Returns both an ng1 config and an ng2 config allowing either ng1 or ng2 ui-view components to be targeted.
$view._pluginapi._viewConfigFactory('ng1-to-ng2', (path: PathNode[], config: Ng2ViewDeclaration) => {
let ng1ViewConfig: ViewConfig = <any> new Ng1ViewConfig(<any> path, <any> Object.assign({}, config, { $type: 'ng1'}), $templateFactory);
let ng2ViewConfig: ViewConfig = <any> new Ng2ViewConfig(<any> path, <any> Object.assign({}, config, { $type: 'ng2'}));
const ng1ViewConfig: ViewConfig = <any> new Ng1ViewConfig(<any> path, <any> Object.assign({}, config, { $type: 'ng1'}), $templateFactory);
const ng2ViewConfig: ViewConfig = <any> new Ng2ViewConfig(<any> path, <any> Object.assign({}, config, { $type: 'ng2'}));

return [ ng2ViewConfig, ng1ViewConfig ];
});
Expand All @@ -308,8 +307,8 @@ function applyHybridAdapter(ng2Injector: Injector) {
* which that provides a ng1 -> ng2 boundary in the component tree.
*/
upgradeModule.config(['$stateProvider', ($stateProvider: StateProvider) => {
$stateProvider.decorator('views', function(state: State, parentFn: Function) {
let views = parentFn(state);
$stateProvider.decorator('views', function(state: StateObject, parentFn: Function) {
const views = parentFn(state);

forEach(views, (viewDecl: any, viewName: string) => {
if (viewDecl.$type === 'ng1-to-ng2' || isNg2ComponentClass(viewDecl.component)) {
Expand All @@ -327,19 +326,19 @@ upgradeModule.config(['$stateProvider', ($stateProvider: StateProvider) => {

// UI-Router ViewConfig factories take a view declaration object from a state.views: { foo: <ViewDeclaration> }
// and return a runtime config object (a ViewConfig)
upgradeModule.run(['$view', ($view: ViewService) => {
upgradeModule.run(['$view', '$templateFactory', ($view: ViewService, $templateFactory: any) => {
// Register a ViewConfig factory for views of type `ng2`
$view.viewConfigFactory('ng2', (path: PathNode[], config: Ng2ViewDeclaration) => new Ng2ViewConfig(path, config));
$view._pluginapi._viewConfigFactory('ng2', (path: PathNode[], config: Ng2ViewDeclaration) => new Ng2ViewConfig(path, config));

// Register a ViewConfig factory for views of type `ng1-to-ng2`.
// Returns both an ng1 config and an ng2 config allowing either ng1 or ng2 ui-view components to be targeted.
$view.viewConfigFactory('ng1-to-ng2', (path: PathNode[], config: Ng2ViewDeclaration) => {
var ng1ViewConfig: ViewConfig = <any> new Ng1ViewConfig(<any> path, <any> Object.assign({}, config, { $type: 'ng1'}));
var ng2ViewConfig: ViewConfig = <any> new Ng2ViewConfig(<any> path, <any> Object.assign({}, config, { $type: 'ng2'}));
$view._pluginapi._viewConfigFactory('ng1-to-ng2', (path: PathNode[], config: Ng2ViewDeclaration) => {
const ng1ViewConfig: ViewConfig = <any> new Ng1ViewConfig(<any> path, <any> Object.assign({}, config, { $type: 'ng1'}), $templateFactory);
const ng2ViewConfig: ViewConfig = <any> new Ng2ViewConfig(<any> path, <any> Object.assign({}, config, { $type: 'ng2'}));

return [ ng2ViewConfig, ng1ViewConfig ];
});
}])
}]);

/** Predicate fn that returns true if an object is a NG2 Component Class */
export function isNg2ComponentClass(def: any) {
Expand Down

0 comments on commit 4a0dcaf

Please sign in to comment.