Newer
Older
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
import { Injectable } from '@angular/core';
import { DropListComponent } from 'common/components/input-elements/drop-list/drop-list.component';
import { DragNDropValueObject } from 'common/models/elements/label-interfaces';
import { DragOperation } from 'common/components/input-elements/drop-list/drag-operation';
@Injectable({
providedIn: 'root'
})
export class DragOperatorService {
dropLists: { [id: string]: DropListComponent } = {};
dragOperation: DragOperation | undefined;
registerComponent(comp: DropListComponent): void {
this.dropLists[comp.elementModel.id] = comp;
}
startDrag(sourceElement: HTMLElement, sourceListComponent: DropListComponent, sourceIndex: number,
item: DragNDropValueObject, dragType: 'mouse' | 'touch') {
this.dragOperation =
new DragOperation(sourceElement, sourceListComponent, sourceIndex, item, dragType, this.dropLists);
this.initDrag();
}
initDrag(): void {
if (!this.dragOperation) throw new Error('dragOP undefined');
this.dragOperation.sourceElement?.classList.add('show-as-placeholder');
this.dragOperation.sourceElement.style.pointerEvents = 'none';
this.dragOperation.sourceComponent.isHovered = true;
if (this.dragOperation.sourceComponent.elementModel.highlightReceivingDropList) {
this.dragOperation.eligibleTargetListsIDs.forEach(listID => {
this.dropLists[listID].isHighlighted = true; // TODO kapseln
this.dropLists[listID].cdr.detectChanges();
});
}
if (this.dragOperation.dragType === 'mouse') this.setListItemListeners();
}
endDrag(): void {
if (!this.dragOperation) throw new Error('dragOP undefined');
this.dragOperation.sourceElement.classList.remove('show-as-placeholder');
this.dragOperation.sourceElement.style.pointerEvents = 'auto';
this.dragOperation.placeholderElement?.classList.remove('show-as-placeholder');
this.resetListEffects();
if (this.dragOperation.dragType === 'mouse') {
Object.values(this.dropLists).forEach(listComp => {
listComp.stopListenForHover();
});
}
}
private resetListEffects(): void {
if (!this.dragOperation) throw new Error('dragOP undefined');
[...this.dragOperation.eligibleTargetListsIDs, this.dragOperation.sourceComponent.elementModel.id]
.forEach(listID => {
this.dropLists[listID].isHovered = false;
this.dropLists[listID].isHighlighted = false;
this.dropLists[listID].cdr.detectChanges();
});
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
}
setTargetList(listId: string): void {
if (!this.dragOperation) throw new Error('dragOP undefined');
const targetListComp = this.dropLists[listId];
this.dragOperation.targetComponent = targetListComp;
if (targetListComp.elementModel.isSortList) {
if (this.dragOperation.sourceComponent !== targetListComp) {
this.addSortPlaceholder();
} else {
this.dragOperation.sortingPlaceholderIndex = this.dragOperation.sourceIndex;
}
}
}
addSortPlaceholder(): void {
if (!this.dragOperation?.targetComponent) throw new Error('dragOP undefined');
this.dragOperation.isForeignPlaceholderActive = true;
this.dragOperation.sortingPlaceholderIndex =
this.dragOperation.targetComponent.viewModel.push(this.dragOperation.draggedItem) - 1;
this.dragOperation.targetComponent.cdr.detectChanges();
this.dragOperation.placeholderElement =
this.dragOperation.targetComponent.droplistItems?.toArray()[this.dragOperation.sortingPlaceholderIndex]
.nativeElement as HTMLElement;
this.dragOperation.placeholderElement.classList.add('show-as-placeholder');
}
unSetTargetList(): void {
if (!this.dragOperation) throw new Error('dragOP undefined');
this.dragOperation.targetComponent = undefined;
this.dragOperation.sortingPlaceholderIndex = undefined;
}
private setListItemListeners(): void {
this.dragOperation?.eligibleTargetListsIDs.forEach(listID => {
this.dropLists[listID].listenForHover();
});
}
positionSortPlaceholder(targetIndex: number): void {
if (!this.dragOperation) throw new Error('dragOP undefined');
const list = this.dragOperation.targetComponent!.viewModel;
const sourceIndex = this.dragOperation.sortingPlaceholderIndex!;
const item = list.splice(sourceIndex, 1)[0];
list.splice(targetIndex, 0, item);
this.dragOperation.sortingPlaceholderIndex = targetIndex;
}
handleDrop(): void {
if (!this.dragOperation) throw new Error('dragOP undefined');
if (this.dragOperation.sourceComponent && this.dragOperation.targetComponent &&
DragOperatorService.isDropAllowed(this.dragOperation.draggedItem,
this.dragOperation.sourceComponent,
this.dragOperation.targetComponent,
this.dropLists)) {
if (this.dragOperation.sourceComponent === this.dragOperation.targetComponent) {
if (!this.dragOperation.targetComponent.elementModel.isSortList) return;
const item =
this.dragOperation.targetComponent!.elementFormControl.value.splice(this.dragOperation.sourceIndex, 1)[0];
this.dragOperation.targetComponent!.elementFormControl.value
.splice(this.dragOperation.sortingPlaceholderIndex, 0, item);
} else {
this.moveItem(this.dragOperation.draggedItem,
this.dragOperation.sourceComponent,
this.dragOperation.sourceIndex,
this.dragOperation.targetComponent);
}
this.dragOperation.sourceComponent?.updateFormvalue();
this.dragOperation.targetComponent?.updateFormvalue();
this.dragOperation.sourceComponent?.refreshViewModel();
this.dragOperation.targetComponent?.refreshViewModel();
}
}
moveItem(item: DragNDropValueObject | undefined,
sourceList: DropListComponent,
sourceListIndex: number,
targetList: DropListComponent): void {
DragOperatorService.removeItem(sourceList, sourceListIndex);
this.addItem(item as DragNDropValueObject, targetList);
}
static removeItem(list: DropListComponent, index: number): DragNDropValueObject {
return list.elementModel.copyOnDrop ?
list.elementFormControl.value[index] :
list.elementFormControl.value.splice(index, 1)[0];
}
addItem(item: DragNDropValueObject, targetList: DropListComponent): void {
if (DragOperatorService.isPutBack(item, targetList)) {
return;
}
if (DragOperatorService.isReplace(targetList, this.dropLists)) {
const originList = this.dropLists[targetList.elementFormControl.value[0].originListID];
this.moveItem(targetList.elementFormControl.value[0], targetList, 0, originList);
originList.refreshViewModel();
originList.cdr.detectChanges();
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
}
// TODO haut logisch noch nicht ganz hin mit den festen Indize
const targetIndex = this.dragOperation?.sortingPlaceholderIndex !== undefined ?
this.dragOperation?.sortingPlaceholderIndex :
targetList.elementFormControl.value.length;
targetList.elementFormControl.value.splice(targetIndex, 0, item);
}
hoverToggle = true;
checkHovered(x: number, y: number): void {
const el = document.elementFromPoint(x, y);
const sourceElement: HTMLElement | null = (el as HTMLElement).closest('.drop-list');
const targetListID = sourceElement?.id;
if (targetListID &&
this.dragOperation?.eligibleTargetListsIDs.includes(this.dropLists[targetListID].elementModel.id)) {
if (!this.hoverToggle) {
this.dropLists[targetListID].dragEnter();
}
this.hoverToggle = true;
this.checkHoverSort(el);
} else {
if (this.hoverToggle) {
this.dragOperation?.targetComponent?.dragLeave();
}
this.hoverToggle = false;
}
}
private checkHoverSort(el: Element | null): void {
if (this.dragOperation?.targetComponent?.elementModel.isSortList &&
el?.getAttribute('data-aspect-draggable')) {
const targetIndex = Array.from((el.parentNode as HTMLElement).children).indexOf(el);
this.positionSortPlaceholder(targetIndex);
}
}
static isDropAllowed(draggedItem: DragNDropValueObject | undefined,
sourceList: DropListComponent,
targetList: DropListComponent,
allLists: { [id: string]: DropListComponent }): boolean {
return (sourceList.elementModel.id === targetList.elementModel.id && sourceList.elementModel.isSortList) ||
(DragOperatorService.checkConnected(sourceList, targetList) &&
DragOperatorService.checkOnlyOneItem(targetList, allLists) &&
DragOperatorService.checkAddForeignItemToCopyList(draggedItem, targetList));
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
}
/* Drop is only allowed in connected Lists AND THE SAME LIST. */
private static checkConnected(sourceList: DropListComponent, targetList: DropListComponent): boolean {
return sourceList.elementModel.connectedTo.includes(targetList.elementModel.id);
}
/* Return false, when drop is not allowed */
private static checkOnlyOneItem(targetList: DropListComponent,
allLists: { [id: string]: DropListComponent }): boolean {
return !(targetList.elementModel.onlyOneItem &&
targetList.elementFormControl.value.length > 0 &&
!DragOperatorService.isReplace(targetList, allLists));
}
/* Don't allow moving item into copy list that not originate from there. */
private static checkAddForeignItemToCopyList(draggedItem: DragNDropValueObject | undefined,
targetList: DropListComponent): boolean {
return !(targetList.elementModel.copyOnDrop && draggedItem?.originListID !== targetList.elementModel.id);
}
static isPutBack(draggedItem: DragNDropValueObject | undefined, targetList: DropListComponent): boolean {
return targetList.elementModel.copyOnDrop && draggedItem?.originListID === targetList.elementModel.id;
}
static isReplace(targetList: DropListComponent,
allLists: { [id: string]: DropListComponent }): boolean {
return targetList.elementModel.onlyOneItem &&
targetList.elementFormControl.value.length === 1 &&
targetList.elementModel.allowReplacement &&
DragOperatorService.isDropAllowed(
targetList.elementFormControl.value[0],
targetList,
allLists[targetList.elementFormControl.value[0].originListID],
allLists
);
}
}