Skip to content

Commit 6c42285

Browse files
docs(view): Document ViewService
refactor(view): Reorganize private code into pluginapi chore(test): Update to awesome-typescript-loader 3.0.0-beta.10
1 parent 62809d9 commit 6c42285

File tree

4 files changed

+63
-27
lines changed

4 files changed

+63
-27
lines changed

karma.conf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = function (karma) {
4343

4444
module: {
4545
loaders: [
46-
{ test: /\.ts$/, loader: "awesome-typescript-loader?declaration=false&tsconfig=test/tsconfig.json" }
46+
{ test: /\.ts$/, loader: "awesome-typescript-loader?declaration=false&configFileName=test/tsconfig.json" }
4747
]
4848
},
4949

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"devDependencies": {
5555
"@types/jasmine": "^2.2.34",
5656
"@types/jquery": "^1.10.31",
57-
"awesome-typescript-loader": "^2.2.4",
57+
"awesome-typescript-loader": "3.0.0-beta.10",
5858
"conventional-changelog": "^1.1.0",
5959
"conventional-changelog-cli": "^1.1.1",
6060
"conventional-changelog-ui-router-core": "^1.3.0",
@@ -73,7 +73,7 @@
7373
"shelljs": "^0.7.0",
7474
"shx": "^0.1.4",
7575
"tslint": "=2.5.0",
76-
"typescript": "rc",
76+
"typescript": "^2.1.4",
7777
"webpack": "^1.13.3"
7878
}
7979
}

src/router.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class UIRouter {
7777
}
7878

