Skip to content

Commit e31e14d

Browse files
refactor($view): implement ui-view/viewConfig registry
1 parent e6d12e4 commit e31e14d

File tree

11 files changed

+244
-309
lines changed

11 files changed

+244
-309
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"email": "[email protected]",
1010
"web": "https://radify.io"
1111
},
12+
{
13+
"name": "Chris Thielen",
14+
"web": "https://github.com/christopherthielen"
15+
},
1216
{
1317
"name": "Tim Kindberg",
1418
"web": "https://github.com/timkindberg"
@@ -63,7 +67,7 @@
6367
"load-grunt-tasks": "~0.4.0",
6468
"grunt-conventional-changelog": "~1.1.0",
6569
"grunt-ngdocs": "~0.1.7",
66-
"grunt-ts": "https://github.com/christopherthielen/grunt-ts/tarball/2529971636e0eada4b81712f8d2f12fa89545ca8",
70+
"grunt-ts": "~4.2.0",
6771
"grunt-webpack": "^1.0.10"
6872
},
6973
"main": "release/angular-ui-router"

src/common/common.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ var { isDefined, isFunction, isNumber, isString, isObject, isArray, forEach, ext
33
export { isDefined, isFunction, isNumber, isString, isObject, isArray, forEach, extend, copy, noop, toJson, fromJson, equals, identity };
44
"use strict";
55

6+
export interface Predicate {
7+
(any): boolean;
8+
}
9+
export interface F {
10+
(any): any;
11+
}
12+
export interface HOF {
13+
(fn1: F, fn2: F): F
14+
}
15+
616
export var abstractKey = 'abstract';
717
export function inherit(parent, extra) {
818
return extend(new (extend(function() {}, { prototype: parent }))(), extra);
@@ -89,6 +99,12 @@ export function indexOf(array, value) {
8999
return -1;
90100
}
91101

102+
export const removeFrom = (array) => (obj) => {
103+
var idx = array.indexOf(obj);
104+
if (idx >= 0) array.splice(idx, 1);
105+
return array;
106+
};
107+
92108
/**
93109
* Merges a set of parameters with all parameters inherited between the common parents of the
94110
* current state and a given destination state.
@@ -207,9 +223,8 @@ export function filter<T>(collection: T, callback: Function): T {
207223
return <T>(arr ? resultarray : resultobj);
208224
}
209225

210-
export function _filter(callback) {
211-
return function (collection) { return filter(collection, callback); };
212-
}
226+
export const _filter = (callback) =>
227+
(collection) => filter(collection, callback);
213228

214229
export function find(collection, callback) {
215230
var result;
@@ -231,9 +246,8 @@ export function map<T> (collection: T, callback): T {
231246
return <T> result;
232247
}
233248

234-
export function _map(callback) {
235-
return function mapper(collection) { return map(collection, callback); };
236-
}
249+
export const _map = (callback) =>
250+
(collection) => map(collection, callback);
237251

238252
export function unnest(list) {
239253
var result = [];
@@ -326,7 +340,7 @@ export function pipe(...funcs: Function[]) {
326340
return compose.apply(null, [].slice.call(arguments).reverse());
327341
}
328342

329-
export function prop(name): Function {
343+
export function prop(name): F {
330344
return function(obj) { return obj && obj[name]; };
331345
}
332346

@@ -350,22 +364,22 @@ export function or(fn1, fn2): Function {
350364
};
351365
}
352366

353-
export function is(ctor): Function {
367+
export function is(ctor): Predicate {
354368
return function(val) { return val != null && val.constructor === ctor || val instanceof ctor; };
355369
}
356370

357-
export function eq(comp): Function {
371+
export function eq(comp): Predicate {
358372
return function(val) { return val === comp; };
359373
}
360374

361-
export function isEq(fn1, fn2): Function {
375+
export function isEq(fn1: F, fn2: F): () => boolean {
362376
return function() {
363377
var args = [].slice.call(arguments);
364378
return fn1.apply(null, args) === fn2.apply(null, args);
365379
};
366380
}
367381

368-
export function val(v): Function {
382+
export function val(v): F {
369383
return function() { return v; };
370384
}
371385

src/state/state.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactoryProvider) {
786786

787787
root.navigable = null;
788788

789+
$view.rootContext(root);
790+
789791
extend($state, {
790792
params: new StateParams(),
791793
current: root.self,
@@ -1016,15 +1018,11 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactoryProvider) {
10161018
},
10171019

10181020
transitionSuccess: function transitionSuccess(transition) {
1019-
// TODO: sync on entering state, not transition success?
1020-
$view.sync(transition.views());
1021-
// TODO: Refactor this stuff. Maybe a Path can return views() for elements
1022-
transition.exiting().forEach(function(state) {
1023-
forEach(state.views, function (view, key) {
1024-
var found = $view.find(key, state.parent);
1025-
found && $view.reset(found);
1026-
});
1027-
});
1021+
// TODO: sync on entering/exiting state, not transition success?
1022+
transition.views("exiting").forEach($view.reset.bind($view));
1023+
$view.sync();
1024+
transition.views("entering").forEach($view.registerStateViewConfig.bind($view));
1025+
$view.sync();
10281026

10291027
// Update globals in $state
10301028
$state.$current = transition.$to().$state();

src/transition/transition.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import PathElement from "../resolve/pathElement";
1313
import {RejectFactory} from "./rejectFactory"
1414
import {StateParams, StateReference, IState, IPublicState} from "../state/state";
1515
import {ViewContext} from "../view/viewContext";
16+
import {StateViewConfig} from "../view/view";
1617
import {defaults, eq, extend, filter, flatten, forEach, identity, invoke, is, isEq, isFunction, isObject, isPromise, isDefined,
17-
map, noop, not, objectKeys, parse, pattern, pipe, pluck, prop, toJson, unnest, unroll, val} from "../common/common";
18+
map, noop, not, objectKeys, parse, pattern, pipe, pluck, prop, toJson, unnest, unroll, val, pairs} from "../common/common";
1819

1920
var transitionCount = 0, REJECT = new RejectFactory();
2021

@@ -261,13 +262,15 @@ export class Transition {
261262
return new ViewContext(pathElement, this._treeChanges.to, this._options, runtime.$injector);
262263
}
263264

264-
views(states) {
265-
states = states || this._treeChanges.entering.states();
265+
views(pathname: string = "entering") {
266+
var path = this._treeChanges[pathname];
267+
var states = states || path.states();
268+
var params = this._to.params();
266269

267270
return unnest(map(states, (state) => {
268-
var elem = this._treeChanges.to.elementForState(state);
269-
var toList = unroll((view) => [this.context(elem), view, this._to.params()]);
270-
return toList(state.views);
271+
var context = state, locals = this.context(path.elementForState(state));
272+
const makeViewConfig = ([name, view]) => { return {name, view, context, locals, params} };
273+
return pairs(state.views).map(makeViewConfig)
271274
}));
272275
}
273276

src/transition/transitionService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface ITransitionOptions {
1616
trace ?: boolean,
1717
custom ?: any,
1818
previous ?: Transition,
19-
current() : Transition
19+
current ?: () => Transition
2020
}
2121

2222
/**

0 commit comments

Comments
 (0)