This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
/
dropdown.ts
180 lines (153 loc) · 4.44 KB
/
dropdown.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { coerceBooleanProperty, coerceCssPixelValue, coerceNumberProperty } from "@angular/cdk/coercion";
import { CdkOverlayOrigin, ConnectedPosition, ScrollStrategy, ScrollStrategyOptions } from "@angular/cdk/overlay";
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, Input, OnInit, Renderer2, TemplateRef } from "@angular/core";
import { timeHours } from "d3";
import { fadeInAnimation, fadeOutAnimation } from '../animations';
@Component({
selector: 'sfng-dropdown',
exportAs: 'sfngDropdown',
templateUrl: './dropdown.html',
styles: [
`
:host {
display: block;
}
`
],
changeDetection: ChangeDetectionStrategy.OnPush,
animations: [fadeInAnimation, fadeOutAnimation],
})
export class SfngDropdown implements OnInit {
/** The trigger origin used to open the drop-down */
trigger: CdkOverlayOrigin | null = null;
/**
* The button/drop-down label. Only when not using
* {@Link SfngDropdown.externalTrigger}
*/
@Input()
label: string = '';
/** The trigger template to use when {@Link SfngDropdown.externalTrigger} */
@Input()
triggerTemplate: TemplateRef<any> | null = null;
/** Set to true to provide an external dropdown trigger template using {@Link SfngDropdown.triggerTemplate} */
@Input()
set externalTrigger(v: any) {
this._externalTrigger = coerceBooleanProperty(v)
}
get externalTrigger() {
return this._externalTrigger;
}
private _externalTrigger = false;
/** Whether or not the drop-down is disabled. */
@Input()
set disabled(v: any) {
this._disabled = coerceBooleanProperty(v)
}
get disabled() {
return this._disabled;
}
private _disabled = false;
/** The Y-offset of the drop-down overlay */
@Input()
set offsetY(v: any) {
this._offsetY = coerceNumberProperty(v);
}
get offsetY() { return this._offsetY }
private _offsetY = 4;
/** The scrollStrategy of the drop-down */
@Input()
scrollStrategy!: ScrollStrategy;
/** Whether or not the pop-over is currently shown. Do not modify this directly */
isOpen = false;
/** The minimum width of the drop-down */
@Input()
set minWidth(val: any) {
this._minWidth = coerceCssPixelValue(val)
}
get minWidth() { return this._minWidth }
private _minWidth: string | number = 0;
/** The maximum width of the drop-down */
@Input()
set maxWidth(val: any) {
this._maxWidth = coerceCssPixelValue(val)
}
get maxWidth() { return this._maxWidth }
private _maxWidth: string | number | null = null;
/** The minimum height of the drop-down */
@Input()
set minHeight(val: any) {
this._minHeight = coerceCssPixelValue(val)
}
get minHeight() { return this._minHeight }
private _minHeight: string | number | null = null;
/** The maximum width of the drop-down */
@Input()
set maxHeight(val: any) {
this._maxHeight = coerceCssPixelValue(val)
}
get maxHeight() { return this._maxHeight }
private _maxHeight: string | number | null = null;
positions: ConnectedPosition[] = [
{
originX: 'end',
originY: 'bottom',
overlayX: 'end',
overlayY: 'top',
},
{
originX: 'end',
originY: 'top',
overlayX: 'end',
overlayY: 'bottom',
},
]
constructor(
public readonly elementRef: ElementRef,
private changeDetectorRef: ChangeDetectorRef,
private renderer: Renderer2,
private scrollOptions: ScrollStrategyOptions,
) {
}
ngOnInit() {
this.scrollStrategy = this.scrollStrategy || this.scrollOptions.close();
}
onOutsideClick(event: MouseEvent) {
if (!!this.trigger) {
const triggerEl = this.trigger.elementRef.nativeElement;
let node = event.target;
while (!!node) {
if (node === triggerEl) {
return;
}
node = this.renderer.parentNode(node);
}
}
this.close();
}
onOverlayClosed() {
this.trigger = null;
}
close() {
this.isOpen = false;
this.changeDetectorRef.markForCheck();
}
toggle(t: CdkOverlayOrigin) {
if (this.isOpen) {
this.close();
return;
}
this.show(t);
}
show(t: CdkOverlayOrigin) {
if (this.isOpen || this._disabled) {
return;
}
if (!!t) {
this.trigger = t;
const rect = (this.trigger.elementRef.nativeElement as HTMLElement).getBoundingClientRect()
this.minWidth = rect ? rect.width : this.trigger.elementRef.nativeElement.offsetWidth;
}
this.isOpen = true;
this.changeDetectorRef.markForCheck();
}
}