7979
constructor() {
80-
this.viewService.rootContext(this.stateRegistry.root());
80+
this.viewService._pluginapi._rootViewContext(this.stateRegistry.root());
8181
this.globals.$current = this.stateRegistry.root();
8282
this.globals.current = this.globals.$current.self;
8383

src/view/view.ts

+59-23
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,49 @@ import {_ViewDeclaration} from "../state/interface";
1010

1111
export type ViewConfigFactory = (path: PathNode[], decl: _ViewDeclaration) => ViewConfig|ViewConfig[];
1212

13+
export interface ViewServicePluginAPI {
14+
_rootViewContext(context?: ViewContext): ViewContext;
15+
_viewConfigFactory(viewType: string, factory: ViewConfigFactory);
16+
_registeredUIViews(): ActiveUIView[];
17+
_activeViewConfigs(): ViewConfig[];
18+
}
19+
1320
/**
1421
* The View service
22+
*
23+
* This service pairs existing `ui-view` components (which live in the DOM)
24+
* with view configs (from the state declaration objects: [[StateDeclaration.views]]).
25+
*
26+
* - After a successful Transition, the views from the newly entered states are activated via [[activateViewConfig]].
27+
* The views from exited states are deactivated via [[deactivateViewConfig]].
28+
* (See: the [[registerActivateViews]] Transition Hook)
29+
*
30+
* - As `ui-view` components pop in and out of existence, they register themselves using [[registerUIView]].
31+
*
32+
* - When the [[sync]] function is called, the registered `ui-view`(s) ([[UIViewConfig]]
33+
* are configured with the matching `ViewConfig`(s) ([[ActiveUIView]]).
34+
*
1535
*/
1636
export class ViewService {
17-
private uiViews: ActiveUIView[] = [];
18-
private viewConfigs: ViewConfig[] = [];
37+
private _uiViews: ActiveUIView[] = [];
38+
private _viewConfigs: ViewConfig[] = [];
1939
private _rootContext: ViewContext;
2040
private _viewConfigFactories: { [key: string]: ViewConfigFactory } = {};
2141

2242
constructor() { }
2343

24-
rootContext(context?: ViewContext): ViewContext {
44+
public _pluginapi: ViewServicePluginAPI = {
45+
_rootViewContext: this._rootViewContext.bind(this),
46+
_viewConfigFactory: this._viewConfigFactory.bind(this),
47+
_registeredUIViews: () => this._uiViews,
48+
_activeViewConfigs: () => this._viewConfigs,
49+
};
50+
51+
private _rootViewContext(context?: ViewContext): ViewContext {
2552
return this._rootContext = context || this._rootContext;
2653
};
2754

28-
viewConfigFactory(viewType: string, factory: ViewConfigFactory) {
55+
private _viewConfigFactory(viewType: string, factory: ViewConfigFactory) {
2956
this._viewConfigFactories[viewType] = factory;
3057
}
3158

@@ -37,23 +64,26 @@ export class ViewService {
3764
}
3865

3966
/**
40-
* De-registers a ViewConfig.
67+
* Deactivates a ViewConfig.
68+
*
69+
* This function deactivates a `ViewConfig`.
70+
* After calling [[sync]], it will un-pair from any `ui-view` with which it is currently paired.
4171
*
4272
* @param viewConfig The ViewConfig view to deregister.
4373
*/
4474
deactivateViewConfig(viewConfig: ViewConfig) {
4575
trace.traceViewServiceEvent("<- Removing", viewConfig);
46-
removeFrom(this.viewConfigs, viewConfig);
47-
};
76+
removeFrom(this._viewConfigs, viewConfig);
77+
}
4878

4979
activateViewConfig(viewConfig: ViewConfig) {
5080
trace.traceViewServiceEvent("-> Registering", <any> viewConfig);
51-
this.viewConfigs.push(viewConfig);
52-
};
81+
this._viewConfigs.push(viewConfig);
82+
}
5383

54-
sync = () => {
84+
sync() {
5585
let uiViewsByFqn: TypedMap<ActiveUIView> =
56-
this.uiViews.map(uiv => [uiv.fqn, uiv]).reduce(applyPairs, <any> {});
86+
this._uiViews.map(uiv => [uiv.fqn, uiv]).reduce(applyPairs, <any> {});
5787

5888
/**
5989
* Given a ui-view and a ViewConfig, determines if they "match".
@@ -148,7 +178,7 @@ export class ViewService {
148178
const depthCompare = curry((depthFn, posNeg, left, right) => posNeg * (depthFn(left) - depthFn(right)));
149179

150180
const matchingConfigPair = (uiView: ActiveUIView) => {
151-
let matchingConfigs = this.viewConfigs.filter(matches(uiView));
181+
let matchingConfigs = this._viewConfigs.filter(matches(uiView));
152182
if (matchingConfigs.length > 1) {
153183
// This is OK. Child states can target a ui-view that the parent state also targets (the child wins)
154184
// Sort by depth and return the match from the deepest child
@@ -161,25 +191,31 @@ export class ViewService {
161191
const configureUIView = ([uiView, viewConfig]) => {
162192
// If a parent ui-view is reconfigured, it could destroy child ui-views.
163193
// Before configuring a child ui-view, make sure it's still in the active uiViews array.
164-
if (this.uiViews.indexOf(uiView) !== -1)
194+
if (this._uiViews.indexOf(uiView) !== -1)
165195
uiView.configUpdated(viewConfig);
166196
};
167197

168-
this.uiViews.sort(depthCompare(uiViewDepth, 1)).map(matchingConfigPair).forEach(configureUIView);
198+
this._uiViews.sort(depthCompare(uiViewDepth, 1)).map(matchingConfigPair).forEach(configureUIView);
169199
};
170200

171201
/**
172-
* Allows a `ui-view` element to register its canonical name with a callback that allows it to
173-
* be updated with a template, controller, and local variables.
202+
* Registers a `ui-view` component
203+
*
204+
* When a `ui-view` component is created, it uses this method to register itself.
205+
* After registration the [[sync]] method is used to ensure all `ui-view` are configured with the proper [[ViewConfig]].
206+
*
207+
* Note: the `ui-view` component uses the `ViewConfig` to determine what view should be loaded inside the `ui-view`,
208+
* and what the view's state context is.
209+
*
210+
* Note: There is no corresponding `deregisterUIView`.
211+
* A `ui-view` should hang on to the return value of `registerUIView` and invoke it to deregister itself.
174212
*
175-
* @param {String} name The fully-qualified name of the `ui-view` object being registered.
176-
* @param {Function} configUpdatedCallback A callback that receives updates to the content & configuration
177-
* of the view.
178-
* @return {Function} Returns a de-registration function used when the view is destroyed.
213+
* @param uiView The metadata for a UIView
214+
* @return a de-registration function used when the view is destroyed.
179215
*/
180216
registerUIView(uiView: ActiveUIView) {
181217
trace.traceViewServiceUIViewEvent("-> Registering", uiView);
182-
let uiViews = this.uiViews;
218+
let uiViews = this._uiViews;
183219
const fqnMatches = uiv => uiv.fqn === uiView.fqn;
184220
if (uiViews.filter(fqnMatches).length)
185221
trace.traceViewServiceUIViewEvent("!!!! duplicate uiView named:", uiView);
@@ -204,7 +240,7 @@ export class ViewService {
204240
* @return {Array} Returns an array of fully-qualified view names.
205241
*/
206242
available() {
207-
return this.uiViews.map(prop("fqn"));
243+
return this._uiViews.map(prop("fqn"));
208244
}
209245

210246
/**
@@ -213,7 +249,7 @@ export class ViewService {
213249
* @return {Array} Returns an array of fully-qualified view names.
214250
*/
215251
active() {
216-
return this.uiViews.filter(prop("$config")).map(prop("name"));
252+
return this._uiViews.filter(prop("$config")).map(prop("name"));
217253
}
218254

219255
/**

0 commit comments

Comments
 (0)