calendar.component.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { Component, Inject, OnDestroy, OnInit, ViewChild } from '@angular/core';
  2. import { CalendarOptions } from '@fullcalendar/core';
  3. import dayGridPlugin from '@fullcalendar/daygrid';
  4. import interactionPlugin from '@fullcalendar/interaction';
  5. import timeGridPlugin from '@fullcalendar/timegrid'
  6. import listPlugin from '@fullcalendar/list';
  7. import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from '@angular/material/dialog';
  8. import { CalendarService } from '../../Administrador/services/calendar.service';
  9. import { EventoServiceService } from '../../Administrador/services/evento-service.service';
  10. @Component({
  11. selector: 'app-calendar',
  12. templateUrl: './calendar.component.html',
  13. styleUrl: './calendar.component.css'
  14. })
  15. export class CalendarComponent implements OnInit {
  16. public valor: string = localStorage.getItem('vista') || '';
  17. public fecha: string = localStorage.getItem('dia') || '';
  18. public primerColorTitulos: string = '';
  19. public calendarOptions: CalendarOptions = {
  20. initialDate: this.fecha,
  21. locale: 'es',
  22. weekNumbers: true,
  23. weekText: '',
  24. buttonText: {
  25. today: 'Hoy',
  26. timeGridWeek: 'Semana',
  27. dayGridMonth: 'Mes',
  28. listWeek: 'Agenda',
  29. dayGridDay: 'Día'
  30. },
  31. headerToolbar: {
  32. start: 'today prev,next',
  33. center: 'title',
  34. end: 'dayGridMonth,timeGridWeek,dayGridDay,listWeek',
  35. },
  36. initialView: this.valor,
  37. navLinks: true,
  38. editable: false,
  39. selectable: true,
  40. selectMirror: true,
  41. dayMaxEvents: true,
  42. plugins: [dayGridPlugin, interactionPlugin, timeGridPlugin, listPlugin],
  43. events: [],
  44. eventClick: this.handleEventClick.bind(this)
  45. };
  46. constructor(
  47. private calendarService: CalendarService,
  48. public dialog: MatDialog,
  49. private eventoService: EventoServiceService
  50. ) { }
  51. ngOnInit() {
  52. let primerColorTitulos = localStorage.getItem('primerColorTitulos') || ''
  53. this.primerColorTitulos = primerColorTitulos;
  54. this.getEvents();
  55. }
  56. handleEventClick(info: any) {
  57. const event = info.event;
  58. this.dialog.open(EventInfoModal, {
  59. width: '500px',
  60. data: {
  61. title: event.title,
  62. description: event.extendedProps.description,
  63. start: event.start,
  64. end: event.end,
  65. color: event.backgroundColor,
  66. textColor: event.textColor
  67. }
  68. });
  69. }
  70. getEvents() {
  71. this.eventoService.obtenerEventos().subscribe((response: any) => {
  72. const events = response.map((event: any) => ({
  73. id: event.idEvento,
  74. title: event.titulo,
  75. start: new Date(event.fechaInicio).toISOString(),
  76. end: new Date(event.fechaFin).toISOString(),
  77. description: event.descripcion,
  78. backgroundColor: event.colorEvento,
  79. textColor: event.colorTexto || '#FFFFFF'
  80. }));
  81. this.calendarOptions.events = events;
  82. console.log(this.calendarOptions.events);
  83. });
  84. }
  85. }
  86. @Component({
  87. selector: 'app-event-info-modal',
  88. template: `
  89. <h2 mat-dialog-title [style.color]="data.color">{{ data.title }}</h2>
  90. <mat-dialog-content>
  91. <p [innerHTML]="data.description || 'Sin descripción disponible.'"></p>
  92. <p><strong>Inicio:</strong> {{ data.start | date: 'medium' }}</p>
  93. <p><strong>Fin:</strong> {{ data.end | date: 'medium' }}</p>
  94. </mat-dialog-content>
  95. <mat-dialog-actions align="end">
  96. <button mat-button mat-dialog-close color="primary">Cerrar</button>
  97. </mat-dialog-actions>
  98. `,
  99. })
  100. export class EventInfoModal {
  101. constructor(
  102. public dialogRef: MatDialogRef<EventInfoModal>,
  103. @Inject(MAT_DIALOG_DATA) public data: any
  104. ) {}
  105. }