Skip to content

Commit 935b7be

Browse files
committed
Remove view decorators from router
1 parent 45b451a commit 935b7be

15 files changed

Lines changed: 335 additions & 234 deletions

File tree

src/ng.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,15 @@ export function registerNgModule(angular: ng.Angular): ng.NgModule {
297297
},
298298
],
299299
)
300+
.run([
301+
$t._log,
302+
/**
303+
* Initializes the router trace singleton with the DI-backed logger.
304+
*/
305+
($log: Pick<Console, "log" | "table">) => {
306+
trace._setLogger($log);
307+
},
308+
])
300309
.factory("$stateParams", [
301310
$t._router,
302311
/**

src/router/common/trace.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import {
4040
padString,
4141
stringify,
4242
} from "../../shared/strings.ts";
43-
import { $injectTokens } from "../../injection-tokens.ts";
4443
import type { HookResult } from "../transition/interface.ts";
4544
import type {
4645
TransitionHook,
@@ -135,10 +134,12 @@ export class Trace {
135134

136135
constructor() {
137136
this._enabled = {};
138-
this._logger =
139-
(window.angular?.$injector?.get($injectTokens._log) as
140-
| TraceLogger
141-
| undefined) || console;
137+
this._logger = console;
138+
}
139+
140+
/** @internal */
141+
_setLogger(logger: TraceLogger): void {
142+
this._logger = logger;
142143
}
143144

144145
/** @internal */

src/router/directives/view-directive.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ ViewDirectiveFill.$inject = [
519519
$injectTokens._compile,
520520
$injectTokens._controller,
521521
$injectTokens._transitions,
522+
$injectTokens._injector,
522523
];
523524

524525
/**
@@ -528,6 +529,7 @@ export function ViewDirectiveFill(
528529
$compile: ng.CompileService,
529530
$controller: ng.ControllerService,
530531
$transitions: ng.TransitionService,
532+
$injector: ng.InjectorService,
531533
): ng.Directive {
532534
const getControllerAs = parse("viewDecl.controllerAs");
533535

@@ -561,7 +563,7 @@ export function ViewDirectiveFill(
561563
}) as Pick<ViewConfig, "viewDecl" | "getTemplate" | "controller"> &
562564
Partial<Pick<ViewConfig, "path">>;
563565

564-
const resolveCtx = cfg.path && new ResolveContext(cfg.path);
566+
const resolveCtx = cfg.path && new ResolveContext(cfg.path, $injector);
565567

566568
$element.innerHTML =
567569
cfg.getTemplate($element, resolveCtx as ResolveContext) || initial;
@@ -618,7 +620,8 @@ export function ViewDirectiveFill(
618620
const componentName = (cfg as ViewConfig & { component?: string })
619621
.component;
620622

621-
const callbackConfig = cfg as Pick<ViewConfig, "viewDecl" | "path">;
623+
const callbackConfig = cfg as Pick<ViewConfig, "viewDecl" | "path"> &
624+
Partial<Pick<ViewConfig, "factory">>;
622625

623626
if (typeof componentName === "string") {
624627
const kebobName = componentName
@@ -718,7 +721,8 @@ function registerControllerCallbacks(
718721
$transitions: ng.TransitionService,
719722
controllerInstance: ViewControllerInstance,
720723
$scope: ng.Scope,
721-
cfg: Pick<ViewConfig, "viewDecl" | "path">,
724+
cfg: Pick<ViewConfig, "viewDecl" | "path"> &
725+
Partial<Pick<ViewConfig, "factory">>,
722726
): void {
723727
let registeredScopes = controllerRegisteredScopes.get(controllerInstance);
724728

@@ -753,7 +757,7 @@ function registerControllerCallbacks(
753757
trans: ng.Transition,
754758
) => void;
755759

756-
const resolveContext = new ResolveContext(cfg.path);
760+
const resolveContext = new ResolveContext(cfg.path, cfg.factory?._injector);
757761

758762
const viewCreationTrans = resolveContext.getResolvable("$transition$")
759763
.data as ng.Transition;

src/router/hooks/resolve.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import type { StateDeclaration } from "../state/interface.ts";
1010
export const RESOLVE_HOOK_PRIORITY = 1000;
1111

1212
const eagerResolvePath = (trans: Transition) =>
13-
new ResolveContext((trans.treeChanges() as TreeChanges).to)
13+
new ResolveContext(
14+
(trans.treeChanges() as TreeChanges).to,
15+
trans._globals._injector,
16+
)
1417
.resolvePath("EAGER", trans)
1518
.then(() => {
1619
/* empty */
@@ -27,7 +30,10 @@ export const registerEagerResolvePath = (
2730
* Resolves the entering state's lazy resolvables at `onEnter`.
2831
*/
2932
const lazyResolveState = (trans: Transition, state: StateDeclaration) =>
30-
new ResolveContext((trans.treeChanges() as TreeChanges).to)
33+
new ResolveContext(
34+
(trans.treeChanges() as TreeChanges).to,
35+
trans._globals._injector,
36+
)
3137
.subContext((state._state as Function)())
3238
.resolvePath("LAZY", trans)
3339
.then(() => {
@@ -45,7 +51,10 @@ export const registerLazyResolveState = (
4551
* Resolves any remaining lazy resolvables before the transition finishes.
4652
*/
4753
const resolveRemaining = (trans: Transition) =>
48-
new ResolveContext((trans.treeChanges() as TreeChanges).to)
54+
new ResolveContext(
55+
(trans.treeChanges() as TreeChanges).to,
56+
trans._globals._injector,
57+
)
4958
.resolvePath("LAZY", trans)
5059
.then(() => {
5160
/* empty */

src/router/params/param-types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import type { ParamTypeDefinition } from "./interface.ts";
2828
* ```
2929
*/
3030
export class ParamTypes {
31+
/** @internal */
32+
_angular: ng.AngularService;
3133
$injector: InjectorService;
3234
enqueue: boolean;
3335
typeQueue: {
@@ -43,6 +45,7 @@ export class ParamTypes {
4345
* @param {ng.AngularService} $angular
4446
*/
4547
constructor($angular: ng.AngularService) {
48+
this._angular = $angular;
4649
this.$injector = $angular.$injector;
4750
this.enqueue = true;
4851
this.typeQueue = [];
@@ -133,7 +136,7 @@ export class ParamTypes {
133136

134137
/** @internal */
135138
_getInjector(): InjectorService | undefined {
136-
return (this.$injector ||= window.angular?.$injector);
139+
return (this.$injector ||= this._angular.$injector);
137140
}
138141
}
139142
function initDefaultTypes() {

src/router/params/param.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ export class Param {
180180
matchingKeys: RawParams | undefined;
181181
/** @internal */
182182
_defaultValueCache?: { defaultValue: any };
183+
/** @internal */
184+
_getInjector: () => ng.InjectorService | undefined;
183185

184186
/**
185187
*
@@ -245,6 +247,7 @@ export class Param {
245247
this.array = arrayMode;
246248
this.config = config;
247249
this.matchingKeys = undefined;
250+
this._getInjector = () => urlConfig.paramTypes._getInjector();
248251
}
249252

250253
/**
@@ -266,11 +269,13 @@ export class Param {
266269
const getDefaultValue = () => {
267270
if (this._defaultValueCache) return this._defaultValueCache.defaultValue;
268271

269-
if (!window.angular.$injector)
272+
const injector = this._getInjector();
273+
274+
if (!injector)
270275
throw new Error(
271276
"Injectable functions cannot be called at configuration time",
272277
);
273-
const defaultValue = window.angular.$injector.invoke(this.config._fn);
278+
const defaultValue = injector.invoke(this.config._fn);
274279

275280
if (
276281
defaultValue !== null &&

0 commit comments

Comments
 (0)