funky-framadate-front/src/app/features/administration/form/date/list/time/time-list.component.ts

95 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-05-20 10:54:28 +02:00
import { ChangeDetectorRef, Component, Inject, Input, OnInit } from '@angular/core';
2021-05-17 16:15:54 +02:00
import { moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
2021-05-20 10:54:28 +02:00
import { DOCUMENT } from '@angular/common';
import { TimeSlices } from '../../../../../../core/models/dateChoice.model';
2022-03-10 11:06:18 +01:00
import { ToastService } from '../../../../../../core/services/toast.service';
import { TranslateService } from '@ngx-translate/core';
2021-05-17 15:25:22 +02:00
@Component({
selector: 'app-time-list',
templateUrl: './time-list.component.html',
styleUrls: ['./time-list.component.scss'],
})
export class TimeListComponent implements OnInit {
@Input()
public timeSlices: TimeSlices[];
2021-05-20 10:54:28 +02:00
@Input()
public prefix_choice_id: number;
2021-05-20 10:54:28 +02:00
2022-03-10 11:06:18 +01:00
constructor(
@Inject(DOCUMENT) private document: any,
private cd: ChangeDetectorRef,
private toastService: ToastService,
private translate: TranslateService
) {}
2021-05-17 15:25:22 +02:00
ngOnInit(): void {}
2021-05-17 16:15:54 +02:00
dropTimeItem(event: any): void {
// moveItemInArray(this.timeSlices, event.previousIndex, event.currentIndex);
if (event.previousContainer === event.container) {
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
} else {
transferArrayItem(
event.previousContainer.data,
event.container.data,
event.previousIndex,
event.currentIndex
);
}
}
2021-05-20 10:54:28 +02:00
removeTime(id: number) {
this.timeSlices.splice(id, 1);
this.focusOnLastField();
2022-03-10 11:06:18 +01:00
this.translate.get('success.deleted_option').subscribe((resp) => {
console.log('resp', resp);
this.toastService.display(`${resp}`);
});
2021-05-20 10:54:28 +02:00
}
focusOnFieldNumber(fieldId: number) {
console.log('focus on fieldId', fieldId);
const selector = '#choice_' + this.prefix_choice_id + '_timeChoices_' + fieldId;
const firstField = this.document.querySelector(selector);
if (firstField) {
console.log('found', firstField);
firstField.focus();
}
return firstField;
}
focusOnNextField(currentId: number) {
const fieldFound = this.focusOnFieldNumber(currentId + 1);
if (!fieldFound) {
console.log('pas trouvé de field avec id', currentId + 1);
this.createNewField();
}
}
focusOnLastField() {
this.cd.detectChanges();
if (!this.focusOnFieldNumber(this.timeSlices.length - 1)) {
console.log('no last time timeSlice found');
2021-05-20 10:54:28 +02:00
this.createNewField();
this.focusOnLastField();
}
}
removeTimeIfEmpty(timeLiteral, id) {
if (timeLiteral === '') {
this.removeTime(id);
this.focusOnFieldNumber(0);
}
}
createNewField() {
// create new field
this.timeSlices.push({
literal: '',
});
this.cd.detectChanges();
}
2021-05-17 15:25:22 +02:00
}