Skip to content

Commit b49bfce

Browse files
jelbourntinayuangao
authored andcommitted
feat: remove the need for forRoot on material NgModules (#2556)
1 parent fed5d7b commit b49bfce

Some content is hidden

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

45 files changed

+365
-82
lines changed

src/lib/autocomplete/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export * from './autocomplete-trigger';
1515
declarations: [MdAutocomplete, MdAutocompleteTrigger],
1616
})
1717
export class MdAutocompleteModule {
18+
/** @deprecated */
1819
static forRoot(): ModuleWithProviders {
1920
return {
2021
ngModule: MdAutocompleteModule,

src/lib/button-toggle/button-toggle.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
UniqueSelectionDispatcher,
2525
coerceBooleanProperty,
2626
DefaultStyleCompatibilityModeModule,
27+
UNIQUE_SELECTION_DISPATCHER_PROVIDER,
2728
} from '../core';
2829

2930
/** Acceptable types for a button toggle. */
@@ -471,12 +472,14 @@ export class MdButtonToggle implements OnInit {
471472
DefaultStyleCompatibilityModeModule,
472473
],
473474
declarations: [MdButtonToggleGroup, MdButtonToggleGroupMultiple, MdButtonToggle],
475+
providers: [UNIQUE_SELECTION_DISPATCHER_PROVIDER]
474476
})
475477
export class MdButtonToggleModule {
478+
/** @deprecated */
476479
static forRoot(): ModuleWithProviders {
477480
return {
478481
ngModule: MdButtonToggleModule,
479-
providers: [UniqueSelectionDispatcher]
482+
providers: []
480483
};
481484
}
482485
}

src/lib/button/button.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
} from '@angular/core';
1212
import {CommonModule} from '@angular/common';
1313
import {MdRippleModule, coerceBooleanProperty, DefaultStyleCompatibilityModeModule} from '../core';
14-
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
1514

1615

1716
// TODO(jelbourn): Make the `isMouseDown` stuff done with one global listener.
@@ -167,10 +166,11 @@ export class MdAnchor extends MdButton {
167166
declarations: [MdButton, MdAnchor],
168167
})
169168
export class MdButtonModule {
169+
/** @deprecated */
170170
static forRoot(): ModuleWithProviders {
171171
return {
172172
ngModule: MdButtonModule,
173-
providers: [ViewportRuler]
173+
providers: []
174174
};
175175
}
176176
}

