-
Notifications
You must be signed in to change notification settings - Fork 78
/
selection-area.ts
293 lines (261 loc) · 9.5 KB
/
selection-area.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
* @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 {
ComponentPortal,
DomPortalOutlet,
TemplatePortal,
} from '@angular/cdk/portal';
import {
AfterViewInit,
ApplicationRef,
ChangeDetectionStrategy,
Component,
ComponentFactoryResolver,
DoCheck,
Input,
NgZone,
OnChanges,
OnDestroy,
Output,
Renderer2,
SimpleChanges,
TemplateRef,
ViewChild,
ViewContainerRef,
ViewEncapsulation,
} from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { map, switchMap, take } from 'rxjs/operators';
import { addCssClass } from '@dynatrace/barista-components/core';
import {
DtSelectionAreaContainer,
DtSelectionAreaContainerChange,
} from './selection-area-container';
/**
* @deprecated The selection area will be replaced with the chart selection area
* @breaking-change To be removed with 5.0.0.
* Change event object emitted by DtSelectionArea
*/
// tslint:disable-next-line: deprecation
export interface DtSelectionAreaChange extends DtSelectionAreaContainerChange {
// tslint:disable-next-line: deprecation
source: DtSelectionArea;
}
/** The global container that holds all selection area containers */
let globalContainerElement: HTMLElement | null = null;
/**
* @deprecated The selection area will be replaced with the chart selection area
* @breaking-change To be removed with 5.0.0.
*/
@Component({
selector: 'dt-selection-area',
exportAs: 'dtSelectionArea',
templateUrl: 'selection-area.html',
host: {
class: 'dt-selection-area',
},
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.Emulated,
})
export class DtSelectionArea
implements OnChanges, AfterViewInit, OnDestroy, DoCheck {
/** The aria label used for the selected area of the selection area */
@Input('aria-label-selected-area') ariaLabelSelectedArea: string;
/** The aria label used for the left handle of the selection area */
@Input('aria-label-left-handle') ariaLabelLeftHandle: string;
/** The aria label used for the right handle of the selection area */
@Input('aria-label-right-handle') ariaLabelRightHandle: string;
/** The aria label used in the close button of the overlay */
@Input('aria-label-close-button') ariaLabelClose: string;
/** Emits when the selected area changes position or size */
@Output() readonly changed: Observable<
// tslint:disable-next-line: deprecation
DtSelectionAreaChange
// tslint:disable-next-line: deprecation
> = this._deferContainerEvent<DtSelectionAreaContainerChange>('changed').pipe(
map(changed => ({ source: this, ...changed })),
);
/** Emits when the selected area is closed */
@Output() readonly closed: Observable<void> = this._deferContainerEvent<void>(
'closed',
);
/** @internal Emits whenever the grabbing state changes */
readonly _grabbingChange: Observable<boolean> = this._deferContainerEvent<
boolean
>('_grabbingChange');
/** The portal outlet used to render the container to the body to properly position the selection area container */
private _portalOutlet: DomPortalOutlet;
/** The instance of the container that is dynamically generated in the outlet */
// tslint:disable-next-line: deprecation
private _containerInstance: DtSelectionAreaContainer;
/**
* @internal Emits every time the domrect of the origin changes
* It is triggered in the origin since the selection area does not have a connection to the origin itself
* This needs to be run after zone is stable because we need to wait until the origin is actually rendered
* to get the correct boundaries
*/
_boundariesChanged: BehaviorSubject<ClientRect | null> = new BehaviorSubject(
null,
);
/** @internal Reference to the overlay content template. */
@ViewChild('content', { read: TemplateRef, static: true })
_overlayContent: TemplateRef<void>;
/** @internal Reference to the overlay actions template. */
@ViewChild('actions', { read: TemplateRef, static: true })
_overlayActions: TemplateRef<void>;
constructor(
private _renderer: Renderer2,
private _viewContainerRef: ViewContainerRef,
private _componentFactoryResolver: ComponentFactoryResolver,
private _appRef: ApplicationRef,
private _ngZone: NgZone,
) {
const containerInstance = this._createContainer();
if (containerInstance) {
this._containerInstance = containerInstance;
this._boundariesChanged.subscribe(boundaries => {
if (boundaries) {
this._applyBoundariesToContainer(boundaries);
}
});
}
}
ngAfterViewInit(): void {
if (this._containerInstance) {
this._containerInstance._overlayContentPortal = new TemplatePortal(
this._overlayContent,
this._viewContainerRef,
);
this._containerInstance._overlayActionsPortal = new TemplatePortal(
this._overlayActions,
this._viewContainerRef,
);
}
}
ngOnChanges(changes: SimpleChanges): void {
if (
changes &&
this._containerInstance &&
(changes.ariaLabelSelectedArea ||
changes.ariaLabelLeftHandle ||
changes.ariaLabelRightHandle ||
changes.ariaLabelClose)
) {
this._containerInstance._updateAriaLabels(
this.ariaLabelSelectedArea,
this.ariaLabelLeftHandle,
this.ariaLabelRightHandle,
this.ariaLabelClose,
);
}
}
ngDoCheck(): void {
// We need to trigger CD manually here, because we had to remove the container component from the embedded views
// of the selection area. Therefore the containers CD would never run. With this construct we would trigger
// CD on the container the same way as if it would still be in the tree under the selection area
if (this._containerInstance) {
this._containerInstance._changeDetectorRef.detectChanges();
}
}
ngOnDestroy(): void {
this._portalOutlet.dispose();
}
/** Closes and destroys the selected area */
close(): void {
this._containerInstance.close();
}
/** Sets the focus to the selection area */
focus(): void {
this._containerInstance.focus();
}
/** @internal creates the selected area on the container with the given position */
_createSelectedArea(posX: number): void {
this._containerInstance._create(posX);
}
/** @internal Passes the interpolateFn from the origin to the container */
_setInterpolateFnOnContainer(fn: (pxValue: number) => number): void {
if (this._containerInstance) {
this._containerInstance._interpolateFn = fn;
}
}
/** Apply boundaries to the container element to match the origin */
private _applyBoundariesToContainer(boundaries: ClientRect): void {
if (this._containerInstance) {
this._containerInstance._applyBoundaries(boundaries);
}
}
/** Defers the container events until the zone is stable and the container is ready */
private _deferContainerEvent<T>(eventName: string): Observable<T> {
return this._containerInstance
? this._containerInstance[eventName]
: this._ngZone.onStable.asObservable().pipe(
take(1),
switchMap(() => this._deferContainerEvent(eventName)),
);
}
/** Creates the container and the host element in the domportal on the body */
// tslint:disable-next-line: deprecation
private _createContainer(): DtSelectionAreaContainer | null {
if (document) {
// the viewContainerRef as a parameter to the component portal needs to be null so we dont create an
// embedded view
const portal = new ComponentPortal(
// tslint:disable-next-line: deprecation
DtSelectionAreaContainer,
null,
null,
this._componentFactoryResolver,
);
this._portalOutlet = new DomPortalOutlet(
this._createHostElement(),
this._componentFactoryResolver,
this._appRef,
this._viewContainerRef.injector,
);
const componentRef = this._portalOutlet.attachComponentPortal(portal);
return componentRef.instance;
}
return null;
}
/**
* Creates the host element that wraps around an overlay
* and can be used for advanced positioning.
* @returns Newly-create host element.
*/
private _createHostElement(): HTMLElement {
const host = this._renderer.createElement('div');
this._renderer.appendChild(this._getGlobalContainerElement(), host);
return host;
}
/** Creates a new global container holds all selection area containers if needed and returns it */
private _getGlobalContainerElement(): HTMLElement {
if (!globalContainerElement) {
this._createGlobalContainerInBody();
}
return globalContainerElement!;
}
/**
* Create the overlay container element, which is simply a div
* with the 'selection-area-container' class on the document body.
*/
private _createGlobalContainerInBody(): void {
const container = this._renderer.createElement('div');
addCssClass(container, 'dt-selection-area-global-container');
this._renderer.appendChild(document.body, container);
globalContainerElement = container;
}
}