Skip to content

Commit 002e8d1

Browse files
refactor(common): simplify references to angular.* functions
refactor(*): for typescript 1.8: export all interfaces/classes/enums exposed externally refactor(*): for typescript 1.8: switch up /// <reference paths refactor(Router): rename to UIRouter to avoid same name as component Router refactor(*): Switch types from angular IPromise to ES6 Promise
1 parent b3bd2fe commit 002e8d1

30 files changed

+65
-75
lines changed

src/common/common.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
import {isFunction, isString, isArray, isRegExp, isDate} from "./predicates";
44
import { all, pattern, any, not, prop, curry, val } from "./hof";
55

6-
let angular = (<any> window).angular;
7-
export const fromJson = angular && angular.fromJson || _fromJson;
8-
export const toJson = angular && angular.toJson || _toJson;
9-
export const copy = angular && angular.copy || _copy;
10-
export const forEach = angular && angular.forEach || _forEach;
11-
export const extend = angular && angular.extend || _extend;
12-
export const equals = angular && angular.equals || _equals;
6+
let angular = (<any> window).angular || {};
7+
export const fromJson = angular.fromJson || _fromJson;
8+
export const toJson = angular.toJson || _toJson;
9+
export const copy = angular.copy || _copy;
10+
export const forEach = angular.forEach || _forEach;
11+
export const extend = angular.extend || _extend;
12+
export const equals = angular.equals || _equals;
1313
export const identity = (x) => x;
1414
export const noop = () => undefined;
1515

16-
type Mapper<X, T> = (x: X, key?: (string|number)) => T;
16+
export type Mapper<X, T> = (x: X, key?: (string|number)) => T;
1717
export interface TypedMap<T> { [key: string]: T; }
1818
export type Predicate<X> = (X) => boolean;
1919
export type IInjectable = (Function|any[]);
@@ -570,3 +570,9 @@ function _arraysEq(a1, a2) {
570570
if (a1.length !== a2.length) return false;
571571
return arrayTuples(a1, a2).reduce((b, t) => b && _equals(t[0], t[1]), true);
572572
}
573+
//
574+
//const _addToGroup = (result, keyFn) => (item) =>
575+
// (result[keyFn(item)] = result[keyFn(item)] || []).push(item) && result;
576+
//const groupBy = (array, keyFn) => array.reduce((memo, item) => _addToGroup(memo, keyFn), {});
577+
//
578+
//

