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- readonly HALF_HOUR : number = 30 ;
26+ readonly DEFAULT_HEADER_HEIGHT = 52 ;
2027 readonly VARIATION_HEIGHT : number = 2 ;
2128 readonly VISIBLE_TARGETS_FOR_TIME_ENTRIES_DESCRIPTION : CalendarView [ ] = [ CalendarView . Week , CalendarView . Day ] ;
2229 readonly CALENDAR_VIEW_ENUM : typeof CalendarView = CalendarView ;
2330
24- @Input ( ) set timeEntries$ ( timeEntries : Observable < DataSource < Entry > > ) {
31+ @ViewChild ( 'scrollContainer' ) scrollContainer : ElementRef < HTMLElement > ;
32+
33+ @Input ( ) set timeEntries$ ( timeEntries : Observable < DataSource < Entry > > ) {
2534 this . castEntryToCalendarEvent ( timeEntries ) ;
2635 }
2736 @Input ( ) calendarView : CalendarView = CalendarView . Month ;
@@ -30,7 +39,7 @@ export class CalendarComponent implements OnInit {
3039 @Output ( ) viewModal : EventEmitter < any > = new EventEmitter < string > ( ) ;
3140 @Output ( ) deleteTimeEntry : EventEmitter < any > = new EventEmitter < string > ( ) ;
3241 @Output ( ) changeDate : EventEmitter < any > = new EventEmitter < {
33- date : Date
42+ date : Date ;
3443 } > ( ) ;
3544
3645 initialDate : Date ;
@@ -39,83 +48,100 @@ export class CalendarComponent implements OnInit {
3948 timeEntriesAsEvent : CalendarEvent [ ] ;
4049 nextDateDisabled : boolean ;
4150
42- constructor ( ) {
51+ constructor ( private referenceChangeDetector : ChangeDetectorRef ) {
4352 this . initialDate = new Date ( ) ;
4453 this . previusDate = new Date ( ) ;
4554 this . isToday = false ;
4655 this . timeEntriesAsEvent = [ ] ;
4756 this . nextDateDisabled = true ;
48- }
57+ }
4958
5059 ngOnInit ( ) : void {
5160 this . isToday = this . isVisibleForCurrentDate ( ) ;
5261 this . navigationEnable ( this . calendarView ) ;
5362 }
5463
64+ scrollToCurrentTimeMarker ( ) {
65+ if ( this . calendarView === CalendarView . Week || CalendarView . Day ) {
66+ const minutesSinceStartOfDay = differenceInMinutes ( startOfHour ( this . currentDate ) , startOfDay ( this . currentDate ) ) ;
67+ const headerHeight = this . calendarView === CalendarView . Week ? this . DEFAULT_HEADER_HEIGHT : 0 ;
68+ this . scrollContainer . nativeElement . scrollTop = minutesSinceStartOfDay * this . VARIATION_HEIGHT + headerHeight ;
69+ }
70+ }
71+
5572 castEntryToCalendarEvent ( timeEntries$ : Observable < DataSource < Entry > > ) {
56- timeEntries$ . pipe (
57- map ( ( timeEntriesDatasorce ) => timeEntriesDatasorce . data . map (
58- ( timeEntries ) => ( {
59- start : new Date ( timeEntries . start_date ) ,
60- end : timeEntries . end_date ? new Date ( timeEntries . end_date ) : null ,
61- title : timeEntries . description ,
62- id : timeEntries . id ,
63- meta : timeEntries
64- } as CalendarEvent )
73+ timeEntries$
74+ . pipe (
75+ map ( ( timeEntriesDatasorce ) =>
76+ timeEntriesDatasorce . data . map (
77+ ( timeEntries ) =>
78+ ( {
79+ start : new Date ( timeEntries . start_date ) ,
80+ end : timeEntries . end_date ? new Date ( timeEntries . end_date ) : null ,
81+ title : timeEntries . description ,
82+ id : timeEntries . id ,
83+ meta : timeEntries ,
84+ } as CalendarEvent )
85+ )
6586 )
6687 )
67- )
68- . subscribe ( ( timeEntriesAsEvent ) => {
88+ . subscribe ( ( timeEntriesAsEvent ) => {
6989 this . timeEntriesAsEvent = [ ...timeEntriesAsEvent ] . reverse ( ) ;
70- } ) ;
90+ } ) ;
7191 }
7292
7393 handleEditEvent ( timeEntryAsEvent : CalendarEvent ) : void {
74- this . viewModal . emit ( {
75- id : timeEntryAsEvent . id
94+ this . viewModal . emit ( {
95+ id : timeEntryAsEvent . id ,
7696 } ) ;
7797 }
7898
7999 handleDeleteEvent ( timeEntryAsEvent : CalendarEvent ) : void {
80100 this . deleteTimeEntry . emit ( {
81- timeEntry : timeEntryAsEvent . meta
101+ timeEntry : timeEntryAsEvent . meta ,
82102 } ) ;
83103 }
84104
85- handleChangeDateEvent ( ) : void {
105+ handleChangeDateEvent ( ) : void {
86106 const date = this . currentDate ;
87107 this . isToday = this . isVisibleForCurrentDate ( ) ;
88108 this . navigationEnable ( this . calendarView ) ;
89- this . changeDate . emit ( { date} ) ;
109+ this . changeDate . emit ( { date } ) ;
90110 }
91111
92- changeCalendarView ( calendarView : CalendarView ) {
112+ changeCalendarView ( calendarView : CalendarView ) {
93113 this . calendarView = calendarView ;
114+ this . scrollContainer . nativeElement . scrollTop = 0 ;
115+ if ( this . calendarView !== CalendarView . Month ) {
116+ this . referenceChangeDetector . detectChanges ( ) ;
117+ this . scrollToCurrentTimeMarker ( ) ;
118+ }
94119 }
95120
96- navigationEnable ( calendarView : CalendarView ) {
121+ navigationEnable ( calendarView : CalendarView ) {
97122 let enable = false ;
98123 const currentDate = moment ( this . currentDate ) ;
99124 const initialDate = moment ( this . initialDate ) ;
100- if ( calendarView === CalendarView . Month ) {
125+ if ( calendarView === CalendarView . Month ) {
101126 if ( currentDate . month ( ) === initialDate . month ( ) && currentDate . year ( ) === initialDate . year ( ) ) {
102127 enable = true ;
103128 }
104129 }
105130 currentDate . add ( 1 , 'day' ) ;
106- if ( currentDate > initialDate ) {
131+ if ( currentDate > initialDate ) {
107132 enable = true ;
108133 }
109134 this . nextDateDisabled = enable ;
110135 }
111136
112- getTimeWork ( startDate : Date , endDate : Date ) : number {
113- if ( ! endDate ) {
137+ getTimeWork ( startDate : Date , endDate : Date ) : number {
138+ if ( ! endDate ) {
114139 return 30 ;
115140 }
116- return new SubstractDatePipe ( ) . transformInMinutes ( endDate , startDate ) ;
141+ return new SubstractDatePipe ( ) . transformInMinutes ( endDate , startDate ) ;
117142 }
118143
144+ timeIsGreaterThan ( startDate : Date , endDate : Date , timeRange : number ) : boolean {
119145 getCardEntryHeight ( startDate : Date , endDate : Date ) : number{
120146 const heightCard = this . getTimeWork ( startDate , endDate ) * this . VARIATION_HEIGHT ;
121147 const finalHeightCard = heightCard / 10 ;
@@ -131,7 +157,7 @@ export class CalendarComponent implements OnInit {
131157 return desiredView . includes ( currentCalendarView ) ;
132158 }
133159
134- isVisibleForCurrentDate ( ) : boolean {
160+ isVisibleForCurrentDate ( ) : boolean {
135161 const currentDate : Date = new Date ( this . currentDate ) ;
136162 const initialDate : Date = new Date ( this . initialDate ) ;
137163 return currentDate . setHours ( 0 , 0 , 0 , 0 ) === initialDate . setHours ( 0 , 0 , 0 , 0 ) ;
0 commit comments