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

214 lines
5.5 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[] {
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}) {
2022-04-28 09:32:27 +01:00
//
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')
// )
// }
if(segment!='Combinado') {
eventSource = this.filterSegment(eventSource, segment)
}
2021-09-02 16:06:53 +01:00
2021-07-18 18:56:53 +01:00
if(profile != 'all') {
eventSource = this.filterProfile(eventSource, profile)
}
2021-11-10 17:03:53 +01:00
let newStracture:CustomCalendarEvent[];
2021-08-31 16:32:16 +01:00
2021-07-18 18:56:53 +01:00
if(profile == 'md') {
2021-08-31 09:00:33 +01:00
newStracture = this.encapsulation(eventSource, 'mdgpr');
2021-07-18 18:56:53 +01:00
} else {
2021-08-31 09:00:33 +01:00
newStracture = this.encapsulation(eventSource, 'pr');
2021-07-18 18:56:53 +01:00
}
2021-09-03 12:19:21 +01:00
return this.display(newStracture, profile, selectedDate)
2021-07-18 18:56:53 +01:00
}
2021-09-03 12:19:21 +01:00
display(list: CustomCalendarEvent[], profile, selectedDate) {
2021-07-18 18:56:53 +01:00
let days = {};
2021-08-31 16:32:16 +01:00
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({
2021-07-18 18:56:53 +01:00
startTime: startDate,
endTime: event.end
})
2021-09-03 12:19:21 +01:00
const day = this.dateService.getDay(event.start)
2021-07-18 18:56:53 +01:00
2021-08-31 16:32:16 +01:00
event['manyDays'] = false
event['todayOnly'] = this.dateService.isSameDate(event.start, event.end)
2021-07-18 18:56:53 +01:00
if(!days.hasOwnProperty(day)) {
days[day] = []
}
2021-08-31 16:44:03 +01:00
if (this.dateService.notSameDate(startDate, endDate)) {
2021-08-31 16:32:16 +01:00
const diffDays = this.dateService.deferenceBetweenDays(endDate, startDate)
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) {
2021-09-03 12:19:21 +01:00
const StartEvent = this.transForm(event, {startMany: true,endMany: false, middle: false, profile})
2021-11-15 15:29:43 +01:00
if(this.push(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
2021-09-03 12:19:21 +01:00
const EndEvent = this.transForm(event, {startMany: false,endMany: true, middle: false, profile})
if(this.push(event, selectedDate)) days[otherDays].push(EndEvent)
2021-07-18 18:56:53 +01:00
} else {
2021-08-31 16:44:03 +01:00
2021-09-03 12:19:21 +01:00
const EndEvent = this.transForm(event, {startMany: false,endMany: true, middle: true, profile})
if(this.push(event, selectedDate)) days[otherDays].push(EndEvent)
2021-07-18 18:56:53 +01:00
}
}
} else {
2021-11-15 15:29:43 +01:00
2021-09-03 12:19:21 +01:00
if(this.push(event, selectedDate)) days[day].push(event)
2021-07-18 18:56:53 +01:00
}
} else {
2021-11-15 15:29:43 +01:00
2021-09-03 12:19:21 +01:00
if(this.push(event, selectedDate)) days[day].push(event)
2021-07-18 18:56:53 +01:00
}
2021-11-15 15:29:43 +01:00
}
2021-07-18 18:56:53 +01:00
2021-09-03 12:19:21 +01:00
if(this.push(event, selectedDate)) days[day].push(event)
2021-07-18 18:56:53 +01:00
})
2021-09-03 12:19:21 +01:00
// remove days that haven't event
Object.entries(days).forEach(([index, value]) => {
const _value: any = value
if(_value.length == 0) {
delete days[index]
}
})
2021-07-18 18:56:53 +01:00
return days
}
2021-09-03 12:19:21 +01:00
2021-11-22 15:46:17 +01:00
push(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
2021-08-31 16:32:16 +01:00
encapsulation(eventsList:EventListStore[], profile): 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,
});
});
return events;
}
2021-08-31 16:44:03 +01:00
2021-09-03 12:19:21 +01:00
transForm(event: CustomCalendarEvent, {startMany, endMany, middle, profile}) {
2021-08-31 16:44:03 +01:00
return Object.assign({}, {
start: event.start,
end: event.end,
id: event.id,
2021-09-03 12:19:21 +01:00
profile: profile,
2021-08-31 16:44:03 +01:00
event: {
Subject: event.event.Subject,
StartDate: event.event.StartDate,
EndDate: event.event.EndDate,
Location: event.event.Location,
EventId: event.event.EventId,
2022-04-02 14:13:37 +01:00
CalendarName: event.event.CalendarName,
CalendarId: event.event.CalendarId
2021-08-31 16:44:03 +01:00
},
Subject: event.event.Subject,
startMany: false,
endMany: true,
middle: true
})
}
2021-08-31 16:32:16 +01:00
}