aboutsummaryrefslogtreecommitdiffstats
path: root/deprecated-workflow-designer/sdc-workflow-designer-ui/src/app/paletx/core/overlay/overlay-directives.ts
blob: 5b8c1623ddf94b417783467236d233ad46d83e43 (plain)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
 * @license
 * Copyright Google Inc. All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */
/* tslint:disable:array-type member-access variable-name typedef
 only-arrow-functions directive-class-suffix component-class-suffix
 component-selector no-unnecessary-type-assertion arrow-parens*/
import {TemplatePortal} from '@angular/cdk';
import {Direction, Directionality} from '@angular/cdk';
import {ESCAPE} from '@angular/cdk';
import {Directive, ElementRef, EventEmitter, Input, OnChanges, OnDestroy, Optional, Output, Renderer2, SimpleChanges, TemplateRef, ViewContainerRef} from '@angular/core';
import {Subscription} from 'rxjs/Subscription';

import {Overlay} from './overlay';
import {OverlayRef} from './overlay-ref';
import {OverlayState} from './overlay-state';
import {ConnectedOverlayPositionChange, ConnectionPositionPair} from './position/connected-position';
import {ConnectedPositionStrategy} from './position/connected-position-strategy';
import {ScrollStrategy} from './scroll/scroll-strategy';

/** Coerces a data-bound value (typically a string) to a boolean. */
export function coerceBooleanProperty(value: any): boolean {
  return value  !== null && `${value}` !== 'false';
}


/** Default set of positions for the overlay. Follows the behavior of a
 * dropdown. */
const defaultPositionList = [
  new ConnectionPositionPair(
		{originX: 'start', originY: 'bottom'},
		{overlayX: 'start', overlayY: 'top'}),
  new ConnectionPositionPair(
		{originX: 'start', originY: 'top'},
		{overlayX: 'start', overlayY: 'bottom'}),
];


/**
 * Directive applied to an element to make it usable as an origin for an Overlay
 * using a ConnectedPositionStrategy.
 */
@Directive({
  selector: '[nz-overlay-origin]',
  exportAs: 'nzOverlayOrigin',
})
export class OverlayOrigin {
  constructor(public elementRef: ElementRef) {}
}


/**
 * Directive to facilitate declarative creation of an Overlay using a
 * ConnectedPositionStrategy.
 */
@Directive({selector: '[nz-connected-overlay]', exportAs: 'nzConnectedOverlay'})
export class ConnectedOverlayDirective implements OnDestroy, OnChanges {
  private _overlayRef: OverlayRef;
  private _templatePortal: TemplatePortal;
  private _hasBackdrop = false;
  private _backdropSubscription: Subscription|null;
  private _positionSubscription: Subscription;
  private _offsetX = 0;
  private _offsetY = 0;
  private _position: ConnectedPositionStrategy;
  private _escapeListener: Function;

  /** Origin for the connected overlay. */
  @Input() origin: OverlayOrigin;

  /** Registered connected position pairs. */
  @Input() positions: ConnectionPositionPair[];

  /** The offset in pixels for the overlay connection point on the x-axis */
  @Input()
  get offsetX(): number {
	return this._offsetX;
  }

  set offsetX(offsetX: number) {
	this._offsetX = offsetX;
	if (this._position) {
		this._position.withOffsetX(offsetX);
	}
  }

  /** The offset in pixels for the overlay connection point on the y-axis */
  @Input()
  get offsetY() {
	return this._offsetY;
  }

  set offsetY(offsetY: number) {
	this._offsetY = offsetY;
	if (this._position) {
		this._position.withOffsetY(offsetY);
	}
  }

  /** The width of the overlay panel. */
  @Input() width: number|string;

  /** The height of the overlay panel. */
  @Input() height: number|string;

  /** The min width of the overlay panel. */
  @Input() minWidth: number|string;

  /** The min height of the overlay panel. */
  @Input() minHeight: number|string;

  /** The custom class to be set on the backdrop element. */
  @Input() backdropClass: string;

  /** The custom class to be set on the pane element. */
  @Input() paneClass: string;

  /** Strategy to be used when handling scroll events while the overlay is open.
   */
  @Input()
  scrollStrategy: ScrollStrategy = this._overlay.scrollStrategies.reposition();

  /** Whether the overlay is open. */
  @Input() open = false;

  /** Whether or not the overlay should attach a backdrop. */
  @Input()
  get hasBackdrop() {
	return this._hasBackdrop;
  }

  set hasBackdrop(value: any) {
	this._hasBackdrop = coerceBooleanProperty(value);
  }

  /** Event emitted when the backdrop is clicked. */
  @Output() backdropClick = new EventEmitter<void>();

  /** Event emitted when the position has changed. */
  @Output() positionChange = new EventEmitter<ConnectedOverlayPositionChange>();

  /** Event emitted when the overlay has been attached. */
  @Output() attach = new EventEmitter<void>();

  /** Event emitted when the overlay has been detached. */
  @Output() detach = new EventEmitter<void>();

