Skip to content

Commit

Permalink
feat(positioning): auto option for positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
vfcosta committed May 19, 2017
1 parent ead8d52 commit 55100da
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@
placement="right">
Popover on right
</button>

<button type="button" class="btn btn-default btn-secondary"
popover="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."
popoverTitle="Popover auto"
placement="auto">
Popover auto
</button>
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ let titleDoc = require('html-loader!markdown-loader!./docs/title.md');
<!-- four directions -->
<h2 routerLink="." fragment="four-directions" id="four-directions">Four directions</h2>
Four positioning options are available: top, right, bottom, and left aligned.
Besides that, auto option may be used to detect a position that fits the component on screen.
<ng-sample-box [ts]="demos.forDirections.component" [html]="demos.forDirections.html">
<demo-popover-four-directions></demo-popover-four-directions>
</ng-sample-box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@
placement="right">
Tooltip on right
</button>

<button type="button" class="btn btn-default btn-secondary"
tooltip="Vivamus sagittis lacus vel augue laoreet rutrum faucibus."
placement="auto">
Tooltip auto
</button>
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ let titleDoc = require('html-loader!markdown-loader!./docs/title.md');
<!-- four directions -->
<h2 routerLink="." fragment="four-directions" id="four-directions">Four directions</h2>
Four positioning options are available: top, right, bottom, and left aligned.
Besides that, auto option may be used to detect a position that fits the component on screen.
<ng-sample-box [ts]="demos.forDirections.component" [html]="demos.forDirections.html">
<demo-tooltip-four-directions></demo-tooltip-four-directions>
</ng-sample-box>
Expand Down
2 changes: 1 addition & 1 deletion src/popover/popover.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Injectable } from '@angular/core';
@Injectable()
export class PopoverConfig {
/**
* Placement of a popover. Accepts: "top", "bottom", "left", "right"
* Placement of a popover. Accepts: "top", "bottom", "left", "right", "auto"
*/
public placement: string = 'top';
/**
Expand Down
2 changes: 1 addition & 1 deletion src/popover/popover.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class PopoverDirective implements OnInit, OnDestroy {
/**
* Placement of a popover. Accepts: "top", "bottom", "left", "right"
*/
@Input() public placement: 'top' | 'bottom' | 'left' | 'right';
@Input() public placement: 'top' | 'bottom' | 'left' | 'right' | 'auto';
/**
* Specifies events that should trigger. Supports a space separated list of
* event names.
Expand Down
18 changes: 17 additions & 1 deletion src/positioning/ng-positioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class Positioning {
bottom: hostElPosition.top + hostElPosition.height
};
const targetElBCR = targetElement.getBoundingClientRect();
const placementPrimary = placement.split(' ')[0] || 'top';
let placementPrimary = placement.split(' ')[0] || 'top';
const placementSecondary = placement.split(' ')[1] || 'center';

let targetElPosition: ClientRect = {
Expand All @@ -95,6 +95,22 @@ export class Positioning {
right: targetElBCR.width || targetElement.offsetWidth
};

if (placementPrimary==="auto") {
if (targetElPosition.top + hostElPosition.top - targetElement.offsetHeight < 0) {
placementPrimary = "bottom";
}
if (targetElPosition.bottom + hostElPosition.bottom + targetElement.offsetHeight > window.innerHeight) {
placementPrimary = "top";
}
if (targetElPosition.left + hostElPosition.left - targetElement.offsetWidth < 0) {
placementPrimary = "right";
}
if (targetElPosition.right + hostElPosition.right + targetElement.offsetWidth > window.innerWidth ) {
placementPrimary = "left";
}
targetElement.classList.add(placementPrimary);
}

switch (placementPrimary) {
case 'top':
targetElPosition.top = hostElPosition.top - targetElement.offsetHeight;
Expand Down
18 changes: 18 additions & 0 deletions src/spec/ng-bootstrap/popover.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,24 @@ describe('popover', () => {
expect(windowEl.textContent.trim())
.toBe('Great tip!');
});

it('should use auto position', () => {
const fixture = createTestComponent(`<div popover="Great tip!" placement="auto"></div>`);
const directive = fixture.debugElement.query(By.directive(PopoverDirective));

directive.triggerEventHandler('click', {});
fixture.detectChanges();
const windowEl = getWindow(fixture.nativeElement);

expect(windowEl)
.toHaveCssClass('popover');
expect(windowEl)
.toHaveCssClass('popover-auto');
expect(windowEl)
.toHaveCssClass('left');
expect(windowEl.textContent.trim())
.toBe('Great tip!');
});
});

describe('container', () => {
Expand Down
14 changes: 14 additions & 0 deletions src/spec/ng-bootstrap/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ describe('tooltip', () => {
expect(windowEl).toHaveCssClass('tooltip-left');
expect(windowEl.textContent.trim()).toBe('Great tip!');
});

it('should use auto position', () => {
const fixture = createTestComponent(`<div tooltip="Great tip!" placement="auto"></div>`);
const directive = fixture.debugElement.query(By.directive(TooltipDirective));

directive.triggerEventHandler('mouseenter', {});
fixture.detectChanges();
const windowEl = getWindow(fixture.nativeElement);

expect(windowEl).toHaveCssClass('tooltip');
expect(windowEl).toHaveCssClass('tooltip-auto');
expect(windowEl).toHaveCssClass('left');
expect(windowEl.textContent.trim()).toBe('Great tip!');
});
});

describe('triggers', () => {
Expand Down

0 comments on commit 55100da

Please sign in to comment.