| 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
38×
38×
38×
38×
38×
38×
38×
38×
1×
38×
38×
38×
38×
38×
1×
104×
1×
38×
38×
38×
38×
15×
15×
38×
4×
4×
38×
2×
2×
38×
7×
7×
7×
38×
10×
10×
10×
38×
53×
53×
38×
38×
38×
1×
103×
2×
38×
2×
38×
2×
38×
2×
38×
2×
38×
2×
40×
1088×
2×
19×
57×
2×
57×
19×
5×
5×
5×
38×
19×
19×
57×
2×
57×
19×
5×
5×
1×
38×
19×
19×
57×
2×
57×
19×
5×
5×
5×
1×
38×
19×
19×
76×
2×
76×
38×
10×
10×
10×
2×
38×
38×
38×
2×
9×
1×
19×
229×
19×
19×
1×
1×
1×
38×
38×
1×
38×
38×
38×
38×
38×
38×
1×
1×
1×
1×
2×
1×
1×
1×
1×
1×
2×
1×
1×
1×
1×
1×
1×
2×
2×
2×
1×
1×
1×
1×
1×
1×
44×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
103×
103×
1×
1×
1×
3×
1×
2×
1×
| /*
* @license
* Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
*
* Use of this source code is governed by an Apache-2.0 license that can be
* found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
*/
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ElementRef, EventEmitter, Input, OnDestroy, Optional, Output, Self, ViewChild } from '@angular/core';
import { ControlValueAccessor, NgControl } from '@angular/forms';
import { from as observableFrom, fromEvent as observableFromEvent, Observable, Subject, Subscription } from 'rxjs';
import { debounceTime, filter, takeWhile } from 'rxjs/operators';
import { Rect } from '../../common/core/graphics/rect';
import { KeyCodes } from '../../common/core/keycodes.enum';
import { IDropCursorInfos } from '../mouse-dragdrop/mouse-dragdrop.service';
import { IDejaMouseDroppableContext } from '../mouse-dragdrop/mouse-droppable.directive';
import { DejaTileGroupComponent } from './tile-group.component';
import { DejaTile } from './tile.class';
import { IDejaTile } from './tile.interface';
import { DejaTilesLayoutProvider, IDejaTilesRefreshParams } from './tiles-layout.provider';
import { IDejaTilesAddEvent, IDejaTilesEvent, IDejaTilesModelEvent, IDejaTilesRemoveEvent } from './tiles.event';
const noop = () => { };
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [DejaTilesLayoutProvider],
selector: 'deja-tiles',
styles: [
require('./tiles.component.scss'),
],
template: require('./tiles.component.html'),
})
export class DejaTilesComponent implements AfterViewInit, ControlValueAccessor, OnDestroy {
/**
* Raised when the selected items has changed
*/
@Output() public selectionChanged = new EventEmitter<IDejaTilesEvent>();
/**
* Raised when the layout has changed with a drag and drop
*/
@Output() public layoutChanged = new EventEmitter<IDejaTilesEvent>();
/**
* Raised when the layout is completed and all tiles are binded
*/
@Output() public layoutCompleted = new EventEmitter<IDejaTilesEvent>();
/**
* Raised before some tiles will be added to the data model with a paste
*/
@Output() public contentAdding = new EventEmitter<IDejaTilesAddEvent>();
/**
* Raised before some tiles will be removed from the data model with a delete
*/
@Output() public contentRemoving = new EventEmitter<IDejaTilesRemoveEvent>();
/**
* Raised when some tiles model has changed
*/
@Output() public modelChanged = new EventEmitter<IDejaTilesModelEvent>();
/**
* Raised when some tiles are copied in the clipboard service. Can result from a copy or paste operation on the tiles.
*/
@Output() public contentCopied = new EventEmitter<IDejaTilesEvent>();
/**
* Tab index of the focusable element
*/
@Input() public tabIndex = 0;
@ContentChild('tileTemplate')
protected tileTemplate: any;
// NgModel implementation
protected onTouchedCallback: () => void = noop;
protected onChangeCallback: (_: any) => void = noop;
private _models = [] as IDejaTile[];
private delete$sub: Subscription;
private copy$sub: Subscription;
private cut$sub: Subscription;
private paste$sub: Subscription;
private keyup$: Observable<KeyboardEvent>;
private isAlive = true;
private hasFocus = false;
public get tiles(): DejaTile[] {
return this.layoutProvider.tiles;
}
@ViewChild('tilesContainer') private tilesContainer: ElementRef;
constructor(el: ElementRef, private changeDetectorRef: ChangeDetectorRef, private layoutProvider: DejaTilesLayoutProvider, @Self() @Optional() public _control: NgControl) {
Iif (this._control) {
this._control.valueAccessor = this;
}
const element = el.nativeElement as HTMLElement;
observableFrom(this.layoutProvider.selectionChanged).pipe(
takeWhile(() => this.isAlive))
.subscribe((e) => this.selectionChanged.emit(e));
observableFrom(this.layoutProvider.contentAdding).pipe(
takeWhile(() => this.isAlive))
.subscribe((e) => this.contentAdding.emit(e));
observableFrom(this.layoutProvider.contentRemoving).pipe(
takeWhile(() => this.isAlive))
.subscribe((e) => this.contentRemoving.emit(e));
observableFrom(this.layoutProvider.modelChanged).pipe(
takeWhile(() => this.isAlive))
.subscribe((event) => {
this.modelChanged.emit(event);
this.onChangeCallback(event.tiles);
});
observableFrom(this.layoutProvider.layoutChanged).pipe(
takeWhile(() => this.isAlive))
.subscribe((event) => {
this.layoutChanged.emit(event);
this.onChangeCallback(event.tiles);
});
observableFrom(this.layoutProvider.layoutCompleted).pipe(
takeWhile(() => this.isAlive))
.subscribe((event) => this.layoutCompleted.emit(event));
this.keyup$ = observableFromEvent(element.ownerDocument, 'keyup') as Observable<KeyboardEvent>;
observableFromEvent(window, 'resize').pipe(
takeWhile(() => this.isAlive),
debounceTime(5))
.subscribe(() => this.refresh({ resetWidth: true }));
}
// provide a public access
public get selectionRect$(): Subject<Rect> {
return this.layoutProvider.selectionRect$;
}
@Input()
public set tileminwidth(value: string) {
this.layoutProvider.tileMinWidth = value;
}
@Input()
public set tilemaxwidth(value: string) {
this.layoutProvider.tileMaxWidth = value;
}
@Input()
public set tileminheight(value: string) {
this.layoutProvider.tileMinHeight = value;
}
@Input()
public set tilemaxheight(value: string) {
this.layoutProvider.tileMaxHeight = value;
}
@Input()
public set maxwidth(value: string) {
this.layoutProvider.maxWidth = value;
}
@Input()
public set designMode(value: boolean | string) {
this.layoutProvider.designMode = coerceBooleanProperty(value);
}
public get designMode() {
return this.layoutProvider.designMode;
}
@Input()
public set models(models: IDejaTile[]) {
this.writeValue(models);
}
@Input()
public set canDelete(value: boolean) {
if (coerceBooleanProperty(value) && !this.delete$sub) {
this.delete$sub = this.keyup$.pipe(
filter(() => this.layoutProvider.designMode),
filter((event: KeyboardEvent) => {
const keyCode = event.keyCode || (<any>KeyCodes)[event.code];
return keyCode === KeyCodes.Delete && this.hasFocus;
}))
.subscribe(() => this.layoutProvider.deleteSelection());
} else if (this.delete$sub) {
this.delete$sub.unsubscribe();
this.delete$sub = undefined;
}
}
@Input()
public set canCopy(value: boolean) {
if (coerceBooleanProperty(value) && !this.copy$sub) {
this.copy$sub = this.keyup$.pipe(
filter((event: KeyboardEvent) => {
const keyCode = event.keyCode || (<any>KeyCodes)[event.code];
return keyCode === KeyCodes.KeyC && event.ctrlKey && this.hasFocus;
}))
.subscribe(() => {
this.copySelection();
});
} else if (this.copy$sub) {
this.copy$sub.unsubscribe();
this.copy$sub = undefined;
}
}
@Input()
public set canCut(value: boolean) {
if (coerceBooleanProperty(value) && !this.cut$sub) {
this.cut$sub = this.keyup$.pipe(
filter(() => this.layoutProvider.designMode),
filter((event: KeyboardEvent) => {
const keyCode = event.keyCode || (<any>KeyCodes)[event.code];
return keyCode === KeyCodes.KeyX && event.ctrlKey && this.hasFocus;
}))
.subscribe(() => {
this.cutSelection();
});
} else if (this.cut$sub) {
this.cut$sub.unsubscribe();
this.cut$sub = undefined;
}
}
@Input()
public set canPaste(value: boolean) {
if (coerceBooleanProperty(value) && !this.paste$sub) {
this.paste$sub = this.keyup$.pipe(
filter(() => this.layoutProvider.designMode),
filter((event: KeyboardEvent) => {
const keyCode = event.keyCode || (<any>KeyCodes)[event.code];
return keyCode === KeyCodes.KeyV && event.ctrlKey && this.hasFocus;
}))
.subscribe(() => this.paste());
} else Eif (this.paste$sub) {
this.paste$sub.unsubscribe();
this.paste$sub = undefined;
}
}
@Input()
public set selectedTiles(selectedTiles: Array<IDejaTile | string>) {
this.layoutProvider.selectedTiles = selectedTiles.map((tile) => typeof tile === 'string' ? tile : (<IDejaTile>tile).id);
}
// ************* ControlValueAccessor Implementation **************
public writeValue(models: any) {
this._models = models || [];
const tiles = this._models.map((tile) => new DejaTile(tile));
this.layoutProvider.tiles = tiles;
this.changeDetectorRef.markForCheck();
}
public registerOnChange(fn: any) {
this.onChangeCallback = fn;
}
public registerOnTouched(fn: any) {
this.onTouchedCallback = fn;
}
// ************* End of ControlValueAccessor Implementation **************
public ngAfterViewInit() {
this.layoutProvider.container = this.tilesContainer.nativeElement;
this.refresh({ resetWidth: true });
}
public ngOnDestroy() {
this.layoutProvider.ngOnDestroy();
this.canCopy = false;
this.canCut = false;
this.canDelete = false;
this.canPaste = false;
this.isAlive = false;
}
public copySelection() {
const tiles = this.layoutProvider.copySelection();
Eif (tiles && tiles.length) {
const event = new CustomEvent('DejaTilesCopied', { cancelable: true }) as IDejaTilesEvent;
event.tiles = tiles.map((tile) => tile.toTileModel());
this.contentCopied.emit(event);
}
}
public cutSelection() {
const tiles = this.layoutProvider.cutSelection();
Eif (tiles && tiles.length) {
const event = new CustomEvent('DejaTilesCutted', { cancelable: true }) as IDejaTilesEvent;
event.tiles = tiles.map((tile) => tile.toTileModel());
this.contentCopied.emit(event);
}
}
public deleteSelection() {
const tiles = this.layoutProvider.deleteSelection();
this.changeDetectorRef.markForCheck();
return tiles;
}
public paste() {
const tiles = this.layoutProvider.paste();
this.changeDetectorRef.markForCheck();
return tiles;
}
public ensureVisible(id: string) {
this.layoutProvider.ensureVisible$.next(id);
}
public expandTile(tile: IDejaTile, pixelHeight: number) {
this.layoutProvider.expandTile(tile, pixelHeight);
}
public cancelExpand() {
this.layoutProvider.cancelExpand();
}
public refresh(params?: IDejaTilesRefreshParams) {
this.layoutProvider.refreshTiles$.next(params);
}
public addTiles(tiles: IDejaTile[]) {
this.layoutProvider.addTiles(tiles.map((tile) => new DejaTile(tile)));
}
public removeTiles(tileIds: string[]) {
this.layoutProvider.removeTiles(tileIds);
}
public addGroup(title?: string, bounds?: Rect) {
const tile = {
type: 'group',
bounds: bounds || this.getFreePlace(0, 0, 15, 5),
color: DejaTileGroupComponent.defaultColor,
templateModel: {
title: title || 'New Group',
},
} as IDejaTile;
this.layoutProvider.createTiles([tile]);
return tile;
}
public getFreePlace(pageX?: number, pageY?: number, width?: number, height?: number) {
Iif (!this._models || this._models.length === 0) {
return new Rect(0, 0, width, height);
}
// Check if we drag on a tile
const containerElement = this.tilesContainer.nativeElement as HTMLElement;
const containerBounds = containerElement.getBoundingClientRect();
const x = pageX ? (pageX - containerBounds.left) : 0;
const y = pageY ? (pageY - containerBounds.top) : 0;
const percentBounds = new Rect(this.layoutProvider.getPercentSize(x), this.layoutProvider.getPercentSize(y), width, height);
return this.layoutProvider.getFreePlace(percentBounds);
}
public moveTile(id: string, bounds: Rect) {
this.layoutProvider.moveTile(id, bounds);
}
public getDropContext() {
return {
dragEnter: (dragContext, dragCursor) => {
return this.layoutProvider.dragEnter(dragContext, dragCursor) && {
className: 'hidden', // Hide drag cursor
} as IDropCursorInfos;
},
dragOver: (_dragContext, dragCursor) => {
this.layoutProvider.dragover$.next(dragCursor);
},
dragLeave: (_dragContext) => {
this.layoutProvider.dragleave$.next();
},
} as IDejaMouseDroppableContext;
}
public onTileClosed(tile: DejaTile) {
this.layoutProvider.removeTiles([tile.id]);
}
public onTileModelChanged() {
const event = new CustomEvent('DejaTilesModelEvent', { cancelable: false }) as IDejaTilesModelEvent;
event.tiles = this.layoutProvider.tiles.map((t) => t.toTileModel());
this.modelChanged.emit(event);
}
public onFocus() {
this.hasFocus = true;
}
public onBlur() {
this.hasFocus = false;
}
}
|