Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
* limitations under the License.
*/

import { Component, Input, Pipe, PipeTransform } from '@angular/core';
import {
ChangeDetectionStrategy,
Component,
Input,
Pipe,
PipeTransform,
} from '@angular/core';
import { AbstractControl, FormArray, FormGroup } from '@angular/forms';
import { List } from 'immutable';

Expand All @@ -23,7 +29,6 @@ import { Task } from 'app/models/task/task.model';

@Pipe({
name: 'getTaskOptions',
pure: false,
standalone: false,
})
export class TaskOptionsPipe implements PipeTransform {
Expand All @@ -40,6 +45,7 @@ export class TaskOptionsPipe implements PipeTransform {
templateUrl: './task-condition-form.component.html',
styleUrls: ['./task-condition-form.component.scss'],
standalone: false,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TaskConditionFormComponent {
@Input() formGroup!: FormGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

<div
class="task-container"
*ngFor="let formGroup of formArray.controls; let i = index;"
*ngFor="let formGroup of formArray.controls; let i = index; trackBy: trackByTaskId"
[ngClass]="{'loi-task-container': formGroup.get('addLoiTask').value || formGroup.get('condition')}"
(click)="onTaskContainerClick(i, $event)"
>
<div *ngIf="formGroup.get('addLoiTask').value" class="loi-task-label">
<ng-container i18n="@@app.texts.whenAdding"
Expand All @@ -40,6 +41,8 @@
[formGroup]="formGroup"
[formGroupIndex]="i"
[isCreationMode]="isCreationMode()"
[expanded]="expandedIndex() === i"
(expand)="expandedIndex.set(i)"
(delete)="onTaskDelete(i)"
(duplicate)="onTaskDuplicate(i)"
(toggleCondition)="onTaskConditionToggle(i)"
Expand Down
63 changes: 43 additions & 20 deletions web/src/app/components/shared/task-editor/task-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
*/

import { CdkDragDrop } from '@angular/cdk/drag-drop';
import { Component, EventEmitter, Input, Output, input } from '@angular/core';
import {
Component,
EventEmitter,
HostListener,
Output,
effect,
input,
signal,
} from '@angular/core';
import {
AbstractControl,
FormArray,
Expand Down Expand Up @@ -92,7 +100,7 @@ export const taskTypeToGroup = new Map([
export class TaskEditorComponent {
formGroup!: FormGroup;

@Input() tasks?: List<Task>;
tasks = input<List<Task>>();
isCreationMode = input<boolean>(false);

@Output() onValidationChanges: EventEmitter<boolean> =
Expand All @@ -106,36 +114,47 @@ export class TaskEditorComponent {
TaskGroup.CAPTURE_LOCATION,
];

multipleChoiceTasks = List<Task>();

expandedIndex = signal<number | null>(null);

constructor(
private dataStoreService: DataStoreService,
private dialogService: DialogService,
private taskService: TaskService,
private formBuilder: FormBuilder
) {}

multipleChoiceTasks = List<Task>();
) {
effect(() => {
this.formGroup = this.formBuilder.group({
tasks: this.formBuilder.array(
this.tasks()?.toArray().map((task: Task) => this.toControl(task)) ||
[],
Validators.required
),
}) as FormGroup;

this.formGroup.statusChanges.subscribe(_ => {
this.onValidationChanges.emit(this.formGroup?.valid);
});

ngOnChanges(): void {
this.formGroup = this.formBuilder.group({
tasks: this.formBuilder.array(
this.tasks?.toArray().map((task: Task) => this.toControl(task)) || [],
Validators.required
),
}) as FormGroup;
this.formGroup.valueChanges.subscribe(_ => {
this.multipleChoiceTasks = this.toTasks();
this.onValueChanges.emit(this.formGroup?.valid);
});

this.formGroup.statusChanges.subscribe(_ => {
this.onValidationChanges.emit(this.formGroup?.valid);
});

this.formGroup.valueChanges.subscribe(_ => {
this.multipleChoiceTasks = this.toTasks();

this.onValueChanges.emit(this.formGroup?.valid);
});
}

this.onValidationChanges.emit(this.formGroup?.valid);
@HostListener('document:click')
onDocumentClick(): void {
this.expandedIndex.set(null);
}

this.multipleChoiceTasks = this.toTasks();
onTaskContainerClick(index: number, event: MouseEvent): void {
event.stopPropagation();
this.expandedIndex.set(index);
}

get formArray() {
Expand Down Expand Up @@ -306,4 +325,8 @@ export class TaskEditorComponent {
toTasks(): List<Task> {
return List(this.formArray.controls.map((_, i: number) => this.toTask(i)));
}

trackByTaskId(_index: number, control: AbstractControl): string {
return control.get('id')?.value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
-->

<form [formGroup]="formGroup" class="task-input" [ngClass]="{'selected': expanded}" cdkDrag>
<form [formGroup]="formGroup" class="task-input" [ngClass]="{'selected': expanded()}" cdkDrag>
<mat-icon class="drag-icon" cdkDragHandle>drag_handle</mat-icon>

<div class="task-toolbar">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import '@angular/localize/init';

import { CdkDragDrop } from '@angular/cdk/drag-drop';
import {
ChangeDetectionStrategy,
Component,
EventEmitter,
HostListener,
Input,
Output,
input,
Expand Down Expand Up @@ -156,21 +156,19 @@ const AddLoiTaskGroups = List([TaskGroup.DROP_PIN, TaskGroup.DRAW_AREA]);
templateUrl: './task-form.component.html',
styleUrls: ['./task-form.component.scss'],
standalone: false,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TaskFormComponent {
@Input() formGroup!: FormGroup;
@Input() formGroupIndex!: number;
isCreationMode = input<boolean>(false);

expanded = input<boolean>(false);

@Output() delete = new EventEmitter();
@Output() duplicate = new EventEmitter();
@Output() toggleCondition = new EventEmitter();

/** When expanded, options and actions below the fold are visible to the user. */
expanded: boolean;

/** Set to true when question gets focus, false when it loses focus. */
selected: boolean;
@Output() expand = new EventEmitter<void>();

addLoiTask?: boolean;

Expand Down Expand Up @@ -200,21 +198,10 @@ export class TaskFormComponent {
public dialog: MatDialog,
private dataStoreService: DataStoreService,
private formBuilder: FormBuilder
) {
this.expanded = false;
this.selected = false;
}
) {}

@HostListener('click')
onTaskFocus() {
this.expanded = true;
this.selected = true;
}

@HostListener('document:click')
onTaskBlur() {
if (!this.selected) this.expanded = false;
this.selected = false;
this.expand.emit();
}

ngOnInit(): void {
Expand Down
Loading