import { Injectable } from '@angular/core' ; import { ControlContainer } from '@angular/forms' ; import { TranslateService } from '@ngx-translate/core' ; import { NgbNav , NgbNavChangeEvent } from '@ng-bootstrap/ng-bootstrap' ; import { Subscription } from 'rxjs' ; import { ObUnsavedChangesService } from '../unsaved-changes/unsaved-changes.service' ; @ Injectable ( { providedIn : 'root' } ) export class ObUnsavedChangesTabsService { private readonly listener : { [ key ] : Subscription ; } = { } ; constructor ( private readonly translateService , private readonly unsavedChanges ) { } watch ( formId , form ) { this . unsavedChanges . watch ( formId , form ) ; } unWatch ( formId ) { this . unsavedChanges . unWatch ( formId ) ; } listenTo ( ngbNav ) { const id = ngbNav . items . first . id ; if ( ! this . listener [ id ] ) { this . listener [ id ] = ngbNav . navChange . subscribe ( ( event ) : void => { if ( ! this . ignoreChanges ( [ event . activeId ] ) ) { event . preventDefault ( ) ; } } ) ; } } unListenTo ( ngbNav ) { const id = ngbNav ?. items . first . id ; if ( this . listener [ id ] ) { this . listener [ id ] . unsubscribe ( ) ; delete this . listener [ id ] ; } } canDeactivate ( ) { return this . unsavedChanges . canDeactivate ( ) ; } ignoreChanges ( formIds ) { return this . unsavedChanges . ignoreChanges ( formIds ) ; } }