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

170 lines
4.5 KiB
TypeScript
Raw Normal View History

import { ChangeDetectorRef, Component, Inject, Input } from '@angular/core';
import { moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
2021-05-17 15:25:22 +02:00
import { DateChoice } from '../../../../../../../../mocks/old-stuff/config/defaultConfigs';
import { DOCUMENT } from '@angular/common';
import { FormGroup } from '@angular/forms';
2021-05-17 16:15:54 +02:00
import { ToastService } from '../../../../../../core/services/toast.service';
import { StorageService } from '../../../../../../core/services/storage.service';
import { MatDialog } from '@angular/material/dialog';
import { ShortcutsHelpComponent } from '../../../../../shared/components/ui/shortcuts-help/shortcuts-help.component';
2021-05-17 15:25:22 +02:00
@Component({
selector: 'app-day-list',
templateUrl: './day-list.component.html',
styleUrls: ['./day-list.component.scss'],
entryComponents: [ShortcutsHelpComponent],
2021-05-17 15:25:22 +02:00
})
2021-05-17 16:15:54 +02:00
export class DayListComponent {
2021-05-17 15:25:22 +02:00
@Input()
2021-05-17 16:15:54 +02:00
form: FormGroup;
@Input()
public dateList: Array<any> = [];
2021-05-17 15:25:22 +02:00
@Input()
public hasSeveralHours: boolean;
timeList: any;
2021-05-17 16:15:54 +02:00
display = false;
2021-05-17 15:25:22 +02:00
2021-05-17 16:15:54 +02:00
constructor(
public dialog: MatDialog,
2021-05-17 16:15:54 +02:00
private toastService: ToastService,
private cd: ChangeDetectorRef,
2021-05-17 16:15:54 +02:00
@Inject(DOCUMENT) private document: any,
private storageService: StorageService
) {
this.setDemoTextChoices();
2021-05-17 16:15:54 +02:00
}
2021-05-17 15:25:22 +02:00
2021-05-17 16:15:54 +02:00
reinitChoices(): void {
this.dateList = [];
2021-05-17 15:25:22 +02:00
}
setDemoTextChoices(): void {
2021-05-17 16:15:54 +02:00
this.addChoice('orange');
this.addChoice('raisin');
this.addChoice('abricot');
}
2021-05-17 15:25:22 +02:00
dropDayItem(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-17 16:15:54 +02:00
2021-05-17 15:25:22 +02:00
/**
* add a time period to a specific date choice,
* focus on the new input
2021-05-18 12:45:51 +02:00
* @param choice DateChoice
* @param id number
2021-05-17 15:25:22 +02:00
*/
2021-05-18 12:45:51 +02:00
addTimeToDate(choice: DateChoice, id: number) {
2021-05-18 22:51:06 +02:00
if (!choice.timeSlices) {
choice.timeSlices = [];
2021-05-18 15:19:11 +02:00
}
2021-05-18 22:51:06 +02:00
choice.timeSlices.push({
2021-05-17 15:25:22 +02:00
literal: '',
});
const selector =
'[ng-reflect-choice_label="dateTime_' + id + '_ dateList_' + (choice.timeSlices.length - 1) + '"]';
this.cd.detectChanges();
2021-05-17 15:25:22 +02:00
const elem = this.document.querySelector(selector);
if (elem) {
elem.focus();
}
}
2021-05-17 16:15:54 +02:00
2021-05-17 15:25:22 +02:00
/**
* handle keyboard shortcuts
* @param $event
* @param choice_number
*/
keyOnChoice($event: KeyboardEvent, choice_number: number): void {
$event.preventDefault();
2021-05-18 12:12:08 +02:00
console.log('this. dateChoices.length', this.dateList.length);
2021-05-17 15:25:22 +02:00
console.log('choice_number', choice_number);
2021-05-17 16:15:54 +02:00
const lastChoice = this.dateList.length - 1 === choice_number;
2021-05-17 15:25:22 +02:00
// TODO handle shortcuts
// reset field with Ctrl + D
// add a field with Ctrl + N
// go to previous choice with arrow up
// go to next choice with arrow down
console.log('$event', $event);
2021-05-17 16:15:54 +02:00
if ($event.ctrlKey && $event.key == 'ArrowUp' && choice_number > 0) {
2021-05-17 15:25:22 +02:00
this.focusOnChoice(choice_number - 1);
}
2021-05-17 16:15:54 +02:00
if ($event.ctrlKey && ($event.key == 'ArrowDown' || $event.key == 'ArrowRight')) {
2021-05-17 15:25:22 +02:00
// add a field if we are on the last choice
if (lastChoice) {
this.addChoice();
this.toastService.display('choix ajouté par raccourci "flèche bas"');
} else {
this.focusOnChoice(choice_number + 1);
}
}
if ($event.ctrlKey && $event.key == 'Backspace') {
this.deleteChoiceField(choice_number);
this.toastService.display('choix supprimé par raccourci "Ctrl + retour"');
// this.cd.detectChanges();
this.focusOnChoice(Math.min(choice_number - 1, 0));
}
if ($event.ctrlKey && $event.key == 'Enter') {
// go to other fields
const elem = this.document.querySelector('#creatorEmail');
if (elem) {
elem.focus();
}
}
}
2021-05-17 16:15:54 +02:00
addChoice(optionalLabel = ''): void {
2021-05-18 12:12:08 +02:00
this.storageService.dateChoices.push({
2021-05-17 16:15:54 +02:00
literal: '',
2021-05-18 22:51:06 +02:00
timeSlices: [],
2021-05-17 16:15:54 +02:00
date_object: new Date(),
});
// this.cd.detectChanges();
2021-05-18 12:12:08 +02:00
this.focusOnChoice(this.storageService.dateChoices.length - 1);
2021-05-17 16:15:54 +02: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.dateList.length !== 1) {
this.dateList.splice(index, 1);
}
}
openKeyboardShortcutsModal() {
this.display = true;
}
isWeekendDay(date_object: Date) {
2021-05-18 12:45:51 +02:00
if (date_object) {
const day = date_object.getDay();
return day === 6 || day === 0;
}
return false;
}
openSimple() {
this.display = !this.display;
}
2021-05-17 15:25:22 +02:00
}