import { Component , Input , OnChanges , OnInit , SimpleChanges } from "@angular/core" ; import { YearProgressModel } from "../shared/models/year-progress.model" ; import { YearProgressTypeModel } from "../shared/models/year-progress-type.model" ; import * as moment from "moment" ; import { Moment } from "moment" ; import { ProgressAtDayModel } from "../shared/models/progress-at-date.model" ; import { YearProgressService } from "../shared/services/year-progress.service" ; import { ProgressType } from "../shared/enums/progress-type.enum" ; import * as _ from "lodash" ; import { ProgressionAtDayRow } from "./models/progression-at-day-row.model" ; import { YearProgressStyleModel } from "../year-progress-graph/models/year-progress-style.model" ; import { DeltaType } from "./models/delta-type.enum" ; import { MatTableDataSource } from "@angular/material" ; import { Delta } from "./models/delta.model" ; import { TargetProgressModel } from "../shared/models/target-progress.model" ; import { ProgressConfig } from "../shared/interfaces/progress-config" ; import { ProgressMode } from "../shared/enums/progress-mode.enum" ; @ Component ( { selector : "app-year-progress-table" , templateUrl : "./year-progress-table.component.html" , styleUrls : [ "./year-progress-table.component.scss" ] } ) export class YearProgressTableComponent implements OnInit , OnChanges { public static readonly COLUMN_YEAR : string = "year" ; public static readonly COLUMN_PROGRESS_TYPE_VALUE : string = "progressTypeValue" ; public static readonly COLUMN_DELTA_PREVIOUS_VALUE : string = "deltaPrevious" ; public static readonly COLUMN_DELTA_CURRENT_VALUE : string = "deltaCurrent" ; public static readonly COLUMN_DELTA_CURRENT_TARGET : string = "deltaTarget" ; public static readonly DELTA_SIGN_POSITIVE : string = "+" ; public static readonly DELTA_SIGN_NEGATIVE : string = "-" ; public static readonly DELTA_SIGN_UNSIGNED : string = "" ; public displayedColumns : string [ ] = [ YearProgressTableComponent . COLUMN_YEAR , YearProgressTableComponent . COLUMN_PROGRESS_TYPE_VALUE , YearProgressTableComponent . COLUMN_DELTA_PREVIOUS_VALUE , YearProgressTableComponent . COLUMN_DELTA_CURRENT_VALUE , YearProgressTableComponent . COLUMN_DELTA_CURRENT_TARGET ] ; public readonly ProgressType = ProgressType ; public readonly DeltaType = DeltaType ; public readonly ProgressMode = ProgressMode ; public todayMoment : Moment ; public currentYear : number ; public currentYearProgressAtDayModel : ProgressAtDayModel ; public dataSource : MatTableDataSource < ProgressionAtDayRow > ; public initialized = false ; @ Input ( "progressConfig" ) public progressConfig : ProgressConfig ; @ Input ( "hideYearsColumn" ) public hideYearsColumn : boolean ; @ Input ( "selectedYears" ) public selectedYears : number [ ] ; @ Input ( "momentWatched" ) public momentWatched : Moment ; @ Input ( "selectedProgressType" ) public selectedProgressType : YearProgressTypeModel ; @ Input ( "yearProgressions" ) public yearProgressions : YearProgressModel [ ] ; @ Input ( "targetProgressModels" ) public targetProgressModels : TargetProgressModel [ ] ; @ Input ( "yearProgressStyleModel" ) public yearProgressStyleModel : YearProgressStyleModel ; constructor ( public yearProgressService ) { } public ngOnInit ( ) { this . todayMoment = moment ( ) ; this . currentYear = this . todayMoment . year ( ) ; if ( this . hideYearsColumn ) { this . displayedColumns = _ . remove ( this . displayedColumns , ( column ) => { return ( column !== YearProgressTableComponent . COLUMN_YEAR ) ; } ) ; } this . yearProgressService . momentWatchedChanges . subscribe ( ( momentWatched ) => { if ( this . momentWatched . isSame ( momentWatched ) ) { return ; } this . momentWatched = momentWatched ; this . updateData ( ) ; } ) ; this . updateData ( ) ; this . initialized = true ; } public ngOnChanges ( changes ) { if ( ! this . initialized ) { return ; } if ( this . targetProgressModels ) { const hasDeltaTargetColumn = _ . indexOf ( this . displayedColumns , YearProgressTableComponent . COLUMN_DELTA_CURRENT_TARGET ) !== - 1 ; if ( ! hasDeltaTargetColumn ) { this . displayedColumns . push ( YearProgressTableComponent . COLUMN_DELTA_CURRENT_TARGET ) ; } } else { this . displayedColumns = _ . remove ( this . displayedColumns , ( column ) => { return ( column !== YearProgressTableComponent . COLUMN_DELTA_CURRENT_TARGET ) ; } ) ; } this . updateData ( ) ; } public updateData ( ) { this . currentYearProgressAtDayModel = _ . first ( this . yearProgressService . findProgressionsAtDay ( this . yearProgressions , this . momentWatched , this . selectedProgressType . type , [ this . currentYear ] , this . yearProgressStyleModel . yearsColorsMap ) ) ; const progressAtDayModels = this . yearProgressService . findProgressionsAtDay ( this . yearProgressions , this . momentWatched , this . selectedProgressType . type , this . selectedYears , this . yearProgressStyleModel . yearsColorsMap ) ; const targetProgressModel = ( this . targetProgressModels ) ? _ . find ( this . targetProgressModels , { dayOfYear : this . momentWatched . dayOfYear ( ) } ) : null ; this . dataSource = new MatTableDataSource < ProgressionAtDayRow > ( ) ; this . dataSource . data = this . rows ( progressAtDayModels , targetProgressModel ) ; } public rows ( progressAtDayModels , targetProgressModel ) { const progressionAtDayRows = [ ] ; _ . forEach ( progressAtDayModels , ( progressAtDayModel , index ) => { const previousYearProgressAtDay = progressAtDayModels [ index + 1 ] ; const deltaPreviousYear = this . getDeltaValueBetween ( progressAtDayModel , previousYearProgressAtDay ) ; const deltaCurrentYear = this . getDeltaValueBetween ( progressAtDayModel , this . currentYearProgressAtDayModel ) ; const deltaTarget = ( targetProgressModel ) ? this . getDeltaFromTarget ( progressAtDayModel , targetProgressModel ) : null ; const progressionAtDayRow = { year : progressAtDayModel . year , color : progressAtDayModel . color , progressTypeLabel : this . selectedProgressType . label , progressTypeUnit : ( this . selectedProgressType . shortUnit ) ? this . selectedProgressType . shortUnit : "" , currentValue : progressAtDayModel . value , deltaPreviousYear : deltaPreviousYear , deltaCurrentYear : deltaCurrentYear , deltaTarget : deltaTarget } ; progressionAtDayRows . push ( progressionAtDayRow ) ; } ) ; return progressionAtDayRows ; } public getDeltaFromTarget ( progressAtDayModel , targetProgressModel ) { const deltaValue = ( _ . isNumber ( targetProgressModel . value ) ) ? Math . floor ( progressAtDayModel . value - targetProgressModel . value ) : null ; let deltaType ; let deltaSignSymbol ; if ( _ . isNull ( deltaValue ) ) { deltaType = DeltaType . NAN ; deltaSignSymbol = null ; } else if ( deltaValue === 0 ) { deltaType = DeltaType . UNSIGNED ; deltaSignSymbol = YearProgressTableComponent . DELTA_SIGN_UNSIGNED ; } else if ( deltaValue < 0 ) { deltaType = DeltaType . NEGATIVE ; deltaSignSymbol = YearProgressTableComponent . DELTA_SIGN_NEGATIVE ; } else { deltaType = DeltaType . POSITIVE ; deltaSignSymbol = YearProgressTableComponent . DELTA_SIGN_POSITIVE ; } return { type : deltaType , date : ( progressAtDayModel ) ? moment ( progressAtDayModel . date ) . format ( "MMMM DD, YYYY" ) : null , value : ( ! _ . isNull ( deltaValue ) ) ? Math . abs ( deltaValue ) : null , signSymbol : deltaSignSymbol , class : ( progressAtDayModel . year === this . currentYear && this . momentWatched . dayOfYear ( ) > this . todayMoment . dayOfYear ( ) ) ? DeltaType . NAN : deltaType . toString ( ) } ; } public getDeltaValueBetween ( progressAtDayModel_A , progressAtDayModel_B ) { const isSameYearComparison = ( progressAtDayModel_A && progressAtDayModel_B && progressAtDayModel_A . year === progressAtDayModel_B . year ) ; const B_value = ( progressAtDayModel_B && _ . isNumber ( progressAtDayModel_B . value ) ) ? progressAtDayModel_B . value : null ; const deltaValue = ( _ . isNumber ( B_value ) ) ? ( progressAtDayModel_A . value - B_value ) : null ; let deltaType ; let deltaSignSymbol ; if ( _ . isNull ( deltaValue ) || isSameYearComparison ) { deltaType = DeltaType . NAN ; deltaSignSymbol = null ; } else if ( deltaValue === 0 ) { deltaType = DeltaType . UNSIGNED ; deltaSignSymbol = YearProgressTableComponent . DELTA_SIGN_UNSIGNED ; } else if ( deltaValue < 0 ) { deltaType = DeltaType . NEGATIVE ; deltaSignSymbol = YearProgressTableComponent . DELTA_SIGN_NEGATIVE ; } else { deltaType = DeltaType . POSITIVE ; deltaSignSymbol = YearProgressTableComponent . DELTA_SIGN_POSITIVE ; } return { type : deltaType , date : ( progressAtDayModel_B ) ? moment ( progressAtDayModel_B . date ) . format ( "MMMM DD, YYYY" ) : null , value : ( ! _ . isNull ( deltaValue ) ) ? Math . abs ( deltaValue ) : null , signSymbol : deltaSignSymbol , class : deltaType . toString ( ) , } ; } param { number } hours returns { string } public readableTimeProgress ( hours ) { return this . yearProgressService . readableTimeProgress ( hours ) ; } }