src/common/coreservices.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let services: CoreServices = {
2727
["port", "protocol", "host", "baseHref", "html5Mode", "hashPrefix" ]
2828
.forEach(key => services.locationConfig[key] = notImplemented(key));
2929

30-
interface CoreServices {
30+
export interface CoreServices {
3131
$q; // : IQService;
3232
$injector; // : IInjectorService;
3333
/** Services related to getting or setting the browser location (url) */
@@ -37,7 +37,7 @@ interface CoreServices {
3737
template: TemplateServices;
3838
}
3939

40-
interface LocationServices {
40+
export interface LocationServices {
4141
replace(): void;
4242
url(newurl: string): string;
4343
url(): string;
@@ -47,7 +47,7 @@ interface LocationServices {
4747
onChange(callback: Function): Function;
4848
}
4949

50-
interface LocationConfig {
50+
export interface LocationConfig {
5151
port(): number;
5252
protocol(): string;
5353
host(): string;
@@ -58,7 +58,7 @@ interface LocationConfig {
5858
hashPrefix(newprefix: string): string;
5959
}
6060

61-
interface TemplateServices {
61+
export interface TemplateServices {
6262
get(url: string): string;
6363
}
6464

src/common/trace.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ function stringify(o) {
4343
return JSON.stringify(o, (key, val) => format(val)).replace(/\\"/g, '"');
4444
}
4545

46-
enum Category {
46+
export enum Category {
4747
RESOLVE, TRANSITION, HOOK, INVOKE, UIVIEW, VIEWCONFIG
4848
}
4949

50-
class Trace {
50+
export class Trace {
5151
approximateDigests: number;
5252

5353
constructor() {

src/ng1.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference path='../typings/angularjs/angular.d.ts' />
2+
/// <reference path='../typings/es6-shim/es6-shim.d.ts' />
13
/**
24
* Main entry point for angular 1.x build
35
*/

src/ng1/services.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
* @preferred
1010
*/
1111

12-
/** for typedoc */
1312
/// <reference path='../../typings/angularjs/angular.d.ts' />
14-
import {Router} from "../router";
13+
/// <reference path='../../typings/es6-shim/es6-shim.d.ts' />
14+
15+
/** for typedoc */
16+
import {UIRouter} from "../router";
1517
import {services} from "../common/coreservices";
1618
import {map, bindFunctions, removeFrom, find, noop} from "../common/common";
1719
import {prop, propEq} from "../common/hof";
@@ -148,14 +150,14 @@ function runBlock($injector, $q) {
148150

149151
app.run(runBlock);
150152

151-
let router: Router = null;
153+
let router: UIRouter = null;
152154

153155
ng1UIRouter.$inject = ['$locationProvider'];
154156
/** This angular 1 provider instantiates a Router and exposes its services via the angular injector */
155157
function ng1UIRouter($locationProvider) {
156158

157159
// Create a new instance of the Router when the ng1UIRouterProvider is initialized
158-
router = new Router();
160+
router = new UIRouter();
159161

160162
// Bind LocationConfig.hashPrefix to $locationProvider.hashPrefix
161163
bindFunctions($locationProvider, services.locationConfig, $locationProvider, ['hashPrefix']);

src/ng1/stateEvents.ts

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
*/
1919

2020
/** for typedoc */
21-
/// <reference path='../../typings/angularjs/angular.d.ts' />
22-
2321
import {IServiceProviderFactory} from "angular";
2422
import {StateService, StateProvider} from "../state/interface";
2523
import {TargetState} from "../state/module";

src/ng1/stateFilters.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/** @module state */ /** for typedoc */
2-
/// <reference path='../../typings/angularjs/angular.d.ts' />
32

43
/**
54
* @ngdoc filter

src/ng1/viewDirective.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
var ngMajorVer = angular.version.major;
22
var ngMinorVer = angular.version.minor;
33
/** @module view */ /** for typedoc */
4-
/// <reference path='../../typings/angularjs/angular.d.ts' />
5-
64
import {extend} from "../common/common";
75
import {isDefined} from "../common/predicates";
86
import {trace} from "../common/trace";

src/ng1/viewScroll.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/** @module view */ /** for typedoc */
2-
/// <reference path='../../typings/angularjs/angular.d.ts' />
32
import {IServiceProviderFactory} from "angular";
43

54
/**

src/params/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export interface ParamDeclaration {
214214
dynamic: boolean;
215215
}
216216

217-
interface Replace {
217+
export interface Replace {
218218
from: string;
219219
to: string;
220220
}

src/params/paramTypes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {Type} from "./type";
1212
function valToString(val) { return val != null ? val.toString().replace(/~/g, "~~").replace(/\//g, "~2F") : val; }
1313
function valFromString(val) { return val != null ? val.toString().replace(/~2F/g, "/").replace(/~~/g, "~") : val; }
1414

15-
class ParamTypes {
15+
export class ParamTypes {
1616
types: any;
1717
enqueue: boolean = true;
1818
typeQueue: any[] = [];

src/resolve/resolvable.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
/** @module path */ /** for typedoc */
2-
/// <reference path='../../typings/angularjs/angular.d.ts' />
32
import {extend, pick, map, filter} from "../common/common";
43
import {not} from "../common/hof";
54
import {isInjectable} from "../common/predicates";
65

76
import {services} from "../common/coreservices";
87
import {trace} from "../common/trace";
9-
import {IPromise} from "angular";
108
import {Resolvables, IOptions1} from "./interface";
119

1210
import {ResolveContext} from "./resolveContext";
@@ -32,7 +30,7 @@ export class Resolvable {
3230
resolveFn: Function;
3331
deps: string[];
3432

35-
promise: IPromise<any> = undefined;
33+
promise: Promise<any> = undefined;
3634
data: any;
3735

3836
// synchronous part:
@@ -80,7 +78,7 @@ export class Resolvable {
8078
});
8179
}
8280

83-
get(resolveContext: ResolveContext, options?: IOptions1): IPromise<any> {
81+
get(resolveContext: ResolveContext, options?: IOptions1): Promise<any> {
8482
return this.promise || this.resolveResolvable(resolveContext, options);
8583
}
8684

src/resolve/resolveContext.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/** @module path */ /** for typedoc */
2-
/// <reference path='../../typings/angularjs/angular.d.ts' />
32
import {IInjectable, find, filter, map, tail, defaults, extend, pick, omit} from "../common/common";
43
import {prop, propEq} from "../common/hof";
54
import {isString, isObject} from "../common/predicates";
65
import {trace} from "../common/trace";
76
import {services} from "../common/coreservices";
8-
import {IPromise} from "angular";
97
import {Resolvables, ResolvePolicy, IOptions1} from "./interface";
108

119
import {Node} from "../path/module";
@@ -17,7 +15,7 @@ import {mergeR} from "../common/common";
1715
let defaultResolvePolicy = ResolvePolicy[ResolvePolicy.LAZY];
1816

1917
interface IPolicies { [key: string]: string; }
20-
interface IPromises { [key: string]: IPromise<any>; }
18+
interface Promises { [key: string]: Promise<any>; }
2119

2220
export class ResolveContext {
2321

@@ -93,7 +91,7 @@ export class ResolveContext {
9391
}
9492

9593
// Returns a promise for an array of resolved path Element promises
96-
resolvePath(options: IOptions1 = {}): IPromise<any> {
94+
resolvePath(options: IOptions1 = {}): Promise<any> {
9795
trace.traceResolvePath(this._path, options);
9896
const promiseForNode = (node: Node) => this.resolvePathElement(node.state, options);
9997
return services.$q.all(<any> map(this._path, promiseForNode)).then(all => all.reduce(mergeR, {}));
@@ -103,7 +101,7 @@ export class ResolveContext {
103101
// options.resolvePolicy: only return promises for those Resolvables which are at
104102
// the specified policy, or above. i.e., options.resolvePolicy === 'lazy' will
105103
// resolve both 'lazy' and 'eager' resolves.
106-
resolvePathElement(state: State, options: IOptions1 = {}): IPromise<any> {
104+
resolvePathElement(state: State, options: IOptions1 = {}): Promise<any> {
107105
// The caller can request the path be resolved for a given policy and "below"
108106
let policy: string = options && options.resolvePolicy;
109107
let policyOrdinal: number = ResolvePolicy[policy || defaultResolvePolicy];
@@ -114,7 +112,7 @@ export class ResolveContext {
114112
let matchingResolves = filter(resolvables, matchesRequestedPolicy);
115113

116114
const getResolvePromise = (resolvable: Resolvable) => resolvable.get(this.isolateRootTo(state), options);
117-
let resolvablePromises: IPromises = <any> map(matchingResolves, getResolvePromise);
115+
let resolvablePromises: Promises = <any> map(matchingResolves, getResolvePromise);
118116

119117
trace.traceResolvePathElement(this, matchingResolves, options);
120118

@@ -135,11 +133,11 @@ export class ResolveContext {
135133
* @param locals: are the angular $injector-style locals to inject
136134
* @param options: options (TODO: document)
137135
*/
138-
invokeLater(fn: IInjectable, locals: any = {}, options: IOptions1 = {}): IPromise<any> {
136+
invokeLater(fn: IInjectable, locals: any = {}, options: IOptions1 = {}): Promise<any> {
139137
let resolvables = this.getResolvablesForFn(fn);
140138
trace.tracePathElementInvoke(tail(this._path), fn, Object.keys(resolvables), extend({when: "Later"}, options));
141139
const getPromise = (resolvable: Resolvable) => resolvable.get(this, options);
142-
let promises: IPromises = <any> map(resolvables, getPromise);
140+
let promises: Promises = <any> map(resolvables, getPromise);
143141

144142
return services.$q.all(promises).then(() => {
145143
try {

src/resolve/resolveInjector.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {map} from "../common/common";
44
import {Resolvable} from "./resolvable";
55
import {ResolveContext} from "./resolveContext";
66
import {State} from "../state/module";
7+
import {TypedMap} from "../common/common";
78

89
export class ResolveInjector {
910
constructor(private _resolveContext: ResolveContext, private _state: State) { }

src/router.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {UrlMatcherFactory} from "./url/urlMatcherFactory";
22
import {UrlRouterProvider} from "./url/urlRouter";
33
import {StateProvider} from "./state/state";
44
import {stateParamsFactory} from "./params/stateParams";
5+
import {StateParams} from "./params/stateParams";
56
import {UrlRouter} from "./url/urlRouter";
67
import {TransitionService} from "./transition/transitionService";
78
import {TemplateFactory} from "./view/templateFactory";
@@ -18,7 +19,7 @@ import {StateService} from "./state/stateService";
1819
* your app states with the [[stateRegistry]] (and set url options using ...). Then, tell UI-Router to monitor
1920
* the URL by calling `urlRouter.listen()` ([[URLRouter.listen]])
2021
*/
21-
class Router {
22+
class UIRouter {
2223

2324
stateParams = stateParamsFactory();
2425

@@ -46,4 +47,4 @@ class Router {
4647
}
4748
}
4849

49-
export { Router };
50+
export { UIRouter };

src/state/hooks/resolveHooks.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ export class ResolveHooks {
2424
let treeChanges = this.transition.treeChanges();
2525

2626
/** a function which resolves any EAGER Resolvables for a Path */
27-
$eagerResolvePath.$inject = ['$transition$'];
27+
(<any> $eagerResolvePath).$inject = ['$transition$'];
2828
function $eagerResolvePath($transition$) {
2929
return tail(<any[]> treeChanges.to).resolveContext.resolvePath(extend({ transition: $transition$ }, { resolvePolicy: EAGER }));
3030
}
3131

3232
/** Returns a function which pre-resolves any LAZY Resolvables for a Node in a Path */
33-
$lazyResolveEnteringState.$inject = ['$state$', '$transition$'];
33+
(<any> $lazyResolveEnteringState).$inject = ['$state$', '$transition$'];
3434
function $lazyResolveEnteringState($state$, $transition$) {
3535
let node = find(<any[]> treeChanges.entering, propEq('state', $state$));
3636
return node.resolveContext.resolvePathElement(node.state, extend({transition: $transition$}, { resolvePolicy: LAZY }));

src/state/hooks/transitionManager.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/** @module state */ /** for typedoc */
2-
import {IPromise, IQService} from "angular";
32
import {copy} from "../../common/common";
43
import {prop} from "../../common/hof";
54
import {Queue} from "../../common/queue";
@@ -45,7 +44,7 @@ export class TransitionManager {
4544
private $view, // service
4645
private $state: StateService,
4746
private $stateParams, // service/obj
48-
private $q: IQService, // TODO: get from runtime.$q
47+
private $q, // TODO: get from runtime.$q
4948
private activeTransQ: Queue<Transition>,
5049
private changeHistory: Queue<TreeChanges>
5150
) {
@@ -61,7 +60,7 @@ export class TransitionManager {
6160
this.resolveHooks.registerHooks();
6261
}
6362

64-
runTransition(): IPromise<any> {
63+
runTransition(): Promise<any> {
6564
this.activeTransQ.clear(); // TODO: nuke this
6665
this.activeTransQ.enqueue(this.transition);
6766
this.$state.transition = this.transition;
@@ -92,7 +91,7 @@ export class TransitionManager {
9291
this.updateStateParams();
9392
}
9493

95-
transRejected(error): (StateDeclaration|IPromise<any>) {
94+
transRejected(error): (StateDeclaration|Promise<any>) {
9695
let {transition, $state, $stateParams, $q} = this;
9796
// Handle redirect and abort
9897
if (error instanceof TransitionRejection) {

src/state/hooks/viewHooks.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/** @module state */ /** for typedoc */
2-
import {IPromise} from "angular";
32
import {find, noop} from "../../common/common";
43
import {propEq} from "../../common/hof";
54
import {services} from "../../common/coreservices";
@@ -15,7 +14,7 @@ export class ViewHooks {
1514
private enteringViews: ViewConfig[];
1615
private exitingViews: ViewConfig[];
1716
private transition: Transition;
18-
private $view; // service
17+
private $view: ViewService; // service
1918

2019
constructor(transition: Transition, $view: ViewService) {
2120
this.transition = transition;
@@ -29,7 +28,7 @@ export class ViewHooks {
2928
loadAllEnteringViews() {
3029
const loadView = (vc: ViewConfig) => {
3130
let resolveInjector = find(this.treeChanges.to, propEq('state', vc.context)).resolveInjector;
32-
return <IPromise<ViewConfig>> this.$view.load(vc, resolveInjector);
31+
return <Promise<ViewConfig>> this.$view.load(vc, resolveInjector);
3332
};
3433
return services.$q.all(this.enteringViews.map(loadView)).then(noop);
3534
}

src/state/interface.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/** @module state */ /** for typedoc */
2-
import {IPromise} from "angular";
3-
42
import {TransitionOptions} from "../transition/interface";
53
import {ParamDeclaration, RawParams, ParamsOrArray} from "../params/interface";
64

@@ -417,10 +415,10 @@ export interface StateService {
417415
current: StateDeclaration;
418416
$current: State;
419417
transition: Transition;
420-
reload (stateOrName: StateOrName): IPromise<State>;
418+
reload (stateOrName: StateOrName): Promise<State>;
421419
target (identifier: StateOrName, params: ParamsOrArray, options: TransitionOptions): TargetState;
422-
go (to: StateOrName, params: RawParams, options: TransitionOptions): IPromise<State>;
423-
transitionTo (to: StateOrName, toParams: ParamsOrArray, options: TransitionOptions): IPromise<State>;
420+
go (to: StateOrName, params: RawParams, options: TransitionOptions): Promise<State>;
421+
transitionTo (to: StateOrName, toParams: ParamsOrArray, options: TransitionOptions): Promise<State>;
424422
is (stateOrName: StateOrName, params?: RawParams, options?: TransitionOptions): boolean;
425423
includes (stateOrName: StateOrName, params?: RawParams, options?: TransitionOptions): boolean;
426424
href (stateOrName: StateOrName, params?: RawParams, options?: HrefOptions): string;

src/state/state.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {isObject} from "../common/predicates";
33
import {bindFunctions} from "../common/common";
44
import {BuilderFunction} from "./module";
55
import {StateRegistry} from "./stateRegistry";
6+
import {State} from "./stateObject";
67

78
/**
89
* @ngdoc object

0 commit comments

Comments
 (0)