Files
doneit-web/src/app/store/calendar.service.ts
T
2023-06-22 12:53:35 +01:00

126 lines
3.0 KiB
TypeScript

import { Injectable } from '@angular/core';
import { SHA1 } from 'crypto-js'
import { localstoreService } from './localstore.service'
import { EventList, EventListStore } from '../models/agenda/AgendaEventList';
@Injectable({
providedIn: 'root'
})
export class CalendarService {
private _eventSource : EventListStore[] = []
private keyName: string;
localstoreService = localstoreService
constructor() {
this.keyName = (SHA1("CalendarService"+ 'eventSource')).toString()
let restore = this.localstoreService.get(this.keyName, [])
restore.forEach((element:EventListStore, eventIndex) => {
this._eventSource.push({
startTime: new Date(element.startTime),
endTime: new Date(element.endTime),
allDay: element.allDay,
event: element.event,
calendarName: element.calendarName,
CalendarId: element.CalendarId,
profile: element.profile,
id: element.id,
});
});
}
ResetList(eventSource: EventListStore[]) {
this._eventSource = eventSource
setTimeout(() => {
this.localstoreService.set(this.keyName, this._eventSource)
}, 10)
}
get eventSource() {
return this._eventSource || []
}
removeRange(rangeStartDate, rangeEndDate, profile) {
this._eventSource = this._eventSource.filter((e)=> {
if(new Date(rangeStartDate).getTime() <= new Date(e.startTime).getTime() &&
new Date(rangeEndDate).getTime() >= new Date(e.endTime).getTime() && e.profile == profile) {
return false
}
return true
})
}
removeRangeForCalendar(rangeStartDate, rangeEndDate, profile, calendarId) {
this._eventSource = this._eventSource.filter((e)=> {
if(new Date(rangeStartDate).getTime() <= new Date(e.startTime).getTime() &&
new Date(rangeEndDate).getTime() >= new Date(e.endTime).getTime() && e.CalendarId == calendarId) {
return false
}
return true
})
}
pushEvent(eventsList: EventList[], profile: 'pr' | 'md') {
let news = []
eventsList.forEach((element, eventIndex) => {
news.push({
startTime: new Date(element.StartDate),
endTime: new Date(element.EndDate),
allDay: false,
event: element,
calendarName: element.CalendarName,
profile: profile,
id: element.EventId,
CalendarId: element.CalendarId
});
});
let instance = this._eventSource.concat(news)
const ids = instance.map(o => o.id)
const filtered = instance.filter(({id}, index) => !ids.includes(id, index + 1))
this._eventSource = (filtered)
setTimeout(() => {
this.localstoreService.set(this.keyName, this._eventSource)
}, 10)
}
getEventsByCalendarIds(ids:any[]): EventListStore[] {
let result = [];
for (const calendar of ids) {
const events = this._eventSource.filter(e => e.CalendarId == calendar.CalendarId)
result = result.concat(events)
}
return result
}
delete() {
this._eventSource = []
}
}
export const CalendarStore = new CalendarService()