import { DashboardService } from './../../../services/api/dashboard.service' ; import { Component , OnInit } from '@angular/core' ; import { Router } from '@angular/router' ; import { User } from '@eda/models/model.index' ; import { SidebarService , UserService , AlertService , DataSourceService , StyleProviderService } from '@eda/services/service.index' ; import { LogoSidebar } from '@eda/configs/index' ; import Swal , { SweetAlertOptions } from 'sweetalert2' ; @ Component ( { selector : 'app-sidebar' , templateUrl : './sidebar.component.html' } ) export class SidebarComponent implements OnInit { user : User ; isAdmin : boolean ; dataSourceMenu : any [ ] = [ ] ; edit_mode : boolean = true ; mobileSize : boolean = false ; sideBtn : boolean = false ; logoSidebar : string ; homeLink = '/home' ; constructor ( public router , public userService , public sidebarService , private alertService , private dashboardService , public dataSourceService , public styleProviderService ) { this . logoSidebar = LogoSidebar ; this . sidebarService . getToggleSideNav ( ) . subscribe ( ( value ) => { this . sideBtn = value ; } ) ; this . getMobileSize ( ) ; this . getDataSourcesNames ( ) ; } ngOnInit ( ) { this . user = this . userService . getUserObject ( ) ; this . setEditMode ( ) ; this . sidebarService . currentDatasources . subscribe ( data => this . dataSourceMenu = data , err => this . alertService . addError ( err ) ) ; } getMobileSize ( event ? ) { if ( ! event ) { if ( window . innerWidth <= 767 ) { this . mobileSize = true ; this . sideBtn = true ; this . sidebarService . toggleSideNav = true ; } else if ( window . innerWidth > 767 ) { this . mobileSize = false ; this . sidebarService . setManualHideSideNav ( false ) ; } } else { if ( event . target . innerWidth <= 767 ) { this . mobileSize = true ; this . sideBtn = true ; this . sidebarService . toggleSideNav = true ; } else if ( event . target . innerWidth > 767 ) { this . mobileSize = false ; this . sidebarService . toggleSideNav = false ; this . sidebarService . setManualHideSideNav ( false ) ; } } } toggleClassSide ( ) { this . sidebarService . setHideSideNav ( ) ; } setEditMode ( ) { const user = sessionStorage . getItem ( 'user' ) ; const userName = JSON . parse ( user ) . name ; this . edit_mode = ( userName !== 'edaanonim' ) ; } logout ( ) { this . userService . logout ( ) ; } getDataSourcesNames ( ) { this . sidebarService . getDataSourceNames ( ) ; } goToDataSource ( datasource ) { if ( datasource ) { this . styleProviderService . setDefaultBackgroundColor ( ) ; if ( this . dashboardService . _notSaved . value === false ) { this . router . navigate ( [ '/data-source/' , datasource . _id ] ) ; } else { this . dashboardService . _notSaved . next ( false ) ; Swal . fire ( { text : $localize `:@@NotSavedWarning:Hay cambios sin guardar. ¿Seguro que quieres salir?` , icon : 'warning' , showDenyButton : true , denyButtonText : $localize `:@@cancelarButton:Cancelar` , } ) . then ( ( result ) => { if ( result . isConfirmed ) { this . router . navigate ( [ '/data-source/' , datasource . _id ] ) ; } } ) ; } } else { this . alertService . addError ( 'Ha ocurrido un error' ) ; } } ignoreNotSaved ( ) { this . styleProviderService . setDefaultBackgroundColor ( ) ; this . dashboardService . _notSaved . next ( false ) ; } public redirectLocale ( lan ) { let baseUrl = window . location . href . split ( '#' ) [ 0 ] ; if ( baseUrl . substr ( - 4 ) == '/es/' || baseUrl . substr ( - 4 ) == '/ca/' || baseUrl . substr ( - 4 ) == '/en/' ) { baseUrl = baseUrl . substr ( 0 , baseUrl . length - 3 ) ; } switch ( lan ) { case 'EN' : window . location . href = baseUrl + 'en/#/home' ; break ; case 'CAT' : window . location . href = baseUrl + 'ca/#/home' ; break ; case 'ES' : window . location . href = baseUrl + 'es/#/home' ; break ; } } public checkNotSaved ( ) { this . styleProviderService . setDefaultBackgroundColor ( ) ; let url = window . location . href ; if ( url . includes ( 'data-source' ) ) { this . checkNotSavedDatasource ( ) ; } else if ( url . includes ( 'dashboard' ) ) { this . checkNotSavedHome ( ) ; } else { this . router . navigate ( [ '/home/' ] ) ; } } public checkNotSavedHome ( ) { const options = { text : $localize `:@@NotSavedWarning:Hay cambios sin guardar. ¿Seguro que quieres salir?` , icon : 'warning' , showDenyButton : true , denyButtonText : $localize `:@@cancelarButton:Cancelar` , } as SweetAlertOptions ; if ( this . dashboardService . _notSaved . value === false ) { this . router . navigate ( [ '/home/' ] ) ; } else { Swal . fire ( options ) . then ( ( result ) => { if ( result . isConfirmed ) { this . dashboardService . _notSaved . next ( false ) ; this . router . navigate ( [ '/home/' ] ) ; } } ) ; } } public checkNotSavedDatasource ( ) { const options = { text : $localize `:@@NotSavedWarning:Hay cambios sin guardar. ¿Seguro que quieres salir?` , icon : 'warning' , showDenyButton : true , denyButtonText : $localize `:@@cancelarButton:Cancelar` , } as SweetAlertOptions ; if ( this . dataSourceService . _unsaved . value === false ) { this . router . navigate ( [ '/home/' ] ) ; } else { Swal . fire ( options ) . then ( ( result ) => { if ( result . isConfirmed ) { this . dataSourceService . _unsaved . next ( false ) ; this . router . navigate ( [ '/home/' ] ) ; } } ) ; } } }