import { ChangeDetectorRef , Component , Input , OnInit } from '@angular/core' ; import { Settings } from '../../../classes/settings/settings' ; import { UISettingsStorage } from '../../../services/uiSettingsStorage' ; import { UIAnalytics } from '../../../services/uiAnalytics' ; import { Preparation } from '../../../classes/preparation/preparation' ; import { UIPreparationStorage } from '../../../services/uiPreparationStorage' ; import PREPARATION_TRACKING from '../../../data/tracking/preparationTracking' ; import SETTINGS_TRACKING from '../../../data/tracking/settingsTracking' ; @ Component ( { selector : 'sort-custom-parameter' , templateUrl : './sort-custom-parameter.component.html' , styleUrls : [ './sort-custom-parameter.component.scss' ] , } ) export class SortCustomParameterComponent implements OnInit { public brewOrdersBefore : Array < { number : number ; label : string ; enum : string ; } > = [ ] ; public brewOrdersWhile : Array < { number : number ; label : string ; enum : string ; } > = [ ] ; public brewOrdersAfter : Array < { number : number ; label : string ; enum : string ; } > = [ ] ; @ Input ( ) public data : Settings | Preparation ; constructor ( public uiSettingsStorage , private readonly uiPreparationStorage , private readonly uiAnalytics , private readonly changeDetectorRef ) { } public ngOnInit ( ) { this . __initializeData ( ) ; } public async save ( ) { this . changeDetectorRef . detectChanges ( ) ; if ( this . data instanceof Settings ) { await this . uiSettingsStorage . saveSettings ( this . data as Settings ) ; } else { await this . uiPreparationStorage . update ( this . data as Preparation ) ; } } public reorder_brew ( ev , _type ) { if ( this . data instanceof Settings ) { this . uiAnalytics . trackEvent ( SETTINGS_TRACKING . TITLE , SETTINGS_TRACKING . ACTIONS . REORDER_BREW ) ; } else { this . uiAnalytics . trackEvent ( PREPARATION_TRACKING . TITLE , PREPARATION_TRACKING . ACTIONS . REORDER_BREW ) ; } let reorderVar = [ ] ; if ( _type === 'before' ) { reorderVar = this . brewOrdersBefore ; } else if ( _type === 'while' ) { reorderVar = this . brewOrdersWhile ; } else if ( _type === 'after' ) { reorderVar = this . brewOrdersAfter ; } reorderVar . splice ( ev . detail . to , 0 , reorderVar . splice ( ev . detail . from , 1 ) [ 0 ] ) ; let count = 0 ; for ( const order of reorderVar ) { order . number = count ; this . data . brew_order [ _type ] [ order . enum ] = order . number ; count ++ ; } ev . detail . complete ( ) ; this . save ( ) ; } private __initializeData ( ) { this . __initializeBrewOrders ( 'before' ) ; this . __initializeBrewOrders ( 'while' ) ; this . __initializeBrewOrders ( 'after' ) ; } private __initializeBrewOrders ( _type ) { let initializeOrder ; switch ( _type ) { case 'before' : initializeOrder = this . brewOrdersBefore ; break ; case 'while' : initializeOrder = this . brewOrdersWhile ; break ; case 'after' : initializeOrder = this . brewOrdersAfter ; break ; } for ( const key in this . data . brew_order [ _type ] ) { if ( this . data . brew_order [ _type ] . hasOwnProperty ( key ) ) { initializeOrder . push ( { number : this . data . brew_order [ _type ] [ key ] , label : this . data . brew_order . getLabel ( key ) , enum : key , } ) ; } } initializeOrder . sort ( ( obj1 , obj2 ) => { if ( obj1 . number > obj2 . number ) { return 1 ; } if ( obj1 . number < obj2 . number ) { return - 1 ; } return 0 ; } ) ; } }