[CST-3782] Drag drop restored

This commit is contained in:
Alessandro Martelli
2021-02-17 17:57:29 +01:00
parent 151b02aeec
commit d208cf16fa
4 changed files with 49 additions and 38 deletions

View File

@@ -3,11 +3,17 @@
[formArrayName]="model.id" [formArrayName]="model.id"
[ngClass]="getClass('element', 'control')"> [ngClass]="getClass('element', 'control')">
<div *ngFor="let groupModel of model.groups; let idx = index" role="group" <!-- Draggable Container -->
[formGroupName]="idx" [ngClass]="[getClass('element', 'group'), getClass('grid', 'group')]"> <div cdkDropList cdkDropListLockAxis="y" (cdkDropListDropped)="moveSelection($event)">
<!-- Draggable Items -->
<div *ngFor="let groupModel of model.groups; let idx = index"
role="group"
[formGroupName]="idx"
[ngClass]="[getClass('element', 'group'), getClass('grid', 'group')]"
cdkDrag cdkDragHandle>
<!-- Item content -->
<i class="drag-icon fas fa-grip-vertical fa-fw"></i>
<ng-container *ngTemplateOutlet="startTemplate?.templateRef; context: groupModel"></ng-container> <ng-container *ngTemplateOutlet="startTemplate?.templateRef; context: groupModel"></ng-container>
<ds-dynamic-form-control-container *ngFor="let _model of groupModel.group" <ds-dynamic-form-control-container *ngFor="let _model of groupModel.group"
[bindId]="false" [bindId]="false"
[formGroup]="group" [formGroup]="group"
@@ -24,8 +30,10 @@
(ngbEvent)="onCustomEvent($event, null, true)"></ds-dynamic-form-control-container> (ngbEvent)="onCustomEvent($event, null, true)"></ds-dynamic-form-control-container>
<ng-container *ngTemplateOutlet="endTemplate?.templateRef; context: groupModel"></ng-container> <ng-container *ngTemplateOutlet="endTemplate?.templateRef; context: groupModel"></ng-container>
</div> </div>
</div>
</div> </div>

View File

@@ -44,9 +44,15 @@ export class DsDynamicFormArrayComponent extends DynamicFormArrayComponent {
} }
moveSelection(event: CdkDragDrop<Relationship>) { moveSelection(event: CdkDragDrop<Relationship>) {
// prevent propagating events generated releasing on the same position
if (event.previousIndex === event.currentIndex) {
return;
}
this.model.moveGroup(event.previousIndex, event.currentIndex - event.previousIndex); this.model.moveGroup(event.previousIndex, event.currentIndex - event.previousIndex);
const prevIndex = event.previousIndex - 1; const prevIndex = event.previousIndex;
const index = event.currentIndex - 1; const index = event.currentIndex;
if (hasValue(this.model.groups[index]) && hasValue((this.control as any).controls[index])) { if (hasValue(this.model.groups[index]) && hasValue((this.control as any).controls[index])) {
const $event = { const $event = {

View File

@@ -418,7 +418,7 @@ describe('FormComponent test suite', () => {
})); }));
it('should dispatch FormChangeAction when an item has been removed from an array', inject([FormBuilderService], (service: FormBuilderService) => { it('should dispatch FormChangeAction when an item has been removed from an array', inject([FormBuilderService], (service: FormBuilderService) => {
formComp.removeItem(new Event('click'), formComp.formModel[0] as DynamicFormArrayModel, 1); formComp.removeItem(new Event('click'), formComp.formModel[0] as DynamicFormArrayModel, 0);
expect(store.dispatch).toHaveBeenCalledWith(new FormChangeAction('testFormArray', service.getValueFromModel(formComp.formModel))); expect(store.dispatch).toHaveBeenCalledWith(new FormChangeAction('testFormArray', service.getValueFromModel(formComp.formModel)));
})); }));
@@ -426,7 +426,7 @@ describe('FormComponent test suite', () => {
it('should emit removeArrayItem Event when an item has been removed from an array', inject([FormBuilderService], (service: FormBuilderService) => { it('should emit removeArrayItem Event when an item has been removed from an array', inject([FormBuilderService], (service: FormBuilderService) => {
spyOn(formComp.removeArrayItem, 'emit'); spyOn(formComp.removeArrayItem, 'emit');
formComp.removeItem(new Event('click'), formComp.formModel[0] as DynamicFormArrayModel, 1); formComp.removeItem(new Event('click'), formComp.formModel[0] as DynamicFormArrayModel, 0);
expect(formComp.removeArrayItem.emit).toHaveBeenCalled(); expect(formComp.removeArrayItem.emit).toHaveBeenCalled();
})); }));

View File

@@ -359,8 +359,6 @@ export class SubmissionSectionformComponent extends SectionModelComponent {
* the [[DynamicFormControlEvent]] emitted * the [[DynamicFormControlEvent]] emitted
*/ */
onChange(event: DynamicFormControlEvent): void { onChange(event: DynamicFormControlEvent): void {
// don't handle change events for things with an index < 0, those are template rows.
if (hasNoValue(event.context) || hasNoValue(event.context.index) || event.context.index >= 0) {
this.formOperationsService.dispatchOperationsFromEvent( this.formOperationsService.dispatchOperationsFromEvent(
this.pathCombiner, this.pathCombiner,
event, event,
@@ -373,7 +371,6 @@ export class SubmissionSectionformComponent extends SectionModelComponent {
this.submissionService.dispatchSave(this.submissionId); this.submissionService.dispatchSave(this.submissionId);
} }
} }
}
/** /**
* Method called when a form dfFocus event is fired. * Method called when a form dfFocus event is fired.