Selaa lähdekoodia

costimize tooltip

kroenen 5 vuotta sitten
vanhempi
commit
f3b2801712

+ 7
- 4
src/app/app.component.html Näytä tiedosto

@@ -1,9 +1,13 @@
1 1
 <app-componentWithContentOrNot>
2
-  <button [c3Tooltip]="content">Sourie !</button>
2
+  <button [c3Tooltip]="content" [c3TooltipHideDelay]="10000" [c3TooltipShowDelay]="1000">Sourie !</button>
3 3
   <c3-tooltip #content="c3Tooltip">
4 4
     Bonjour, je suis le content avec du <strong>html !!!</strong>
5 5
   </c3-tooltip>
6 6
 </app-componentWithContentOrNot>
7
+<p [c3Tooltip]="another" [c3TooltipDisabled]="true">message</p>
8
+<c3-tooltip #another="c3Tooltip">
9
+  Bonjour, je suis une tooltip mis sur un p avec du <strong>html !!!</strong>
10
+</c3-tooltip>
7 11
 <p>message</p>
8 12
 <p>message</p>
9 13
 <p>message</p>
@@ -12,8 +16,7 @@
12 16
 <p>message</p>
13 17
 <p>message</p>
14 18
 <p>message</p>
15
-<p>message</p>
16
-<p [c3Tooltip]="another">message</p>
17
-<c3-tooltip #another="c3Tooltip">
19
+<p [c3Tooltip]="another1">message</p>
20
+<c3-tooltip #another1="c3Tooltip">
18 21
   Bonjour, je suis une tooltip mis sur un p avec du <strong>html !!!</strong>
19 22
 </c3-tooltip>

+ 4
- 4
src/app/c3-tooltip/c3-tooltip.component.html Näytä tiedosto

@@ -1,5 +1,5 @@
1
-<ng-template>	
2
-	<div class="c3-tooltip-panel">
3
-		<ng-content ></ng-content>
4
-	</div>
1
+<ng-template>
2
+  <div class="c3-tooltip-panel" [ngClass]="tooltipClass">
3
+    <ng-content></ng-content>
4
+  </div>
5 5
 </ng-template>

+ 14
- 10
src/app/c3-tooltip/c3-tooltip.component.ts Näytä tiedosto

@@ -1,17 +1,21 @@
1
-import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core';
1
+import { Component, ViewChild, TemplateRef, ChangeDetectorRef } from '@angular/core';
2
+
2 3
 
3 4
 @Component({
4
-	selector: 'c3-tooltip',
5
-	templateUrl: './c3-tooltip.component.html',
6
-	styleUrls: ['./c3-tooltip.component.css'],
7
-	exportAs: 'c3Tooltip'
5
+  selector: 'c3-tooltip',
6
+  templateUrl: './c3-tooltip.component.html',
7
+  styleUrls: ['./c3-tooltip.component.css'],
8
+  exportAs: 'c3Tooltip'
8 9
 })
