Skip to content

Commit d6d61c4

Browse files
committed
feat(tooltip): allow tooltip to be auto positioned
BREAKING CHANGE: all tooltips now default to auto positioning, you can use the tooltipPlacement input on all components to override this behaviour though Closes #617
1 parent 6ada99e commit d6d61c4

11 files changed

+27
-24
lines changed

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
"angular-draggable-droppable": "^4.0.0-beta.7",
166166
"angular-resizable-element": "^3.1.0",
167167
"calendar-utils": "^0.2.0-alpha.12",
168-
"positioning": "^1.3.1"
168+
"positioning": "^1.4.0"
169169
},
170170
"sideEffects": [
171171
"*.css",

src/modules/common/calendar-tooltip.directive.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
TemplateRef
1616
} from '@angular/core';
1717
import { DOCUMENT } from '@angular/common';
18-
import { Positioning } from 'positioning';
18+
import { PlacementArray, positionElements } from 'positioning';
1919
import { CalendarEvent } from 'calendar-utils';
2020

2121
@Component({
@@ -57,7 +57,7 @@ export class CalendarTooltipWindowComponent {
5757
export class CalendarTooltipDirective implements OnDestroy {
5858
@Input('mwlCalendarTooltip') contents: string; // tslint:disable-line no-input-rename
5959

60-
@Input('tooltipPlacement') placement: string = 'top'; // tslint:disable-line no-input-rename
60+
@Input('tooltipPlacement') placement: PlacementArray = 'auto'; // tslint:disable-line no-input-rename
6161

6262
@Input('tooltipTemplate') customTemplate: TemplateRef<any>; // tslint:disable-line no-input-rename
6363

@@ -67,7 +67,6 @@ export class CalendarTooltipDirective implements OnDestroy {
6767

6868
private tooltipFactory: ComponentFactory<CalendarTooltipWindowComponent>;
6969
private tooltipRef: ComponentRef<CalendarTooltipWindowComponent>;
70-
private positioning: Positioning = new Positioning();
7170

7271
constructor(
7372
private elementRef: ElementRef,
@@ -105,7 +104,6 @@ export class CalendarTooltipDirective implements OnDestroy {
105104
[]
106105
);
107106
this.tooltipRef.instance.contents = this.contents;
108-
this.tooltipRef.instance.placement = this.placement;
109107
this.tooltipRef.instance.customTemplate = this.customTemplate;
110108
this.tooltipRef.instance.event = this.event;
111109
if (this.appendToBody) {
@@ -126,20 +124,19 @@ export class CalendarTooltipDirective implements OnDestroy {
126124
}
127125
}
128126

129-
private positionTooltip(): void {
127+
private positionTooltip(previousPosition?: string): void {
130128
if (this.tooltipRef) {
131-
const targetPosition: ClientRect = this.positioning.positionElements(
129+
this.tooltipRef.changeDetectorRef.detectChanges();
130+
this.tooltipRef.instance.placement = positionElements(
132131
this.elementRef.nativeElement,
133132
this.tooltipRef.location.nativeElement.children[0],
134133
this.placement,
135134
this.appendToBody
136135
);
137-
138-
const elm: HTMLElement = this.tooltipRef.location.nativeElement
139-
.children[0];
140-
141-
this.renderer.setStyle(elm, 'top', `${targetPosition.top}px`);
142-
this.renderer.setStyle(elm, 'left', `${targetPosition.left}px`);
136+
// keep re-positioning the tooltip until the arrow position doesn't make a difference
137+
if (previousPosition !== this.tooltipRef.instance.placement) {
138+
this.positionTooltip(this.tooltipRef.instance.placement);
139+
}
143140
}
144141
}
145142
}

src/modules/day/calendar-day-view-event.component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
TemplateRef
77
} from '@angular/core';
88
import { DayViewEvent } from 'calendar-utils';
9+
import { PlacementArray } from 'positioning';
910

1011
@Component({
1112
selector: 'mwl-calendar-day-view-event',
@@ -51,7 +52,7 @@ import { DayViewEvent } from 'calendar-utils';
5152
export class CalendarDayViewEventComponent {
5253
@Input() dayEvent: DayViewEvent;
5354

54-
@Input() tooltipPlacement: string;
55+
@Input() tooltipPlacement: PlacementArray;
5556

5657
@Input() tooltipAppendToBody: boolean;
5758

src/modules/day/calendar-day-view.component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { CalendarUtils } from '../common/calendar-utils.provider';
2828
import { validateEvents, trackByEventId, roundToNearest } from '../common/util';
2929
import { DateAdapter } from '../../date-adapters/date-adapter';
3030
import { DragEnd } from 'angular-draggable-droppable/draggable.directive';
31+
import { PlacementArray } from 'positioning';
3132

3233
export interface CalendarDayViewBeforeRenderEvent {
3334
body: {
@@ -200,7 +201,7 @@ export class CalendarDayViewComponent implements OnChanges, OnInit, OnDestroy {
200201
/**
201202
* The placement of the event tooltip
202203
*/
203-
@Input() tooltipPlacement: string = 'top';
204+
@Input() tooltipPlacement: PlacementArray = 'auto';
204205

205206
/**
206207
* A custom template to use for the event tooltips

src/modules/month/calendar-month-cell.component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from '@angular/core';
88
import { MonthViewDay, CalendarEvent } from 'calendar-utils';
99
import { trackByEventId } from '../common/util';
10+
import { PlacementArray } from 'positioning';
1011

1112
@Component({
1213
selector: 'mwl-calendar-month-cell',
@@ -83,7 +84,7 @@ export class CalendarMonthCellComponent {
8384

8485
@Input() locale: string;
8586

86-
@Input() tooltipPlacement: string;
87+
@Input() tooltipPlacement: PlacementArray;
8788

8889
@Input() tooltipAppendToBody: boolean;
8990

src/modules/month/calendar-month-view.component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { CalendarEventTimesChangedEvent } from '../common/calendar-event-times-c
2323
import { CalendarUtils } from '../common/calendar-utils.provider';
2424
import { validateEvents, trackByIndex } from '../common/util';
2525
import { DateAdapter } from '../../date-adapters/date-adapter';
26+
import { PlacementArray } from 'positioning';
2627

2728
export interface CalendarMonthViewBeforeRenderEvent {
2829
header: WeekDay[];
@@ -129,7 +130,7 @@ export class CalendarMonthViewComponent
129130
/**
130131
* The placement of the event tooltip
131132
*/
132-
@Input() tooltipPlacement: string = 'top';
133+
@Input() tooltipPlacement: PlacementArray = 'auto';
133134

134135
/**
135136
* A custom template to use for the event tooltips

src/modules/week/calendar-week-view-event.component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
TemplateRef
77
} from '@angular/core';
88
import { WeekViewEvent } from 'calendar-utils';
9+
import { PlacementArray } from 'positioning';
910

1011
@Component({
1112
selector: 'mwl-calendar-week-view-event',
@@ -51,7 +52,7 @@ import { WeekViewEvent } from 'calendar-utils';
5152
export class CalendarWeekViewEventComponent {
5253
@Input() weekEvent: WeekViewEvent;
5354

54-
@Input() tooltipPlacement: string;
55+
@Input() tooltipPlacement: PlacementArray;
5556

5657
@Input() tooltipAppendToBody: boolean;
5758

src/modules/week/calendar-week-view.component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { CalendarUtils } from '../common/calendar-utils.provider';
2727
import { validateEvents, trackByIndex, roundToNearest } from '../common/util';
2828
import { DateAdapter } from '../../date-adapters/date-adapter';
2929
import { DragEnd } from 'angular-draggable-droppable/draggable.directive';
30+
import { PlacementArray } from 'positioning';
3031

3132
export interface WeekViewEventResize {
3233
originalOffset: number;
@@ -135,7 +136,7 @@ export class CalendarWeekViewComponent implements OnChanges, OnInit, OnDestroy {
135136
/**
136137
* The placement of the event tooltip
137138
*/
138-
@Input() tooltipPlacement: string = 'bottom';
139+
@Input() tooltipPlacement: PlacementArray = 'auto';
139140

140141
/**
141142
* A custom template to use for the event tooltips

test/calendar-day-view.component.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ describe('CalendarDayViewComponent component', () => {
705705
expect(tooltip.querySelector('.cal-tooltip-inner').innerHTML).to.equal(
706706
'title: foo <b>bar</b>'
707707
);
708-
expect(tooltip.classList.contains('cal-tooltip-top')).to.equal(true);
708+
expect(tooltip.classList.contains('cal-tooltip-left-top')).to.equal(true);
709709
expect(!!tooltip.style.top).to.equal(true);
710710
expect(!!tooltip.style.left).to.equal(true);
711711
triggerDomEvent('mouseleave', event);

test/calendar-week-view.component.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ describe('calendarWeekView component', () => {
299299
expect(tooltip.querySelector('.cal-tooltip-inner').innerHTML).to.equal(
300300
'title: foo <b>bar</b>'
301301
);
302-
expect(tooltip.classList.contains('cal-tooltip-bottom')).to.equal(true);
302+
expect(tooltip.classList.contains('cal-tooltip-top')).to.equal(true);
303303
expect(!!tooltip.style.top).to.equal(true);
304304
expect(!!tooltip.style.left).to.equal(true);
305305
triggerDomEvent('mouseleave', event);

0 commit comments

Comments
 (0)