Files
doneit-web/src/app/services/agenda/list-box.service.ts
T

215 lines
5.4 KiB
TypeScript
Raw Normal View History

2021-08-31 16:32:16 +01:00
import { Injectable } from '@angular/core';
import { CustomCalendarEvent, EventListStore } from 'src/app/models/agenda/AgendaEventList';
import { DateService } from '../date.service';
2021-07-18 18:56:53 +01:00
2021-08-31 16:32:16 +01:00
@Injectable({
providedIn: 'root'
})
export class ListBoxService {
2021-07-18 18:56:53 +01:00
2021-08-31 16:32:16 +01:00
constructor(
2021-11-10 17:03:53 +01:00
private dateService: DateService
){}
2021-07-18 18:56:53 +01:00
2021-11-23 17:04:54 +01:00
filterProfile(eventSource: EventListStore[] = [], profile: 'md' | 'pr' | 'all') {
2021-07-18 18:56:53 +01:00
return eventSource.filter((e) => e.profile == profile)
}
2021-08-31 09:00:33 +01:00
getEventInsideRange(eventSource: EventListStore[], rangeStartDate, randEndDate) {
2021-07-18 18:56:53 +01:00
return eventSource.filter((e)=> {
if(new Date(rangeStartDate).getTime() <= new Date(e.startTime).getTime() &&
new Date(randEndDate).getTime() >= new Date(e.endTime).getTime()) {
return true
}
return false
})
}
2021-11-10 17:03:53 +01:00
filterSegment(eventSource: EventListStore[], segment): EventListStore[] {
2023-03-30 15:22:40 +01:00
2021-11-10 17:03:53 +01:00
return eventSource.filter( data => data.calendarName == segment)
}
2021-09-02 16:06:53 +01:00
2021-07-18 18:56:53 +01:00
daysBetween(){ }
2021-09-02 16:06:53 +01:00
list(eventSource: EventListStore[], profile: 'md' | 'pr' | 'all', rangeStartDate, randEndDate, {segment = 'Combinado', selectedDate= null}) {
2021-11-10 17:03:53 +01:00
// // filter range
// if(selectedDate) {
// eventSource = eventSource.filter(data =>
// data.startTime.toLocaleDateString('pt') >= new Date(rangeStartDate).toLocaleDateString('pt') &&
// data.startTime.toLocaleDateString('pt') <= new Date(rangeStartDate).toLocaleDateString('pt')
// )
// }
2023-03-30 15:22:40 +01:00
if(segment!='Combinado') {
eventSource = this.filterSegment(eventSource, segment)
}
2021-09-02 16:06:53 +01:00
2023-03-30 14:31:58 +01:00
// if(profile != 'all') {
// eventSource = this.filterProfile(eventSource, profile)
// }
2021-07-18 18:56:53 +01:00
2021-11-10 17:03:53 +01:00
let newStracture:CustomCalendarEvent[];
2021-08-31 16:32:16 +01:00
2023-03-30 14:31:58 +01:00
newStracture = this.encapsulation(eventSource);
2021-07-18 18:56:53 +01:00
2023-04-05 15:15:42 +01:00
return this.display(newStracture, selectedDate)
2021-07-18 18:56:53 +01:00
}
2023-04-05 15:15:42 +01:00
display(list: CustomCalendarEvent[], selectedDate) {
2021-07-18 18:56:53 +01:00
let days = {};
list.forEach( (event:CustomCalendarEvent, index) => {
2021-07-18 18:56:53 +01:00
var startDate: any = new Date(event.start);
2021-08-31 16:32:16 +01:00
var endDate: any = this.dateService.EventEndDateTreatment({
startTime: startDate,
2021-07-18 18:56:53 +01:00
endTime: event.end
})
2021-07-18 18:56:53 +01:00
2021-09-03 12:19:21 +01:00
const day = this.dateService.getDay(event.start)
2021-07-18 18:56:53 +01:00
event['manyDays'] = !this.dateService.isSameDate(event.start, event.end)
2021-08-31 16:32:16 +01:00
event['todayOnly'] = this.dateService.isSameDate(event.start, event.end)
2021-07-18 18:56:53 +01:00
2021-07-18 18:56:53 +01:00
if(!days.hasOwnProperty(day)) {
days[day] = []
}
const diffDays = this.dateService.deferenceBetweenDays(endDate, startDate)
2021-08-31 16:44:03 +01:00
if (this.dateService.notSameDate(startDate, endDate)) {
2021-08-31 16:32:16 +01:00
2021-07-18 18:56:53 +01:00
2021-09-03 12:19:21 +01:00
if (diffDays <= 150 && !event.event.IsAllDayEvent ) {
2021-07-18 18:56:53 +01:00
if (diffDays >= 1) {
2023-03-30 14:31:58 +01:00
const StartEvent = this.transForm(event, {startMany: true, endMany: false, middle: false})
2021-11-15 15:29:43 +01:00
if(this.CanPush(event, selectedDate)) days[day].push(StartEvent)
2021-07-18 18:56:53 +01:00
let i = 1;
2021-11-15 15:29:43 +01:00
// create event between date
2021-07-18 18:56:53 +01:00
while (startDate.getFullYear() != endDate.getFullYear() ||
startDate.getMonth() != endDate.getMonth() ||
startDate.getDate() != endDate.getDate()) {
const newDate = startDate.setDate(startDate.getDate()+ i)
2021-09-03 12:19:21 +01:00
let otherDays = this.dateService.getDay(newDate)
2021-07-18 18:56:53 +01:00
2021-08-31 16:32:16 +01:00
event['other'] = true
2021-07-18 18:56:53 +01:00
event.start = newDate
if(!days.hasOwnProperty(otherDays)) {
days[otherDays] = []
}
if (!(startDate.getFullYear() != endDate.getFullYear() ||
startDate.getMonth() != endDate.getMonth() ||
startDate.getDate() != endDate.getDate())) {
// last push
2023-03-30 14:31:58 +01:00
const EndEvent = this.transForm(event, {startMany: false, endMany: true, middle: false})
if(this.CanPush(event, selectedDate)) days[otherDays].push(EndEvent)
2021-07-18 18:56:53 +01:00
} else {
2021-08-31 16:44:03 +01:00
2023-03-30 14:31:58 +01:00
const EndEvent = this.transForm(event, {startMany: false,endMany: true, middle: true})
if(this.CanPush(event, selectedDate)) days[otherDays].push(EndEvent)
2021-07-18 18:56:53 +01:00
}
}
} else {
2021-11-15 15:29:43 +01:00
if(this.CanPush(event, selectedDate)) days[day].push(event)
2021-07-18 18:56:53 +01:00
}
} else {
2021-11-15 15:29:43 +01:00
if(this.CanPush(event, selectedDate)) days[day].push(event)
2021-07-18 18:56:53 +01:00
}
2022-12-16 17:33:00 +01:00
} else {
if(this.CanPush(event, selectedDate) && diffDays != 2) days[day].push(event)
2021-11-15 15:29:43 +01:00
}
2021-07-18 18:56:53 +01:00
2022-12-16 17:33:00 +01:00
//
2021-07-18 18:56:53 +01:00
})
// remove days that haven't event
Object.entries(days).forEach(([index, value]) => {
const _value: any = value
2021-09-03 12:19:21 +01:00
if(_value.length == 0) {
delete days[index]
}
2021-09-03 12:19:21 +01:00
})
2021-09-03 12:19:21 +01:00
2021-07-18 18:56:53 +01:00
return days
}
CanPush(event: any, selectedDate: Date) {
2021-11-15 15:29:43 +01:00
2021-11-22 15:46:17 +01:00
return new Date(event.start).getMonth() == selectedDate.getMonth() &&
new Date(event.start).getFullYear() == selectedDate.getFullYear() &&
new Date(event.start).getDate() >= selectedDate.getDate()
}
2021-09-03 12:19:21 +01:00
2023-03-30 14:31:58 +01:00
encapsulation(eventsList:EventListStore[]): CustomCalendarEvent[] {
2021-07-18 18:56:53 +01:00
// remove all event
2021-08-31 09:00:33 +01:00
let events: CustomCalendarEvent[] = [];
2021-07-18 18:56:53 +01:00
eventsList.forEach((element, eventIndex) => {
events.push({
2021-08-31 09:00:33 +01:00
start: new Date(element.startTime),
end: new Date(element.endTime),
id: element.id,
2021-07-18 18:56:53 +01:00
event: element.event,
2023-03-30 14:31:58 +01:00
profile: element.profile
2021-07-18 18:56:53 +01:00
});
});
return events;
}
2021-08-31 16:44:03 +01:00
2023-03-30 14:31:58 +01:00
transForm(event: CustomCalendarEvent, {startMany, endMany, middle}) {
return Object.assign({}, {
start: event.start,
end: event.end,
id: event.id,
2023-03-30 14:31:58 +01:00
profile: event.profile,
event: {
Subject: event.event.Subject,
StartDate: event.event.StartDate,
EndDate: event.event.EndDate,
Location: event.event.Location,
EventId: event.event.EventId,
CalendarName: event.event.CalendarName,
CalendarId: event.event.CalendarId
},
Subject: event.event.Subject,
startMany: startMany,
endMany: endMany,
middle: middle
})
}
2021-08-31 16:44:03 +01:00
2021-08-31 16:32:16 +01:00
}