1- import { Component , EventEmitter , Input , OnInit , Output } from '@angular/core' ;
21import {
3- CalendarEvent ,
4- CalendarView ,
5- } from 'angular-calendar' ;
2+ ChangeDetectorRef ,
3+ Component ,
4+ ElementRef ,
5+ EventEmitter ,
6+ Input ,
7+ OnInit ,
8+ Output ,
9+ ViewChild ,
10+ } from '@angular/core' ;
11+ import { CalendarEvent , CalendarView } from 'angular-calendar' ;
612import { Observable } from 'rxjs' ;
713import * as moment from 'moment' ;
814import { DataSource } from '../../../shared/models/data-source.model' ;
915import { Entry } from 'src/app/modules/shared/models' ;
1016import { map } from 'rxjs/operators' ;
1117import { SubstractDatePipe } from 'src/app/modules/shared/pipes/substract-date/substract-date.pipe' ;
18+ import { differenceInMinutes , startOfDay , startOfHour } from 'date-fns' ;
1219
1320@Component ( {
1421 selector : 'app-calendar' ,
1522 templateUrl : './calendar.component.html' ,
1623 styleUrls : [ './calendar.component.scss' ] ,
1724} )
1825export class CalendarComponent implements OnInit {
19- @Input ( ) set timeEntries$ ( timeEntries : Observable < DataSource < Entry > > ) {
26+ readonly DEFAULT_HEADER_HEIGHT = 52 ;
27+ readonly VARIATION_HEIGHT : number = 2 ;
28+
29+ @ViewChild ( 'scrollContainer' ) scrollContainer : ElementRef < HTMLElement > ;
30+
31+ @Input ( ) set timeEntries$ ( timeEntries : Observable < DataSource < Entry > > ) {
2032 this . castEntryToCalendarEvent ( timeEntries ) ;
2133 }
2234 @Input ( ) calendarView : CalendarView = CalendarView . Month ;
@@ -25,7 +37,7 @@ export class CalendarComponent implements OnInit {
2537 @Output ( ) viewModal : EventEmitter < any > = new EventEmitter < string > ( ) ;
2638 @Output ( ) deleteTimeEntry : EventEmitter < any > = new EventEmitter < string > ( ) ;
2739 @Output ( ) changeDate : EventEmitter < any > = new EventEmitter < {
28- date : Date
40+ date : Date ;
2941 } > ( ) ;
3042
3143 initialDate : Date ;
@@ -34,97 +46,115 @@ export class CalendarComponent implements OnInit {
3446 timeEntriesAsEvent : CalendarEvent [ ] ;
3547 nextDateDisabled : boolean ;
3648
37- constructor ( ) {
49+ constructor ( private referenceChangeDetector : ChangeDetectorRef ) {
3850 this . initialDate = new Date ( ) ;
3951 this . previusDate = new Date ( ) ;
4052 this . isToday = false ;
4153 this . timeEntriesAsEvent = [ ] ;
4254 this . nextDateDisabled = true ;
43- }
55+ }
4456
4557 ngOnInit ( ) : void {
4658 this . isToday = this . isVisibleForCurrentDate ( ) ;
4759 this . navigationEnable ( this . calendarView ) ;
4860 }
4961
50- get CalendarViewEnum ( ) : typeof CalendarView {
62+ get CalendarViewEnum ( ) : typeof CalendarView {
5163 return CalendarView ;
5264 }
5365
66+ scrollToCurrentTimeMarker ( ) {
67+ console . log ( 'Si entro' ) ;
68+ if ( this . calendarView === CalendarView . Week || CalendarView . Day ) {
69+ console . log ( `Calendar View ${ this . calendarView } ` ) ;
70+ const minutesSinceStartOfDay = differenceInMinutes ( startOfHour ( this . currentDate ) , startOfDay ( this . currentDate ) ) ;
71+ const headerHeight = this . calendarView === CalendarView . Week ? this . DEFAULT_HEADER_HEIGHT : 0 ;
72+ this . scrollContainer . nativeElement . scrollTop = minutesSinceStartOfDay * this . VARIATION_HEIGHT + headerHeight ;
73+ }
74+ }
75+
5476 castEntryToCalendarEvent ( timeEntries$ : Observable < DataSource < Entry > > ) {
55- timeEntries$ . pipe (
56- map ( ( timeEntriesDatasorce ) => timeEntriesDatasorce . data . map (
57- ( timeEntries ) => ( {
58- start : new Date ( timeEntries . start_date ) ,
59- end : timeEntries . end_date ? new Date ( timeEntries . end_date ) : null ,
60- title : timeEntries . description ,
61- id : timeEntries . id ,
62- meta : timeEntries
63- } as CalendarEvent )
77+ timeEntries$
78+ . pipe (
79+ map ( ( timeEntriesDatasorce ) =>
80+ timeEntriesDatasorce . data . map (
81+ ( timeEntries ) =>
82+ ( {
83+ start : new Date ( timeEntries . start_date ) ,
84+ end : timeEntries . end_date ? new Date ( timeEntries . end_date ) : null ,
85+ title : timeEntries . description ,
86+ id : timeEntries . id ,
87+ meta : timeEntries ,
88+ } as CalendarEvent )
89+ )
6490 )
6591 )
66- )
67- . subscribe ( ( timeEntriesAsEvent ) => {
92+ . subscribe ( ( timeEntriesAsEvent ) => {
6893 this . timeEntriesAsEvent = [ ...timeEntriesAsEvent ] . reverse ( ) ;
69- } ) ;
94+ } ) ;
7095 }
7196
7297 handleEditEvent ( timeEntryAsEvent : CalendarEvent ) : void {
73- this . viewModal . emit ( {
74- id : timeEntryAsEvent . id
98+ this . viewModal . emit ( {
99+ id : timeEntryAsEvent . id ,
75100 } ) ;
76101 }
77102
78103 handleDeleteEvent ( timeEntryAsEvent : CalendarEvent ) : void {
79104 this . deleteTimeEntry . emit ( {
80- timeEntry : timeEntryAsEvent . meta
105+ timeEntry : timeEntryAsEvent . meta ,
81106 } ) ;
82107 }
83108
84- handleChangeDateEvent ( ) : void {
109+ handleChangeDateEvent ( ) : void {
85110 const date = this . currentDate ;
86111 this . isToday = this . isVisibleForCurrentDate ( ) ;
87112 this . navigationEnable ( this . calendarView ) ;
88- this . changeDate . emit ( { date} ) ;
113+ this . changeDate . emit ( { date } ) ;
89114 }
90115
91- changeCalendarView ( calendarView : CalendarView ) {
116+ changeCalendarView ( calendarView : CalendarView ) {
92117 this . calendarView = calendarView ;
118+ this . scrollContainer . nativeElement . scrollTop = 0 ;
119+ if ( this . calendarView !== CalendarView . Month ) {
120+ this . referenceChangeDetector . detectChanges ( ) ;
121+ this . scrollToCurrentTimeMarker ( ) ;
122+ }
93123 }
94124
95- navigationEnable ( calendarView : CalendarView ) {
125+ navigationEnable ( calendarView : CalendarView ) {
96126 let enable = false ;
97127 const currentDate = moment ( this . currentDate ) ;
98128 const initialDate = moment ( this . initialDate ) ;
99- if ( calendarView === CalendarView . Month ) {
129+ if ( calendarView === CalendarView . Month ) {
100130 if ( currentDate . month ( ) === initialDate . month ( ) && currentDate . year ( ) === initialDate . year ( ) ) {
101131 enable = true ;
102132 }
103133 }
104134 currentDate . add ( 1 , 'day' ) ;
105- if ( currentDate > initialDate ) {
135+ if ( currentDate > initialDate ) {
106136 enable = true ;
107137 }
108138 this . nextDateDisabled = enable ;
109139 }
110140
111- getTimeWork ( startDate : Date , endDate : Date ) : number {
112- if ( ! endDate ) {
141+ getTimeWork ( startDate : Date , endDate : Date ) : number {
142+ if ( ! endDate ) {
113143 return 30 ;
114144 }
115- return new SubstractDatePipe ( ) . transformInMinutes ( endDate , startDate ) ;
145+ return new SubstractDatePipe ( ) . transformInMinutes ( endDate , startDate ) ;
116146 }
117147
118- timeIsGreaterThan ( startDate : Date , endDate : Date , timeRange : number ) : boolean {
148+ timeIsGreaterThan ( startDate : Date , endDate : Date , timeRange : number ) : boolean {
119149 const timeWorkInMinutes = this . getTimeWork ( startDate , endDate ) ;
120150 return timeWorkInMinutes > timeRange ;
121151 }
122152
123- isVisibleForCurrentView ( currentCalendarView : CalendarView , desiredView : CalendarView ) : boolean {
153+ isVisibleForCurrentView ( currentCalendarView : CalendarView , desiredView : CalendarView ) : boolean {
124154 return currentCalendarView === desiredView ;
125155 }
126156
127- isVisibleForCurrentDate ( ) : boolean {
157+ isVisibleForCurrentDate ( ) : boolean {
128158 const currentDate : Date = new Date ( this . currentDate ) ;
129159 const initialDate : Date = new Date ( this . initialDate ) ;
130160 return currentDate . setHours ( 0 , 0 , 0 , 0 ) === initialDate . setHours ( 0 , 0 , 0 , 0 ) ;
0 commit comments