import { Component , OnInit , OnDestroy , ViewEncapsulation , Input , NgZone } from '@angular/core' ; import { Notebook } from '../../data/entities/notebook' ; import { remote } from 'electron' ; import { Constants } from '../../core/constants' ; import { NoteDetailsResult } from '../../services/results/note-details-result' ; import { Utils } from '../../core/utils' ; @ Component ( { selector : 'app-notebook-switcher' , templateUrl : './notebook-switcher.component.html' , styleUrls : [ './notebook-switcher.component.scss' ] , encapsulation : ViewEncapsulation . None } ) export class NotebookSwitcherComponent implements OnInit , OnDestroy { private globalEmitter : any = remote . getGlobal ( 'globalEmitter' ) ; private languageChangedListener : any = this . languageChangedHandler . bind ( this ) ; private notebookChangedListener : any = this . notebookChangedHandler . bind ( this ) ; constructor ( private zone ) { } public selectedNotebookName : string ; @ Input ( ) public noteId : string ; public notebooks : Notebook [ ] ; public ngOnDestroy ( ) { this . removeListeners ( ) ; } public async ngOnInit ( ) { this . addListeners ( ) ; while ( ! this . noteId ) { await Utils . sleep ( 50 ) ; } this . getNotebookName ( ) ; } private removeListeners ( ) { this . globalEmitter . removeListener ( Constants . notebookChangedEvent , this . notebookChangedListener ) ; this . globalEmitter . removeListener ( Constants . languageChangedEvent , this . languageChangedListener ) ; } private addListeners ( ) { this . globalEmitter . on ( Constants . notebookChangedEvent , this . notebookChangedListener ) ; this . globalEmitter . on ( Constants . languageChangedEvent , this . languageChangedListener ) ; } public getNotebooks ( ) { this . globalEmitter . emit ( Constants . getNotebooksEvent , this . getNotebooksCallback . bind ( this ) ) ; } public changeNotebook ( notebook ) { this . globalEmitter . emit ( Constants . setNotebookEvent , notebook . id , [ this . noteId ] ) ; } private getNotebookName ( ) { this . globalEmitter . emit ( Constants . getNoteDetailsEvent , this . noteId , this . getNotebookNameCallback . bind ( this ) ) ; } private getNotebooksCallback ( notebooks ) { this . notebooks = notebooks ; } private getNotebookNameCallback ( result ) { this . zone . run ( ( ) => { this . selectedNotebookName = result . notebookName ; } ) ; } private notebookChangedHandler ( noteId , notebookName ) { if ( this . noteId === noteId ) { this . zone . run ( ( ) => this . selectedNotebookName = notebookName ) ; } } private languageChangedHandler ( noteId ) { this . getNotebookName ( ) ; } }