Skip to content

Commit 89a4108

Browse files
committed
refactor to use existing Platform class
1 parent bea3885 commit 89a4108

19 files changed

+110
-79
lines changed

src/lib/button-toggle/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import {MdButtonToggleGroup, MdButtonToggleGroupMultiple, MdButtonToggle} from '
44
import {
55
UNIQUE_SELECTION_DISPATCHER_PROVIDER,
66
MdCommonModule,
7-
FocusOriginMonitor,
7+
StyleModule,
88
} from '../core';
99

1010

1111
@NgModule({
12-
imports: [FormsModule, MdCommonModule],
12+
imports: [FormsModule, MdCommonModule, StyleModule],
1313
exports: [
1414
MdButtonToggleGroup,
1515
MdButtonToggleGroupMultiple,
1616
MdButtonToggle,
1717
MdCommonModule,
1818
],
1919
declarations: [MdButtonToggleGroup, MdButtonToggleGroupMultiple, MdButtonToggle],
20-
providers: [UNIQUE_SELECTION_DISPATCHER_PROVIDER, FocusOriginMonitor]
20+
providers: [UNIQUE_SELECTION_DISPATCHER_PROVIDER]
2121
})
2222
export class MdButtonToggleModule {}
2323

src/lib/button/button.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import {
99
Renderer2,
1010
ViewEncapsulation
1111
} from '@angular/core';
12-
import {coerceBooleanProperty, FocusOriginMonitor} from '../core';
12+
import {coerceBooleanProperty, FocusOriginMonitor, Platform} from '../core';
1313
import {mixinDisabled, CanDisable} from '../core/common-behaviors/disabled';
14-
import {isBrowser} from '../core/platform/browser';
1514

1615

1716
// TODO(kara): Convert attribute selectors to classes when attr maps become available
@@ -111,8 +110,11 @@ export class MdButton extends _MdButtonMixinBase implements OnDestroy, CanDisabl
111110
get disableRipple() { return this._disableRipple; }
112111
set disableRipple(v) { this._disableRipple = coerceBooleanProperty(v); }
113112

