Skip to content

Commit 52b092f

Browse files
committed
feat(cdk): switch injectables to new scope API
1 parent 877eb85 commit 52b092f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1248
-899
lines changed

package-lock.json

Lines changed: 998 additions & 535 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cdk/a11y/a11y-module.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,12 @@
99
import {PlatformModule} from '@angular/cdk/platform';
1010
import {CommonModule} from '@angular/common';
1111
import {NgModule} from '@angular/core';
12-
import {ARIA_DESCRIBER_PROVIDER, AriaDescriber} from './aria-describer/aria-describer';
13-
import {CdkMonitorFocus, FOCUS_MONITOR_PROVIDER} from './focus-monitor/focus-monitor';
14-
import {
15-
CdkTrapFocus,
16-
FocusTrapDeprecatedDirective,
17-
FocusTrapFactory,
18-
} from './focus-trap/focus-trap';
19-
import {InteractivityChecker} from './interactivity-checker/interactivity-checker';
20-
import {LIVE_ANNOUNCER_PROVIDER} from './live-announcer/live-announcer';
12+
import {CdkMonitorFocus} from './focus-monitor/focus-monitor';
13+
import {CdkTrapFocus, FocusTrapDeprecatedDirective} from './focus-trap/focus-trap';
2114

2215
@NgModule({
2316
imports: [CommonModule, PlatformModule],
2417
declarations: [CdkTrapFocus, FocusTrapDeprecatedDirective, CdkMonitorFocus],
2518
exports: [CdkTrapFocus, FocusTrapDeprecatedDirective, CdkMonitorFocus],
26-
providers: [
27-
InteractivityChecker,
28-
FocusTrapFactory,
29-
AriaDescriber,
30-
LIVE_ANNOUNCER_PROVIDER,
31-
ARIA_DESCRIBER_PROVIDER,
32-
FOCUS_MONITOR_PROVIDER,
33-
]
3419
})
3520
export class A11yModule {}

src/cdk/a11y/aria-describer/aria-describer.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Injectable, Inject, InjectionToken, Optional, SkipSelf} from '@angular/core';
109
import {DOCUMENT} from '@angular/common';
10+
import {APP_ROOT_SCOPE, Inject, Injectable} from '@angular/core';
1111
import {addAriaReferencedId, getAriaReferenceIds, removeAriaReferencedId} from './aria-reference';
1212

13+
1314
/**
1415
* Interface used to register message elements and keep a count of how many registrations have
1516
* the same message and the reference to the message element used for the `aria-describedby`.
@@ -46,7 +47,7 @@ let messagesContainer: HTMLElement | null = null;
4647
* content.
4748
* @docs-private
4849
*/
49-
@Injectable()
50+
@Injectable({scope: APP_ROOT_SCOPE})
5051
export class AriaDescriber {
5152
private _document: Document;
5253

@@ -203,19 +204,3 @@ export class AriaDescriber {
203204
}
204205

205206
}
206-
207-
/** @docs-private */
208-
export function ARIA_DESCRIBER_PROVIDER_FACTORY(parentDispatcher: AriaDescriber, _document: any) {
209-
return parentDispatcher || new AriaDescriber(_document);
210-
}
211-
212-
/** @docs-private */
213-
export const ARIA_DESCRIBER_PROVIDER = {
214-
// If there is already an AriaDescriber available, use that. Otherwise, provide a new one.
215-
provide: AriaDescriber,
216-
deps: [
217-
[new Optional(), new SkipSelf(), AriaDescriber],
218-
DOCUMENT as InjectionToken<any>
219-
],
220-
useFactory: ARIA_DESCRIBER_PROVIDER_FACTORY
221-
};

