import { Component } from '@angular/core' ; import * as fs from 'fs' ; import * as path from 'path' ; import { Menu , MenuItemConstructorOptions , OpenDialogOptions , remote } from 'electron' ; import { Hero } from './model/hero' ; import { Settings } from './model/settings' ; import { TheDb } from './model/thedb' ; import '../assets/sass/style.scss' ; @ Component ( { selector : 'mycmp-app' , templateUrl : 'app.component.html' , } ) export class AppComponent { public heroes : Hero [ ] ; constructor ( ) { Settings . initialize ( ) ; if ( fs . existsSync ( Settings . dbPath ) ) { this . openDb ( Settings . dbPath ) ; } else if ( Settings . hasFixedDbLocation ) { this . createDb ( Settings . dbPath ) ; } else { this . createDb ( ) ; } } public openDb ( filename ) { TheDb . openDb ( filename ) . then ( ( ) => { if ( ! Settings . hasFixedDbLocation ) { Settings . dbPath = filename ; Settings . write ( ) ; } } ) . then ( ( ) => { this . getHeroes ( ) ; } ) . catch ( ( reason ) => { console . log ( 'Error occurred while opening database: ' , reason ) ; } ) ; } public async createDb ( filename ? ) { if ( ! filename ) { const options = { title : 'Create file' , defaultPath : remote . app . getPath ( 'documents' ) , filters : [ { name : 'Database' , extensions : [ 'db' ] , } , ] , } ; filename = await remote . dialog . showSaveDialogSync ( remote . getCurrentWindow ( ) , options ) ; } if ( ! filename ) { return ; } TheDb . createDb ( filename ) . then ( ( dbPath ) => { if ( ! Settings . hasFixedDbLocation ) { Settings . dbPath = dbPath ; Settings . write ( ) ; } } ) . then ( ( ) => { this . getHeroes ( ) ; } ) . catch ( ( reason ) => { console . log ( reason ) ; } ) ; } public onRestoreDb ( ) { TheDb . importJson ( path . join ( Settings . dbFolder , 'database.init.json' ) , false ) . then ( ( ) => { this . getHeroes ( ) ; } ) ; } public getHeroes ( ) { Hero . getAll ( ) . then ( ( heroes ) => { this . heroes = heroes ; } ) ; } public onMenu ( hero ) { const menu = this . initMenu ( hero ) ; menu . popup ( { } ) ; } private deleteHero ( hero ) { hero . delete ( ) ; this . getHeroes ( ) ; } private initMenu ( hero ) { const template = [ { label : `Delete ${ hero . name }` , click : ( ) => this . deleteHero ( hero ) , } , ] ; return remote . Menu . buildFromTemplate ( template ) ; } }