From b730ab0d063f61d356d5590f45edda46581dbe00 Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Fri, 13 Jan 2017 10:07:37 -0800 Subject: [PATCH 01/10] feat(scrolling): add throttle to scroll dispatcher; add scroll adjust to tooltip --- src/demo-app/tooltip/tooltip-demo.html | 7 +- .../core/overlay/scroll/scroll-dispatcher.ts | 17 +++-- src/lib/sidenav/sidenav-container.html | 2 +- src/lib/tooltip/tooltip.spec.ts | 72 ++++++++++++++++--- src/lib/tooltip/tooltip.ts | 33 ++++++++- tools/gulp/tasks/components.ts | 1 + 6 files changed, 111 insertions(+), 21 deletions(-) diff --git a/src/demo-app/tooltip/tooltip-demo.html b/src/demo-app/tooltip/tooltip-demo.html index 79691037bea0..155f3c3da872 100644 --- a/src/demo-app/tooltip/tooltip-demo.html +++ b/src/demo-app/tooltip/tooltip-demo.html @@ -1,8 +1,8 @@

Tooltip Demo

-

- -

+ Scroll down while tooltip is open to see it hide automatically +

diff --git a/src/lib/core/overlay/scroll/scroll-dispatcher.ts b/src/lib/core/overlay/scroll/scroll-dispatcher.ts index e4f8b3864713..1d0ba26a6474 100644 --- a/src/lib/core/overlay/scroll/scroll-dispatcher.ts +++ b/src/lib/core/overlay/scroll/scroll-dispatcher.ts @@ -4,14 +4,20 @@ import {Subject} from 'rxjs/Subject'; import {Observable} from 'rxjs/Observable'; import {Subscription} from 'rxjs/Subscription'; import 'rxjs/add/observable/fromEvent'; +import 'rxjs/add/operator/auditTime'; +/** Time in ms to throttle the scrolling events by default. */ +export const DEFAULT_AUDIT_TIME = 20; + /** * Service contained all registered Scrollable references and emits an event when any one of the * Scrollable references emit a scrolled event. */ @Injectable() export class ScrollDispatcher { + events = 0; + /** Subject for notifying that a registered scrollable reference element has been scrolled. */ _scrolled: Subject = new Subject(); @@ -50,11 +56,11 @@ export class ScrollDispatcher { /** * Returns an observable that emits an event whenever any of the registered Scrollable - * references (or window, document, or body) fire a scrolled event. + * references (or window, document, or body) fire a scrolled event. Can provide a time in ms + * to override the default throttling time. */ - scrolled(): Observable { - // TODO: Add an event limiter that includes throttle with the leading and trailing events. - return this._scrolled.asObservable(); + scrolled(auditTimeInMs: number = DEFAULT_AUDIT_TIME): Observable { + return this._scrolled.asObservable().auditTime(auditTimeInMs); } /** Returns all registered Scrollables that contain the provided element. */ @@ -84,13 +90,14 @@ export class ScrollDispatcher { /** Sends a notification that a scroll event has been fired. */ _notify() { + this.events++; this._scrolled.next(); } } 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. diff --git a/src/lib/sidenav/sidenav-container.html b/src/lib/sidenav/sidenav-container.html index 75eb4b037ab8..24f0789dcde2 100644 --- a/src/lib/sidenav/sidenav-container.html +++ b/src/lib/sidenav/sidenav-container.html @@ -3,6 +3,6 @@ -

+
diff --git a/src/lib/tooltip/tooltip.spec.ts b/src/lib/tooltip/tooltip.spec.ts index 7305715ca920..345bade4a855 100644 --- a/src/lib/tooltip/tooltip.spec.ts +++ b/src/lib/tooltip/tooltip.spec.ts @@ -6,17 +6,13 @@ import { fakeAsync, flushMicrotasks } from '@angular/core/testing'; -import { - Component, - DebugElement, - AnimationTransitionEvent, - ChangeDetectionStrategy -} from '@angular/core'; +import {Component, DebugElement, AnimationTransitionEvent, ViewChild, ChangeDetectionStrategy} from '@angular/core'; import {By} from '@angular/platform-browser'; import {TooltipPosition, MdTooltip, MdTooltipModule} from './tooltip'; import {OverlayContainer} from '../core'; import {Dir, LayoutDirection} from '../core/rtl/dir'; import {OverlayModule} from '../core/overlay/overlay-directives'; +import {Scrollable} from '../core/overlay/scroll/scrollable'; const initialTooltipMessage = 'initial tooltip message'; @@ -27,7 +23,7 @@ describe('MdTooltip', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [MdTooltipModule.forRoot(), OverlayModule], - declarations: [BasicTooltipDemo, OnPushTooltipDemo], + declarations: [BasicTooltipDemo, ScrollableTooltipDemo, OnPushTooltipDemo], providers: [ {provide: OverlayContainer, useFactory: () => { overlayContainerElement = document.createElement('div'); @@ -312,6 +308,33 @@ describe('MdTooltip', () => { }); }); + describe('scrollable usage', () => { + let fixture: ComponentFixture; + let buttonDebugElement: DebugElement; + let buttonElement: HTMLButtonElement; + let tooltipDirective: MdTooltip; + + beforeEach(() => { + fixture = TestBed.createComponent(ScrollableTooltipDemo); + fixture.detectChanges(); + buttonDebugElement = fixture.debugElement.query(By.css('button')); + buttonElement = buttonDebugElement.nativeElement; + tooltipDirective = buttonDebugElement.injector.get(MdTooltip); + }); + + it('should hide tooltip if clipped after changing positions', fakeAsync(() => { + expect(tooltipDirective._tooltipInstance).toBeUndefined(); + + tooltipDirective.show(); + tick(0); // Tick for the show delay (default is 0) + expect(tooltipDirective._isTooltipVisible()).toBe(true); + + fixture.componentInstance.scrollDown(); + tick(); + expect(tooltipDirective._isTooltipVisible()).toBe(false); + })); + }); + describe('with OnPush', () => { let fixture: ComponentFixture; let buttonDebugElement: DebugElement; @@ -374,6 +397,38 @@ class BasicTooltipDemo { message: string = initialTooltipMessage; showButton: boolean = true; } + +@Component({ + selector: 'app', + template: ` +
+ +
` +}) +class ScrollableTooltipDemo { + position: string = 'below'; + message: string = initialTooltipMessage; + showButton: boolean = true; + + @ViewChild(Scrollable) scrollingContainer: Scrollable; + + scrollDown() { + const scrollingContainerEl = this.scrollingContainer.getElementRef().nativeElement; + scrollingContainerEl.scrollTop = 50; + + // Emit a scroll event from the scrolling element in our component. + // This event should be picked up by the scrollable directive and notify. + // The notification should be picked up by the service. + const scrollEvent = document.createEvent('UIEvents'); + scrollEvent.initUIEvent('scroll', true, true, window, 0); + scrollingContainerEl.dispatchEvent(scrollEvent); + } +} + @Component({ selector: 'app', template: ` @@ -386,5 +441,4 @@ class BasicTooltipDemo { class OnPushTooltipDemo { position: string = 'below'; message: string = initialTooltipMessage; -} - +} \ No newline at end of file diff --git a/src/lib/tooltip/tooltip.ts b/src/lib/tooltip/tooltip.ts index 94dc7780062a..219ef1a2aec4 100644 --- a/src/lib/tooltip/tooltip.ts +++ b/src/lib/tooltip/tooltip.ts @@ -15,6 +15,7 @@ import { NgZone, Optional, OnDestroy, + OnInit, ChangeDetectorRef } from '@angular/core'; import { @@ -32,6 +33,7 @@ import {Observable} from 'rxjs/Observable'; import {Subject} from 'rxjs/Subject'; import {Dir} from '../core/rtl/dir'; import 'rxjs/add/operator/first'; +import {ScrollDispatcher} from '../core/overlay/scroll/scroll-dispatcher'; export type TooltipPosition = 'left' | 'right' | 'above' | 'below' | 'before' | 'after'; @@ -54,7 +56,7 @@ export const TOUCHEND_HIDE_DELAY = 1500; }, exportAs: 'mdTooltip', }) -export class MdTooltip implements OnDestroy { +export class MdTooltip implements OnInit, OnDestroy { _overlayRef: OverlayRef; _tooltipInstance: TooltipComponent; @@ -123,11 +125,26 @@ export class MdTooltip implements OnDestroy { set _matShowDelay(v) { this.showDelay = v; } constructor(private _overlay: Overlay, + private _scrollDispatcher: ScrollDispatcher, private _elementRef: ElementRef, private _viewContainerRef: ViewContainerRef, private _ngZone: NgZone, @Optional() private _dir: Dir) { } + ngOnInit() { + // When a scroll on the page occurs, update the position in case this tooltip needs + // to be repositioned. + this._scrollDispatcher.scrolled().subscribe(() => { + if (this._scrollDispatcher.events > 1) { + console.log(`Saved ${this._scrollDispatcher.events - 1} ${this._scrollDispatcher.events > 1 ? 'events' : 'event'}`); + } + this._scrollDispatcher.events = 0; + if (this._overlayRef) { + this._overlayRef.updatePosition(); + } + }); + } + /** * Dispose the tooltip when destroyed. */ @@ -185,7 +202,17 @@ export class MdTooltip implements OnDestroy { private _createOverlay(): void { let origin = this._getOrigin(); let position = this._getOverlayPosition(); + + // Create connected position strategy that listens for scroll events to reposition. + // After position changes occur and the overlay is clipped by a parent scrollable then + // close the tooltip. let strategy = this._overlay.position().connectedTo(this._elementRef, origin, position); + strategy.withScrollableContainers(this._scrollDispatcher.getScrollContainers(this._elementRef)); + strategy.onPositionChange.subscribe(change => { + if (change.scrollableViewProperties.isOverlayClipped) { + this.hide(0); + } + }); let config = new OverlayState(); config.positionStrategy = strategy; @@ -331,7 +358,7 @@ export class TooltipComponent { // trigger interaction and close the tooltip right after it was displayed. this._closeOnInteraction = false; - // Mark for check so if any parent component has set the + // Mark for check so if any parent component has set the // ChangeDetectionStrategy to OnPush it will be checked anyways this._changeDetectorRef.markForCheck(); setTimeout(() => { this._closeOnInteraction = true; }, 0); @@ -352,7 +379,7 @@ export class TooltipComponent { this._visibility = 'hidden'; this._closeOnInteraction = false; - // Mark for check so if any parent component has set the + // Mark for check so if any parent component has set the // ChangeDetectionStrategy to OnPush it will be checked anyways this._changeDetectorRef.markForCheck(); }, delay); diff --git a/tools/gulp/tasks/components.ts b/tools/gulp/tasks/components.ts index bf00567ebef9..7c219e42e677 100644 --- a/tools/gulp/tasks/components.ts +++ b/tools/gulp/tasks/components.ts @@ -74,6 +74,7 @@ task(':build:components:rollup', () => { 'rxjs/Subject': 'Rx', 'rxjs/add/observable/fromEvent': 'Rx.Observable', 'rxjs/add/observable/forkJoin': 'Rx.Observable', + 'rxjs/add/observable/auditTime': 'Rx.Observable', 'rxjs/add/observable/of': 'Rx.Observable', 'rxjs/add/observable/merge': 'Rx.Observable', 'rxjs/add/observable/throw': 'Rx.Observable', From 4cce3ba4bdb7f9303fdee7bc160554636eef8a71 Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Tue, 17 Jan 2017 14:11:38 -0800 Subject: [PATCH 02/10] finish tests --- .../overlay/scroll/scroll-dispatcher.spec.ts | 23 ++++++++---- .../core/overlay/scroll/scroll-dispatcher.ts | 2 +- src/lib/tooltip/tooltip.spec.ts | 35 ++++++++++++------- src/lib/tooltip/tooltip.ts | 11 +++--- 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/lib/core/overlay/scroll/scroll-dispatcher.spec.ts b/src/lib/core/overlay/scroll/scroll-dispatcher.spec.ts index f342d56cb10d..77342af30f20 100644 --- a/src/lib/core/overlay/scroll/scroll-dispatcher.spec.ts +++ b/src/lib/core/overlay/scroll/scroll-dispatcher.spec.ts @@ -1,5 +1,5 @@ -import {inject, TestBed, async, ComponentFixture} from '@angular/core/testing'; -import {NgModule, Component, ViewChild, ElementRef, QueryList, ViewChildren} from '@angular/core'; +import {inject, TestBed, async, fakeAsync, ComponentFixture, tick} from '@angular/core/testing'; +import {NgModule, Component, ViewChild, ElementRef} from '@angular/core'; import {ScrollDispatcher} from './scroll-dispatcher'; import {OverlayModule} from '../overlay-directives'; import {Scrollable} from './scrollable'; @@ -38,15 +38,17 @@ describe('Scroll Dispatcher', () => { expect(scroll.scrollableReferences.has(componentScrollable)).toBe(false); }); - it('should notify through the directive and service that a scroll event occurred', () => { + it('should notify through the directive and service that a scroll event occurred', + fakeAsync(() => { let hasDirectiveScrollNotified = false; // Listen for notifications from scroll directive let scrollable = fixture.componentInstance.scrollable; scrollable.elementScrolled().subscribe(() => { hasDirectiveScrollNotified = true; }); - // Listen for notifications from scroll service + // Listen for notifications from scroll service with a throttle of 100ms + const throttleTime = 100; let hasServiceScrollNotified = false; - scroll.scrolled().subscribe(() => { hasServiceScrollNotified = true; }); + scroll.scrolled(throttleTime).subscribe(() => { hasServiceScrollNotified = true; }); // Emit a scroll event from the scrolling element in our component. // This event should be picked up by the scrollable directive and notify. @@ -55,9 +57,17 @@ describe('Scroll Dispatcher', () => { scrollEvent.initUIEvent('scroll', true, true, window, 0); fixture.componentInstance.scrollingElement.nativeElement.dispatchEvent(scrollEvent); + // The scrollable directive should have notified the service immediately. expect(hasDirectiveScrollNotified).toBe(true); + + // Verify that the throttle is used, the service should wait for the throttle time until + // sending the notification. + expect(hasServiceScrollNotified).toBe(false); + + // After the throttle time, the notification should be sent. + tick(throttleTime); expect(hasServiceScrollNotified).toBe(true); - }); + })); }); describe('Nested scrollables', () => { @@ -107,7 +117,6 @@ class ScrollingComponent { }) class NestedScrollingComponent { @ViewChild('interestingElement') interestingElement: ElementRef; - @ViewChildren(Scrollable) scrollables: QueryList; } const TEST_COMPONENTS = [ScrollingComponent, NestedScrollingComponent]; diff --git a/src/lib/core/overlay/scroll/scroll-dispatcher.ts b/src/lib/core/overlay/scroll/scroll-dispatcher.ts index 1d0ba26a6474..16d720d0a7bd 100644 --- a/src/lib/core/overlay/scroll/scroll-dispatcher.ts +++ b/src/lib/core/overlay/scroll/scroll-dispatcher.ts @@ -57,7 +57,7 @@ export class ScrollDispatcher { /** * Returns an observable that emits an event whenever any of the registered Scrollable * references (or window, document, or body) fire a scrolled event. Can provide a time in ms - * to override the default throttling time. + * to override the default "throttle" time. */ scrolled(auditTimeInMs: number = DEFAULT_AUDIT_TIME): Observable { return this._scrolled.asObservable().auditTime(auditTimeInMs); diff --git a/src/lib/tooltip/tooltip.spec.ts b/src/lib/tooltip/tooltip.spec.ts index 345bade4a855..8648447bff94 100644 --- a/src/lib/tooltip/tooltip.spec.ts +++ b/src/lib/tooltip/tooltip.spec.ts @@ -8,7 +8,7 @@ import { } from '@angular/core/testing'; import {Component, DebugElement, AnimationTransitionEvent, ViewChild, ChangeDetectionStrategy} from '@angular/core'; import {By} from '@angular/platform-browser'; -import {TooltipPosition, MdTooltip, MdTooltipModule} from './tooltip'; +import {TooltipPosition, MdTooltip, MdTooltipModule, SCROLL_THROTTLE_MS} from './tooltip'; import {OverlayContainer} from '../core'; import {Dir, LayoutDirection} from '../core/rtl/dir'; import {OverlayModule} from '../core/overlay/overlay-directives'; @@ -27,6 +27,7 @@ describe('MdTooltip', () => { providers: [ {provide: OverlayContainer, useFactory: () => { overlayContainerElement = document.createElement('div'); + document.body.appendChild(overlayContainerElement); return {getContainerElement: () => overlayContainerElement}; }}, {provide: Dir, useFactory: () => { @@ -315,22 +316,32 @@ describe('MdTooltip', () => { let tooltipDirective: MdTooltip; beforeEach(() => { - fixture = TestBed.createComponent(ScrollableTooltipDemo); - fixture.detectChanges(); - buttonDebugElement = fixture.debugElement.query(By.css('button')); - buttonElement = buttonDebugElement.nativeElement; - tooltipDirective = buttonDebugElement.injector.get(MdTooltip); - }); + fixture = TestBed.createComponent(ScrollableTooltipDemo); + fixture.detectChanges(); + buttonDebugElement = fixture.debugElement.query(By.css('button')); + buttonElement = buttonDebugElement.nativeElement; + tooltipDirective = buttonDebugElement.injector.get(MdTooltip); + }); it('should hide tooltip if clipped after changing positions', fakeAsync(() => { expect(tooltipDirective._tooltipInstance).toBeUndefined(); + // Show the tooltip and tick for the show delay (default is 0) tooltipDirective.show(); - tick(0); // Tick for the show delay (default is 0) + fixture.detectChanges(); + tick(0); + + // Expect that the tooltip is displayed expect(tooltipDirective._isTooltipVisible()).toBe(true); + // Scroll the page but tick just before the default throttle should update. fixture.componentInstance.scrollDown(); - tick(); + tick(SCROLL_THROTTLE_MS - 1); + expect(tooltipDirective._isTooltipVisible()).toBe(true); + + // Finish ticking to the throttle's limit and check that the scroll event notified the + // tooltip and it was hidden. + tick(1); expect(tooltipDirective._isTooltipVisible()).toBe(false); })); }); @@ -380,7 +391,6 @@ describe('MdTooltip', () => { flushMicrotasks(); expect(tooltipDirective._tooltipInstance).toBeNull(); })); - }); }); @Component({ @@ -401,7 +411,8 @@ class BasicTooltipDemo { @Component({ selector: 'app', template: ` -
+
- Scroll down while tooltip is open to see it hide automatically +
Scroll down while tooltip is open to see it hide automatically
+

diff --git a/src/demo-app/tooltip/tooltip-demo.scss b/src/demo-app/tooltip/tooltip-demo.scss index d3ff6bac1a6c..c2483c856273 100644 --- a/src/demo-app/tooltip/tooltip-demo.scss +++ b/src/demo-app/tooltip/tooltip-demo.scss @@ -1,6 +1,12 @@ .demo-tooltip { .centered { text-align: center; + height: 200px; + overflow: auto; + + button { + margin: 16px; + } } .mat-radio-button { display: block; diff --git a/src/lib/tooltip/tooltip.ts b/src/lib/tooltip/tooltip.ts index ce6a809fecef..39cdab9eceae 100644 --- a/src/lib/tooltip/tooltip.ts +++ b/src/lib/tooltip/tooltip.ts @@ -41,7 +41,7 @@ export type TooltipPosition = 'left' | 'right' | 'above' | 'below' | 'before' | export const TOUCHEND_HIDE_DELAY = 1500; /** Time in ms to throttle repositioning after scroll events. */ -export const SCROLL_THROTTLE_MS = 20; +export const SCROLL_THROTTLE_MS = 10; /** * Directive that attaches a material design tooltip to the host element. Animates the showing and @@ -208,7 +208,8 @@ export class MdTooltip implements OnInit, OnDestroy { let strategy = this._overlay.position().connectedTo(this._elementRef, origin, position); strategy.withScrollableContainers(this._scrollDispatcher.getScrollContainers(this._elementRef)); strategy.onPositionChange.subscribe(change => { - if (change.scrollableViewProperties.isOverlayClipped && this._tooltipInstance.isVisible()) { + if (change.scrollableViewProperties.isOverlayClipped && + this._tooltipInstance && this._tooltipInstance.isVisible()) { this.hide(0); } }); From 560922e4c9532047df830264262819199b51bbbb Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Tue, 17 Jan 2017 14:39:12 -0800 Subject: [PATCH 04/10] remove events logger from scroll dispatcher --- src/lib/core/overlay/scroll/scroll-dispatcher.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/lib/core/overlay/scroll/scroll-dispatcher.ts b/src/lib/core/overlay/scroll/scroll-dispatcher.ts index 16d720d0a7bd..9f70621a017c 100644 --- a/src/lib/core/overlay/scroll/scroll-dispatcher.ts +++ b/src/lib/core/overlay/scroll/scroll-dispatcher.ts @@ -16,8 +16,6 @@ export const DEFAULT_AUDIT_TIME = 20; */ @Injectable() export class ScrollDispatcher { - events = 0; - /** Subject for notifying that a registered scrollable reference element has been scrolled. */ _scrolled: Subject = new Subject(); @@ -90,7 +88,6 @@ export class ScrollDispatcher { /** Sends a notification that a scroll event has been fired. */ _notify() { - this.events++; this._scrolled.next(); } } From 4122667c0a8e467fdaf546e3f4e948c7c323ce1a Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Tue, 17 Jan 2017 14:40:15 -0800 Subject: [PATCH 05/10] fix auditTime in components.ts --- tools/gulp/tasks/components.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/gulp/tasks/components.ts b/tools/gulp/tasks/components.ts index 7c219e42e677..f2cc6030ef45 100644 --- a/tools/gulp/tasks/components.ts +++ b/tools/gulp/tasks/components.ts @@ -74,10 +74,10 @@ task(':build:components:rollup', () => { 'rxjs/Subject': 'Rx', 'rxjs/add/observable/fromEvent': 'Rx.Observable', 'rxjs/add/observable/forkJoin': 'Rx.Observable', - 'rxjs/add/observable/auditTime': 'Rx.Observable', 'rxjs/add/observable/of': 'Rx.Observable', 'rxjs/add/observable/merge': 'Rx.Observable', 'rxjs/add/observable/throw': 'Rx.Observable', + 'rxjs/add/operator/auditTime': 'Rx.Observable.prototype', 'rxjs/add/operator/toPromise': 'Rx.Observable.prototype', 'rxjs/add/operator/map': 'Rx.Observable.prototype', 'rxjs/add/operator/filter': 'Rx.Observable.prototype', From 89b0ab746c99ba39ffb7e61bf4c0d7d30461f68a Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Tue, 17 Jan 2017 15:34:03 -0800 Subject: [PATCH 06/10] skip auditTime if set to 0ms --- src/lib/core/overlay/scroll/scroll-dispatcher.ts | 6 ++++++ src/lib/tooltip/tooltip.ts | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/core/overlay/scroll/scroll-dispatcher.ts b/src/lib/core/overlay/scroll/scroll-dispatcher.ts index 9f70621a017c..0d4539019e57 100644 --- a/src/lib/core/overlay/scroll/scroll-dispatcher.ts +++ b/src/lib/core/overlay/scroll/scroll-dispatcher.ts @@ -58,6 +58,12 @@ export class ScrollDispatcher { * to override the default "throttle" time. */ scrolled(auditTimeInMs: number = DEFAULT_AUDIT_TIME): Observable { + // In the case of a 0ms delay, return the observable without auditTime since it does add + // a perceptible delay in processing overhead. + if (auditTimeInMs == 0) { + return this._scrolled.asObservable(); + } + return this._scrolled.asObservable().auditTime(auditTimeInMs); } diff --git a/src/lib/tooltip/tooltip.ts b/src/lib/tooltip/tooltip.ts index 39cdab9eceae..60f4fa213c06 100644 --- a/src/lib/tooltip/tooltip.ts +++ b/src/lib/tooltip/tooltip.ts @@ -41,7 +41,7 @@ export type TooltipPosition = 'left' | 'right' | 'above' | 'below' | 'before' | export const TOUCHEND_HIDE_DELAY = 1500; /** Time in ms to throttle repositioning after scroll events. */ -export const SCROLL_THROTTLE_MS = 10; +export const SCROLL_THROTTLE_MS = 0; /** * Directive that attaches a material design tooltip to the host element. Animates the showing and From a9d6c52768d7b4b938ec2ead9900491ee64e7c4a Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Tue, 31 Jan 2017 13:42:28 -0800 Subject: [PATCH 07/10] fix tests --- src/lib/core/overlay/scroll/scroll-dispatcher.ts | 4 ++-- src/lib/tooltip/tooltip.ts | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib/core/overlay/scroll/scroll-dispatcher.ts b/src/lib/core/overlay/scroll/scroll-dispatcher.ts index 0d4539019e57..15cefb2d2443 100644 --- a/src/lib/core/overlay/scroll/scroll-dispatcher.ts +++ b/src/lib/core/overlay/scroll/scroll-dispatcher.ts @@ -8,7 +8,7 @@ import 'rxjs/add/operator/auditTime'; /** Time in ms to throttle the scrolling events by default. */ -export const DEFAULT_AUDIT_TIME = 20; +export const DEFAULT_SCROLL_TIME = 20; /** * Service contained all registered Scrollable references and emits an event when any one of the @@ -57,7 +57,7 @@ export class ScrollDispatcher { * references (or window, document, or body) fire a scrolled event. Can provide a time in ms * to override the default "throttle" time. */ - scrolled(auditTimeInMs: number = DEFAULT_AUDIT_TIME): Observable { + scrolled(auditTimeInMs: number = DEFAULT_SCROLL_TIME): Observable { // In the case of a 0ms delay, return the observable without auditTime since it does add // a perceptible delay in processing overhead. if (auditTimeInMs == 0) { diff --git a/src/lib/tooltip/tooltip.ts b/src/lib/tooltip/tooltip.ts index 60f4fa213c06..07c0e4cf9352 100644 --- a/src/lib/tooltip/tooltip.ts +++ b/src/lib/tooltip/tooltip.ts @@ -34,6 +34,7 @@ import {Subject} from 'rxjs/Subject'; import {Dir} from '../core/rtl/dir'; import 'rxjs/add/operator/first'; import {ScrollDispatcher} from '../core/overlay/scroll/scroll-dispatcher'; +import {Subscription} from 'rxjs'; export type TooltipPosition = 'left' | 'right' | 'above' | 'below' | 'before' | 'after'; @@ -41,7 +42,7 @@ export type TooltipPosition = 'left' | 'right' | 'above' | 'below' | 'before' | export const TOUCHEND_HIDE_DELAY = 1500; /** Time in ms to throttle repositioning after scroll events. */ -export const SCROLL_THROTTLE_MS = 0; +export const SCROLL_THROTTLE_MS = 20; /** * Directive that attaches a material design tooltip to the host element. Animates the showing and @@ -62,6 +63,7 @@ export const SCROLL_THROTTLE_MS = 0; export class MdTooltip implements OnInit, OnDestroy { _overlayRef: OverlayRef; _tooltipInstance: TooltipComponent; + scrollSubscription: Subscription; private _position: TooltipPosition = 'below'; @@ -137,7 +139,7 @@ export class MdTooltip implements OnInit, OnDestroy { ngOnInit() { // When a scroll on the page occurs, update the position in case this tooltip needs // to be repositioned. - this._scrollDispatcher.scrolled(SCROLL_THROTTLE_MS).subscribe(() => { + this.scrollSubscription = this._scrollDispatcher.scrolled(SCROLL_THROTTLE_MS).subscribe(() => { if (this._overlayRef) { this._overlayRef.updatePosition(); } @@ -151,6 +153,8 @@ export class MdTooltip implements OnInit, OnDestroy { if (this._tooltipInstance) { this._disposeTooltip(); } + + this.scrollSubscription.unsubscribe(); } /** Shows the tooltip after the delay in ms, defaults to tooltip-delay-show or 0ms if no input */ From fc13392410c06092c363a22da89250a3e4fe67ca Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Tue, 31 Jan 2017 16:14:16 -0800 Subject: [PATCH 08/10] fixing tests --- src/lib/tooltip/tooltip.spec.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/lib/tooltip/tooltip.spec.ts b/src/lib/tooltip/tooltip.spec.ts index 8648447bff94..d04549873a82 100644 --- a/src/lib/tooltip/tooltip.spec.ts +++ b/src/lib/tooltip/tooltip.spec.ts @@ -6,7 +6,13 @@ import { fakeAsync, flushMicrotasks } from '@angular/core/testing'; -import {Component, DebugElement, AnimationTransitionEvent, ViewChild, ChangeDetectionStrategy} from '@angular/core'; +import { + Component, + DebugElement, + AnimationTransitionEvent, + ViewChild, + ChangeDetectionStrategy +} from '@angular/core'; import {By} from '@angular/platform-browser'; import {TooltipPosition, MdTooltip, MdTooltipModule, SCROLL_THROTTLE_MS} from './tooltip'; import {OverlayContainer} from '../core'; @@ -391,6 +397,7 @@ describe('MdTooltip', () => { flushMicrotasks(); expect(tooltipDirective._tooltipInstance).toBeNull(); })); + }); }); @Component({ @@ -452,4 +459,4 @@ class ScrollableTooltipDemo { class OnPushTooltipDemo { position: string = 'below'; message: string = initialTooltipMessage; -} \ No newline at end of file +} From 86a78cde12202aa281f6627404dd54674cad4f89 Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Wed, 1 Feb 2017 14:10:58 -0800 Subject: [PATCH 09/10] fix IE and FF test failures --- src/lib/tooltip/tooltip.spec.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/tooltip/tooltip.spec.ts b/src/lib/tooltip/tooltip.spec.ts index d04549873a82..5873f340f0e6 100644 --- a/src/lib/tooltip/tooltip.spec.ts +++ b/src/lib/tooltip/tooltip.spec.ts @@ -417,10 +417,10 @@ class BasicTooltipDemo { @Component({ selector: 'app', - template: ` -

-