Permalink
Browse files

feat: remove the need for forRoot on material NgModules (#2556)

  • Loading branch information...
1 parent fed5d7b commit b49bfce5a9796441314bd2eed93e26e47a48e9ca @jelbourn jelbourn committed with tinayuangao Jan 11, 2017
Showing with 365 additions and 82 deletions.
  1. +1 −0 src/lib/autocomplete/index.ts
  2. +4 −1 src/lib/button-toggle/button-toggle.ts
  3. +2 −2 src/lib/button/button.ts
  4. +1 −0 src/lib/card/card.ts
  5. +2 −2 src/lib/checkbox/checkbox.ts
  6. +1 −0 src/lib/chips/chip-list.ts
  7. +4 −7 src/lib/core/a11y/index.ts
  8. +0 −1 src/lib/core/a11y/live-announcer.spec.ts
  9. +16 −1 src/lib/core/a11y/live-announcer.ts
  10. +1 −0 src/lib/core/compatibility/default-mode.ts
  11. +1 −0 src/lib/core/compatibility/no-conflict-mode.ts
  12. +13 −1 src/lib/core/coordination/unique-selection-dispatcher.ts
  13. +6 −6 src/lib/core/core.ts
  14. +1 −0 src/lib/core/observe-content/observe-content.ts
  15. +13 −1 src/lib/core/overlay/overlay-container.ts
  16. +3 −1 src/lib/core/overlay/overlay-directives.ts
  17. +10 −8 src/lib/core/overlay/overlay.ts
  18. +12 −2 src/lib/core/overlay/position/viewport-ruler.ts
  19. +11 −1 src/lib/core/overlay/scroll/scroll-dispatcher.ts
  20. +5 −2 src/lib/core/platform/index.ts
  21. +1 −0 src/lib/core/portal/portal-directives.ts
  22. +2 −1 src/lib/core/projection/projection.ts
  23. +4 −2 src/lib/core/ripple/ripple.ts
  24. +1 −0 src/lib/core/rtl/dir.ts
  25. +70 −0 src/lib/dialog/dialog.spec.ts
  26. +10 −3 src/lib/dialog/dialog.ts
  27. +6 −6 src/lib/dialog/index.ts
  28. +1 −0 src/lib/grid-list/grid-list.ts
  29. +18 −2 src/lib/icon/icon.ts
  30. +2 −1 src/lib/input/input.ts
  31. +1 −0 src/lib/list/list.ts
  32. +3 −2 src/lib/menu/menu.ts
  33. +1 −0 src/lib/module.ts
  34. +1 −0 src/lib/progress-bar/progress-bar.ts
  35. +1 −0 src/lib/progress-spinner/progress-spinner.ts
  36. +5 −2 src/lib/radio/radio.ts
  37. +2 −2 src/lib/select/index.ts
  38. +2 −2 src/lib/sidenav/sidenav.ts
  39. +3 −1 src/lib/slide-toggle/slide-toggle.ts
  40. +3 −4 src/lib/slider/slider.ts
  41. +77 −2 src/lib/snack-bar/snack-bar.spec.ts
  42. +37 −12 src/lib/snack-bar/snack-bar.ts
  43. +4 −2 src/lib/tabs/tab-group.ts
  44. +1 −0 src/lib/toolbar/toolbar.ts
  45. +2 −2 src/lib/tooltip/tooltip.ts
@@ -15,6 +15,7 @@ export * from './autocomplete-trigger';
declarations: [MdAutocomplete, MdAutocompleteTrigger],
})
export class MdAutocompleteModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: MdAutocompleteModule,
@@ -24,6 +24,7 @@ import {
UniqueSelectionDispatcher,
coerceBooleanProperty,
DefaultStyleCompatibilityModeModule,
+ UNIQUE_SELECTION_DISPATCHER_PROVIDER,
} from '../core';
/** Acceptable types for a button toggle. */
@@ -471,12 +472,14 @@ export class MdButtonToggle implements OnInit {
DefaultStyleCompatibilityModeModule,
],
declarations: [MdButtonToggleGroup, MdButtonToggleGroupMultiple, MdButtonToggle],
+ providers: [UNIQUE_SELECTION_DISPATCHER_PROVIDER]
})
export class MdButtonToggleModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: MdButtonToggleModule,
- providers: [UniqueSelectionDispatcher]
+ providers: []
};
}
}
@@ -11,7 +11,6 @@ import {
} from '@angular/core';
import {CommonModule} from '@angular/common';
import {MdRippleModule, coerceBooleanProperty, DefaultStyleCompatibilityModeModule} from '../core';
-import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
// TODO(jelbourn): Make the `isMouseDown` stuff done with one global listener.
@@ -167,10 +166,11 @@ export class MdAnchor extends MdButton {
declarations: [MdButton, MdAnchor],
})
export class MdButtonModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: MdButtonModule,
- providers: [ViewportRuler]
+ providers: []
};
}
}
@@ -119,6 +119,7 @@ export class MdCardTitleGroup {}
],
})
export class MdCardModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: MdCardModule,
@@ -17,7 +17,6 @@ import {CommonModule} from '@angular/common';
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from '@angular/forms';
import {coerceBooleanProperty} from '../core/coercion/boolean-property';
import {MdRippleModule, DefaultStyleCompatibilityModeModule} from '../core';
-import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
/** Monotonically increasing integer used to auto-generate unique ids for checkbox components. */
@@ -399,10 +398,11 @@ export class MdCheckbox implements ControlValueAccessor {
declarations: [MdCheckbox],
})
export class MdCheckboxModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: MdCheckboxModule,
- providers: [ViewportRuler]
+ providers: []
};
}
}
@@ -215,6 +215,7 @@ export class MdChipList implements AfterContentInit {
declarations: [MdChipList, MdChip]
})
export class MdChipsModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: MdChipsModule,
@@ -1,25 +1,22 @@
import {NgModule, ModuleWithProviders} from '@angular/core';
import {FocusTrap} from './focus-trap';
-import {LiveAnnouncer} from './live-announcer';
+import {LIVE_ANNOUNCER_PROVIDER} from './live-announcer';
import {InteractivityChecker} from './interactivity-checker';
import {CommonModule} from '@angular/common';
import {PlatformModule} from '../platform/index';
-export const A11Y_PROVIDERS = [LiveAnnouncer, InteractivityChecker];
-
@NgModule({
imports: [CommonModule, PlatformModule],
declarations: [FocusTrap],
exports: [FocusTrap],
+ providers: [InteractivityChecker, LIVE_ANNOUNCER_PROVIDER]
})
export class A11yModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: A11yModule,
- providers: [
- PlatformModule.forRoot().providers,
- A11Y_PROVIDERS,
- ],
+ providers: [],
};
}
}
@@ -115,4 +115,3 @@ class TestApp {
this.live.announce(message);
}
}
-
@@ -2,7 +2,8 @@ import {
Injectable,
OpaqueToken,
Optional,
- Inject
+ Inject,
+ SkipSelf,
} from '@angular/core';
export const LIVE_ANNOUNCER_ELEMENT_TOKEN = new OpaqueToken('liveAnnouncerElement');
@@ -62,3 +63,17 @@ export class LiveAnnouncer {
}
}
+
+export function LIVE_ANNOUNCER_PROVIDER_FACTORY(parentDispatcher: LiveAnnouncer, liveElement: any) {
+ return parentDispatcher || new LiveAnnouncer(liveElement);
+};
+
+export const LIVE_ANNOUNCER_PROVIDER = {
+ // If there is already a LiveAnnouncer available, use that. Otherwise, provide a new one.
+ provide: LiveAnnouncer,
+ deps: [
+ [new Optional(), new SkipSelf(), LiveAnnouncer],
+ [new Optional(), new Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN)]
+ ],
+ useFactory: LIVE_ANNOUNCER_PROVIDER_FACTORY
+};
@@ -71,6 +71,7 @@ export class MatPrefixEnforcer {
}]
})
export class DefaultStyleCompatibilityModeModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: DefaultStyleCompatibilityModeModule,
@@ -60,6 +60,7 @@ export class MdPrefixEnforcer {
}],
})
export class NoConflictStyleCompatibilityMode {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: NoConflictStyleCompatibilityMode,
@@ -1,4 +1,4 @@
-import {Injectable} from '@angular/core';
+import {Injectable, Optional, SkipSelf} from '@angular/core';
// Users of the Dispatcher never need to see this type, but TypeScript requires it to be exported.
@@ -33,3 +33,15 @@ export class UniqueSelectionDispatcher {
this._listeners.push(listener);
}
}
+
+export function UNIQUE_SELECTION_DISPATCHER_PROVIDER_FACTORY(
+ parentDispatcher: UniqueSelectionDispatcher) {
+ return parentDispatcher || new UniqueSelectionDispatcher();
+}
+
+export const UNIQUE_SELECTION_DISPATCHER_PROVIDER = {
+ // If there is already a dispatcher available, use that. Otherwise, provide a new one.
+ provide: UniqueSelectionDispatcher,
+ deps: [[new Optional(), new SkipSelf(), UniqueSelectionDispatcher]],
+ useFactory: UNIQUE_SELECTION_DISPATCHER_PROVIDER_FACTORY
+};
View
@@ -6,8 +6,7 @@ import {MdOptionModule} from './option/option';
import {MdRippleModule} from './ripple/ripple';
import {PortalModule} from './portal/portal-directives';
import {OverlayModule} from './overlay/overlay-directives';
-import {A11yModule, A11Y_PROVIDERS} from './a11y/index';
-import {OVERLAY_PROVIDERS} from './overlay/overlay';
+import {A11yModule} from './a11y/index';
// RTL
@@ -71,8 +70,8 @@ export {
AriaLivePoliteness,
LiveAnnouncer,
LIVE_ANNOUNCER_ELEMENT_TOKEN,
+ LIVE_ANNOUNCER_PROVIDER,
} from './a11y/live-announcer';
-
/** @deprecated */
export {LiveAnnouncer as MdLiveAnnouncer} from './a11y/live-announcer';
@@ -84,9 +83,9 @@ export {A11yModule} from './a11y/index';
export {
UniqueSelectionDispatcher,
- UniqueSelectionDispatcherListener
+ UniqueSelectionDispatcherListener,
+ UNIQUE_SELECTION_DISPATCHER_PROVIDER,
} from './coordination/unique-selection-dispatcher';
-
/** @deprecated */
export {
UniqueSelectionDispatcher as MdUniqueSelectionDispatcher
@@ -143,10 +142,11 @@ export {NoConflictStyleCompatibilityMode} from './compatibility/no-conflict-mode
],
})
export class MdCoreModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: MdCoreModule,
- providers: [A11Y_PROVIDERS, OVERLAY_PROVIDERS],
+ providers: [],
};
}
}
@@ -46,6 +46,7 @@ export class ObserveContent implements AfterContentInit, OnDestroy {
declarations: [ObserveContent]
})
export class ObserveContentModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: ObserveContentModule,
@@ -1,4 +1,5 @@
-import {Injectable} from '@angular/core';
+import {Injectable, Optional, SkipSelf} from '@angular/core';
+
/**
* The OverlayContainer is the container in which all overlays will load.
@@ -30,3 +31,14 @@ export class OverlayContainer {
this._containerElement = container;
}
}
+
+export function OVERLAY_CONTAINER_PROVIDER_FACTORY(parentContainer: OverlayContainer) {
+ return parentContainer || new OverlayContainer();
+};
+
+export const OVERLAY_CONTAINER_PROVIDER = {
+ // If there is already an OverlayContainer available, use that. Otherwise, provide a new one.
+ provide: OverlayContainer,
+ deps: [[new Optional(), new SkipSelf(), OverlayContainer]],
+ useFactory: OVERLAY_CONTAINER_PROVIDER_FACTORY
+};
@@ -295,12 +295,14 @@ export class ConnectedOverlayDirective implements OnDestroy {
imports: [PortalModule],
exports: [ConnectedOverlayDirective, OverlayOrigin, Scrollable],
declarations: [ConnectedOverlayDirective, OverlayOrigin, Scrollable],
+ providers: [OVERLAY_PROVIDERS],
})
export class OverlayModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: OverlayModule,
- providers: OVERLAY_PROVIDERS,
+ providers: [],
};
}
}
@@ -4,14 +4,16 @@ import {
ApplicationRef,
Injector,
NgZone,
+ Provider,
} from '@angular/core';
import {OverlayState} from './overlay-state';
import {DomPortalHost} from '../portal/dom-portal-host';
import {OverlayRef} from './overlay-ref';
import {OverlayPositionBuilder} from './position/overlay-position-builder';
-import {ViewportRuler} from './position/viewport-ruler';
-import {OverlayContainer} from './overlay-container';
-import {ScrollDispatcher} from './scroll/scroll-dispatcher';
+import {VIEWPORT_RULER_PROVIDER} from './position/viewport-ruler';
+import {OverlayContainer, OVERLAY_CONTAINER_PROVIDER} from './overlay-container';
+import {SCROLL_DISPATCHER_PROVIDER} from './scroll/scroll-dispatcher';
+
/** Next overlay unique ID. */
let nextUniqueId = 0;
@@ -88,10 +90,10 @@ export class Overlay {
}
/** Providers for Overlay and its related injectables. */
-export const OVERLAY_PROVIDERS = [
- ViewportRuler,
- OverlayPositionBuilder,
+export const OVERLAY_PROVIDERS: Provider[] = [
Overlay,
- OverlayContainer,
- ScrollDispatcher,
+ OverlayPositionBuilder,
+ VIEWPORT_RULER_PROVIDER,
+ SCROLL_DISPATCHER_PROVIDER,
+ OVERLAY_CONTAINER_PROVIDER,
];
@@ -1,5 +1,4 @@
-import {Injectable} from '@angular/core';
-
+import {Injectable, Optional, SkipSelf} from '@angular/core';
/**
@@ -56,3 +55,14 @@ export class ViewportRuler {
return {top, left};
}
}
+
+export function VIEWPORT_RULER_PROVIDER_FACTORY(parentDispatcher: ViewportRuler) {
+ return parentDispatcher || new ViewportRuler();
+};
+
+export const VIEWPORT_RULER_PROVIDER = {
+ // If there is already a ViewportRuler available, use that. Otherwise, provide a new one.
+ provide: ViewportRuler,
+ deps: [[new Optional(), new SkipSelf(), ViewportRuler]],
+ useFactory: VIEWPORT_RULER_PROVIDER_FACTORY
+};
@@ -1,4 +1,4 @@
-import {Injectable, ElementRef} from '@angular/core';
+import {Injectable, ElementRef, Optional, SkipSelf} from '@angular/core';
import {Scrollable} from './scrollable';
import {Subject} from 'rxjs/Subject';
import {Observable} from 'rxjs/Observable';
@@ -88,3 +88,13 @@ export class ScrollDispatcher {
}
}
+export function SCROLL_DISPATCHER_PROVIDER_FACTORY(parentDispatcher: ScrollDispatcher) {
+ return parentDispatcher || new ScrollDispatcher();
+};
+
+export const SCROLL_DISPATCHER_PROVIDER = {
+ // If there is already a ScrollDispatcher available, use that. Otherwise, provide a new one.
+ provide: ScrollDispatcher,
+ deps: [[new Optional(), new SkipSelf(), ScrollDispatcher]],
+ useFactory: SCROLL_DISPATCHER_PROVIDER_FACTORY
+};
@@ -5,12 +5,15 @@ export * from './platform';
export * from './features';
-@NgModule({})
+@NgModule({
+ providers: [Platform]
+})
export class PlatformModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: PlatformModule,
- providers: [Platform],
+ providers: [],
};
}
}
@@ -130,6 +130,7 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
declarations: [TemplatePortalDirective, PortalHostDirective],
})
export class PortalModule {
+ /** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: PortalModule,
Oops, something went wrong.

0 comments on commit b49bfce

Please sign in to comment.