import { Component , OnDestroy , OnInit } from "@angular/core" ; import { UserSettingsService } from "../shared/services/user-settings/user-settings.service" ; import { UserSettingsModel } from "@elevate/shared/models" ; import { userSettingsData } from "@elevate/shared/data" ; import { GlobalSettingsService } from "./services/global-settings.service" ; import * as _ from "lodash" ; import { MatDialog } from "@angular/material" ; import { ActivatedRoute } from "@angular/router" ; import { OptionHelperDialogComponent } from "./option-helper-dialog/option-helper-dialog.component" ; import { OptionHelperReaderService } from "./services/option-helper-reader.service" ; import { PlatformLocation } from "@angular/common" ; import { SectionModel } from "./models/section.model" ; import { OptionModel } from "./models/option.model" ; import { OptionHelperDataModel } from "./option-helper-dialog/option-helper-data.model" ; import { Subscription } from "rxjs" ; import { LoggerService } from "../shared/services/logging/logger.service" ; @ Component ( { selector : "app-global-settings" , templateUrl : "./global-settings.component.html" , styleUrls : [ "./global-settings.component.scss" ] , } ) export class GlobalSettingsComponent implements OnInit , OnDestroy { public sections : SectionModel [ ] ; public searchText = null ; public routeQueryParamsSubscription : Subscription ; public static getOptionHelperDir ( pathname ) { if ( _ . isEmpty ( pathname ) ) { return null ; } const pathNames = pathname . split ( "/" ) ; pathNames . pop ( ) ; return pathNames . join ( "/" ) + "/assets/option-helpers/" ; } constructor ( public platformLocation , public userSettingsService , public globalSettingsService , public optionHelperReaderService , public route , public dialog , public logger ) { } public ngOnInit ( ) { this . sections = this . globalSettingsService . sections ; this . userSettingsService . fetch ( ) . then ( ( userSettingsSynced ) => { this . renderOptionsForEachSection ( userSettingsSynced ) ; } ) ; this . routeQueryParamsSubscription = this . route . queryParams . subscribe ( params => { if ( ! _ . isEmpty ( params . searchText ) ) { this . searchText = params . searchText ; } if ( ! _ . isEmpty ( params . viewOptionHelperId ) ) { _ . defer ( ( ) => this . showOptionHelperDialog ( params . viewOptionHelperId ) ) ; } } ) ; } param { UserSettingsModel } userSettingsSynced public renderOptionsForEachSection ( userSettingsSynced ) { _ . forEach ( this . sections , ( section ) => { _ . forEach ( section . options , ( option ) => { if ( option . type === GlobalSettingsService . TYPE_OPTION_CHECKBOX ) { option . active = _ . propertyOf ( userSettingsSynced ) ( option . key ) ; if ( option . enableSubOption ) { _ . forEach ( option . enableSubOption , ( subKey ) => { this . displaySubOption ( subKey , _ . propertyOf ( userSettingsSynced ) ( option . key ) ) ; } ) ; } } else if ( option . type === GlobalSettingsService . TYPE_OPTION_LIST ) { option . active = _ . find ( option . list , { key : _ . propertyOf ( userSettingsSynced ) ( option . key ) , } ) ; } else if ( option . type === GlobalSettingsService . TYPE_OPTION_NUMBER ) { option . value = _ . propertyOf ( userSettingsSynced ) ( option . key ) ; } else { this . logger . error ( "Option type not supported" ) ; } } ) ; } ) ; } param { OptionModel } option public onOptionChange ( option ) { if ( option . type === GlobalSettingsService . TYPE_OPTION_CHECKBOX ) { this . userSettingsService . saveProperty ( option . key , option . active ) . then ( ( ) => { this . logger . info ( option . key + " has been updated to " , option . active ) ; } ) ; if ( option . enableSubOption ) { _ . forEach ( option . enableSubOption , ( subKey ) => { this . displaySubOption ( subKey , option . active ) ; } ) ; } } else if ( option . type === GlobalSettingsService . TYPE_OPTION_LIST ) { this . userSettingsService . saveProperty ( option . key , option . active . key ) . then ( ( ) => { this . logger . info ( option . key + " has been updated to " , option . active ) ; } ) ; } else if ( option . type === GlobalSettingsService . TYPE_OPTION_NUMBER ) { if ( _ . isNull ( option . value ) || _ . isUndefined ( option . value ) || ! _ . isNumber ( option . value ) ) { this . resetOptionToDefaultValue ( option ) ; } else { if ( option . value < option . min || option . value > option . max ) { this . resetOptionToDefaultValue ( option ) ; } this . userSettingsService . saveProperty ( option . key , option . value ) . then ( ( ) => { this . logger . info ( option . key + " has been updated to " + option . value ) ; } ) ; } } } param { OptionModel } option public resetOptionToDefaultValue ( option ) { const resetValue = _ . propertyOf ( userSettingsData ) ( option . key ) ; this . logger . info ( option . key + " value not compliant, Reset to  " + resetValue ) ; option . value = resetValue ; } param { string } subOptionKey param { boolean } show public displaySubOption ( subOptionKey , show ) { _ . forEach ( this . sections , ( section ) => { const foundOption = _ . find ( section . options , { key : subOptionKey , } ) ; if ( foundOption ) { foundOption . hidden = ! show ; } } ) ; } param { string } optionKeyParam public showOptionHelperDialog ( optionKeyParam ) { let option = null ; _ . forEach ( this . sections , ( section ) => { const foundOption = _ . find ( section . options , { key : optionKeyParam , } ) ; if ( foundOption ) { option = foundOption ; } } ) ; if ( option ) { const pathName = this . platformLocation [ "location" ] . pathname ; const markdownTemplateUri = GlobalSettingsComponent . getOptionHelperDir ( pathName ) + option . key + ".md" ; this . optionHelperReaderService . get ( markdownTemplateUri ) . then ( markdownData => { const optionHelperData = { title : option . title , markdownData : markdownData } ; this . dialog . open ( OptionHelperDialogComponent , { minWidth : OptionHelperDialogComponent . MIN_WIDTH , maxWidth : OptionHelperDialogComponent . MAX_WIDTH , data : optionHelperData , autoFocus : false } ) ; } ) ; } } public ngOnDestroy ( ) { this . routeQueryParamsSubscription . unsubscribe ( ) ; } }