import { Component , EventEmitter , Input , OnInit , Output } from "@angular/core" ; import { ZonesService } from "../shared/zones.service" ; import { MatDialog , MatSnackBar } from "@angular/material" ; import { ZonesImportExportDialogComponent } from "../zones-import-export-dialog/zones-import-export-dialog.component" ; import { ConfirmDialogComponent } from "../../shared/dialogs/confirm-dialog/confirm-dialog.component" ; import { ConfirmDialogDataModel } from "../../shared/dialogs/confirm-dialog/confirm-dialog-data.model" ; import { ZoneImportExportDataModel } from "../zones-import-export-dialog/zone-import-export-data.model" ; import { Mode } from "../zones-import-export-dialog/mode.enum" ; import { ZoneDefinitionModel } from "../../shared/models/zone-definition.model" ; import { LoggerService } from "../../shared/services/logging/logger.service" ; @ Component ( { selector : "app-zone-tool-bar" , templateUrl : "./zone-tool-bar.component.html" , styleUrls : [ "./zone-tool-bar.component.scss" ] } ) export class ZoneToolBarComponent implements OnInit { @ Input ( "currentZonesLength" ) public currentZonesLength : number ; @ Input ( "zoneDefinitions" ) public zoneDefinitions : ZoneDefinitionModel [ ] ; @ Input ( "zoneDefinitionSelected" ) public zoneDefinitionSelected : ZoneDefinitionModel ; @ Output ( "zoneDefinitionSelectedChange" ) public zoneDefinitionSelectedChange : EventEmitter < ZoneDefinitionModel > = new EventEmitter < ZoneDefinitionModel > ( ) ; constructor ( public zonesService , private dialog , private snackBar , public logger ) { } public ngOnInit ( ) { } public onZoneDefinitionSelected ( ) { this . zoneDefinitionSelectedChange . emit ( this . zoneDefinitionSelected ) ; } public onStepChange ( ) { this . zonesService . notifyStepChange ( this . zoneDefinitionSelected . step ) ; } public onAddLastZone ( ) { this . zonesService . addLastZone ( ) . then ( message => this . popSnack ( message ) , error => { this . logger . error ( error ) ; this . popSnack ( error ) ; } ) ; } public onRemoveLastZone ( ) { this . zonesService . removeLastZone ( ) . then ( message => this . popSnack ( message ) , error => { this . logger . error ( error ) ; this . popSnack ( error ) ; } ) ; } public onResetZonesToDefault ( ) { const data = { title : "Reset <" + this . zonesService . zoneDefinition . name + "> zones" , content : "Are you sure? Previous data will be lost." } ; const dialogRef = this . dialog . open ( ConfirmDialogComponent , { minWidth : ConfirmDialogComponent . MIN_WIDTH , maxWidth : ConfirmDialogComponent . MAX_WIDTH , data : data } ) ; const afterClosedSubscription = dialogRef . afterClosed ( ) . subscribe ( ( confirm ) => { if ( confirm ) { this . zonesService . resetZonesToDefault ( ) . then ( ( ) => { this . popSnack ( this . zonesService . zoneDefinition . name + " zones have been set to default" ) ; } , error => { this . logger . error ( error ) ; this . popSnack ( error ) ; } ) ; } afterClosedSubscription . unsubscribe ( ) ; } ) ; } public onSaveZones ( ) { this . zonesService . saveZones ( ) . then ( ( ) => this . popSnack ( this . zonesService . zoneDefinition . name + " zones have been saved" ) , error => { this . logger . error ( error ) ; this . popSnack ( error ) ; } ) ; } public onImportZones ( ) { const importExportData = { zoneDefinition : this . zonesService . zoneDefinition , mode : Mode . IMPORT } ; this . dialog . open ( ZonesImportExportDialogComponent , { minWidth : ZonesImportExportDialogComponent . MIN_WIDTH , maxWidth : ZonesImportExportDialogComponent . MAX_WIDTH , data : importExportData } ) ; } public onExportZones ( ) { const importExportData = { zoneDefinition : this . zonesService . zoneDefinition , zonesData : this . zonesService . currentZones , mode : Mode . EXPORT } ; this . dialog . open ( ZonesImportExportDialogComponent , { minWidth : ZonesImportExportDialogComponent . MIN_WIDTH , maxWidth : ZonesImportExportDialogComponent . MAX_WIDTH , data : importExportData } ) ; } private popSnack ( message ) { this . snackBar . open ( message , "Close" , { duration : 2500 } ) ; } }