import { Component, OnInit, Input, ViewChild } from '@angular/core'; import { Calendar } from '@fullcalendar/core'; import dayGridPlugin from '@fullcalendar/daygrid'; import timeGridPlugin from '@fullcalendar/timegrid'; import interactionPlugin from '@fullcalendar/interaction'; import { EventCalendarService } from '../shared/service/event-calendar.service'; import { ISelectedFilter } from '../shared/core/ISelectedFilter'; import { StaffActivityService } from '../shared/service/staff-activity.service'; import { LoginHistoryService } from '../shared/service/login-history.service'; import { SelectItem } from 'primeng/api'; import { IEventCalendar } from '../shared/core/IEventCalendar'; import { ActivatedRoute } from '@angular/router'; import { FullCalendar } from 'primeng/fullcalendar'; @Component({ selector: 'app-staff-event-calendar', templateUrl: './staff-event-calendar.component.html', styleUrls: ['./staff-event-calendar.component.css'] }) export class StaffEventCalendarComponent implements OnInit { //when fillter type set externally, @Input() filterType : string; // staff id gets value when calendar component using for specific staff @Input() staffId : string = '0'; @ViewChild('fc') fc: FullCalendar; //this will contain data that display in calendar events: IEventCalendar[]; //this will keep loaded data temporarily to bind to events var when no any filter selected filteredEvents: IEventCalendar[]; //this will use for calendar options when configuring options: any; //this will be default filter type unless its set externally type = "AEL"; //default organisation id value set here OrganisationId = '0'; //default date for calendar startDate = new Date(); //list of filters bind to these vars dateRangeFilterList: SelectItem[] =[]; modulesFilterList: SelectItem[] =[]; personTypeFilterList: SelectItem[] =[]; //to get search value searchValue: string; //temporary var to keep currently selected filters list selectedFilterList: ISelectedFilter; constructor( private eventCalendarService: EventCalendarService, private staffActivityService: StaffActivityService, private loginHistoryService: LoginHistoryService, private route: ActivatedRoute) { } ngOnInit() { // debugger; //getting company id from url this.OrganisationId = this.route.snapshot.paramMap.get("company"); //filter type will be selected by the input if(this.filterType){ this.type = this.filterType; }else{ //if no input set, default filter will be used this.type = "AEL"; } //options needed for calnedar this.options = { themeSystem:'bootstrap', plugins: [dayGridPlugin, timeGridPlugin, interactionPlugin], defaultDate: this.startDate, header: { left: 'prev,next', center:'title',right: 'month,week,day,today'}, titleFormat: { year: 'numeric', month: 'long', day: 'numeric'}, buttonText :{ today: 'Today', month: 'Month', week: 'Week', } }; //call method to get event data this.getStaffEvents(this.staffId, this.OrganisationId); //calling to get filters this.getDateRangeFilters(); this.getModuleListFilters(); this.getPerosonTypeFilters(); } //this method will call service to get staff events list getStaffEvents(id: string, companyId : string){ this.eventCalendarService.getStaffEvents(id,companyId).subscribe(events => { this.events = events; this.filteredEvents = events; }); } //search box value access function for searching getSearchValue(value){ this.searchValue = value.target.value; this.bindFilter(this.selectedFilterList); } //this method call service for date range filters list getDateRangeFilters(){ this.loginHistoryService.getMonthYearFilters().subscribe(f =>{ //filters list as a response from service this.dateRangeFilterList = f; }) } //this method call service for module filters list getModuleListFilters(){ this.staffActivityService.getModulesList().subscribe(f =>{ //filters list as a response from service this.modulesFilterList = f; }) } //this method call service for person type filters list getPerosonTypeFilters(){ this.loginHistoryService.getPersonTypeFilters().subscribe(f =>{ //filters list as a response from service this.personTypeFilterList = f; }) } bindFilter(value : ISelectedFilter){ //temporary filter list this.selectedFilterList = value; //clear existing filtered data before apply new data this.clearAllFilters(); // if all values are null, all the data will bind if(value){ if(!value.Module && !value.PersonType && !this.searchValue){ this.events = this.filteredEvents; this.fc.getCalendar().gotoDate(new Date( this.events[0].start.toString().substring(0, 10))); }else{ this.events =this.events.filter(x => x.title.includes(this.searchValue)); this.fc.getCalendar().gotoDate(new Date( this.events[0].start.toString().substring(0, 10))); } if(value.DateRange){ this.fc.getCalendar().gotoDate(new Date(value.DateRange)); } } else{ if(this.searchValue && this.searchValue!==""){ this.events =this.events.filter(x => x.title.includes(this.searchValue)); this.fc.getCalendar().gotoDate(new Date(this.events[0].start.toString().substring(0, 10))); }else{ this.events = this.filteredEvents; this.fc.getCalendar().gotoDate(new Date()); } } } //this method clear all filters and reset data clearAllFilters(){ this.events = this.filteredEvents; } }