  // TODO(jelbourn): inputs for size, scroll behavior, animation, etc.

  constructor(
		private _overlay: Overlay, private _renderer: Renderer2,
		templateRef: TemplateRef<any>, viewContainerRef: ViewContainerRef,
		@Optional() private _dir: Directionality) {
	this._templatePortal = new TemplatePortal(templateRef, viewContainerRef);
  }

  /** The associated overlay reference. */
  get overlayRef(): OverlayRef {
	return this._overlayRef;
  }

  /** The element's layout direction. */
  get dir(): Direction {
	return this._dir ? this._dir.value : 'ltr';
  }

  ngOnDestroy() {
	this._destroyOverlay();
  }

  ngOnChanges(changes: SimpleChanges) {
	if (changes['open']) {
		this.open ? this._attachOverlay() : this._detachOverlay();
	}
  }

  /** Creates an overlay */
  private _createOverlay() {
	if (!this.positions || !this.positions.length) {
		this.positions = defaultPositionList;
	}

	this._overlayRef =
		this._overlay.create(this._buildConfig(), this.paneClass);
  }

  /** Builds the overlay config based on the directive's inputs */
  private _buildConfig(): OverlayState {
	const overlayConfig = new OverlayState();

	if (this.width || this.width === 0) {
		overlayConfig.width = this.width;
	}

	if (this.height || this.height === 0) {
		overlayConfig.height = this.height;
	}

	if (this.minWidth || this.minWidth === 0) {
		overlayConfig.minWidth = this.minWidth;
	}

	if (this.minHeight || this.minHeight === 0) {
		overlayConfig.minHeight = this.minHeight;
	}

	overlayConfig.hasBackdrop = this.hasBackdrop;

	if (this.backdropClass) {
		overlayConfig.backdropClass = this.backdropClass;
	}

	this._position =
		this._createPositionStrategy() as ConnectedPositionStrategy;
	overlayConfig.positionStrategy = this._position;
	overlayConfig.scrollStrategy = this.scrollStrategy;

	return overlayConfig;
  }

  /** Returns the position strategy of the overlay to be set on the overlay
   * config */
  private _createPositionStrategy(): ConnectedPositionStrategy {
	const pos = this.positions[0];
	const originPoint = {originX: pos.originX, originY: pos.originY};
	const overlayPoint = {overlayX: pos.overlayX, overlayY: pos.overlayY};

	const strategy =
		this._overlay.position()
			.connectedTo(this.origin.elementRef, originPoint, overlayPoint)
			.withOffsetX(this.offsetX)
			.withOffsetY(this.offsetY);

	this._handlePositionChanges(strategy);

	return strategy;
  }

  private _handlePositionChanges(strategy: ConnectedPositionStrategy): void {
	for (let i = 1; i < this.positions.length; i++) {
		strategy.withFallbackPosition(
			{
			originX: this.positions[i].originX,
			originY: this.positions[i].originY
			},
			{
			overlayX: this.positions[i].overlayX,
			overlayY: this.positions[i].overlayY
			});
	}

	this._positionSubscription = strategy.onPositionChange.subscribe(
		pos => this.positionChange.emit(pos));
  }

  /** Attaches the overlay and subscribes to backdrop clicks if backdrop exists
   */
  private _attachOverlay() {
	if (!this._overlayRef) {
		this._createOverlay();
	}

	this._position.withDirection(this.dir);
	this._overlayRef.getState().direction = this.dir;
	this._initEscapeListener();

	if (!this._overlayRef.hasAttached()) {
		this._overlayRef.attach(this._templatePortal);
		this.attach.emit();
	}

	if (this.hasBackdrop) {
		this._backdropSubscription =
			this._overlayRef.backdropClick().subscribe(() => {
			this.backdropClick.emit();
			});
	}
  }

  /** Detaches the overlay and unsubscribes to backdrop clicks if backdrop
   * exists */
  private _detachOverlay() {
	if (this._overlayRef) {
		this._overlayRef.detach();
		this.detach.emit();
	}

	if (this._backdropSubscription) {
		this._backdropSubscription.unsubscribe();
		this._backdropSubscription = null;
	}

	if (this._escapeListener) {
		this._escapeListener();
	}
  }

  /** Destroys the overlay created by this directive. */
  private _destroyOverlay() {
	if (this._overlayRef) {
		this._overlayRef.dispose();
	}

	if (this._backdropSubscription) {
		this._backdropSubscription.unsubscribe();
	}

	if (this._positionSubscription) {
		this._positionSubscription.unsubscribe();
	}

	if (this._escapeListener) {
		this._escapeListener();
	}
  }

  /** Sets the event listener that closes the overlay when pressing Escape. */
  private _initEscapeListener() {
	this._escapeListener =
		this._renderer.listen('document', 'keydown', (event: KeyboardEvent) => {
			if (event.keyCode === ESCAPE) {
			this._detachOverlay();
			}
		});
  }
}