Skip to content

Commit

Permalink
incorpoate comments
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewseguin committed Oct 31, 2016
1 parent a5bb856 commit 827b434
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
14 changes: 7 additions & 7 deletions src/lib/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('MdTooltip', () => {
});

it('should show and hide the tooltip', fakeAsync(() => {
expect(tooltipDirective._tooltipRef).toBeUndefined();
expect(tooltipDirective._tooltipInstance).toBeUndefined();

tooltipDirective.show();
expect(tooltipDirective._isTooltipVisible()).toBe(true);
Expand All @@ -61,27 +61,27 @@ describe('MdTooltip', () => {
const initialPosition: TooltipPosition = 'below';
const changedPosition: TooltipPosition = 'above';

expect(tooltipDirective._tooltipRef).toBeUndefined();
expect(tooltipDirective._tooltipInstance).toBeUndefined();

tooltipDirective.position = initialPosition;
tooltipDirective.show();
expect(tooltipDirective._tooltipRef).toBeDefined();
expect(tooltipDirective._tooltipInstance).toBeDefined();

// Same position value should not remove the tooltip
tooltipDirective.position = initialPosition;
expect(tooltipDirective._tooltipRef).toBeDefined();
expect(tooltipDirective._tooltipInstance).toBeDefined();

// Different position value should destroy the tooltip
tooltipDirective.position = changedPosition;
expect(tooltipDirective._tooltipRef).toBeNull();
expect(tooltipDirective._tooltipInstance).toBeNull();
expect(tooltipDirective._overlayRef).toBeNull();
});

it('should be able to modify the tooltip message', () => {
expect(tooltipDirective._tooltipRef).toBeUndefined();
expect(tooltipDirective._tooltipInstance).toBeUndefined();

tooltipDirective.show();
expect(tooltipDirective._tooltipRef._visibility).toBe('visible');
expect(tooltipDirective._tooltipInstance._visibility).toBe('visible');

fixture.detectChanges();
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);
Expand Down
36 changes: 16 additions & 20 deletions src/lib/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const TOOLTIP_HIDE_DELAY = 1500;
})
export class MdTooltip {
_overlayRef: OverlayRef;
_tooltipRef: TooltipComponent;
_tooltipInstance: TooltipComponent;

/** Allows the user to define the position of the tooltip relative to the parent element */
private _position: TooltipPosition = 'below';
Expand All @@ -42,7 +42,7 @@ export class MdTooltip {

// TODO(andrewjs): When the overlay's position can be dynamically changed, do not destroy
// the tooltip.
if (this._tooltipRef) {
if (this._tooltipInstance) {
this._disposeTooltip();
}
}
Expand All @@ -55,7 +55,7 @@ export class MdTooltip {
}
set message(value: string) {
this._message = value;
if (this._tooltipRef) {
if (this._tooltipInstance) {
this._setTooltipMessage(this._message);
}
}
Expand All @@ -65,46 +65,42 @@ export class MdTooltip {

/** Shows the tooltip */
show(): void {
if (!this._tooltipRef) {
if (!this._tooltipInstance) {
this._createTooltip();
}

this._setTooltipMessage(this._message);
this._tooltipRef.show(this._position);
this._tooltipInstance.show(this._position);
}

/**
* Hides the tooltip after the provided delay in ms. Defaults the delay to the material design
* prescribed delay time
*/
hide(delay: number = TOOLTIP_HIDE_DELAY): void {
if (this._tooltipRef) {
this._tooltipRef.hide(delay);
if (this._tooltipInstance) {
this._tooltipInstance.hide(delay);
}
}

/** Shows/hides the tooltip */
toggle(): void {
if (this._isTooltipVisible()) {
this.hide();
} else {
this.show();
}
this._isTooltipVisible() ? this.hide() : this.show();
}

/** Returns true if the tooltip is currently visible to the user */
_isTooltipVisible(): boolean {
return this._tooltipRef && this._tooltipRef.isVisible();
return this._tooltipInstance && this._tooltipInstance.isVisible();
}

/** Create the tooltip to display */
private _createTooltip(): void {
this._createOverlay();
let portal = new ComponentPortal(TooltipComponent, this._viewContainerRef);
this._tooltipRef = this._overlayRef.attach(portal).instance;
this._tooltipInstance = this._overlayRef.attach(portal).instance;

// Dispose the overlay when finished the shown tooltip.
this._tooltipRef.afterHidden().subscribe(() => {
this._tooltipInstance.afterHidden().subscribe(() => {
this._disposeTooltip();
});
}
Expand All @@ -124,7 +120,7 @@ export class MdTooltip {
private _disposeTooltip(): void {
this._overlayRef.dispose();
this._overlayRef = null;
this._tooltipRef = null;
this._tooltipInstance = null;
}

/** Returns the origin position based on the user's position preference */
Expand All @@ -151,9 +147,9 @@ export class MdTooltip {
private _setTooltipMessage(message: string) {
// Must wait for the message to be painted to the tooltip so that the overlay can properly
// calculate the correct positioning based on the size of the text.
this._tooltipRef.message = message;
this._tooltipInstance.message = message;
this._ngZone.onMicrotaskEmpty.first().subscribe(() => {
if (this._tooltipRef) {
if (this._tooltipInstance) {
this._overlayRef.updatePosition();
}
});
Expand Down Expand Up @@ -190,7 +186,7 @@ export class TooltipComponent {
/** Property watched by the animation framework to show or hide the tooltip */
_visibility: TooltipVisibility;

/** Set to true when interactions on the page should close the tooltip */
/** Whether interactions on the page should close the tooltip */
_closeOnInteraction: boolean = false;

/** The transform origin used in the animation for showing and hiding the tooltip */
Expand Down Expand Up @@ -228,7 +224,7 @@ export class TooltipComponent {
return this._onHide.asObservable();
}

/** Returns true if the tooltip is being displayed */
/** Whether the tooltip is being displayed */
isVisible(): boolean {
return this._visibility === 'visible';
}
Expand Down

0 comments on commit 827b434

Please sign in to comment.