Skip to content

Commit

Permalink
feat: remove the need for forRoot on material NgModules (#2556)
Browse files Browse the repository at this point in the history
  • Loading branch information
jelbourn authored and tinayuangao committed Jan 11, 2017
1 parent fed5d7b commit b49bfce
Show file tree
Hide file tree
Showing 45 changed files with 365 additions and 82 deletions.
1 change: 1 addition & 0 deletions src/lib/autocomplete/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export * from './autocomplete-trigger';
declarations: [MdAutocomplete, MdAutocompleteTrigger],
})
export class MdAutocompleteModule {
/** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: MdAutocompleteModule,
Expand Down
5 changes: 4 additions & 1 deletion src/lib/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
UniqueSelectionDispatcher,
coerceBooleanProperty,
DefaultStyleCompatibilityModeModule,
UNIQUE_SELECTION_DISPATCHER_PROVIDER,
} from '../core';

/** Acceptable types for a button toggle. */
Expand Down Expand Up @@ -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: []
};
}
}
4 changes: 2 additions & 2 deletions src/lib/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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: []
};
}
}
1 change: 1 addition & 0 deletions src/lib/card/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export class MdCardTitleGroup {}
],
})
export class MdCardModule {
/** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: MdCardModule,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -399,10 +398,11 @@ export class MdCheckbox implements ControlValueAccessor {
declarations: [MdCheckbox],
})
export class MdCheckboxModule {
/** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: MdCheckboxModule,
providers: [ViewportRuler]
providers: []
};
}
}
1 change: 1 addition & 0 deletions src/lib/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export class MdChipList implements AfterContentInit {
declarations: [MdChipList, MdChip]
})
export class MdChipsModule {
/** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: MdChipsModule,
Expand Down
11 changes: 4 additions & 7 deletions src/lib/core/a11y/index.ts
Original file line number Diff line number Diff line change
@@ -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: [],
};
}
}
1 change: 0 additions & 1 deletion src/lib/core/a11y/live-announcer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,3 @@ class TestApp {
this.live.announce(message);
}
}

17 changes: 16 additions & 1 deletion src/lib/core/a11y/live-announcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
Injectable,
OpaqueToken,
Optional,
Inject
Inject,
SkipSelf,
} from '@angular/core';

export const LIVE_ANNOUNCER_ELEMENT_TOKEN = new OpaqueToken('liveAnnouncerElement');
Expand Down Expand Up @@ -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
};
1 change: 1 addition & 0 deletions src/lib/core/compatibility/default-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class MatPrefixEnforcer {
}]
})
export class DefaultStyleCompatibilityModeModule {
/** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: DefaultStyleCompatibilityModeModule,
Expand Down
1 change: 1 addition & 0 deletions src/lib/core/compatibility/no-conflict-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class MdPrefixEnforcer {
}],
})
export class NoConflictStyleCompatibilityMode {
/** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: NoConflictStyleCompatibilityMode,
Expand Down
14 changes: 13 additions & 1 deletion src/lib/core/coordination/unique-selection-dispatcher.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
};
12 changes: 6 additions & 6 deletions src/lib/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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';

Expand All @@ -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
Expand Down Expand Up @@ -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: [],
};
}
}
1 change: 1 addition & 0 deletions src/lib/core/observe-content/observe-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class ObserveContent implements AfterContentInit, OnDestroy {
declarations: [ObserveContent]
})
export class ObserveContentModule {
/** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: ObserveContentModule,
Expand Down
14 changes: 13 additions & 1 deletion src/lib/core/overlay/overlay-container.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
};
4 changes: 3 additions & 1 deletion src/lib/core/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
};
}
}
18 changes: 10 additions & 8 deletions src/lib/core/overlay/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
];
14 changes: 12 additions & 2 deletions src/lib/core/overlay/position/viewport-ruler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Injectable} from '@angular/core';

import {Injectable, Optional, SkipSelf} from '@angular/core';


/**
Expand Down Expand Up @@ -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
};
12 changes: 11 additions & 1 deletion src/lib/core/overlay/scroll/scroll-dispatcher.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
};
7 changes: 5 additions & 2 deletions src/lib/core/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
};
}
}
1 change: 1 addition & 0 deletions src/lib/core/portal/portal-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
declarations: [TemplatePortalDirective, PortalHostDirective],
})
export class PortalModule {
/** @deprecated */
static forRoot(): ModuleWithProviders {
return {
ngModule: PortalModule,
Expand Down
Loading

0 comments on commit b49bfce

Please sign in to comment.