Files
doneit-web/src/app/services/ageanda/list-box.service.ts
T
2021-08-31 19:30:08 +01:00

197 lines
4.9 KiB
TypeScript

import { Injectable } from '@angular/core';
import { CustomCalendarEvent, EventListStore } from 'src/app/models/agenda/AgendaEventList';
import { DateService } from '../date.service';
@Injectable({
providedIn: 'root'
})
export class ListBoxService {
constructor(
private dateService: DateService
){}
filterProfile(eventSource: EventListStore[], profile: 'md' | 'pr' | 'all') {
return eventSource.filter((e) => e.profile == profile)
}
getEventInsideRange(eventSource: EventListStore[], rangeStartDate, randEndDate) {
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
})
}
daysBetween(){ }
list(eventSource: EventListStore[], profile: 'md' | 'pr' | 'all', rangeStartDate, randEndDate, {segment = 'todo'}) {
if(profile != 'all') {
eventSource = this.filterProfile(eventSource, profile)
}
eventSource = this.getEventInsideRange(eventSource, rangeStartDate, randEndDate)
let newStracture:CustomCalendarEvent[];
if(profile == 'md') {
newStracture = this.encapsulation(eventSource, 'mdgpr');
} else {
newStracture = this.encapsulation(eventSource, 'pr');
}
return this.display(newStracture)
}
display(list: CustomCalendarEvent[]) {
let days = {};
console.log(list)
list.forEach( (event:CustomCalendarEvent, index)=> {
var startDate: any = new Date(event.start);
var endDate: any = this.dateService.EventEndDateTreatment({
startTime: startDate,
endTime: event.end
})
const day = (((new Date (event.start)).getDate())).toString().padStart(2,'0')
event['manyDays'] = false
event['todayOnly'] = this.dateService.isSameDate(event.start, event.end)
if(!days.hasOwnProperty(day)) {
days[day] = []
}
if (this.dateService.notSameDate(startDate, endDate)) {
const diffDays = this.dateService.deferenceBetweenDays(endDate, startDate)
if (diffDays <= 150 && !event.event.IsAllDayEvent ) {
if (diffDays >= 1) {
const StartEvent = this.transForm(event, {startMany: true,endMany: false, middle: false})
days[day].push(StartEvent)
let i = 1;
while (startDate.getFullYear() != endDate.getFullYear() ||
startDate.getMonth() != endDate.getMonth() ||
startDate.getDate() != endDate.getDate()) {
const newDate = startDate.setDate(startDate.getDate()+ i)
let otherDays = (((new Date (newDate)).getDate())).toString().padStart(2,'0')
event['other'] = true
event.start = newDate
if(!days.hasOwnProperty(otherDays)) {
days[otherDays] = []
}
if (!(startDate.getFullYear() != endDate.getFullYear() ||
startDate.getMonth() != endDate.getMonth() ||
startDate.getDate() != endDate.getDate())) {
// last push
const EndEvent = this.transForm(event, {startMany: false,endMany: true, middle: false})
days[otherDays].push(EndEvent)
} else {
const EndEvent = this.transForm(event, {startMany: false,endMany: true, middle: true})
days[otherDays].push(EndEvent)
}
}
} else {
days[day].push(event)
}
} else {
days[day].push(event)
}
}
days[day].push(event)
})
setTimeout(()=>{
document.querySelectorAll('.EventListBox-container .EventListBox').forEach((e)=>{
if(e.childElementCount == 0) {
e.parentElement.style.display = 'none'
} else {
e.parentElement.style.display = 'block'
}
})
}, 10)
return days
}
encapsulation(eventsList:EventListStore[], profile): CustomCalendarEvent[] {
// remove all event
let events: CustomCalendarEvent[] = [];
eventsList.forEach((element, eventIndex) => {
events.push({
start: new Date(element.startTime),
end: new Date(element.endTime),
color: {
primary: '#0000',
secondary: '#0000'
},
id: element.id,
index: eventIndex,
CalendarName: element.calendarName,
event: element.event,
});
console.log('element.event', element.event)
});
return events;
}
transForm(event: CustomCalendarEvent, {startMany, endMany, middle}) {
return Object.assign({}, {
title: event.event.Subject,
start: event.start,
end: event.end,
color: event.color,
id: event.id,
index: event.index,
profile: event,
CalendarName: event.CalendarName,
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
},
Subject: event.event.Subject,
startMany: false,
endMany: true,
middle: true
})
}
}