Skip to content

Commit

Permalink
refactor(Node): rename Node to PathNode (to avoid conflicts with brow…
Browse files Browse the repository at this point in the history
…ser DOM Node class
  • Loading branch information
christopherthielen committed Jun 6, 2016
1 parent b8e7912 commit 99e07b2
Show file tree
Hide file tree
Showing 24 changed files with 151 additions and 128 deletions.
6 changes: 3 additions & 3 deletions src/ng1/directives/viewDirective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {TransitionService} from "../../transition/transitionService";
import {parse} from "../../common/hof";
import {ResolveContext} from "../../resolve/resolveContext";
import {Transition} from "../../transition/transition";
import {Node} from "../../path/node";
import {PathNode} from "../../path/node";
import {Param} from "../../params/param";
import {kebobString} from "../../common/strings";
import {HookRegOptions} from "../../transition/interface";
Expand Down Expand Up @@ -401,8 +401,8 @@ function registerControllerCallbacks($transitions: TransitionService, controller

let toParams = $transition$.params("to");
let fromParams = $transition$.params("from");
let toSchema: Param[] = $transition$.treeChanges().to.map((node: Node) => node.paramSchema).reduce(unnestR, []);
let fromSchema: Param[] = $transition$.treeChanges().from.map((node: Node) => node.paramSchema).reduce(unnestR, []);
let toSchema: Param[] = $transition$.treeChanges().to.map((node: PathNode) => node.paramSchema).reduce(unnestR, []);
let fromSchema: Param[] = $transition$.treeChanges().from.map((node: PathNode) => node.paramSchema).reduce(unnestR, []);

// Find the to params that have different values than the from params
let changedToParams = toSchema.filter((param: Param) => {
Expand Down
6 changes: 3 additions & 3 deletions src/ng1/legacy/resolveService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {State} from "../../state/stateObject";
import {Node} from "../../path/node";
import {PathNode} from "../../path/node";
import {ResolveContext} from "../../resolve/resolveContext";
import {Resolvable} from "../../resolve/resolvable";
import {map} from "../../common/common";
Expand All @@ -12,8 +12,8 @@ export const resolveFactory = () => ({
* @param parent a promise for a "parent resolve"
*/
resolve: (invocables, locals = {}, parent?) => {
let parentNode = new Node(new State(<any> { params: {} }));
let node = new Node(new State(<any> { params: {} }));
let parentNode = new PathNode(new State(<any> { params: {} }));
let node = new PathNode(new State(<any> { params: {} }));
let context = new ResolveContext([parentNode, node]);

context.addResolvables(Resolvable.makeResolvables(invocables), node.state);
Expand Down
4 changes: 2 additions & 2 deletions src/ng1/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {services} from "../common/coreservices";
import {map, bindFunctions, removeFrom, find, noop} from "../common/common";
import {prop, propEq} from "../common/hof";
import {isObject} from "../common/predicates";
import {Node} from "../path/node";
import {PathNode} from "../path/node";
import {resolveFactory} from "./legacy/resolveService";
import {trace} from "../common/trace";
import {ng1ViewsBuilder, ng1ViewConfigFactory, Ng1ViewConfig} from "./statebuilders/views";
Expand Down Expand Up @@ -260,7 +260,7 @@ function getTransitionsProvider() {
// TODO: check if we can remove loadAllControllerLocals. Shouldn't be necessary without JIT resolve policy
function loadAllControllerLocals($transition$: Transition) {
const loadLocals = (vc: Ng1ViewConfig) => {
let node = (<Node> find($transition$.treeChanges().to, propEq('state', vc.viewDecl.$context)));
let node = (<PathNode> find($transition$.treeChanges().to, propEq('state', vc.viewDecl.$context)));
// Temporary fix; This whole callback should be nuked when fixing #2662
if (!node) return services.$q.when();
let resolveCtx = node.resolveContext;
Expand Down
4 changes: 2 additions & 2 deletions src/ng1/statebuilders/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {ViewService} from "../../view/view";
import {isInjectable, isDefined, isString, isObject} from "../../common/predicates";
import {services} from "../../common/coreservices";
import {trace} from "../../common/trace";
import {Node} from "../../path/node";
import {PathNode} from "../../path/node";
import {TemplateFactory} from "../templateFactory";
import {ResolveContext} from "../../resolve/resolveContext";

Expand Down Expand Up @@ -104,7 +104,7 @@ export class Ng1ViewConfig implements ViewConfig {
template: string;
locals: any; // TODO: delete me

constructor(public node: Node, public viewDecl: Ng1ViewDeclaration) { }
constructor(public node: PathNode, public viewDecl: Ng1ViewDeclaration) { }

load() {
let $q = services.$q;
Expand Down
12 changes: 6 additions & 6 deletions src/ng2/directives/uiSrefStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {Directive, Output, EventEmitter} from "@angular/core";
import {StateService} from "../../state/stateService";
import {UiSref} from "./uiSref";
import {Node} from "../../path/node";
import {PathNode} from "../../path/node";
import {TransitionService} from "../../transition/transitionService";
import {Transition} from "../../transition/transition";
import {TargetState} from "../../state/targetState";
Expand Down Expand Up @@ -95,18 +95,18 @@ export class UiSrefStatus {


/**
* Returns a Predicate<Node[]> that returns true when the target state (and any param values)
* Returns a Predicate<PathNode[]> that returns true when the target state (and any param values)
* match the (tail of) the path, and the path's param values
*/
const pathMatches = (target: TargetState) => {
let state: State = target.$state();
let targetParamVals = target.params();
let targetPath: Node[] = PathFactory.buildPath(target);
let targetPath: PathNode[] = PathFactory.buildPath(target);
let paramSchema: Param[] = targetPath.map(node => node.paramSchema)
.reduce(unnestR, [])
.filter((param: Param) => targetParamVals.hasOwnProperty(param.id));

return (path: Node[]) => {
return (path: PathNode[]) => {
let tailNode = tail(path);
if (!tailNode || tailNode.state !== state) return false;
var paramValues = PathFactory.paramValues(path);
Expand All @@ -121,7 +121,7 @@ export class UiSrefStatus {
* Expands the path to [c], [c, d]
* Then appends each to [a,b,] and returns: [a, b, c], [a, b, c, d]
*/
function spreadToSubPaths (path: Node[], appendTo: Node[] = []): Node[][] {
function spreadToSubPaths (path: PathNode[], appendTo: PathNode[] = []): PathNode[][] {
return path.map(node => appendTo.concat(PathFactory.subPath(path, node.state)));
}

Expand All @@ -135,7 +135,7 @@ export class UiSrefStatus {
this._setStatus(status);
}

let update = (currentPath: Node[]) => () => {
let update = (currentPath: PathNode[]) => () => {
if (this._deregisterHook == null) return; // destroyed
if (!$transition$.isActive()) return; // superseded
status.active = spreadToSubPaths(currentPath).map(isTarget).reduce(anyTrueR, false);
Expand Down
4 changes: 2 additions & 2 deletions src/ng2/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
*/ /** */
import {Provider, provide} from "@angular/core";
import {UIRouter} from "../router";
import {Node} from "../path/node";
import {PathNode} from "../path/node";
import {StateRegistry} from "../state/stateRegistry";
import {StateService} from "../state/stateService";
import {TransitionService} from "../transition/transitionService";
Expand All @@ -67,7 +67,7 @@ let uiRouterFactory = (routerConfig: UIRouterConfig, location: UIRouterLocation)

location.init();

router.viewService.viewConfigFactory("ng2", (node: Node, config: Ng2ViewDeclaration) => new Ng2ViewConfig(node, config));
router.viewService.viewConfigFactory("ng2", (node: PathNode, config: Ng2ViewDeclaration) => new Ng2ViewConfig(node, config));
router.stateRegistry.decorator('views', ng2ViewsBuilder);

router.stateRegistry.stateQueue.autoFlush(router.stateService);
Expand Down
4 changes: 2 additions & 2 deletions src/ng2/statebuilders/views.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @module ng2 */ /** */
import {State} from "../../state/stateObject";
import {Node} from "../../path/node";
import {PathNode} from "../../path/node";
import {pick, forEach} from "../../common/common";
import {ViewConfig} from "../../view/interface";
import {Ng2ViewDeclaration} from "../interface";
Expand Down Expand Up @@ -39,7 +39,7 @@ export function ng2ViewsBuilder(state: State) {
export class Ng2ViewConfig implements ViewConfig {
loaded: boolean = true;

constructor(public node: Node, public viewDecl: Ng2ViewDeclaration) { }
constructor(public node: PathNode, public viewDecl: Ng2ViewDeclaration) { }

load() {
return services.$q.when(this);
Expand Down
48 changes: 35 additions & 13 deletions src/path/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,34 @@ import {Resolvable} from "../resolve/resolvable";
import {ResolveContext} from "../resolve/resolveContext";
import {ViewConfig} from "../view/interface";

export class Node {
/**
* A node in a [[TreeChanges]] path
*
* For a [[TreeChanges]] path, this class holds the stateful information for a single node in the path.
* Each PathNode corresponds to a state being entered, exited, or retained.
* The stateful information includes parameter values and resolve data.
*/
export class PathNode {
/** The state being entered, exited, or retained */
public state: State;
/** The parameters declared on the state */
public paramSchema: Param[];
/** The parameter values that belong to the state */
public paramValues: { [key: string]: any };
/** A context object used in conjunction with [[resolvables]] to manage resolves */
public resolveContext: ResolveContext;
/** The individual (stateful) resolvable objects that belong to the state */
public resolvables: Resolvable[];
/** The state's declared view configuration objects */
public views: ViewConfig[];
public resolveContext: ResolveContext;

constructor(state: Node);
/** Creates a copy of a PathNode */
constructor(state: PathNode);
/** Creates a new (empty) PathNode for a State */
constructor(state: State);
constructor(state) {
if (state instanceof Node) {
let node: Node = state;
if (state instanceof PathNode) {
let node: PathNode = state;
this.state = node.state;
this.paramSchema = node.paramSchema.slice();
this.paramValues = extend({}, node.paramValues);
Expand All @@ -35,23 +50,30 @@ export class Node {
}
}

applyRawParams(params: RawParams): Node {
/** Sets [[paramValues]] for the node, from the values of an object hash */
applyRawParams(params: RawParams): PathNode {
const getParamVal = (paramDef: Param) => [ paramDef.id, paramDef.value(params[paramDef.id]) ];
this.paramValues = this.paramSchema.reduce((memo, pDef) => applyPairs(memo, getParamVal(pDef)), {});
return this;
}

/** Gets a specific [[Param]] metadata that belongs to the node */
parameter(name: string): Param {
return find(this.paramSchema, propEq("id", name));
}

equals(node: Node, keys = this.paramSchema.map(prop('id'))): boolean {
/**
* @returns true if the state and parameter values for another PathNode are
* equal to the state and param values for this PathNode
*/
equals(node: PathNode, keys = this.paramSchema.map(prop('id'))): boolean {
const paramValsEq = key => this.parameter(key).type.equals(this.paramValues[key], node.paramValues[key]);
return this.state === node.state && keys.map(paramValsEq).reduce(allTrueR, true);
}

static clone(node: Node) {
return new Node(node);
/** Returns a clone of the PathNode */
static clone(node: PathNode) {
return new PathNode(node);
}

/**
Expand All @@ -60,17 +82,17 @@ export class Node {
* The new path starts from root and contains any nodes that match the nodes in the second path.
* Nodes are compared using their state property and parameter values.
*/
static matching(pathA: Node[], pathB: Node[]): Node[] {
static matching(pathA: PathNode[], pathB: PathNode[]): PathNode[] {
let matching = [];

for (let i = 0; i < pathA.length && i < pathB.length; i++) {
let a = pathA[i], b = pathB[i];

if (a.state !== b.state) break;
if (!Param.equals(a.paramSchema, a.paramValues, b.paramValues)) break;
matching.push(a);
}

return matching
}
}
Loading

0 comments on commit 99e07b2

Please sign in to comment.