Skip to content

Commit 3514cfd

Browse files
docs(vanilla): Added docs for vanilla code
1 parent f64aace commit 3514cfd

File tree

8 files changed

+103
-3
lines changed

8 files changed

+103
-3
lines changed

src/router.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ export class UIRouter {
6969
this.disposable(this.urlRouterProvider);
7070
this.disposable(this.urlRouter);
7171
this.disposable(this.stateRegistry);
72-
7372
}
7473

74+
/** @hidden */
7575
private _plugins: { [key: string]: UIRouterPlugin } = {};
7676

7777
/**

src/vanilla/$injector.ts

+63-1
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,74 @@
1+
/** @internalapi @module vanilla */ /** */
12
import {
23
extend, assertPredicate, isFunction, isArray, isInjectable, $InjectorLike, IInjectable
34
} from "../common/module";
45

5-
/** angular1-like injector api */
66
// globally available injectables
77
let globals = {};
88
let STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
99
let ARGUMENT_NAMES = /([^\s,]+)/g;
1010

11+
/**
12+
* A basic angular1-like injector api
13+
*
14+
* This object implements four methods similar to the
15+
* [angular 1 dependency injector](https://docs.angularjs.org/api/auto/service/$injector)
16+
*
17+
* UI-Router evolved from an angular 1 library to a framework agnostic library.
18+
* However, some of the `ui-router-core` code uses these ng1 style APIs to support ng1 style dependency injection.
19+
*
20+
* This object provides a naive implementation of a globally scoped dependency injection system.
21+
* It supports the following DI approaches:
22+
*
23+
* ### Function parameter names
24+
*
25+
* A function's `.toString()` is called, and the parameter names are parsed.
26+
* This only works when the parameter names aren't "mangled" by a minifier such as UglifyJS.
27+
*
28+
* ```js
29+
* function injectedFunction(FooService, BarService) {
30+
* // FooService and BarService are injected
31+
* }
32+
* ```
33+
*
34+
* ### Function annotation
35+
*
36+
* A function may be annotated with an array of dependency names as the `$inject` property.
37+
*
38+
* ```js
39+
* injectedFunction.$inject = [ 'FooService', 'BarService' ];
40+
* function injectedFunction(fs, bs) {
41+
* // FooService and BarService are injected as fs and bs parameters
42+
* }
43+
* ```
44+
*
45+
* ### Array notation
46+
*
47+
* An array provides the names of the dependencies to inject (as strings).
48+
* The function is the last element of the array.
49+
*
50+
* ```js
51+
* [ 'FooService', 'BarService', function (fs, bs) {
52+
* // FooService and BarService are injected as fs and bs parameters
53+
* }]
54+
* ```
55+
*
56+
* @type {$InjectorLike}
57+
*/
1158
export const $injector = {
59+
/** Gets an object from DI based on a string token */
1260
get: name => globals[name],
1361

62+
/** Returns true if an object named `name` exists in global DI */
1463
has: (name) => $injector.get(name) != null,
1564

65+
/**
66+
* Injects a function
67+
*
68+
* @param fn the function to inject
69+
* @param context the function's `this` binding
70+
* @param locals An object with additional DI tokens and values, such as `{ someToken: { foo: 1 } }`
71+
*/
1672
invoke: (fn: IInjectable, context?, locals?) => {
1773
let all = extend({}, globals, locals || {});
1874
let params = $injector.annotate(fn);
@@ -22,6 +78,12 @@ export const $injector = {
2278
else return (fn as any[]).slice(-1)[0].apply(context, args);
2379
},
2480

81+
/**
82+
* Returns a function's dependencies
83+
*
84+
* Analyzes a function (or array) and returns an array of DI tokens that the function requires.
85+
* @return an array of `string`s
86+
*/
2587
annotate: (fn: IInjectable): any[] => {
2688
if (!isInjectable(fn)) throw new Error(`Not an injectable function: ${fn}`);
2789
if (fn && (fn as any).$inject) return (fn as any).$inject;

src/vanilla/$q.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
1+
/** @internalapi @module vanilla */ /** */
12
import { isArray, isObject, $QLike } from "../common/module";
23

3-
/** $q-like promise api */
4+
/**
5+
* An angular1-like promise api
6+
*
7+
* This object implements four methods similar to the
8+
* [angular 1 promise api](https://docs.angularjs.org/api/ng/service/$q)
9+
*
10+
* UI-Router evolved from an angular 1 library to a framework agnostic library.
11+
* However, some of the `ui-router-core` code uses these ng1 style APIs to support ng1 style dependency injection.
12+
*
13+
* This API provides native ES6 promise support wrapped as a $q-like API.
14+
* Internally, UI-Router uses this $q object to perform promise operations.
15+
* The `angular-ui-router` (ui-router for angular 1) uses the $q API provided by angular.
16+
*
17+
* $q-like promise api
18+
*/
419
export const $q = {
20+
/** Normalizes a value as a promise */
521
when: (val) => new Promise((resolve, reject) => resolve(val)),
622

23+
/** Normalizes a value as a promise rejection */
724
reject: (val) => new Promise((resolve, reject) => { reject(val); }),
825

26+
/** @returns a deferred object, which has `resolve` and `reject` functions */
927
defer: () => {
1028
let deferred: any = {};
1129
deferred.promise = new Promise((resolve, reject) => {
@@ -15,6 +33,7 @@ export const $q = {
1533
return deferred;
1634
},
1735

36+
/** Like Promise.all(), but also supports object key/promise notation like $q */
1837
all: (promises: { [key: string]: Promise<any> } | Promise<any>[]) => {
1938
if (isArray(promises)) {
2039
return new Promise((resolve, reject) => {

src/vanilla/hashLocation.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @internalapi @module vanilla */ /** */
12
import { isDefined } from '../common/module';
23
import { LocationConfig, LocationServices } from '../common/coreservices';
34
import { splitHash, splitQuery, trimHashVal, getParams, locationPluginFactory } from './utils';
@@ -7,6 +8,7 @@ import { LocationPlugin } from "./interface";
78
let hashPrefix: string = '';
89
let baseHref: string = '';
910

11+
/** A `LocationConfig` that delegates to the browser's `location` object */
1012
export const hashLocationConfig: LocationConfig = {
1113
port: () =>
1214
parseInt(location.port),
@@ -26,6 +28,7 @@ export const hashLocationConfig: LocationConfig = {
2628
}
2729
};
2830

31+
/** A `LocationServices` that uses the browser hash "#" to get/set the current location */
2932
export const hashLocationService: LocationServices = {
3033
hash: () =>
3134
splitHash(trimHashVal(location.hash))[1],
@@ -42,5 +45,6 @@ export const hashLocationService: LocationServices = {
4245
}
4346
};
4447

48+
/** A `UIRouterPlugin` uses the browser hash to get/set the current location */
4549
export const hashLocationPlugin: (router: UIRouter) => LocationPlugin =
4650
locationPluginFactory('vanilla.hashBangLocation', hashLocationService, hashLocationConfig);

src/vanilla/interface.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @internalapi @module vanilla */ /** */
12
import { LocationConfig, LocationServices } from '../common/coreservices';
23
import { UIRouterPlugin } from "../interface";
34
import { $InjectorLike, $QLike } from "../common/module";

src/vanilla/memoryLocation.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @internalapi @module vanilla */ /** */
12
import { services, isDefined } from '../common/module';
23
import { LocationConfig, LocationServices } from '../common/coreservices';
34
import { splitQuery, trimHashVal, getParams, splitHash, locationPluginFactory } from './utils';
@@ -7,6 +8,7 @@ import { LocationPlugin } from "./interface";
78
import { isArray } from "../common/predicates";
89

910
var mlc;
11+
/** A `LocationConfig` mock that gets/sets all config from an in-memory object */
1012
export const memoryLocationConfig: LocationConfig = mlc = {
1113
_hashPrefix: '',
1214
_baseHref: '',
@@ -28,6 +30,7 @@ export const memoryLocationConfig: LocationConfig = mlc = {
2830
};
2931

3032
var mls;
33+
/** A `LocationServices` that gets/sets the current location from an in-memory object */
3134
export const memoryLocationService: LocationServices = mls = {
3235
_listeners: [],
3336
_url: {
@@ -71,5 +74,6 @@ export const memoryLocationService: LocationServices = mls = {
7174
onChange: (cb: EventListener) => (mls._listeners.push(cb), () => removeFrom(mls._listeners, cb))
7275
};
7376

77+
/** A `UIRouterPlugin` that gets/sets the current location from an in-memory object */
7478
export const memoryLocationPlugin: (router: UIRouter) => LocationPlugin =
7579
locationPluginFactory("vanilla.memoryLocation", memoryLocationService, memoryLocationConfig);

src/vanilla/pushStateLocation.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @internalapi @module vanilla */ /** */
12
import { services, isDefined } from '../common/module';
23
import { LocationConfig, LocationServices } from '../common/coreservices';
34
import { splitQuery, trimHashVal, getParams, locationPluginFactory } from './utils';
@@ -7,6 +8,8 @@ import { UIRouter } from "../router";
78
let hashPrefix: string = '';
89
let baseHref: string = '';
910

11+
/** A `LocationConfig` mock that gets/sets all config from the location
12+
* TODO: Merge with LocationConfig impl in hasLocation.ts */
1013
export const pushStateLocationConfig: LocationConfig = {
1114
port: () =>
1215
parseInt(location.port),
@@ -26,6 +29,11 @@ export const pushStateLocationConfig: LocationConfig = {
2629
}
2730
};
2831

32+
/**
33+
* A `LocationServices` that gets/sets the current location using the browser's `location` and `history` apis
34+
*
35+
* Uses `history.pushState` and `history.replaceState`
36+
*/
2937
export const pushStateLocationService: LocationServices = {
3038
hash: () =>
3139
trimHashVal(location.hash),
@@ -50,6 +58,7 @@ export const pushStateLocationService: LocationServices = {
5058
}
5159
};
5260

61+
/** A `UIRouterPlugin` that gets/sets the current location using the browser's `location` and `history` apis */
5362
export const pushStateLocationPlugin: (router: UIRouter) => LocationPlugin =
5463
locationPluginFactory("vanilla.pushStateLocation", pushStateLocationService, pushStateLocationConfig);
5564

src/vanilla/utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @internalapi @module vanilla */ /** */
12
import {isArray} from "../common/module";
23
import { LocationServices, LocationConfig, services } from "../common/coreservices";
34
import { UIRouter } from "../router";

0 commit comments

Comments
 (0)