src/cdk/a11y/focus-monitor/focus-monitor.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
89
import {Platform, supportsPassiveEventListeners} from '@angular/cdk/platform';
910
import {
11+
APP_ROOT_SCOPE,
1012
Directive,
1113
ElementRef,
1214
EventEmitter,
1315
Injectable,
1416
NgZone,
1517
OnDestroy,
16-
Optional,
1718
Output,
1819
Renderer2,
19-
SkipSelf,
2020
} from '@angular/core';
2121
import {Observable} from 'rxjs/Observable';
2222
import {of as observableOf} from 'rxjs/observable/of';
@@ -40,7 +40,7 @@ type MonitoredElementInfo = {
4040

4141

4242
/** Monitors mouse and keyboard events to determine the cause of focus events. */
43-
@Injectable()
43+
@Injectable({scope: APP_ROOT_SCOPE})
4444
export class FocusMonitor implements OnDestroy {
4545
/** The focus origin that the next focus event is a result of. */
4646
private _origin: FocusOrigin = null;
@@ -397,17 +397,3 @@ export class CdkMonitorFocus implements OnDestroy {
397397
this._monitorSubscription.unsubscribe();
398398
}
399399
}
400-
401-
/** @docs-private */
402-
export function FOCUS_MONITOR_PROVIDER_FACTORY(
403-
parentDispatcher: FocusMonitor, ngZone: NgZone, platform: Platform) {
404-
return parentDispatcher || new FocusMonitor(ngZone, platform);
405-
}
406-
407-
/** @docs-private */
408-
export const FOCUS_MONITOR_PROVIDER = {
409-
// If there is already a FocusMonitor available, use that. Otherwise, provide a new one.
410-
provide: FocusMonitor,
411-
deps: [[new Optional(), new SkipSelf(), FocusMonitor], NgZone, Platform],
412-
useFactory: FOCUS_MONITOR_PROVIDER_FACTORY
413-
};

src/cdk/a11y/focus-trap/focus-trap.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {coerceBooleanProperty} from '@angular/cdk/coercion';
10+
import {DOCUMENT} from '@angular/common';
911
import {
12+
AfterContentInit,
13+
APP_ROOT_SCOPE,
1014
Directive,
1115
ElementRef,
16+
Inject,
17+
Injectable,
1218
Input,
1319
NgZone,
1420
OnDestroy,
15-
AfterContentInit,
16-
Injectable,
17-
Inject,
1821
} from '@angular/core';
19-
import {coerceBooleanProperty} from '@angular/cdk/coercion';
2022
import {take} from 'rxjs/operators/take';
2123
import {InteractivityChecker} from '../interactivity-checker/interactivity-checker';
22-
import {DOCUMENT} from '@angular/common';
2324

2425

2526
/**
@@ -278,7 +279,7 @@ export class FocusTrap {
278279

279280

280281
/** Factory that allows easy instantiation of focus traps. */
281-
@Injectable()
282+
@Injectable({scope: APP_ROOT_SCOPE})
282283
export class FocusTrapFactory {
283284
private _document: Document;
284285

src/cdk/a11y/interactivity-checker/interactivity-checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Injectable} from '@angular/core';
109
import {Platform} from '@angular/cdk/platform';
10+
import {APP_ROOT_SCOPE, Injectable} from '@angular/core';
1111

1212

1313
// The InteractivityChecker leans heavily on the ally.js accessibility utilities.
@@ -18,7 +18,7 @@ import {Platform} from '@angular/cdk/platform';
1818
* Utility for checking the interactivity of an element, such as whether is is focusable or
1919
* tabbable.
2020
*/
21-
@Injectable()
21+
@Injectable({scope: APP_ROOT_SCOPE})
2222
export class InteractivityChecker {
2323

2424
constructor(private _platform: Platform) {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {APP_ROOT_SCOPE, InjectionToken} from '@angular/core';
10+
11+
// The token for the live announcer element is defined in a separate file from LiveAnnouncer
12+
// as a workaround for https://github.com/angular/angular/issues/22559
13+
14+
export const LIVE_ANNOUNCER_ELEMENT_TOKEN =
15+
new InjectionToken<HTMLElement | null>('liveAnnouncerElement', {
16+
scope: APP_ROOT_SCOPE,
17+
factory: () => null,
18+
});

src/cdk/a11y/live-announcer/live-announcer.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import {inject, fakeAsync, tick, ComponentFixture, TestBed} from '@angular/core/testing';
21
import {Component} from '@angular/core';
2+
import {ComponentFixture, fakeAsync, inject, TestBed, tick} from '@angular/core/testing';
33
import {By} from '@angular/platform-browser';
4-
import {LiveAnnouncer, LIVE_ANNOUNCER_ELEMENT_TOKEN} from './live-announcer';
54
import {A11yModule} from '../index';
5+
import {LiveAnnouncer} from './live-announcer';
6+
import {LIVE_ANNOUNCER_ELEMENT_TOKEN} from './live-announcer-token';
67

78

89
describe('LiveAnnouncer', () => {

src/cdk/a11y/live-announcer/live-announcer.ts

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,17 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {
10-
Injectable,
11-
InjectionToken,
12-
Optional,
13-
Inject,
14-
SkipSelf,
15-
OnDestroy,
16-
} from '@angular/core';
179
import {DOCUMENT} from '@angular/common';
10+
import {APP_ROOT_SCOPE, Inject, Injectable, OnDestroy, Optional} from '@angular/core';
11+
import {LIVE_ANNOUNCER_ELEMENT_TOKEN} from './live-announcer-token';
1812

1913

20-
export const LIVE_ANNOUNCER_ELEMENT_TOKEN = new InjectionToken<HTMLElement>('liveAnnouncerElement');
21-
2214
/** Possible politeness levels. */
2315
export type AriaLivePoliteness = 'off' | 'polite' | 'assertive';
2416

25-
@Injectable()
17+
@Injectable({scope: APP_ROOT_SCOPE})
2618
export class LiveAnnouncer implements OnDestroy {
27-
private _liveElement: Element;
19+
private readonly _liveElement: Element;
2820

2921
constructor(
3022
@Optional() @Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN) elementToken: any,
@@ -80,21 +72,3 @@ export class LiveAnnouncer implements OnDestroy {
8072
}
8173

8274
}
83-
84-
/** @docs-private */
85-
export function LIVE_ANNOUNCER_PROVIDER_FACTORY(
86-
parentDispatcher: LiveAnnouncer, liveElement: any, _document: any) {
87-
return parentDispatcher || new LiveAnnouncer(liveElement, _document);
88-
}
89-
90-
/** @docs-private */
91-
export const LIVE_ANNOUNCER_PROVIDER = {
92-
// If there is already a LiveAnnouncer available, use that. Otherwise, provide a new one.
93-
provide: LiveAnnouncer,
94-
deps: [
95-
[new Optional(), new SkipSelf(), LiveAnnouncer],
96-
[new Optional(), new Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN)],
97-
DOCUMENT,
98-
],
99-
useFactory: LIVE_ANNOUNCER_PROVIDER_FACTORY
100-
};

src/cdk/a11y/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export * from './key-manager/list-key-manager';
1515
export * from './focus-trap/focus-trap';
1616
export * from './interactivity-checker/interactivity-checker';
1717
export * from './live-announcer/live-announcer';
18+
export * from './live-announcer/live-announcer-token';
1819
export * from './focus-monitor/focus-monitor';
1920
export * from './fake-mousedown';
2021
export * from './a11y-module';

src/cdk/accordion/accordion-module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
*/
88

99
import {NgModule} from '@angular/core';
10-
import {UNIQUE_SELECTION_DISPATCHER_PROVIDER} from '@angular/cdk/collections';
1110
import {CdkAccordion} from './accordion';
1211
import {CdkAccordionItem} from './accordion-item';
1312

13+
1414
@NgModule({
1515
exports: [CdkAccordion, CdkAccordionItem],
1616
declarations: [CdkAccordion, CdkAccordionItem],
17-
providers: [UNIQUE_SELECTION_DISPATCHER_PROVIDER],
1817
})
1918
export class CdkAccordionModule {}

src/cdk/bidi/BUILD.bazel

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package(default_visibility=["//visibility:public"])
22
load("@angular//:index.bzl", "ng_module")
3+
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library", "ts_web_test")
4+
35

46
ng_module(
57
name = "bidi",
@@ -8,3 +10,30 @@ ng_module(
810
deps = ["@rxjs"],
911
tsconfig = ":tsconfig-build.json",
1012
)
13+
14+
ts_library(
15+
name = "bidi_test_sources",
16+
testonly = 1,
17+
srcs = glob(["**/*.spec.ts"]),
18+
deps = [
19+
":bidi",
20+
"@rxjs",
21+
],
22+
tsconfig = ":tsconfig-build.json",
23+
)
24+
25+
ts_web_test(
26+
name = "unit_tests",
27+
bootstrap = [
28+
"//:web_test_bootstrap_scripts",
29+
],
30+
31+
# Do not sort
32+
deps = [
33+
"//:tslib_bundle",
34+
"//:angular_bundles",
35+
"//:angular_test_bundles",
36+
"//test:angular_test_init",
37+
":bidi_test_sources",
38+
],
39+
)

src/cdk/bidi/bidi-module.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,11 @@
77
*/
88

99
import {NgModule} from '@angular/core';
10-
import {DOCUMENT} from '@angular/common';
1110
import {Dir} from './dir';
12-
import {DIR_DOCUMENT, Directionality} from './directionality';
1311

1412

1513
@NgModule({
1614
exports: [Dir],
1715
declarations: [Dir],
18-
providers: [
19-
{provide: DIR_DOCUMENT, useExisting: DOCUMENT},
20-
Directionality,
21-
]
2216
})
2317
export class BidiModule { }

src/cdk/bidi/dir-document-token.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {DOCUMENT} from '@angular/common';
10+
import {APP_ROOT_SCOPE, inject, InjectionToken} from '@angular/core';
11+
12+
13+
/**
14+
* Injection token used to inject the document into Directionality.
15+
* This is used so that the value can be faked in tests.
16+
*
17+
* We can't use the real document in tests because changing the real `dir` causes geometry-based
18+
* tests in Safari to fail.
19+
*
20+
* We also can't re-provide the DOCUMENT token from platform-brower because the unit tests
21+
* themselves use things like `querySelector` in test code.
22+
*
23+
* This token is defined in a separate file from Directionality as a workaround for
24+
* https://github.com/angular/angular/issues/22559
25+
*
26+
* @docs-private
27+
*/
28+
export const DIR_DOCUMENT = new InjectionToken<Document>('cdk-dir-doc', {
29+
scope: APP_ROOT_SCOPE,
30+
factory: () => inject(DOCUMENT),
31+
});

0 commit comments

Comments
 (0)