9
-export class C3Tooltip implements OnInit {
10
-	@ViewChild(TemplateRef, { read: false }) template: TemplateRef<any>;
10
+export class C3Tooltip {
11
+  @ViewChild(TemplateRef, { read: false }) template: TemplateRef<any>;
11 12
 
12
-	constructor() { }
13
+  /** Classes to be added to the tooltip. Supports the same syntax as `ngClass`. */
14
+  tooltipClass: string | string[] | Set<string> | { [key: string]: any };
13 15
 
14
-	ngOnInit() {
15
-	}
16
+  constructor(private _changeDetectorRef: ChangeDetectorRef) { }
16 17
 
18
+  _markForCheck(): void {
19
+    this._changeDetectorRef.markForCheck();
20
+  }
17 21
 }

+ 13
- 13
src/app/c3-tooltip/c3-tooltip.module.ts Näytä tiedosto

@@ -2,20 +2,20 @@ import { NgModule } from '@angular/core';
2 2
 import { CommonModule } from '@angular/common';
3 3
 import { OverlayModule } from '@angular/cdk/overlay';
4 4
 import { C3Tooltip } from './c3-tooltip.component';
5
-import { C3TooltipTrigger } from './c3-tooltipTrigger.directive';
5
+import { C3TooltipTrigger } from './c3-tooltipTrigger';
6 6
 
7 7
 @NgModule({
8
-   imports: [
9
-		CommonModule,
10
-		OverlayModule
11
-   ],
12
-   declarations: [
13
-		C3Tooltip,
14
-		C3TooltipTrigger
15
-	 ],
16
-	 exports: [
17
-		C3Tooltip,
18
-		C3TooltipTrigger
19
-	 ]
8
+  imports: [
9
+    CommonModule,
10
+    OverlayModule
11
+  ],
12
+  declarations: [
13
+    C3Tooltip,
14
+    C3TooltipTrigger
15
+  ],
16
+  exports: [
17
+    C3Tooltip,
18
+    C3TooltipTrigger
19
+  ]
20 20
 })
21 21
 export class C3TooltipModule { }

src/app/c3-tooltip/c3-tooltipTrigger.directive.ts → src/app/c3-tooltip/c3-tooltipTrigger.ts Näytä tiedosto

@@ -1,9 +1,10 @@
1 1
 import { Directive, forwardRef, Input, AfterViewInit, ElementRef, ViewContainerRef, OnDestroy, NgZone } from '@angular/core';
2
-import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
2
+import { NG_VALUE_ACCESSOR } from '@angular/forms';
3 3
 import { C3Tooltip } from './c3-tooltip.component';
4 4
 import { Overlay, OverlayConfig, ScrollDispatcher, OverlayRef } from '@angular/cdk/overlay';
5 5
 import { Platform } from '@angular/cdk/platform';
6 6
 import { TemplatePortal } from '@angular/cdk/portal';
7
+import { coerceBooleanProperty } from '@angular/cdk/coercion';
7 8
 
8 9
 export const MAT_AUTOCOMPLETE_VALUE_ACCESSOR: any = {
9 10
   provide: NG_VALUE_ACCESSOR,
@@ -19,17 +20,37 @@ export const MAT_AUTOCOMPLETE_VALUE_ACCESSOR: any = {
19 20
 export class C3TooltipTrigger implements AfterViewInit, OnDestroy {
20 21
   overlayRef: OverlayRef | null;
21 22
 
23
+  /** The timeout ID of any current timer set to show the tooltip */
24
+  _showTimeoutId: number | null;
25
+
26
+  /** The timeout ID of any current timer set to hide the tooltip */
27
+  _hideTimeoutId: number | null;
28
+
29
+  private _manualListeners = new Map<string, EventListenerOrEventListenerObject>();
30
+  private _tooltipClass: string | string[] | Set<string> | { [key: string]: any };
31
+  private _tootipDisabled: boolean = false;
32
+
22 33
   @Input('c3Tooltip') tooltip: C3Tooltip;
23 34
 
24
-  @Input('c3TooltipDisabled') disabled: Boolean = false;
35
+  @Input('c3TooltipDisabled')
36
+  get tooltipDisabled(): boolean { return this._tootipDisabled; }
37
+  set tooltipDisabled(value: boolean) {
38
+    this._tootipDisabled = coerceBooleanProperty(value)
39
+  };
25 40
 
26
-  /** The default delay in ms before showing the tooltip after show is called */
27 41
   @Input('c3TooltipShowDelay') showDelay = 0;
28 42
 
29
-  /** The default delay in ms before hiding the tooltip after hide is called */
30 43
   @Input('c3TooltipHideDelay') hideDelay = 0;
31 44
 
32
-  private _manualListeners = new Map<string, EventListenerOrEventListenerObject>();
45
+  @Input('c3TooltipClass')
46
+  get tooltipClass() { return this._tooltipClass; }
47
+  set tooltipClass(value: string | string[] | Set<string> | { [key: string]: any }) {
48
+    this._tooltipClass = value;
49
+    if (this.tooltip) {
50
+      this.tooltip.tooltipClass = this._tooltipClass;
51
+      this.tooltip._markForCheck()
52
+    }
53
+  }
33 54
 
34 55
   constructor(
35 56
     private _element: ElementRef<HTMLElement>,
@@ -56,16 +77,38 @@ export class C3TooltipTrigger implements AfterViewInit, OnDestroy {
56 77
     this._manualListeners.clear();
57 78
   }
58 79
 
59
-  show() {
60
-    const overlayRef = this._overlay.create(this.getOverlayConfig());
61
-    const portal = new TemplatePortal(this.tooltip.template, this._viewContainerRef);
62
-    overlayRef.attach(portal);
80
+  show(delay: number = this.showDelay) {
81
+    if (this.tooltipDisabled) {
82
+      return;
83
+    }
84
+
85
+    if (this._hideTimeoutId) {
86
+      clearTimeout(this._hideTimeoutId);
87
+      this._hideTimeoutId = null;
88
+    }
89
+    this._showTimeoutId = window.setTimeout(() => {
90
+      this._showTimeoutId = null;
91
+
92
+      const overlayRef = this._overlay.create(this.getOverlayConfig());
93
+      const portal = new TemplatePortal(this.tooltip.template, this._viewContainerRef);
94
+      overlayRef.attach(portal);
63 95
 
64
-    this.overlayRef = overlayRef;
96
+      this.overlayRef = overlayRef;
97
+    }, delay);
65 98
   }
66 99
 
67
-  hide() {
68
-    this.overlayRef.detach();
100
+  hide(delay: number = this.hideDelay) {
101
+    if (this._showTimeoutId) {
102
+      clearTimeout(this._showTimeoutId);
103
+      this._showTimeoutId = null;
104
+    }
105
+
106
+    this._hideTimeoutId = window.setTimeout(() => {
107
+      this._hideTimeoutId = null;
108
+
109
+      if (this.overlayRef && this.overlayRef.hasAttached())
110
+        this.overlayRef.detach();
111
+    }, delay);
69 112
   }
70 113
 
71 114
   private getOverlayConfig(): OverlayConfig {