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

140 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-02-04 16:14:07 +01:00
import { ChangeDetectorRef, Component, Inject, Input, OnInit } from '@angular/core';
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { UuidService } from '../../../../core/services/uuid.service';
import { ToastService } from '../../../../core/services/toast.service';
import { PollService } from '../../../../core/services/poll.service';
2021-04-29 10:45:22 +02:00
import { DateUtilitiesService } from '../../../../core/services/date.utilities.service';
2021-02-04 16:14:07 +01:00
import { ApiService } from '../../../../core/services/api.service';
import { Router } from '@angular/router';
import { DOCUMENT } from '@angular/common';
2021-04-29 10:41:47 +02:00
import {
DateChoice,
2021-05-03 15:47:27 +02:00
defaultTimeOfDay,
2021-04-29 10:41:47 +02:00
otherDefaultDates,
TimeSlices,
} from '../../../../../../mocks/old-stuff/config/defaultConfigs';
2021-05-03 15:47:27 +02:00
import { moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
import { TranslateService } from '@ngx-translate/core';
2021-04-30 12:37:04 +02:00
import { StorageService } from '../../../../core/services/storage.service';
2021-02-04 16:14:07 +01:00
@Component({
selector: 'app-date-select',
templateUrl: './date-select.component.html',
styleUrls: ['./date-select.component.scss'],
})
export class DateSelectComponent implements OnInit {
@Input()
2021-05-17 15:25:22 +02:00
form: FormGroup;
2021-02-04 16:14:07 +01:00
2021-05-17 15:25:22 +02:00
displaySeveralHoursChoice = false;
allowSeveralHours = false;
today = new Date();
2021-02-04 16:14:07 +01:00
endDateInterval: string;
2021-05-17 15:25:22 +02:00
2021-02-04 16:14:07 +01:00
intervalDaysDefault = 7;
2021-05-17 15:25:22 +02:00
dateList: DateChoice[]; // sets of dateList as strings, config to set identical time for dateList in a special dateList poll
2021-04-30 12:37:04 +02:00
timeList: TimeSlices[]; // ranges of time expressed as strings
2021-05-17 15:25:22 +02:00
2021-02-09 12:10:10 +01:00
selectionKind = 'range';
2021-02-04 16:14:07 +01:00
constructor(
private fb: FormBuilder,
private cd: ChangeDetectorRef,
private uuidService: UuidService,
private toastService: ToastService,
private pollService: PollService,
private apiService: ApiService,
2021-04-30 12:37:04 +02:00
private storageService: StorageService,
2021-02-04 16:14:07 +01:00
private router: Router,
private translateService: TranslateService,
2021-02-04 16:14:07 +01:00
@Inject(DOCUMENT) private document: any
2021-04-30 12:37:04 +02:00
) {
this.dateList = this.storageService.dateList;
this.timeList = this.storageService.timeSlices;
}
2021-02-04 16:14:07 +01:00
get choices(): FormArray {
return this.form.get('choices') as FormArray;
}
2021-05-03 15:47:27 +02:00
get dateChoices() {
return this.form.get('dateChoices') as FormArray;
}
2021-05-17 15:25:22 +02:00
ngOnInit(): void {}
2021-02-04 16:14:07 +01:00
/**
* change time spans
*/
addTime() {
this.timeList.push({
literal: '',
});
}
removeAllTimes() {
this.timeList = [];
2021-05-17 15:25:22 +02:00
this.dateList.map((elem) => (elem.timeList = []));
2021-05-03 15:47:27 +02:00
this.toastService.display('périodes horaires vidées');
2021-02-04 16:14:07 +01:00
}
resetTimes() {
2021-05-03 15:47:27 +02:00
this.timeList = defaultTimeOfDay;
2021-05-17 15:25:22 +02:00
this.dateList.map((elem) => (elem.timeList = Object.create(defaultTimeOfDay)));
2021-05-03 15:47:27 +02:00
this.toastService.display('périodes horaires réinitialisées');
2021-02-04 16:14:07 +01:00
}
addChoice(optionalLabel = ''): void {
2021-05-17 15:25:22 +02:00
if (this.form.value.kind == 'date') {
this.storageService.dateList.push({
literal: '',
timeList: [],
date_object: new Date(),
});
} else {
const newControlGroup = this.fb.group({
label: this.fb.control('', [Validators.required]),
imageUrl: ['', [Validators.required]],
2021-02-04 16:14:07 +01:00
});
2021-05-17 15:25:22 +02:00
if (optionalLabel) {
newControlGroup.patchValue({
label: optionalLabel,
imageUrl: 'mon url',
});
}
this.dateChoices.push(newControlGroup);
2021-02-04 16:14:07 +01:00
}
// this.cd.detectChanges();
2021-05-17 15:25:22 +02:00
console.log('this.choices.length', this.storageService.dateList.length);
2021-02-04 16:14:07 +01:00
2021-05-17 15:25:22 +02:00
this.focusOnChoice(this.storageService.dateList.length - 1);
2021-02-04 16:14:07 +01:00
}
focusOnChoice(index): void {
const selector = '#choice_label_' + index;
const elem = this.document.querySelector(selector);
if (elem) {
elem.focus();
}
}
deleteChoiceField(index: number): void {
if (this.choices.length !== 1) {
this.choices.removeAt(index);
}
}
reinitChoices(): void {
this.choices.setValue([]);
}
setDemoValues(): void {
this.addChoice('orange');
this.addChoice('raisin');
this.addChoice('abricot');
}
}