Skip to content

Commit a2869df

Browse files
committed
Use viewport ruler
1 parent 66f1cf5 commit a2869df

File tree

8 files changed

+93
-11
lines changed

8 files changed

+93
-11
lines changed

src/lib/button/button.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {async, TestBed, ComponentFixture} from '@angular/core/testing';
22
import {Component} from '@angular/core';
33
import {By} from '@angular/platform-browser';
44
import {MdButtonModule} from './button';
5+
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
56

67

78
describe('MdButton', () => {
@@ -10,6 +11,9 @@ describe('MdButton', () => {
1011
TestBed.configureTestingModule({
1112
imports: [MdButtonModule.forRoot()],
1213
declarations: [TestApp],
14+
providers: [
15+
{provide: ViewportRuler, useClass: FakeViewportRuler},
16+
]
1317
});
1418

1519
TestBed.compileComponents();
@@ -210,3 +214,15 @@ class TestApp {
210214
this.clickCount++;
211215
}
212216
}
217+
218+
class FakeViewportRuler {
219+
getViewportRect() {
220+
return {
221+
left: 0, top: 0, width: 1014, height: 686, bottom: 686, right: 1014
222+
};
223+
}
224+
225+
getViewportScrollPosition() {
226+
return {top: 0, left: 0};
227+
}
228+
}

src/lib/checkbox/checkbox.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import {Component, DebugElement} from '@angular/core';
1313
import {By} from '@angular/platform-browser';
1414
import {MdCheckbox, MdCheckboxChange, MdCheckboxModule} from './checkbox';
15+
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
1516

1617

1718
// TODO: Implement E2E tests for spacebar/click behavior for checking/unchecking
@@ -32,6 +33,9 @@ describe('MdCheckbox', () => {
3233
CheckboxWithNameAttribute,
3334
CheckboxWithChangeEvent,
3435
],
36+
providers: [
37+
{provide: ViewportRuler, useClass: FakeViewportRuler},
38+
]
3539
});
3640

3741
TestBed.compileComponents();
@@ -660,3 +664,15 @@ class CheckboxWithNameAttribute {}
660664
class CheckboxWithChangeEvent {
661665
lastEvent: MdCheckboxChange;
662666
}
667+
668+
class FakeViewportRuler {
669+
getViewportRect() {
670+
return {
671+
left: 0, top: 0, width: 1014, height: 686, bottom: 686, right: 1014
672+
};
673+
}
674+
675+
getViewportScrollPosition() {
676+
return {top: 0, left: 0};
677+
}
678+
}

src/lib/core/ripple/ripple.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,14 @@ describe('MdRipple', () => {
190190
let top = 75;
191191

192192
// Add a very large element to make the page scroll
193-
let veryLargeElement: HTMLElement = document.createElement('div');
193+
let veryLargeElement = document.createElement('div');
194194
veryLargeElement.style.width = '4000px';
195195
veryLargeElement.style.height = '4000px';
196196
document.body.appendChild(veryLargeElement);
197197
document.body.scrollTop = pageScrollTop;
198198
document.body.scrollLeft = pageScrollLeft;
199199

200+
rippleElement.style.position = 'absolute';
200201
rippleElement.style.left = `${elementLeft}px`;
201202
rippleElement.style.top = `${elementTop}px`;
202203

src/lib/core/ripple/ripple.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ForegroundRipple,
1616
ForegroundRippleState,
1717
} from './ripple-renderer';
18+
import {ViewportRuler} from "../overlay/position/viewport-ruler";
1819

1920

2021
@Directive({
@@ -60,14 +61,16 @@ export class MdRipple implements OnInit, OnDestroy, OnChanges {
6061
@HostBinding('class.md-ripple-unbounded') @Input('md-ripple-unbounded') unbounded: boolean;
6162

6263
private _rippleRenderer: RippleRenderer;
64+
_ruler: ViewportRuler;
6365

64-
constructor(_elementRef: ElementRef) {
66+
constructor(_elementRef: ElementRef, _ruler: ViewportRuler) {
6567
// These event handlers are attached to the element that triggers the ripple animations.
6668
const eventHandlers = new Map<string, (e: Event) => void>();
6769
eventHandlers.set('mousedown', (event: MouseEvent) => this._mouseDown(event));
6870
eventHandlers.set('click', (event: MouseEvent) => this._click(event));
6971
eventHandlers.set('mouseleave', (event: MouseEvent) => this._mouseLeave(event));
7072
this._rippleRenderer = new RippleRenderer(_elementRef, eventHandlers);
73+
this._ruler = _ruler;
7174
}
7275

7376
/** TODO: internal */
@@ -161,11 +164,10 @@ export class MdRipple implements OnInit, OnDestroy, OnChanges {
161164
// FIXME: This fails on IE11, which still sets pageX/Y and screenX/Y on keyboard clicks.
162165
const isKeyEvent =
163166
(event.screenX === 0 && event.screenY === 0 && event.pageX === 0 && event.pageY === 0);
164-
var scrollTop = window.pageYOffset || document.documentElement.scrollTop ||
165-
document.body.scrollTop || 0;
166-
var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft ||
167-
document.body.scrollLeft || 0;
168-
this.end(event.pageX - scrollLeft, event.pageY - scrollTop, isKeyEvent);
167+
168+
this.end(event.pageX - this._ruler.getViewportScrollPosition().left,
169+
event.pageY - this._ruler.getViewportScrollPosition().top,
170+
isKeyEvent);
169171
}
170172
}
171173

@@ -189,7 +191,7 @@ export class MdRippleModule {
189191
static forRoot(): ModuleWithProviders {
190192
return {
191193
ngModule: MdRippleModule,
192-
providers: []
194+
providers: [ViewportRuler]
193195
};
194196
}
195197
}

src/lib/radio/radio.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {NgControl, FormsModule} from '@angular/forms';
33
import {Component, DebugElement} from '@angular/core';
44
import {By} from '@angular/platform-browser';
55
import {MdRadioGroup, MdRadioButton, MdRadioChange, MdRadioModule} from './radio';
6+
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
67

78

89
describe('MdRadio', () => {
@@ -15,6 +16,9 @@ describe('MdRadio', () => {
1516
RadioGroupWithNgModel,
1617
StandaloneRadioButtons,
1718
],
19+
providers: [
20+
{provide: ViewportRuler, useClass: FakeViewportRuler},
21+
]
1822
});
1923

2024
TestBed.compileComponents();
@@ -593,3 +597,15 @@ function dispatchEvent(eventName: string, element: HTMLElement): void {
593597
event.initEvent(eventName, true, true);
594598
element.dispatchEvent(event);
595599
}
600+
601+
class FakeViewportRuler {
602+
getViewportRect() {
603+
return {
604+
left: 0, top: 0, width: 1014, height: 686, bottom: 686, right: 1014
605+
};
606+
}
607+
608+
getViewportScrollPosition() {
609+
return {top: 0, left: 0};
610+
}
611+
}

src/lib/tabs/tab-group.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {Component, ViewChild} from '@angular/core';
77
import {By} from '@angular/platform-browser';
88
import {Observable} from 'rxjs/Observable';
99
import {LayoutDirection, Dir} from '../core/rtl/dir';
10+
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
1011

1112

1213
describe('MdTabGroup', () => {
@@ -24,7 +25,8 @@ describe('MdTabGroup', () => {
2425
providers: [
2526
{provide: Dir, useFactory: () => {
2627
return {value: dir};
27-
}}
28+
}},
29+
{provide: ViewportRuler, useClass: FakeViewportRuler},
2830
]
2931
});
3032

@@ -493,3 +495,15 @@ class TabGroupWithSimpleApi {
493495
otherContent = 'Apples, grapes';
494496
@ViewChild('legumes') legumes: any;
495497
}
498+
499+
class FakeViewportRuler {
500+
getViewportRect() {
501+
return {
502+
left: 0, top: 0, width: 1014, height: 686, bottom: 686, right: 1014
503+
};
504+
}
505+
506+
getViewportScrollPosition() {
507+
return {top: 0, left: 0};
508+
}
509+
}

src/lib/tabs/tab-nav-bar/tab-nav-bar.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {async, ComponentFixture, TestBed} from '@angular/core/testing';
22
import {MdTabsModule} from '../tabs';
33
import {Component} from '@angular/core';
44
import {By} from '@angular/platform-browser';
5+
import {ViewportRuler} from '../../core/overlay/position/viewport-ruler';
56

67

78
describe('MdTabNavBar', () => {
@@ -12,6 +13,9 @@ describe('MdTabNavBar', () => {
1213
declarations: [
1314
SimpleTabNavBarTestApp
1415
],
16+
providers: [
17+
{provide: ViewportRuler, useClass: FakeViewportRuler},
18+
]
1519
});
1620

1721
TestBed.compileComponents();
@@ -53,3 +57,15 @@ describe('MdTabNavBar', () => {
5357
class SimpleTabNavBarTestApp {
5458
activeIndex = 0;
5559
}
60+
61+
class FakeViewportRuler {
62+
getViewportRect() {
63+
return {
64+
left: 0, top: 0, width: 1014, height: 686, bottom: 686, right: 1014
65+
};
66+
}
67+
68+
getViewportScrollPosition() {
69+
return {top: 0, left: 0};
70+
}
71+
}

src/lib/tabs/tab-nav-bar/tab-nav-bar.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Component, Input, ViewChild, ElementRef, ViewEncapsulation, Directive} from '@angular/core';
22
import {MdInkBar} from '../ink-bar';
33
import {MdRipple} from '../../core/ripple/ripple';
4+
import {ViewportRuler} from "../../core/overlay/position/viewport-ruler";
45

56
/**
67
* Navigation component matching the styles of the tab group header.
@@ -51,7 +52,7 @@ export class MdTabLink {
5152
selector: '[md-tab-link]',
5253
})
5354
export class MdTabLinkRipple extends MdRipple {
54-
constructor(private _element: ElementRef) {
55-
super(_element);
55+
constructor(private _element: ElementRef, _ruler: ViewportRuler) {
56+
super(_element, _ruler);
5657
}
5758
}

0 commit comments

Comments
 (0)