2019-11-19 14:42:59 +01:00
|
|
|
import {ChangeDetectorRef, Component, Inject, OnInit} from '@angular/core';
|
2019-12-03 17:20:57 +01:00
|
|
|
import {ConfigService} from '../../services/config.service';
|
2019-08-10 16:57:36 +02:00
|
|
|
import {BaseComponent} from '../base-page/base.component';
|
2019-11-19 10:36:53 +01:00
|
|
|
import {DOCUMENT} from '@angular/common';
|
2020-01-23 17:36:56 +01:00
|
|
|
import {MessageService} from "primeng/api";
|
2020-02-19 12:26:44 +01:00
|
|
|
import {otherDefaultDates} from "../../config/defaultConfigs";
|
2020-02-05 11:13:36 +01:00
|
|
|
import {DateUtilities} from "../../config/DateUtilities";
|
2019-08-10 16:20:59 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'framadate-dates',
|
|
|
|
templateUrl: './dates.component.html',
|
|
|
|
styleUrls: ['./dates.component.scss']
|
|
|
|
})
|
2019-11-19 14:42:59 +01:00
|
|
|
export class DatesComponent extends BaseComponent implements OnInit {
|
2020-04-11 16:06:56 +02:00
|
|
|
showDateInterval: boolean = false;
|
2019-11-19 14:23:51 +01:00
|
|
|
startDateInterval: any;
|
|
|
|
intervalDays: any;
|
|
|
|
intervalDaysDefault: number = 7;
|
|
|
|
endDateInterval: any;
|
|
|
|
|
2019-11-19 10:36:53 +01:00
|
|
|
constructor(public config: ConfigService,
|
|
|
|
private cd: ChangeDetectorRef,
|
2020-01-23 17:36:56 +01:00
|
|
|
private messageService: MessageService,
|
2020-02-05 11:13:36 +01:00
|
|
|
private dateUtilities: DateUtilities,
|
2019-11-19 10:36:53 +01:00
|
|
|
@Inject(DOCUMENT) private document: any
|
|
|
|
) {
|
2019-08-10 16:20:59 +02:00
|
|
|
super(config);
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:42:59 +01:00
|
|
|
countDays() {
|
2019-11-19 14:23:51 +01:00
|
|
|
// compute the number of days in the date interval
|
|
|
|
if (this.endDateInterval && this.startDateInterval) {
|
2020-02-05 11:13:36 +01:00
|
|
|
this.intervalDays = (this.dateUtilities.dayDiff(this.endDateInterval, this.startDateInterval)).toFixed(0)
|
2019-11-19 14:23:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the interval options
|
|
|
|
*/
|
2019-08-10 16:20:59 +02:00
|
|
|
ngOnInit() {
|
2019-11-19 14:23:51 +01:00
|
|
|
let dateCurrent = new Date();
|
|
|
|
const dateJson = dateCurrent.toISOString();
|
|
|
|
this.startDateInterval = dateJson.substring(0, 10);
|
2020-02-05 11:13:36 +01:00
|
|
|
this.endDateInterval = this.dateUtilities.addDaysToDate(this.intervalDaysDefault, dateCurrent).toISOString().substring(0, 10);
|
2019-08-10 16:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addDate() {
|
2020-01-24 11:15:38 +01:00
|
|
|
this.config.dateList.push({
|
|
|
|
literal: '',
|
|
|
|
date_object: new Date(),
|
2020-01-24 12:24:45 +01:00
|
|
|
timeList: []
|
|
|
|
});
|
2019-11-19 14:42:59 +01:00
|
|
|
let selector = '[ng-reflect-name="dateChoices_' + (this.config.dateList.length - 1) + '"]';
|
|
|
|
this.cd.detectChanges();
|
|
|
|
const elem = this.document.querySelector(selector);
|
|
|
|
if (elem) {
|
|
|
|
elem.focus();
|
|
|
|
}
|
2019-11-15 15:19:42 +01:00
|
|
|
}
|
|
|
|
|
2020-01-24 10:57:54 +01:00
|
|
|
/**
|
|
|
|
* change time spans
|
|
|
|
*/
|
2019-11-19 14:42:59 +01:00
|
|
|
addTime() {
|
2020-01-24 12:24:45 +01:00
|
|
|
this.config.timeList.push(
|
|
|
|
{
|
|
|
|
literal: '',
|
|
|
|
timeList: [],
|
|
|
|
date_object: new Date()
|
|
|
|
}
|
|
|
|
);
|
2019-11-15 15:19:42 +01:00
|
|
|
}
|
|
|
|
|
2020-01-24 10:57:54 +01:00
|
|
|
removeAllTimes() {
|
|
|
|
this.config.timeList = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
resetTimes() {
|
2020-02-19 12:26:44 +01:00
|
|
|
this.config.timeList = otherDefaultDates;
|
2020-01-24 10:57:54 +01:00
|
|
|
}
|
|
|
|
|
2019-11-19 14:23:51 +01:00
|
|
|
|
2019-11-15 15:19:42 +01:00
|
|
|
/**
|
|
|
|
* add a time period to a specific date choice,
|
|
|
|
* focus on the new input
|
|
|
|
* @param config
|
|
|
|
* @param id
|
|
|
|
*/
|
2019-11-19 14:42:59 +01:00
|
|
|
addTimeToDate(config: any, id: number) {
|
2019-11-15 15:19:42 +01:00
|
|
|
config.timeList.push({literal: ''});
|
|
|
|
let selector = '[ng-reflect-name="dateTime_' + id + '_Choices_' + (config.timeList.length - 1) + '"]';
|
|
|
|
this.cd.detectChanges();
|
2019-11-19 10:36:53 +01:00
|
|
|
const elem = this.document.querySelector(selector);
|
2019-11-18 11:36:34 +01:00
|
|
|
if (elem) {
|
2019-11-19 10:36:53 +01:00
|
|
|
elem.focus();
|
2019-11-15 15:19:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:23:51 +01:00
|
|
|
/**
|
|
|
|
* remove all input contents, does not reset to default
|
|
|
|
*/
|
2019-11-15 15:19:42 +01:00
|
|
|
emptyAll() {
|
|
|
|
this.config.dateList.forEach(element => {
|
|
|
|
element.literal = '';
|
2020-01-24 12:24:45 +01:00
|
|
|
element.date_object = new Date();
|
2019-11-19 10:36:53 +01:00
|
|
|
element.timeList = ['', '', ''];
|
|
|
|
});
|
|
|
|
this.config.timeList.forEach(element => {
|
|
|
|
element.literal = '';
|
2019-11-15 15:19:42 +01:00
|
|
|
});
|
2019-08-10 16:20:59 +02:00
|
|
|
}
|
2019-11-19 14:23:51 +01:00
|
|
|
|
2020-01-24 12:24:45 +01:00
|
|
|
|
2019-11-19 14:23:51 +01:00
|
|
|
/**
|
2019-11-19 14:42:59 +01:00
|
|
|
* add all the dates between the start and end dates in the interval section
|
2019-11-19 14:23:51 +01:00
|
|
|
*/
|
|
|
|
addIntervalOfDates() {
|
2020-02-05 11:13:36 +01:00
|
|
|
let newIntervalArray = this.dateUtilities.getDatesInRange(this.startDateInterval, this.endDateInterval, 1);
|
2019-11-19 14:23:51 +01:00
|
|
|
|
|
|
|
const converted = [];
|
|
|
|
newIntervalArray.forEach(element => {
|
2020-01-24 12:24:45 +01:00
|
|
|
converted.push({
|
|
|
|
literal: element.literal,
|
|
|
|
date_object: element.date_object,
|
|
|
|
timeList: []
|
|
|
|
});
|
2019-11-19 14:23:51 +01:00
|
|
|
});
|
|
|
|
this.config.dateList = [...new Set(converted)]; // add only dates that are not already present with a Set of unique items
|
2019-11-19 14:42:59 +01:00
|
|
|
this.showDateInterval = false;
|
2019-11-19 14:23:51 +01:00
|
|
|
|
2020-01-23 17:36:56 +01:00
|
|
|
this.messageService.add({
|
|
|
|
severity: 'success',
|
|
|
|
summary: 'Dates ajoutées',
|
|
|
|
detail: `les dates ont été ajoutées aux réponses possibles`
|
|
|
|
});
|
|
|
|
|
2019-11-19 14:23:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-10 16:20:59 +02:00
|
|
|
}
|