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

322 lines
9.3 KiB
TypeScript
Raw Normal View History

import { ChangeDetectorRef, Component, Inject, Input, OnInit } from '@angular/core';
2020-10-29 18:43:19 +01:00
import { Poll } from '../../../core/models/poll.model';
2020-10-29 21:30:33 +01:00
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
2020-10-29 18:43:19 +01:00
import { UuidService } from '../../../core/services/uuid.service';
import { ApiService } from '../../../core/services/api.service';
2020-11-03 16:13:47 +01:00
import { ToastService } from '../../../core/services/toast.service';
2020-11-05 19:13:43 +01:00
import { PollService } from '../../../core/services/poll.service';
import { DateUtilities } from '../../old-stuff/config/DateUtilities';
import { DOCUMENT } from '@angular/common';
import { DateChoice, otherDefaultDates } from '../../old-stuff/config/defaultConfigs';
2020-11-09 11:42:54 +01:00
import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
2020-10-29 18:43:19 +01:00
@Component({
selector: 'app-admin-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.scss'],
})
export class FormComponent implements OnInit {
@Input()
public poll?: Poll;
2020-11-03 15:44:08 +01:00
public form: FormGroup;
2020-10-29 21:30:33 +01:00
2020-10-29 18:43:19 +01:00
public urlPrefix: string = window.location.origin + '/participation/';
2020-11-05 19:13:43 +01:00
public advancedDisplayEnabled = false;
public showDateInterval = true;
public allowSeveralHours = true;
startDateInterval: string;
endDateInterval: string;
intervalDays: any;
intervalDaysDefault = 7;
dateList: any = otherDefaultDates; // sets of days as strings, config to set identical time for days in a special days poll
timeList: DateChoice[] = otherDefaultDates; // ranges of time expressed as strings
2020-10-29 18:43:19 +01:00
2020-11-03 16:13:47 +01:00
constructor(
private fb: FormBuilder,
private cd: ChangeDetectorRef,
2020-11-03 16:13:47 +01:00
private uuidService: UuidService,
private toastService: ToastService,
2020-11-05 19:13:43 +01:00
private pollService: PollService,
public dateUtilities: DateUtilities,
private apiService: ApiService,
@Inject(DOCUMENT) private document: any
2020-11-03 16:13:47 +01:00
) {}
2020-11-09 11:42:54 +01:00
drop(event: CdkDragDrop<string[]>) {
// moveItemInArray(this.choices, event.previousIndex, event.currentIndex);
}
get choices(): FormArray {
return this.form.get('choices') as FormArray;
}
2020-10-29 18:43:19 +01:00
ngOnInit(): void {
2020-11-03 15:44:08 +01:00
this.initFormDefault();
2020-10-29 18:43:19 +01:00
}
public createPoll(): void {
console.log('this.form', this.form);
2020-11-09 12:44:24 +01:00
this.apiService.createPoll(this.pollService.newPollFromForm(this.form));
// if (this.form.valid) {
// console.log('Le sondage est correctement rempli, prêt à enregistrer.');
// const newpoll = this.pollService.newPollFromForm(this.form);
// // TODO : save the poll
// this.apiService.createPoll(newpoll);
// } else {
// this.toastService.display('invalid form');
// }
2020-10-29 18:43:19 +01:00
}
2020-11-03 15:44:08 +01:00
2020-11-06 12:04:38 +01:00
public updateSlug(): void {
const newValueFormatted = 'TODO';
2020-11-03 15:44:08 +01:00
this.form.patchValue({ slug: newValueFormatted });
2020-10-29 18:43:19 +01:00
}
2020-10-31 17:36:54 +01:00
2020-11-06 12:04:38 +01:00
addChoice(optionalLabel = ''): void {
const newControlGroup = this.fb.group({
2020-11-05 19:13:43 +01:00
label: this.fb.control('', [Validators.required]),
imageUrl: ['', [Validators.required]],
});
if (optionalLabel) {
newControlGroup.patchValue({
label: optionalLabel,
2020-11-09 12:44:24 +01:00
imageUrl: 'mon url',
2020-11-05 19:13:43 +01:00
});
}
this.choices.push(newControlGroup);
this.cd.detectChanges();
console.log('this.choices.length', this.choices.length);
this.focusOnChoice(this.choices.length - 1);
}
focusOnChoice(index): void {
const selector = '#choice_label_' + index;
const elem = this.document.querySelector(selector);
if (elem) {
elem.focus();
}
2020-11-03 15:44:08 +01:00
}
2020-11-06 12:04:38 +01:00
deleteChoiceField(index: number): void {
2020-11-03 15:44:08 +01:00
if (this.choices.length !== 1) {
this.choices.removeAt(index);
}
}
2020-11-06 12:04:38 +01:00
reinitChoices(): void {
2020-11-03 15:44:08 +01:00
this.choices.setValue([]);
}
initFormDefault(showDemoValues = true): void {
2020-11-03 15:44:08 +01:00
this.form = this.fb.group({
title: ['', [Validators.required, Validators.minLength(12)]],
creatorPseudo: ['', [Validators.required]],
creatorEmail: ['', [Validators.required]],
slug: [this.uuidService.getUUID(), [Validators.required]],
description: ['', [Validators.required]],
choices: new FormArray([]),
whoModifiesAnswers: ['', [Validators.required]],
whoCanChangeAnswers: ['', [Validators.required]],
2020-11-03 15:44:08 +01:00
isAboutDate: [true, [Validators.required]],
startDateInterval: ['', [Validators.required]],
endDateInterval: ['', [Validators.required]],
2020-11-03 15:44:08 +01:00
isProtectedByPassword: [false, [Validators.required]],
isOwnerNotifiedByEmailOnNewVote: [false, [Validators.required]],
isOwnerNotifiedByEmailOnNewComment: [false, [Validators.required]],
isMaybeAnswerAvailable: [false, [Validators.required]],
areResultsPublic: [true, [Validators.required]],
expiracyNumberOfDays: [60, [Validators.required, Validators.min(0)]],
});
console.log('this.form ', this.form);
this.setDefaultDatesForInterval();
if (showDemoValues) {
this.setDemoValues();
}
}
/**
* default interval of dates proposed is from today to 7 days more
*/
setDefaultDatesForInterval(): void {
const dateCurrent = new Date();
const dateJson = dateCurrent.toISOString();
this.startDateInterval = dateJson.substring(0, 10);
this.endDateInterval = this.dateUtilities
.addDaysToDate(this.intervalDaysDefault, dateCurrent)
.toISOString()
.substring(0, 10);
this.form.patchValue({
startDateInterval: this.startDateInterval,
endDateInterval: this.endDateInterval,
});
this.countDays();
2020-11-06 12:04:38 +01:00
}
2020-11-03 15:44:08 +01:00
/**
* add example values to the form
*/
2020-11-06 12:04:38 +01:00
setDemoValues(): void {
2020-11-05 19:13:43 +01:00
this.addChoice('orange');
this.addChoice('raisin');
this.addChoice('abricot');
2020-11-03 16:13:47 +01:00
2020-11-03 15:44:08 +01:00
this.form.patchValue({
title: 'mon titre',
description: 'répondez SVP <3 ! *-* ',
2020-11-03 16:13:47 +01:00
slug: this.uuidService.getUUID(),
creatorPseudo: 'Chuck Norris',
2020-11-09 12:44:24 +01:00
creatorEmail: 'chucknorris@example.com',
2020-11-03 16:13:47 +01:00
isAboutDate: true,
whoModifiesAnswers: 'everybody',
whoCanChangeAnswers: 'everybody',
2020-11-03 16:13:47 +01:00
isProtectedByPassword: false,
isOwnerNotifiedByEmailOnNewVote: false,
isOwnerNotifiedByEmailOnNewComment: false,
isMaybeAnswerAvailable: false,
areResultsPublic: true,
expiracyNumberOfDays: 60,
2020-11-03 15:44:08 +01:00
});
2020-11-09 11:42:54 +01:00
this.automaticSlug();
2020-11-03 16:13:47 +01:00
}
2020-11-06 12:04:38 +01:00
askInitFormDefault(): void {
this.initFormDefault(false);
2020-11-06 12:04:38 +01:00
this.toastService.display('formulaire réinitialisé');
2020-10-31 17:36:54 +01:00
}
countDays(): void {
this.intervalDays = this.dateUtilities.countDays(
this.dateUtilities.parseInputDateToDateObject(this.startDateInterval),
this.dateUtilities.parseInputDateToDateObject(this.endDateInterval)
);
this.cd.detectChanges();
}
/**
* add all the dates between the start and end dates in the interval section
*/
addIntervalOfDates(): void {
const newIntervalArray = this.dateUtilities.getDatesInRange(
this.dateUtilities.parseInputDateToDateObject(this.startDateInterval),
this.dateUtilities.parseInputDateToDateObject(this.endDateInterval),
1
);
const converted = [];
newIntervalArray.forEach((element) => {
converted.push({
literal: element.literal,
date_object: element.date_object,
timeList: [],
});
});
this.dateList = [...new Set(converted)];
// add only dates that are not already present with a Set of unique items
console.log('this.dateList', this.dateList);
this.showDateInterval = false;
this.form.patchValue({ choices: this.dateList });
// this.dateList.forEach(elem=>{
// const newControlGroup = this.fb.group({
// label: this.fb.control('', [Validators.required]),
// imageUrl: ['', [Validators.required]],
// });
//
// this.choices.push(newControlGroup);
// })
this.toastService.display(`les dates ont été ajoutées aux réponses possibles.`);
}
/**
* handle keyboard shortcuts
* @param $event
* @param choice_number
*/
keyOnChoice($event: KeyboardEvent, choice_number: number): void {
$event.preventDefault();
console.log('this.choices.length', this.choices.length);
console.log('choice_number', choice_number);
const lastChoice = this.choices.length - 1 === choice_number;
// 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);
if ($event.key == 'ArrowUp' && choice_number > 0) {
this.focusOnChoice(choice_number - 1);
}
if ($event.key == 'ArrowDown') {
// 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();
}
}
}
/**
* change time spans
*/
addTime() {
this.timeList.push({
literal: '',
timeList: [],
date_object: new Date(),
});
}
removeAllTimes() {
this.timeList = [];
}
resetTimes() {
this.timeList = otherDefaultDates;
}
/**
* add a time period to a specific date choice,
* focus on the new input
* @param config
* @param id
*/
addTimeToDate(config: any, id: number) {
2020-11-07 20:12:20 +01:00
this.timeList.push({
literal: '',
timeList: [],
date_object: new Date(),
});
const selector = '[ng-reflect-choice_label="dateTime_' + id + '_Choices_' + (this.timeList.length - 1) + '"]';
this.cd.detectChanges();
const elem = this.document.querySelector(selector);
if (elem) {
elem.focus();
}
}
/**
* set the poll slug from other data of the poll
*/
automaticSlug() {
this.poll.slug = this.pollService.makeSlug(this.poll);
}
2020-10-29 18:43:19 +01:00
}