import * as _ from "lodash" ; import { Helper } from "../helper" ; import { Constant } from "@elevate/shared/constants" ; import { AbstractModifier } from "./abstract.modifier" ; export class ActivitiesSummaryModifier extends AbstractModifier { protected averageSpeedOrPace ( pace , distance , time ) { time /= 60 ; if ( pace ) { const result = time / distance ; const minutes = Math . floor ( result ) ; const seconds = ( result - minutes ) * 60 ; return minutes + ":" + ( "00" + Helper . formatNumber ( seconds , 0 ) ) . slice ( - 2 ) ; } else { time /= 60 ; return Helper . formatNumber ( distance / time ) ; } } public modify ( ) { let activitiesCountElementId = "totals-activities-count" , $totals = $ ( "#totals" ) , requests = [ ] , activityTypes = [ ] , distanceUnit = "km" , elevationUnit = "m" , speedUnit = "km/h" , paceUnit = "/km" ; let speedUnitRatio = 1 ; let elevationUnitRatio = 1 ; const waitForTotalActivitiesCountRemove = ( ) => { if ( $ ( "#" + activitiesCountElementId ) . length !== 0 ) { setTimeout ( function ( ) { waitForTotalActivitiesCountRemove ( ) ; } , 1000 ) ; return ; } this . modify ( ) ; } ; const measurementPreference = window . currentAthlete ? window . currentAthlete . get ( "measurement_preference" ) : "meters" ; if ( measurementPreference != "meters" ) { distanceUnit = "mi" ; elevationUnit = "ft" ; speedUnit = "mph" ; paceUnit = "/mi" ; speedUnitRatio = Constant . KM_TO_MILE_FACTOR ; elevationUnitRatio = 3.281 ; } $totals . show ( ) ; $totals . append ( "<li id='" + activitiesCountElementId + "'></li>" ) ; $ ( "table.activitiesSummary" ) . remove ( ) ; _ . forEach ( $ ( "#interval-rides a[href='/athletes/" + window . currentAthlete . id + "'].athlete-name" ) , ( element ) => { const $this = $ ( element ) , $activityUrl = $this . prev ( ".entry-title" ) . find ( "a[href^='/activities/']" ) , icon = $this . closest ( "div.entity-details" ) . find ( "div.app-icon" ) , pace = icon . hasClass ( "icon-walk" ) || icon . hasClass ( "icon-run" ) ; if ( $activityUrl . attr ( "href" ) !== null ) { if ( $activityUrl . attr ( "href" ) ) { const activityId = parseInt ( _ . last ( $activityUrl . attr ( "href" ) . split ( "/" ) ) ) ; const url = "/athlete/training_activities/" + activityId ; requests . push ( $ . ajax ( { url , type : "GET" , dataType : "json" , context : { pace , } , } ) ) ; } } } ) ; $ . when . apply ( this , requests ) . done ( ( ) => { let index = 0 , total = { type : "Total" , count : 0 , distance : 0 , elevation : 0 , time : 0 , calories : 0 , noAverage : true , } ; _ . forEach ( requests , ( request ) => { let data = request . responseJSON , distance = data . distance_raw / 1000 * speedUnitRatio , movingTime = data . moving_time_raw , elevation = data . elevation_gain_raw * elevationUnitRatio , calories = data . calories || 0 , type = data . display_type , summary ; if ( ! ( summary = activityTypes [ type ] ) ) { index += 1 ; activityTypes [ type ] = activityTypes [ index ] = summary = { type , count : 0 , distance : 0 , elevation : 0 , time : 0 , calories : 0 , index , } ; } summary . pace = ( request . length ? request . pace : request . pace ) || summary . pace ; summary . count += 1 ; summary . distance += distance ; summary . elevation += elevation ; summary . time += movingTime ; summary . calories += calories ; total . count += 1 ; total . distance += distance ; total . elevation += elevation ; total . time += movingTime ; total . calories += calories ; } ) ; activityTypes . sort ( ( left , right ) => { return left . type . localeCompare ( right . type ) ; } ) ; if ( activityTypes . length > 2 ) { activityTypes . push ( total ) ; } const $table = $ ( "<table class='activitiesSummary'><thead><tr><th>Type</th><th style='text-align: right'>Number</th><th style='text-align: right'>Distance</th><th style='text-align: right'>Time</th><th style='text-align: right'>Avg speed/pace</th><th style='text-align: right'>Elevation</th><th style='text-align: right'>Calories</th></tr></thead><tbody></tbody></table>" ) ; activityTypes . forEach ( ( type ) => { const $row = $ ( "<tr></tr>" ) ; $row . append ( "<td>" + type . type + "</td>" ) ; $row . append ( "<td style='text-align: right'>" + type . count + "</td>" ) ; $row . append ( "<td style='text-align: right'>" + Helper . formatNumber ( Math . abs ( type . distance ) , 1 ) + " " + distanceUnit + "</td>" ) ; $row . append ( "<td style='text-align: right'>" + Helper . secondsToDHM ( type . time , true ) + "</td>" ) ; $row . append ( "<td style='text-align: right'>" + ( type . noAverage ? "" : ( this . averageSpeedOrPace ( type . pace , type . distance , type . time ) + " " + ( type . pace ? paceUnit : speedUnit ) ) ) + "</td>" ) ; $row . append ( "<td style='text-align: right'>" + Helper . formatNumber ( Math . abs ( type . elevation ) , 0 ) + " " + elevationUnit + "</td>" ) ; $row . append ( "<td style='text-align: right'>" + Helper . formatNumber ( Math . abs ( type . calories ) , 0 ) + "</td>" ) ; $table . find ( "tbody" ) . append ( $row ) ; } ) ; $totals . before ( $table ) ; $totals . hide ( ) ; waitForTotalActivitiesCountRemove ( ) ; } ) ; } }