Skip to content

Commit

Permalink
refactor(ViewConfig): Change how view decls are processed, loaded, an…
Browse files Browse the repository at this point in the history
…d applied to uiviews
  • Loading branch information
christopherthielen committed Mar 21, 2016
1 parent c1f1606 commit 1541b90
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
7 changes: 3 additions & 4 deletions src/ng1/stateDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {extend, forEach, toJson} from "../common/common";
import {isString, isObject} from "../common/predicates";
import {UIViewData} from "./viewDirective";
import {parse} from "../common/hof";

function parseStateRef(ref, current) {
let preparsed = ref.match(/^\s*({[^}]*})\s*$/), parsed;
Expand All @@ -14,10 +15,8 @@ function parseStateRef(ref, current) {

function stateContext(el) {
let $uiView: UIViewData = el.parent().inheritedData('$uiView');

if ($uiView && $uiView.$cfg.context && $uiView.$cfg.context.name) {
return $uiView.$cfg.context;
}
let context = parse('$cfg.node.state')($uiView);
return context && context.name ? context : undefined;
}

function getTypeInfo(el) {
Expand Down
28 changes: 20 additions & 8 deletions src/ng1/viewDirective.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/** @module view */ /** for typedoc */
"use strict";
import {extend, map} from "../common/common";
import {isDefined, isFunction} from "../common/predicates";
import {trace} from "../common/trace";
import {ActiveUIView} from "../view/interface";
import {Ng1ViewConfig} from "./viewsBuilder";
import {RejectType} from "../transition/rejectFactory";
import {TransitionService} from "../transition/transitionService";
import {parse} from "../common/hof";
import {ResolveContext} from "../resolve/resolveContext";

export type UIViewData = {
$cfg: Ng1ViewConfig;
Expand Down Expand Up @@ -174,7 +179,7 @@ function $ViewDirective( $view, $animate, $uiViewScroll, $interpolate,
}

let rootData = {
$cfg: { context: $view.rootContext() },
$cfg: { viewDecl: { $context: $view.rootContext() } },
$uiView: { }
};

Expand All @@ -201,7 +206,9 @@ function $ViewDirective( $view, $animate, $uiViewScroll, $interpolate,
fqn: inherited.$uiView.fqn ? inherited.$uiView.fqn + "." + name : name, // fully qualified name, describes location in DOM
config: null, // The ViewConfig loaded (from a state.views definition)
configUpdated: configUpdatedCallback, // Called when the matching ViewConfig changes
get creationContext() { return inherited.$cfg.context; } // The context in which this ui-view "tag" was created
get creationContext() { // The context in which this ui-view "tag" was created
return parse('$cfg.viewDecl.$context')(inherited);
}
};

trace.traceUiViewEvent("Linking", activeUIView);
Expand Down Expand Up @@ -298,8 +305,12 @@ function $ViewDirective( $view, $animate, $uiViewScroll, $interpolate,
return directive;
}

$ViewDirectiveFill.$inject = ['$compile', '$controller', '$interpolate', '$injector', '$q'];
function $ViewDirectiveFill ( $compile, $controller, $interpolate, $injector, $q) {
$ViewDirectiveFill.$inject = ['$compile', '$controller', '$transitions', '$view', '$timeout'];
function $ViewDirectiveFill ( $compile, $controller, $transitions, $view, $timeout) {
const getControllerAs = parse('viewDecl.controllerAs');
const getResolveAs = parse('viewDecl.resolveAs');
const getResolveContext = parse('node.resolveContext');

return {
restrict: 'ECA',
priority: -400,
Expand All @@ -310,15 +321,16 @@ function $ViewDirectiveFill ( $compile, $controller, $interpolate, $injec
let data: UIViewData = $element.data('$uiView');
if (!data) return;

let cfg: Ng1ViewConfig = data.$cfg || <any> {};
let cfg: Ng1ViewConfig = data.$cfg || <any> { viewDecl: {} };
$element.html(cfg.template || initial);
trace.traceUiViewFill(data.$uiView, $element.html());

let link = $compile($element.contents());
let controller = cfg.controller;
let controllerAs = cfg.controllerAs;
let resolveAs = cfg.resolveAs;
let locals = map(cfg.node && cfg.node.resolves || {}, r => r.data);
let controllerAs: string = getControllerAs(cfg);
let resolveAs: string = getResolveAs(cfg);
let resolveCtx: ResolveContext = getResolveContext(cfg);
let locals = resolveCtx && map(resolveCtx.getResolvables(), r => r.data);

scope[resolveAs] = locals;

Expand Down
3 changes: 0 additions & 3 deletions src/ng1/viewsBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ export class Ng1ViewConfig implements ViewConfig {
locals: any; // TODO: delete me

constructor(public node: Node, public viewDecl: Ng1ViewDeclaration) { }
get context(): ViewContext { return this.viewDecl.$context; }
get controllerAs() { return this.viewDecl.controllerAs; }
get resolveAs() { return this.viewDecl.resolveAs; }

load() {
let $q = services.$q;
Expand Down
2 changes: 1 addition & 1 deletion src/view/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class ViewService {
*/
deactivateViewConfig(viewConfig: ViewConfig) {
trace.traceViewServiceEvent("<- Removing", viewConfig);
this.viewConfigs.filter(match(viewConfig, "uiViewName", "context")).forEach(removeFrom(this.viewConfigs));
removeFrom(this.viewConfigs, viewConfig)
};

activateViewConfig(viewConfig: ViewConfig) {
Expand Down
2 changes: 1 addition & 1 deletion test/compat/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ beforeEach(function() {
actual.hasClass(clazz) :
angular.element(actual).hasClass(clazz);

var message = pass ? undefined : "Expected '" + angular.mock.dump(this.actual) + "' to have class '" + clazz + "'.";
var message = pass ? undefined : "Expected '" + angular.mock.dump(actual) + "' to have class '" + clazz + "'.";

return { pass: pass, message: message};
}
Expand Down
2 changes: 2 additions & 0 deletions test/stateDirectivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ describe('uiStateRef', function() {
expect(obj($stateParams)).toEqualData({ id: 5 });
}));

if (/PhantomJS/.exec(navigator.userAgent)) {
it('should transition when given a click that contains no data (fake-click)', inject(function($state, $stateParams, $q) {
expect($state.current.name).toEqual('top');

Expand Down Expand Up @@ -213,6 +214,7 @@ describe('uiStateRef', function() {
expect($state.current.name).toEqual('top');
expect(obj($stateParams)).toEqualData({});
}));
}

it('should not transition states if preventDefault() is called in click handler', inject(function($state, $stateParams, $q) {
expect($state.$current.name).toEqual('top');
Expand Down

0 comments on commit 1541b90

Please sign in to comment.