114-
constructor(private _elementRef: ElementRef, private _renderer: Renderer2,
115-
private _focusOriginMonitor: FocusOriginMonitor) {
113+
constructor(
114+
private _elementRef: ElementRef,
115+
private _renderer: Renderer2,
116+
private _platform: Platform,
117+
private _focusOriginMonitor: FocusOriginMonitor) {
116118
super();
117119
this._focusOriginMonitor.monitor(this._elementRef.nativeElement, this._renderer, true);
118120
}
@@ -163,7 +165,7 @@ export class MdButton extends _MdButtonMixinBase implements OnDestroy, CanDisabl
163165
// If not on the browser, say that there are none of the attributes present.
164166
// Since these only affect how the ripple displays (and ripples only happen on the client),
165167
// detecting these attributes isn't necessary when not on the browser.
166-
if (!isBrowser()) {
168+
if (!this._platform.isBrowser) {
167169
return false;
168170
}
169171

@@ -193,8 +195,12 @@ export class MdButton extends _MdButtonMixinBase implements OnDestroy, CanDisabl
193195
encapsulation: ViewEncapsulation.None
194196
})
195197
export class MdAnchor extends MdButton {
196-
constructor(elementRef: ElementRef, renderer: Renderer2, focusOriginMonitor: FocusOriginMonitor) {
197-
super(elementRef, renderer, focusOriginMonitor);
198+
constructor(
199+
elementRef: ElementRef,
200+
renderer: Renderer2,
201+
platform: Platform,
202+
focusOriginMonitor: FocusOriginMonitor) {
203+
super(elementRef, renderer, platform, focusOriginMonitor);
198204
}
199205

200206
/** @docs-private */

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {inject, fakeAsync, tick, ComponentFixture, TestBed} from '@angular/core/
22
import {Component} from '@angular/core';
33
import {By} from '@angular/platform-browser';
44
import {LiveAnnouncer, LIVE_ANNOUNCER_ELEMENT_TOKEN} from './live-announcer';
5+
import {A11yModule} from '../index';
56

67

78
describe('LiveAnnouncer', () => {
@@ -11,8 +12,8 @@ describe('LiveAnnouncer', () => {
1112

1213
describe('with default element', () => {
1314
beforeEach(() => TestBed.configureTestingModule({
15+
imports: [A11yModule],
1416
declarations: [TestApp],
15-
providers: [LiveAnnouncer]
1617
}));
1718

1819
beforeEach(fakeAsync(inject([LiveAnnouncer], (la: LiveAnnouncer) => {
@@ -77,11 +78,9 @@ describe('LiveAnnouncer', () => {
7778
customLiveElement = document.createElement('div');
7879

7980
return TestBed.configureTestingModule({
81+
imports: [A11yModule],
8082
declarations: [TestApp],
81-
providers: [
82-
{provide: LIVE_ANNOUNCER_ELEMENT_TOKEN, useValue: customLiveElement},
83-
LiveAnnouncer,
84-
],
83+
providers: [{provide: LIVE_ANNOUNCER_ELEMENT_TOKEN, useValue: customLiveElement}],
8584
});
8685
});
8786

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
Inject,
66
SkipSelf,
77
} from '@angular/core';
8+
import {Platform} from '../platform/platform';
9+
810

911
export const LIVE_ANNOUNCER_ELEMENT_TOKEN = new InjectionToken<HTMLElement>('liveAnnouncerElement');
1012

@@ -16,12 +18,16 @@ export class LiveAnnouncer {
1618

1719
private _liveElement: Element;
1820

19-
constructor(@Optional() @Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN) elementToken: any) {
20-
21-
// We inject the live element as `any` because the constructor signature cannot reference
22-
// browser globals (HTMLElement) on non-browser environments, since having a class decorator
23-
// causes TypeScript to preserve the constructor signature types.
24-
this._liveElement = elementToken || this._createLiveElement();
21+
constructor(
22+
@Optional() @Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN) elementToken: any,
23+
platform: Platform) {
24+
// Only do anything if we're on the browser platform.
25+
if (platform.isBrowser) {
26+
// We inject the live element as `any` because the constructor signature cannot reference
27+
// browser globals (HTMLElement) on non-browser environments, since having a class decorator
28+
// causes TypeScript to preserve the constructor signature types.
29+
this._liveElement = elementToken || this._createLiveElement();
30+
}
2531
}
2632

2733
/**
@@ -64,16 +70,18 @@ export class LiveAnnouncer {
6470

6571
}
6672

67-
export function LIVE_ANNOUNCER_PROVIDER_FACTORY(parentDispatcher: LiveAnnouncer, liveElement: any) {
68-
return parentDispatcher || new LiveAnnouncer(liveElement);
73+
export function LIVE_ANNOUNCER_PROVIDER_FACTORY(
74+
parentDispatcher: LiveAnnouncer, liveElement: any, platform: Platform) {
75+
return parentDispatcher || new LiveAnnouncer(liveElement, platform);
6976
}
7077

7178
export const LIVE_ANNOUNCER_PROVIDER = {
7279
// If there is already a LiveAnnouncer available, use that. Otherwise, provide a new one.
7380
provide: LiveAnnouncer,
7481
deps: [
7582
[new Optional(), new SkipSelf(), LiveAnnouncer],
76-
[new Optional(), new Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN)]
83+
[new Optional(), new Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN)],
84+
Platform,
7785
],
7886
useFactory: LIVE_ANNOUNCER_PROVIDER_FACTORY
7987
};

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {coerceBooleanProperty} from '../coercion/boolean-property';
2929
import {ESCAPE} from '../keyboard/keycodes';
3030
import {ScrollDispatcher} from './scroll/scroll-dispatcher';
3131
import {Subscription} from 'rxjs/Subscription';
32+
import {ScrollDispatchModule} from './scroll/scroll-dispatcher';
3233

3334

3435
/** Default set of positions for the overlay. Follows the behavior of a dropdown. */
@@ -323,9 +324,9 @@ export class ConnectedOverlayDirective implements OnDestroy {
323324

324325

325326
@NgModule({
326-
imports: [PortalModule],
327-
exports: [ConnectedOverlayDirective, OverlayOrigin, Scrollable],
328-
declarations: [ConnectedOverlayDirective, OverlayOrigin, Scrollable],
327+
imports: [PortalModule, ScrollDispatchModule],
328+
exports: [ConnectedOverlayDirective, OverlayOrigin, ScrollDispatchModule],
329+
declarations: [ConnectedOverlayDirective, OverlayOrigin],
329330
providers: [OVERLAY_PROVIDERS],
330331
})
331332
export class OverlayModule {}

src/lib/core/overlay/overlay.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {OverlayRef} from './overlay-ref';
1212
import {OverlayPositionBuilder} from './position/overlay-position-builder';
1313
import {VIEWPORT_RULER_PROVIDER} from './position/viewport-ruler';
1414
import {OverlayContainer, OVERLAY_CONTAINER_PROVIDER} from './overlay-container';
15-
import {SCROLL_DISPATCHER_PROVIDER} from './scroll/scroll-dispatcher';
1615

1716

1817
/** Next overlay unique ID. */
@@ -94,6 +93,5 @@ export const OVERLAY_PROVIDERS: Provider[] = [
9493
Overlay,
9594
OverlayPositionBuilder,
9695
VIEWPORT_RULER_PROVIDER,
97-
SCROLL_DISPATCHER_PROVIDER,
9896
OVERLAY_CONTAINER_PROVIDER,
9997
];

src/lib/core/overlay/position/connected-position-strategy.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {Scrollable} from '../scroll/scrollable';
77
import {Subscription} from 'rxjs/Subscription';
88
import {TestBed, inject} from '@angular/core/testing';
99
import Spy = jasmine.Spy;
10-
import {SCROLL_DISPATCHER_PROVIDER} from '../scroll/scroll-dispatcher';
10+
import {ScrollDispatchModule} from '../scroll/scroll-dispatcher';
1111

1212

1313
// Default width and height of the overlay and origin panels throughout these tests.
@@ -23,7 +23,8 @@ describe('ConnectedPositionStrategy', () => {
2323
let viewportRuler: ViewportRuler;
2424

2525
beforeEach(() => TestBed.configureTestingModule({
26-
providers: [VIEWPORT_RULER_PROVIDER, SCROLL_DISPATCHER_PROVIDER]
26+
imports: [ScrollDispatchModule],
27+
providers: [VIEWPORT_RULER_PROVIDER]
2728
}));
2829

2930
beforeEach(inject([ViewportRuler], (_ruler: ViewportRuler) => {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {ViewportRuler, VIEWPORT_RULER_PROVIDER} from './viewport-ruler';
22
import {TestBed, inject} from '@angular/core/testing';
3-
import {SCROLL_DISPATCHER_PROVIDER} from '../scroll/scroll-dispatcher';
3+
import {ScrollDispatchModule} from '../scroll/scroll-dispatcher';
4+
45

56
// For all tests, we assume the browser window is 1024x786 (outerWidth x outerHeight).
67
// The karma config has been set to this for local tests, and it is the default size
@@ -22,7 +23,8 @@ describe('ViewportRuler', () => {
2223
veryLargeElement.style.height = '6000px';
2324

2425
beforeEach(() => TestBed.configureTestingModule({
25-
providers: [VIEWPORT_RULER_PROVIDER, SCROLL_DISPATCHER_PROVIDER]
26+
imports: [ScrollDispatchModule],
27+
providers: [VIEWPORT_RULER_PROVIDER]
2628
}));
2729

2830
beforeEach(inject([ViewportRuler], (viewportRuler: ViewportRuler) => {

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {ElementRef, Injectable, NgZone, Optional, SkipSelf} from '@angular/core';
2-
import {isBrowser} from '../../platform/browser';
1+
import {NgModule, ElementRef, Injectable, NgZone, Optional, SkipSelf} from '@angular/core';
2+
import {Platform, PlatformModule} from '../../platform/index';
33
import {Scrollable} from './scrollable';
44
import {Subject} from 'rxjs/Subject';
55
import {Observable} from 'rxjs/Observable';
@@ -18,7 +18,7 @@ export const DEFAULT_SCROLL_TIME = 20;
1818
*/
1919
@Injectable()
2020
export class ScrollDispatcher {
21-
constructor(private _ngZone: NgZone) { }
21+
constructor(private _ngZone: NgZone, private _platform: Platform) { }
2222

2323
/** Subject for notifying that a registered scrollable reference element has been scrolled. */
2424
_scrolled: Subject<void> = new Subject<void>();
@@ -63,7 +63,8 @@ export class ScrollDispatcher {
6363
* to override the default "throttle" time.
6464
*/
6565
scrolled(auditTimeInMs: number = DEFAULT_SCROLL_TIME, callback: () => any): Subscription {
66-
if (!isBrowser()) {
66+
// Scroll events can only happen on the browser, so do nothing if we're not on the browser.
67+
if (!this._platform.isBrowser) {
6768
return Subscription.EMPTY;
6869
}
6970

@@ -131,14 +132,23 @@ export class ScrollDispatcher {
131132
}
132133
}
133134

134-
export function SCROLL_DISPATCHER_PROVIDER_FACTORY(parentDispatcher: ScrollDispatcher,
135-
ngZone: NgZone) {
136-
return parentDispatcher || new ScrollDispatcher(ngZone);
135+
export function SCROLL_DISPATCHER_PROVIDER_FACTORY(
136+
parentDispatcher: ScrollDispatcher, ngZone: NgZone, platform: Platform) {
137+
return parentDispatcher || new ScrollDispatcher(ngZone, platform);
137138
}
138139

139140
export const SCROLL_DISPATCHER_PROVIDER = {
140141
// If there is already a ScrollDispatcher available, use that. Otherwise, provide a new one.
141142
provide: ScrollDispatcher,
142-
deps: [[new Optional(), new SkipSelf(), ScrollDispatcher], NgZone],
143+
deps: [[new Optional(), new SkipSelf(), ScrollDispatcher], NgZone, Platform],
143144
useFactory: SCROLL_DISPATCHER_PROVIDER_FACTORY
144145
};
146+
147+
148+
@NgModule({
149+
imports: [PlatformModule],
150+
exports: [Scrollable],
151+
declarations: [Scrollable],
152+
providers: [SCROLL_DISPATCHER_PROVIDER],
153+
})
154+
export class ScrollDispatchModule { }

src/lib/core/platform/browser.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/lib/core/platform/platform.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const hasV8BreakIterator = typeof(window) !== 'undefined' ?
1515
*/
1616
@Injectable()
1717
export class Platform {
18+
isBrowser: boolean = typeof document === 'object' && !!document;
19+
1820
/** Layout Engines */
1921
EDGE = /(edge)/i.test(navigator.userAgent);
2022
TRIDENT = /(msie|trident)/i.test(navigator.userAgent);

src/lib/core/ripple/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import {NgModule} from '@angular/core';
22
import {MdRipple} from './ripple';
33
import {MdCommonModule} from '../common-behaviors/common-module';
44
import {VIEWPORT_RULER_PROVIDER} from '../overlay/position/viewport-ruler';
5-
import {SCROLL_DISPATCHER_PROVIDER} from '../overlay/scroll/scroll-dispatcher';
5+
import {ScrollDispatchModule} from '../overlay/scroll/scroll-dispatcher';
6+
import {PlatformModule} from '../platform/index';
67

78
export {MdRipple, RippleGlobalOptions, MD_RIPPLE_GLOBAL_OPTIONS} from './ripple';
89
export {RippleRef, RippleState} from './ripple-ref';
910
export {RippleConfig, RIPPLE_FADE_IN_DURATION, RIPPLE_FADE_OUT_DURATION} from './ripple-renderer';
1011

1112
@NgModule({
12-
imports: [MdCommonModule],
13+
imports: [MdCommonModule, PlatformModule, ScrollDispatchModule],
1314
exports: [MdRipple, MdCommonModule],
1415
declarations: [MdRipple],
15-
providers: [VIEWPORT_RULER_PROVIDER, SCROLL_DISPATCHER_PROVIDER],
16+
providers: [VIEWPORT_RULER_PROVIDER],
1617
})
1718
export class MdRippleModule {}

src/lib/core/ripple/ripple-renderer.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ElementRef, NgZone} from '@angular/core';
2-
import {isBrowser} from '../platform/browser';
2+
import {Platform} from '../platform/platform';
33
import {ViewportRuler} from '../overlay/position/viewport-ruler';
44
import {RippleRef, RippleState} from './ripple-ref';
55

@@ -48,10 +48,14 @@ export class RippleRenderer {
4848
/** Whether mouse ripples should be created or not. */
4949
rippleDisabled: boolean = false;
5050

51-
constructor(_elementRef: ElementRef, private _ngZone: NgZone, private _ruler: ViewportRuler) {
51+
constructor(
52+
elementRef: ElementRef,
53+
private _ngZone: NgZone,
54+
private _ruler: ViewportRuler,
55+
platform: Platform) {
5256
// Only do anything if we're on the browser.
53-
if (isBrowser()) {
54-
this._containerElement = _elementRef.nativeElement;
57+
if (platform.isBrowser) {
58+
this._containerElement = elementRef.nativeElement;
5559

5660
// Specify events which need to be registered on the trigger.
5761
this._triggerEvents.set('mousedown', this.onMousedown.bind(this));

src/lib/core/ripple/ripple.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import {
1111
Optional,
1212
} from '@angular/core';
1313
import {RippleConfig, RippleRenderer} from './ripple-renderer';
14-
import {ViewportRuler} from '../overlay/position/viewport-ruler';
1514
import {RippleRef} from './ripple-ref';
15+
import {ViewportRuler} from '../overlay/position/viewport-ruler';
16+
import {Platform} from '../platform/platform';
1617

1718
export interface RippleGlobalOptions {
1819
disabled?: boolean;
@@ -27,7 +28,7 @@ export const MD_RIPPLE_GLOBAL_OPTIONS =
2728
selector: '[md-ripple], [mat-ripple], [mdRipple], [matRipple]',
2829
exportAs: 'mdRipple',
2930
host: {
30-
'[class.mat-ripple]': 'true',
31+
'class': 'mat-ripple',
3132
'[class.mat-ripple-unbounded]': 'unbounded'
3233
}
3334
})
@@ -83,9 +84,10 @@ export class MdRipple implements OnChanges, OnDestroy {
8384
elementRef: ElementRef,
8485
ngZone: NgZone,
8586
ruler: ViewportRuler,
87+
platform: Platform,
8688
@Optional() @Inject(MD_RIPPLE_GLOBAL_OPTIONS) globalOptions: RippleGlobalOptions
8789
) {
88-
this._rippleRenderer = new RippleRenderer(elementRef, ngZone, ruler);
90+
this._rippleRenderer = new RippleRenderer(elementRef, ngZone, ruler, platform);
8991
this._globalOptions = globalOptions ? globalOptions : {};
9092

9193
this._updateRippleRenderer();

0 commit comments

Comments
 (0)