framadate-responsive/mocks/old-stuff/pages/dates/dates.component.ts

135 lines
3.5 KiB
TypeScript
Raw Normal View History

import { DOCUMENT } from '@angular/common';
import { ChangeDetectorRef, Component, Inject, OnInit } from '@angular/core';
2021-04-29 10:41:47 +02:00
import { ToastService } from '../../../../src/app/core/services/toast.service';
2021-04-29 10:45:22 +02:00
import { DateUtilitiesService } from '../../../../src/app/core/services/date.utilities.service';
import { otherDefaultDates } from '../../config/defaultConfigs';
import { ConfigService } from '../../services/config.service';
import { BaseComponent } from '../example/base-page/base.component';
2019-08-10 16:20:59 +02:00
@Component({
2020-04-22 12:56:18 +02:00
selector: 'app-dates',
2020-04-21 10:50:26 +02:00
templateUrl: './dates.component.html',
styleUrls: ['./dates.component.scss'],
2019-08-10 16:20:59 +02:00
})
2019-11-19 14:42:59 +01:00
export class DatesComponent extends BaseComponent implements OnInit {
2020-04-21 10:50:26 +02:00
showDateInterval = false;
startDateInterval: any;
intervalDays: any;
intervalDaysDefault = 7;
endDateInterval: any;
2019-11-19 14:23:51 +01:00
2020-04-21 10:50:26 +02:00
constructor(
public config: ConfigService,
private cd: ChangeDetectorRef,
private toastService: ToastService,
2021-04-29 10:45:22 +02:00
private dateUtilities: DateUtilitiesService,
2020-04-21 10:50:26 +02:00
@Inject(DOCUMENT) private document: any
) {
super(config);
}
2019-08-10 16:20:59 +02:00
2020-04-21 10:50:26 +02:00
countDays() {
// compute the number of days in the date interval
if (this.endDateInterval && this.startDateInterval) {
this.intervalDays = this.dateUtilities.dayDiff(this.endDateInterval, this.startDateInterval).toFixed(0);
}
}
2019-11-19 14:23:51 +01:00
2020-04-21 10:50:26 +02:00
/**
* set the interval options
*/
2020-05-01 19:10:17 +02:00
ngOnInit(): void {
2020-04-21 10:50:26 +02:00
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);
}
2019-08-10 16:20:59 +02:00
2020-04-21 10:50:26 +02:00
addDate() {
this.config.dateList.push({
literal: '',
date_object: new Date(),
timeList: [],
});
const selector = '["choice_label_' + (this.config.dateList.length - 1) + '"]';
2020-04-21 10:50:26 +02:00
this.cd.detectChanges();
const elem = this.document.querySelector(selector);
if (elem) {
elem.focus();
}
}
2019-11-15 15:19:42 +01:00
2020-04-21 10:50:26 +02:00
/**
* change time spans
*/
addTime() {
this.config.timeList.push({
literal: '',
timeList: [],
date_object: new Date(),
});
}
2019-11-15 15:19:42 +01:00
2020-04-21 10:50:26 +02:00
removeAllTimes() {
this.config.timeList = [];
}
2020-01-24 10:57:54 +01:00
2020-04-21 10:50:26 +02:00
resetTimes() {
this.config.timeList = otherDefaultDates;
}
2020-01-24 10:57:54 +01:00
2020-04-21 10:50:26 +02:00
/**
* add a time period to a specific date choice,
* focus on the new input
* @param config
* @param id
*/
addTimeToDate(config: any, id: number) {
config.timeList.push({ literal: '' });
2020-11-03 15:44:08 +01:00
const selector = '[ng-reflect-choice_label="dateTime_' + id + '_Choices_' + (config.timeList.length - 1) + '"]';
2020-04-21 10:50:26 +02:00
this.cd.detectChanges();
const elem = this.document.querySelector(selector);
if (elem) {
elem.focus();
}
}
2019-11-15 15:19:42 +01:00
2020-04-21 10:50:26 +02:00
/**
* remove all input contents, does not reset to default
*/
emptyAll() {
this.config.dateList.forEach((element) => {
element.literal = '';
element.date_object = new Date();
element.timeList = ['', '', ''];
});
this.config.timeList.forEach((element) => {
element.literal = '';
});
}
2019-11-19 14:23:51 +01:00
2020-04-21 10:50:26 +02:00
/**
* add all the dates between the start and end dates in the interval section
*/
addIntervalOfDates() {
const newIntervalArray = this.dateUtilities.getDatesInRange(this.startDateInterval, this.endDateInterval, 1);
2019-11-19 14:23:51 +01:00
2020-04-21 10:50:26 +02:00
const converted = [];
newIntervalArray.forEach((element) => {
converted.push({
literal: element.literal,
date_object: element.date_object,
timeList: [],
});
});
this.config.dateList = [...new Set(converted)]; // add only dates that are not already present with a Set of unique items
this.showDateInterval = false;
2019-11-19 14:23:51 +01:00
this.toastService.display(`les dates ont été ajoutées aux réponses possibles.`);
2020-04-21 10:50:26 +02:00
}
2019-08-10 16:20:59 +02:00
}