-
Notifications
You must be signed in to change notification settings - Fork 78
/
filter-field-range.ts
258 lines (223 loc) · 8.2 KB
/
filter-field-range.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* @license
* Copyright 2019 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { coerceNumberProperty } from '@angular/cdk/coercion';
import { TemplatePortal } from '@angular/cdk/portal';
// tslint:disable: template-cyclomatic-complexity
import {
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
EventEmitter,
Input,
Output,
TemplateRef,
ViewChild,
ViewContainerRef,
ViewEncapsulation,
} from '@angular/core';
import { DtButtonGroup } from '@dynatrace/barista-components/button-group';
import { isDefined } from '@dynatrace/barista-components/core';
import { DtRangeOperatorFlags } from '../types';
let _uniqueIdCounter = 0;
export class DtFilterFieldRangeSubmittedEvent {
constructor(
/** Reference to the filter field range panel that emitted the event. */
public source: DtFilterFieldRange,
/** Operator that has been selected. */
public operator: DtFilterFieldRangeOperator,
/** Entered range value(s). Could be either a single value, or a range defined by two values. */
public range: number | [number, number],
/** Unit for the entered range / value */
public unit: string,
) {}
}
/** Valid FilterfieldRange operators. */
export type DtFilterFieldRangeOperator =
| 'range'
| 'lower-equal'
| 'greater-equal'
| 'equal';
@Component({
selector: 'dt-filter-field-range',
templateUrl: 'filter-field-range.html',
styleUrls: ['filter-field-range.scss'],
encapsulation: ViewEncapsulation.Emulated,
exportAs: 'dtFilterFieldRange',
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DtFilterFieldRange implements AfterViewInit {
/** Unit for the range values */
@Input() unit: string;
/** Operators that are enabled in the range. */
@Input()
get enabledOperators(): DtRangeOperatorFlags {
return this._enabledOperators;
}
set enabledOperators(value: DtRangeOperatorFlags) {
this._enabledOperators = value;
this._hasRangeOperator = !!// tslint:disable-next-line: no-bitwise
(this._enabledOperators & DtRangeOperatorFlags.Range);
this._hasEqualOperator = !!// tslint:disable-next-line: no-bitwise
(this._enabledOperators & DtRangeOperatorFlags.Equal);
this._hasLowerEqualOperator = !!// tslint:disable-next-line: no-bitwise
(this._enabledOperators & DtRangeOperatorFlags.LowerEqual);
this._hasGreaterEqualOperator = !!// tslint:disable-next-line: no-bitwise
(this._enabledOperators & DtRangeOperatorFlags.GreatEqual);
this._setOperator();
}
private _enabledOperators: DtRangeOperatorFlags;
/** @internal Defines if the filter-field-range has the range option */
_hasRangeOperator = false;
/** @internal Defines if the filter-field-range has the equal option */
_hasEqualOperator = false;
/** @internal Defines if the filter-field-range has the lower-equal option */
_hasLowerEqualOperator = false;
/** @internal Defines if the filter-field-range has the greater-equal option */
_hasGreaterEqualOperator = false;
/** Event that is emitted when the filter-field range panel is opened. */
@Output() readonly opened = new EventEmitter<void>();
/** Event that is emitted when the filter-field range panel is closed. */
@Output() readonly closed = new EventEmitter<void>();
/** Event that is emitted whenever an option from the list is selected. */
@Output() readonly rangeSubmitted = new EventEmitter<
DtFilterFieldRangeSubmittedEvent
>();
/** Unique ID to be used by filter-field range trigger's "aria-owns" property. */
id = `dt-filter-field-range-${_uniqueIdCounter++}`;
/** Whether the filter-field range panel is open. */
get isOpen(): boolean {
return this._isOpen;
}
/** @internal Whether the filter-field range panel is open. */
_isOpen = false;
/** @internal The currently selected option. */
_selectedOperator: DtFilterFieldRangeOperator | null;
/** @internal */
@ViewChild(TemplateRef, { static: true }) _template: TemplateRef<{}>;
/** @internal */
@ViewChild(DtButtonGroup, { static: false }) _operatorGroup: DtButtonGroup<
DtFilterFieldRange
>;
/** @internal */
_portal: TemplatePortal;
/** @internal Holds the current value of the input field for the from value */
_valueFrom = '';
/** @internal Holds the current value of the input field for the to value */
_valueTo = '';
constructor(
private _viewContainerRef: ViewContainerRef,
private _changeDetectorRef: ChangeDetectorRef,
) {}
ngAfterViewInit(): void {
this._portal = new TemplatePortal<{}>(
this._template,
this._viewContainerRef,
);
}
/** Sets focus to the operator button group. */
focus(): void {
this._operatorGroup.focus();
}
/** @internal Marks the filter-field range for change detection. */
_markForCheck(): void {
this._changeDetectorRef.markForCheck();
}
/** @internal Unique ID to be used on a local element */
_getLocalId(suffix: string): string {
return `${this.id}-${suffix}`;
}
/** @internal Whether the selected operator's type equals the provided type */
_isOperatorType(type: DtFilterFieldRangeOperator): boolean {
return isDefined(this._selectedOperator) && this._selectedOperator === type;
}
/** @internal Handles the submit of range values. */
_handleSubmit(event: Event): void {
event.preventDefault();
event.stopImmediatePropagation();
const range = this._validateRange();
if (isDefined(range)) {
this.rangeSubmitted.emit(
new DtFilterFieldRangeSubmittedEvent(
this,
this._selectedOperator as DtFilterFieldRangeOperator,
range as number | [number, number],
this.unit,
),
);
// After emission we need to reset the range state, to have a fresh one
// if another range opens.
this._valueFrom = '';
this._valueTo = '';
this._selectedOperator = null;
}
}
/** @internal Sets the operator if it is available. */
_setOperator(operator?: DtFilterFieldRangeOperator): void {
if (operator === 'range' && this._hasRangeOperator) {
this._selectedOperator = operator;
return;
}
if (operator === 'equal' && this._hasEqualOperator) {
this._selectedOperator = operator;
return;
}
if (operator === 'lower-equal' && this._hasLowerEqualOperator) {
this._selectedOperator = operator;
return;
}
if (operator === 'greater-equal' && this._hasGreaterEqualOperator) {
this._selectedOperator = operator;
return;
}
if (!this._selectedOperator) {
this._selectedOperator = this._hasRangeOperator
? 'range'
: this._hasLowerEqualOperator
? 'lower-equal'
: this._hasGreaterEqualOperator
? 'greater-equal'
: this._hasEqualOperator
? 'equal'
: null;
}
}
/** @internal Set values for the range input fields. */
_setValues(values: number | [number, number]): void {
if (Array.isArray(values)) {
this._valueFrom = `${values[0]}`;
this._valueTo = `${values[1]}`;
} else {
this._valueFrom = `${values}`;
}
}
/** @internal Whether the range values are valid. */
_isValidRange(): boolean {
return isDefined(this._validateRange());
}
/** Validates and casts the range values (from & to) and return either the range number(s) or null */
private _validateRange(): number | [number, number] | null {
const valueFromOrNull = coerceNumberProperty(this._valueFrom, null);
if (this._selectedOperator === 'range' && valueFromOrNull !== null) {
const valueToOrNull = coerceNumberProperty(this._valueTo, null);
return valueToOrNull !== null && valueFromOrNull <= valueToOrNull
? [valueFromOrNull, valueToOrNull]
: null;
}
return valueFromOrNull;
}
}