src/lib/card/card.ts

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export class MdCardTitleGroup {}
119119
],
120120
})
121121
export class MdCardModule {
122+
/** @deprecated */
122123
static forRoot(): ModuleWithProviders {
123124
return {
124125
ngModule: MdCardModule,

src/lib/checkbox/checkbox.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {CommonModule} from '@angular/common';
1717
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from '@angular/forms';
1818
import {coerceBooleanProperty} from '../core/coercion/boolean-property';
1919
import {MdRippleModule, DefaultStyleCompatibilityModeModule} from '../core';
20-
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
2120

2221

2322
/** Monotonically increasing integer used to auto-generate unique ids for checkbox components. */
@@ -399,10 +398,11 @@ export class MdCheckbox implements ControlValueAccessor {
399398
declarations: [MdCheckbox],
400399
})
401400
export class MdCheckboxModule {
401+
/** @deprecated */
402402
static forRoot(): ModuleWithProviders {
403403
return {
404404
ngModule: MdCheckboxModule,
405-
providers: [ViewportRuler]
405+
providers: []
406406
};
407407
}
408408
}

src/lib/chips/chip-list.ts

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ export class MdChipList implements AfterContentInit {
215215
declarations: [MdChipList, MdChip]
216216
})
217217
export class MdChipsModule {
218+
/** @deprecated */
218219
static forRoot(): ModuleWithProviders {
219220
return {
220221
ngModule: MdChipsModule,

src/lib/core/a11y/index.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
import {NgModule, ModuleWithProviders} from '@angular/core';
22
import {FocusTrap} from './focus-trap';
3-
import {LiveAnnouncer} from './live-announcer';
3+
import {LIVE_ANNOUNCER_PROVIDER} from './live-announcer';
44
import {InteractivityChecker} from './interactivity-checker';
55
import {CommonModule} from '@angular/common';
66
import {PlatformModule} from '../platform/index';
77

8-
export const A11Y_PROVIDERS = [LiveAnnouncer, InteractivityChecker];
9-
108
@NgModule({
119
imports: [CommonModule, PlatformModule],
1210
declarations: [FocusTrap],
1311
exports: [FocusTrap],
12+
providers: [InteractivityChecker, LIVE_ANNOUNCER_PROVIDER]
1413
})
1514
export class A11yModule {
15+
/** @deprecated */
1616
static forRoot(): ModuleWithProviders {
1717
return {
1818
ngModule: A11yModule,
19-
providers: [
20-
PlatformModule.forRoot().providers,
21-
A11Y_PROVIDERS,
22-
],
19+
providers: [],
2320
};
2421
}
2522
}

src/lib/core/a11y/live-announcer.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,3 @@ class TestApp {
115115
this.live.announce(message);
116116
}
117117
}
118-

src/lib/core/a11y/live-announcer.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {
22
Injectable,
33
OpaqueToken,
44
Optional,
5-
Inject
5+
Inject,
6+
SkipSelf,
67
} from '@angular/core';
78

89
export const LIVE_ANNOUNCER_ELEMENT_TOKEN = new OpaqueToken('liveAnnouncerElement');
@@ -62,3 +63,17 @@ export class LiveAnnouncer {
6263
}
6364

6465
}
66+
67+
export function LIVE_ANNOUNCER_PROVIDER_FACTORY(parentDispatcher: LiveAnnouncer, liveElement: any) {
68+
return parentDispatcher || new LiveAnnouncer(liveElement);
69+
};
70+
71+
export const LIVE_ANNOUNCER_PROVIDER = {
72+
// If there is already a LiveAnnouncer available, use that. Otherwise, provide a new one.
73+
provide: LiveAnnouncer,
74+
deps: [
75+
[new Optional(), new SkipSelf(), LiveAnnouncer],
76+
[new Optional(), new Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN)]
77+
],
78+
useFactory: LIVE_ANNOUNCER_PROVIDER_FACTORY
79+
};

src/lib/core/compatibility/default-mode.ts

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class MatPrefixEnforcer {
7171
}]
7272
})
7373
export class DefaultStyleCompatibilityModeModule {
74+
/** @deprecated */
7475
static forRoot(): ModuleWithProviders {
7576
return {
7677
ngModule: DefaultStyleCompatibilityModeModule,

src/lib/core/compatibility/no-conflict-mode.ts

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export class MdPrefixEnforcer {
6060
}],
6161
})
6262
export class NoConflictStyleCompatibilityMode {
63+
/** @deprecated */
6364
static forRoot(): ModuleWithProviders {
6465
return {
6566
ngModule: NoConflictStyleCompatibilityMode,

src/lib/core/coordination/unique-selection-dispatcher.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Injectable} from '@angular/core';
1+
import {Injectable, Optional, SkipSelf} from '@angular/core';
22

33

44
// Users of the Dispatcher never need to see this type, but TypeScript requires it to be exported.
@@ -33,3 +33,15 @@ export class UniqueSelectionDispatcher {
3333
this._listeners.push(listener);
3434
}
3535
}
36+
37+
export function UNIQUE_SELECTION_DISPATCHER_PROVIDER_FACTORY(
38+
parentDispatcher: UniqueSelectionDispatcher) {
39+
return parentDispatcher || new UniqueSelectionDispatcher();
40+
}
41+
42+
export const UNIQUE_SELECTION_DISPATCHER_PROVIDER = {
43+
// If there is already a dispatcher available, use that. Otherwise, provide a new one.
44+
provide: UniqueSelectionDispatcher,
45+
deps: [[new Optional(), new SkipSelf(), UniqueSelectionDispatcher]],
46+
useFactory: UNIQUE_SELECTION_DISPATCHER_PROVIDER_FACTORY
47+
};

src/lib/core/core.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import {MdOptionModule} from './option/option';
66
import {MdRippleModule} from './ripple/ripple';
77
import {PortalModule} from './portal/portal-directives';
88
import {OverlayModule} from './overlay/overlay-directives';
9-
import {A11yModule, A11Y_PROVIDERS} from './a11y/index';
10-
import {OVERLAY_PROVIDERS} from './overlay/overlay';
9+
import {A11yModule} from './a11y/index';
1110

1211

1312
// RTL
@@ -71,8 +70,8 @@ export {
7170
AriaLivePoliteness,
7271
LiveAnnouncer,
7372
LIVE_ANNOUNCER_ELEMENT_TOKEN,
73+
LIVE_ANNOUNCER_PROVIDER,
7474
} from './a11y/live-announcer';
75-
7675
/** @deprecated */
7776
export {LiveAnnouncer as MdLiveAnnouncer} from './a11y/live-announcer';
7877

@@ -84,9 +83,9 @@ export {A11yModule} from './a11y/index';
8483

8584
export {
8685
UniqueSelectionDispatcher,
87-
UniqueSelectionDispatcherListener
86+
UniqueSelectionDispatcherListener,
87+
UNIQUE_SELECTION_DISPATCHER_PROVIDER,
8888
} from './coordination/unique-selection-dispatcher';
89-
9089
/** @deprecated */
9190
export {
9291
UniqueSelectionDispatcher as MdUniqueSelectionDispatcher
@@ -143,10 +142,11 @@ export {NoConflictStyleCompatibilityMode} from './compatibility/no-conflict-mode
143142
],
144143
})
145144
export class MdCoreModule {
145+
/** @deprecated */
146146
static forRoot(): ModuleWithProviders {
147147
return {
148148
ngModule: MdCoreModule,
149-
providers: [A11Y_PROVIDERS, OVERLAY_PROVIDERS],
149+
providers: [],
150150
};
151151
}
152152
}

