-
Notifications
You must be signed in to change notification settings - Fork 27
/
enum-filter.component.ts
200 lines (185 loc) · 5.24 KB
/
enum-filter.component.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import {
Component,
Input,
OnInit,
OnDestroy,
ElementRef,
ComponentFactoryResolver,
ViewContainerRef,
ComponentRef,
Injector
} from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import forEach from 'lodash-es/forEach';
import values from 'lodash-es/values';
import { SuperTableFilter, ColumnState } from '../models/interfaces';
import { SuperTableState } from '../services/super-table-state.service';
@Component({
selector: 'super-table-enum-filter-dropdown',
template: `
<div class="clear-filter">
<button class="btn btn-outline-secondary clear-filter-btn" role="button" (click)="showAll()">show all</button>
</div>
<div *ngFor="let choice of column.def.filterChoices">
<input type="checkbox" [(ngModel)]="column.filterValue[choice]" (ngModelChange)="onChoiceChange()" />
{{ choice }}
</div>
<button role="button" class="close-dropdown" (click)="destroyMe()">×</button>
`,
styles: [`
:host {
position: absolute;
background: white;
padding: 5px 10px;
border: 1px solid #ddd;
box-shadow: 0 1px 10px -1px rgba(0,0,0,0.2);
}
.clear-filter {
border-bottom: 1px solid #DDD;
padding: 5px 0;
}
.close-dropdown {
position: absolute;
top: 5px;
right: 10px;
border: none;
background: transparent;
color: #CCC;
display: block;
width: 20px;
height: 20px;
line-height: 20px;
}
.close-dropdown:hover {
color: #AAA;
}
`]
})
export class EnumFilterDropdownComponent implements OnInit, OnDestroy {
@Input() column: ColumnState;
top: number;
left: number;
width: number;
destroyMe?: Function;
constructor(
private state: SuperTableState,
private el: ElementRef
) {}
ngOnInit () {
const styles: CSSStyleDeclaration = this.el.nativeElement.style;
styles.top = this.top + 'px';
styles.left = this.left + 'px';
styles.width = this.width + 'px';
}
ngOnDestroy () {
// to ensure that references to parent component
// do not prevent GC
this.destroyMe = undefined;
}
onChoiceChange() {
this.state.notify();
}
showAll() {
forEach(this.column.filterValue, (val, key) => {
this.column.filterValue[key] = true;
});
this.state.notify();
if (this.destroyMe) {
this.destroyMe();
}
}
}
@Component({
/* tslint:disable-next-line */
selector: '[super-table-enum-filter]',
template: `
<button
[attr.title]="filter.title"
role="button"
(click)="toggleVisibility()"
[ngClass]="{ hasDisabled : disabledFilterCount }">
<strong>{{ filter.placeholder }}:</strong>
<span [hidden]="disabledFilterCount != 0">showing all</span>
<span [hidden]="disabledFilterCount == 0">filtering {{disabledFilterCount}} value(s)</span>
</button>
`,
styles: [`
:host {
position: relative;
display: block;
height: 100%;
font-size: 90%;
white-space: nowrap;
text-overflow: ellipsis;
}
button {
text-align: left;
margin: 0 5px;
display: block;
width: calc(100% - 10px);
height: 100%;
background: #ddd;
border: none;
border-radius: 2px;
}
button.hasDisabled {
background: rgba(22, 140, 239, 0.2);
}
.enum-filter-dropdown {
position: absolute;
top: 100%;
z-index: 1;
}
`]
})
export class EnumFilterComponent implements OnInit, OnDestroy {
@Input() filter: SuperTableFilter;
@Input() column: ColumnState;
private dropdown?: ComponentRef<EnumFilterDropdownComponent>;
private disabledChoices: Set<any> = new Set<any>();
disabledFilterCount = 0;
private subscription: Subscription;
constructor(
private state: SuperTableState,
private el: ElementRef,
private viewContainer: ViewContainerRef,
private resolver: ComponentFactoryResolver
) {}
ngOnInit () {
// initialize filtered values to include all
this.column.filterValue = {};
if (this.column.def.filterChoices) {
this.column.def.filterChoices.forEach(choice => {
this.column.filterValue[choice] = true;
});
}
this.subscription = this.state.stateChanged$.subscribe(() => {
this.disabledFilterCount = values(this.column.filterValue)
.filter(isEnabled => !isEnabled)
.length;
});
}
ngOnDestroy () {
this.subscription.unsubscribe();
}
toggleVisibility() {
if (this.dropdown) {
this.dropdown.destroy();
this.dropdown = undefined;
} else {
const clientRect: ClientRect = this.el.nativeElement.getBoundingClientRect();
const cmpFactory = this.resolver.resolveComponentFactory(EnumFilterDropdownComponent);
const ctxInjector: Injector = this.viewContainer.injector;
const cmpRef: ComponentRef<EnumFilterDropdownComponent> = this.viewContainer.createComponent(cmpFactory, 0, ctxInjector);
cmpRef.instance.column = this.column;
cmpRef.instance.top = clientRect.top + clientRect.height;
cmpRef.instance.left = clientRect.left;
cmpRef.instance.width = clientRect.width;
cmpRef.instance.destroyMe = () => {
this.toggleVisibility();
};
this.dropdown = cmpRef;
document.body.appendChild(cmpRef.location.nativeElement);
}
}
}