2021-06-16 23:49:44 +02:00
|
|
|
import { Component, ElementRef, ViewChild } from '@angular/core'
|
|
|
|
|
import { HttpClient } from '@angular/common/http'
|
2021-07-16 23:56:03 +02:00
|
|
|
import { Title } from '@angular/platform-browser'
|
2021-06-16 23:49:44 +02:00
|
|
|
import { AppConnectorService } from '../services/appConnector.service'
|
|
|
|
|
|
2021-07-11 16:13:18 +02:00
|
|
|
import { faCog, faFile, faPlus, faSignOutAlt } from '@fortawesome/free-solid-svg-icons'
|
2021-06-21 09:22:13 +02:00
|
|
|
import { LoginService } from '../services/login.service'
|
2021-06-25 21:55:40 +02:00
|
|
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
|
|
|
|
|
import { SettingsModalComponent } from './settingsModal.component'
|
|
|
|
|
import { ConfigModalComponent } from './configModal.component'
|
|
|
|
|
import { ConfigService } from '../services/config.service'
|
|
|
|
|
import { combineLatest } from 'rxjs'
|
|
|
|
|
import { Config, Version } from '../api'
|
2021-06-27 16:28:49 +02:00
|
|
|
import { Router } from '@angular/router'
|
2021-06-16 23:49:44 +02:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'main',
|
|
|
|
|
templateUrl: './main.component.pug',
|
|
|
|
|
styleUrls: ['./main.component.scss'],
|
|
|
|
|
})
|
|
|
|
|
export class MainComponent {
|
|
|
|
|
_logo = require('../assets/logo.svg')
|
2021-06-25 21:55:40 +02:00
|
|
|
_settingsIcon = faCog
|
|
|
|
|
_logoutIcon = faSignOutAlt
|
2021-06-16 23:49:44 +02:00
|
|
|
_addIcon = faPlus
|
2021-07-11 16:13:18 +02:00
|
|
|
_configIcon = faFile
|
2021-06-16 23:49:44 +02:00
|
|
|
|
2021-06-25 21:55:40 +02:00
|
|
|
showApp = false
|
|
|
|
|
|
2021-06-16 23:49:44 +02:00
|
|
|
@ViewChild('iframe') iframe: ElementRef
|
|
|
|
|
|
|
|
|
|
constructor (
|
2021-07-16 23:56:03 +02:00
|
|
|
titleService: Title,
|
2021-06-25 21:55:40 +02:00
|
|
|
public appConnector: AppConnectorService,
|
2021-06-16 23:49:44 +02:00
|
|
|
private http: HttpClient,
|
2021-06-21 09:22:13 +02:00
|
|
|
public loginService: LoginService,
|
2021-06-25 21:55:40 +02:00
|
|
|
private ngbModal: NgbModal,
|
|
|
|
|
private config: ConfigService,
|
2021-06-27 16:28:49 +02:00
|
|
|
private router: Router,
|
2021-06-16 23:49:44 +02:00
|
|
|
) {
|
2021-07-16 23:56:03 +02:00
|
|
|
titleService.setTitle('Tabby')
|
2021-06-27 16:28:49 +02:00
|
|
|
window.addEventListener('message', this.connectorRequestHandler)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connectorRequestHandler = event => {
|
|
|
|
|
if (event.data === 'request-connector') {
|
|
|
|
|
this.iframe.nativeElement.contentWindow['__connector__'] = this.appConnector
|
|
|
|
|
this.iframe.nativeElement.contentWindow.postMessage('connector-ready', '*')
|
|
|
|
|
}
|
2021-06-16 23:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async ngAfterViewInit () {
|
2021-06-27 16:28:49 +02:00
|
|
|
await this.loginService.ready$.toPromise()
|
|
|
|
|
if (!this.loginService.user) {
|
|
|
|
|
this.router.navigate(['/login'])
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 21:55:40 +02:00
|
|
|
combineLatest(
|
|
|
|
|
this.config.activeConfig$,
|
|
|
|
|
this.config.activeVersion$
|
|
|
|
|
).subscribe(([config, version]) => {
|
|
|
|
|
if (config && version) {
|
|
|
|
|
this.reloadApp(config, version)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
this.config
|
|
|
|
|
await this.config.ready$.toPromise()
|
|
|
|
|
await this.config.selectDefaultConfig()
|
2021-06-16 23:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-27 16:28:49 +02:00
|
|
|
ngOnDestroy () {
|
|
|
|
|
window.removeEventListener('message', this.connectorRequestHandler)
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 23:49:44 +02:00
|
|
|
unloadApp () {
|
2021-06-25 21:55:40 +02:00
|
|
|
this.showApp = false
|
2021-06-16 23:49:44 +02:00
|
|
|
this.iframe.nativeElement.src = 'about:blank'
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 21:55:40 +02:00
|
|
|
async loadApp (config, version) {
|
|
|
|
|
this.showApp = true
|
2021-06-19 23:01:22 +02:00
|
|
|
this.iframe.nativeElement.src = '/terminal'
|
2021-06-25 21:55:40 +02:00
|
|
|
await this.http.patch(`/api/1/configs/${config.id}`, {
|
|
|
|
|
last_used_with_version: version.version,
|
|
|
|
|
}).toPromise()
|
2021-06-16 23:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-25 21:55:40 +02:00
|
|
|
reloadApp (config: Config, version: Version) {
|
2021-06-16 23:49:44 +02:00
|
|
|
// TODO check config incompatibility
|
|
|
|
|
this.unloadApp()
|
|
|
|
|
setTimeout(() => {
|
2021-06-25 21:55:40 +02:00
|
|
|
this.appConnector.setState(config, version)
|
|
|
|
|
this.loadApp(config, version)
|
2021-06-16 23:49:44 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 21:55:40 +02:00
|
|
|
async openConfig () {
|
|
|
|
|
await this.ngbModal.open(ConfigModalComponent).result
|
|
|
|
|
}
|
2021-06-16 23:49:44 +02:00
|
|
|
|
2021-06-25 21:55:40 +02:00
|
|
|
async openSettings () {
|
|
|
|
|
await this.ngbModal.open(SettingsModalComponent).result
|
2021-06-16 23:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async logout () {
|
|
|
|
|
await this.http.post('/api/1/auth/logout', null).toPromise()
|
|
|
|
|
location.href = '/'
|
|
|
|
|
}
|
|
|
|
|
}
|