src/lib/core/observe-content/observe-content.ts

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class ObserveContent implements AfterContentInit, OnDestroy {
4646
declarations: [ObserveContent]
4747
})
4848
export class ObserveContentModule {
49+
/** @deprecated */
4950
static forRoot(): ModuleWithProviders {
5051
return {
5152
ngModule: ObserveContentModule,

src/lib/core/overlay/overlay-container.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {Injectable} from '@angular/core';
1+
import {Injectable, Optional, SkipSelf} from '@angular/core';
2+
23

34
/**
45
* The OverlayContainer is the container in which all overlays will load.
@@ -30,3 +31,14 @@ export class OverlayContainer {
3031
this._containerElement = container;
3132
}
3233
}
34+
35+
export function OVERLAY_CONTAINER_PROVIDER_FACTORY(parentContainer: OverlayContainer) {
36+
return parentContainer || new OverlayContainer();
37+
};
38+
39+
export const OVERLAY_CONTAINER_PROVIDER = {
40+
// If there is already an OverlayContainer available, use that. Otherwise, provide a new one.
41+
provide: OverlayContainer,
42+
deps: [[new Optional(), new SkipSelf(), OverlayContainer]],
43+
useFactory: OVERLAY_CONTAINER_PROVIDER_FACTORY
44+
};

src/lib/core/overlay/overlay-directives.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,14 @@ export class ConnectedOverlayDirective implements OnDestroy {
295295
imports: [PortalModule],
296296
exports: [ConnectedOverlayDirective, OverlayOrigin, Scrollable],
297297
declarations: [ConnectedOverlayDirective, OverlayOrigin, Scrollable],
298+
providers: [OVERLAY_PROVIDERS],
298299
})
299300
export class OverlayModule {
301+
/** @deprecated */
300302
static forRoot(): ModuleWithProviders {
301303
return {
302304
ngModule: OverlayModule,
303-
providers: OVERLAY_PROVIDERS,
305+
providers: [],
304306
};
305307
}
306308
}

src/lib/core/overlay/overlay.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import {
44
ApplicationRef,
55
Injector,
66
NgZone,
7+
Provider,
78
} from '@angular/core';
89
import {OverlayState} from './overlay-state';
910
import {DomPortalHost} from '../portal/dom-portal-host';
1011
import {OverlayRef} from './overlay-ref';
1112
import {OverlayPositionBuilder} from './position/overlay-position-builder';
12-
import {ViewportRuler} from './position/viewport-ruler';
13-
import {OverlayContainer} from './overlay-container';
14-
import {ScrollDispatcher} from './scroll/scroll-dispatcher';
13+
import {VIEWPORT_RULER_PROVIDER} from './position/viewport-ruler';
14+
import {OverlayContainer, OVERLAY_CONTAINER_PROVIDER} from './overlay-container';
15+
import {SCROLL_DISPATCHER_PROVIDER} from './scroll/scroll-dispatcher';
16+
1517

1618
/** Next overlay unique ID. */
1719
let nextUniqueId = 0;
@@ -88,10 +90,10 @@ export class Overlay {
8890
}
8991

9092
/** Providers for Overlay and its related injectables. */
91-
export const OVERLAY_PROVIDERS = [
92-
ViewportRuler,
93-
OverlayPositionBuilder,
93+
export const OVERLAY_PROVIDERS: Provider[] = [
9494
Overlay,
95-
OverlayContainer,
96-
ScrollDispatcher,
95+
OverlayPositionBuilder,
96+
VIEWPORT_RULER_PROVIDER,
97+
SCROLL_DISPATCHER_PROVIDER,
98+
OVERLAY_CONTAINER_PROVIDER,
9799
];

src/lib/core/overlay/position/viewport-ruler.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {Injectable} from '@angular/core';
2-
1+
import {Injectable, Optional, SkipSelf} from '@angular/core';
32

43

54
/**
@@ -56,3 +55,14 @@ export class ViewportRuler {
5655
return {top, left};
5756
}
5857
}
58+
59+
export function VIEWPORT_RULER_PROVIDER_FACTORY(parentDispatcher: ViewportRuler) {
60+
return parentDispatcher || new ViewportRuler();
61+
};
62+
63+
export const VIEWPORT_RULER_PROVIDER = {
64+
// If there is already a ViewportRuler available, use that. Otherwise, provide a new one.
65+
provide: ViewportRuler,
66+
deps: [[new Optional(), new SkipSelf(), ViewportRuler]],
67+
useFactory: VIEWPORT_RULER_PROVIDER_FACTORY
68+
};

src/lib/core/overlay/scroll/scroll-dispatcher.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Injectable, ElementRef} from '@angular/core';
1+
import {Injectable, ElementRef, Optional, SkipSelf} from '@angular/core';
22
import {Scrollable} from './scrollable';
33
import {Subject} from 'rxjs/Subject';
44
import {Observable} from 'rxjs/Observable';
@@ -88,3 +88,13 @@ export class ScrollDispatcher {
8888
}
8989
}
9090

91+
export function SCROLL_DISPATCHER_PROVIDER_FACTORY(parentDispatcher: ScrollDispatcher) {
92+
return parentDispatcher || new ScrollDispatcher();
93+
};
94+
95+
export const SCROLL_DISPATCHER_PROVIDER = {
96+
// If there is already a ScrollDispatcher available, use that. Otherwise, provide a new one.
97+
provide: ScrollDispatcher,
98+
deps: [[new Optional(), new SkipSelf(), ScrollDispatcher]],
99+
useFactory: SCROLL_DISPATCHER_PROVIDER_FACTORY
100+
};

src/lib/core/platform/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ export * from './platform';
55
export * from './features';
66

77

8-
@NgModule({})
8+
@NgModule({
9+
providers: [Platform]
10+
})
911
export class PlatformModule {
12+
/** @deprecated */
1013
static forRoot(): ModuleWithProviders {
1114
return {
1215
ngModule: PlatformModule,
13-
providers: [Platform],
16+
providers: [],
1417
};
1518
}
1619
}

src/lib/core/portal/portal-directives.ts

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
130130
declarations: [TemplatePortalDirective, PortalHostDirective],
131131
})
132132
export class PortalModule {
133+
/** @deprecated */
133134
static forRoot(): ModuleWithProviders {
134135
return {
135136
ngModule: PortalModule,

0 commit comments

Comments
 (0)