Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing tooltipStateChanged and exporting the directive #939

Merged
merged 1 commit into from
Oct 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion components/tooltip/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { TooltipModule } from 'ng2-bootstrap/components/tooltip';
### Annotations
```typescript
// class Tooltip implements OnInit
@Directive({ selector: '[tooltip]' })
@Directive({
selector: '[tooltip]',
exportAs: 'bs-tooltip'
})
export class TooltipDirective {
@Input('tooltip') private content:string;
@Input('tooltipHtml') public htmlContent:string | TemplateRef<any>;
Expand All @@ -18,6 +21,7 @@ export class TooltipDirective {
@Input('tooltipAppendToBody') private appendToBody:boolean;
@Input('tooltipClass') public popupClass:string;
@Input('tooltipContext') public tooltipContext:any;
@Output() public tooltipStateChanged:EventEmitter<boolean>;
}
```

Expand All @@ -33,3 +37,6 @@ export class TooltipDirective {
- `tooltipClass` (`?string`) - custom tooltip class applied to the tooltip container
- `tooltipIsOpen` (`?boolean=false`) - if `true` tooltip is currently visible
- `tooltipContext` (`any`) - if a template is used for the content, then this property can be used to specify a context for that template. The template variable exposed is called 'model'.

### Tooltip events
- `tooltipStateChanged` - This event fires each time the state of the tooltip is changed. Argument is boolean stating if the tooltip is visible or not.
16 changes: 14 additions & 2 deletions components/tooltip/tooltip.directive.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {
ComponentRef, Directive, HostListener, Input, ReflectiveInjector, TemplateRef, ViewContainerRef
ComponentRef, Directive, HostListener, Input, ReflectiveInjector, TemplateRef, ViewContainerRef, Output, EventEmitter
} from '@angular/core';

import { TooltipContainerComponent } from './tooltip-container.component';
import { TooltipOptions } from './tooltip-options.class';
import { ComponentsHelper } from '../utils/components-helper.service';

/* tslint:disable */
@Directive({selector: '[tooltip], [tooltipHtml]'})
@Directive({
selector: '[tooltip], [tooltipHtml]',
exportAs: 'bs-tooltip'
})
/* tslint:enable */
export class TooltipDirective {
/* tslint:disable */
Expand All @@ -22,6 +25,8 @@ export class TooltipDirective {
@Input('tooltipContext') public tooltipContext:any;
/* tslint:enable */

@Output() public tooltipStateChanged:EventEmitter<boolean> = new EventEmitter<boolean>();

public viewContainerRef:ViewContainerRef;
public componentsHelper:ComponentsHelper;

Expand Down Expand Up @@ -58,6 +63,8 @@ export class TooltipDirective {

this.tooltip = this.componentsHelper
.appendNextToLocation(TooltipContainerComponent, this.viewContainerRef, binding);

this.triggerStateChanged();
}

// params event, target
Expand All @@ -69,5 +76,10 @@ export class TooltipDirective {
}
this.visible = false;
this.tooltip.destroy();
this.triggerStateChanged();
}

private triggerStateChanged():void {
this.tooltipStateChanged.emit(this.visible);
}
}
14 changes: 12 additions & 2 deletions demo/components/tooltip/tooltip-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</p>

<p>
I can even contain HTML. <a href="#" [tooltipHtml]="htmlTooltip">Check me out!</a>
I can even contain HTML. <a href="#" [tooltipHtml]="htmlTooltip" (tooltipStateChanged)="tooltipStateChanged($event)">Check me out!</a>
</p>

<template #toolTipTemplate let-model="model">
Expand All @@ -32,8 +32,18 @@ <h5>With context binding: {{model.text}}</h5>
</template>

<p>
Or use a TemplateRef. <a href="#" [tooltipHtml]="toolTipTemplate" [tooltipContext]="tooltipModel">Check me out!</a>
Or use a TemplateRef. <a href="#" [tooltipHtml]="toolTipTemplate"
[tooltipContext]="tooltipModel"
(tooltipStateChanged)="tooltipStateChanged($event)">Check me out!</a>
</p>
<p>
Programatically show/hide tooltip
<a href="#" [tooltip]="'Foo'"
(tooltipStateChanged)="tooltipStateChanged($event)"
#tooltip="bs-tooltip">Check me out!</a>
<button class="btn btn-primary" (click)="tooltip.show()">Show tooltip</button>
<button class="btn btn-danger" (click)="tooltip.hide()">Hide tooltip</button>
</p>

<p>
I can have a custom class. <a href="#" tooltip="I can have a custom class applied to me!" tooltipClass="customClass">Check me out!</a>
Expand Down
4 changes: 4 additions & 0 deletions demo/components/tooltip/tooltip-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ export class TooltipDemoComponent {
public dynamicTooltipText:string = 'dynamic';
public htmlTooltip:string = 'I\'ve been made <b>bold</b>!';
public tooltipModel:any = {text: 'foo', index: 1};

public tooltipStateChanged(state: boolean):void {
console.log(`Tooltip is open: ${state}`);
}
}