Files
tabby-web/src/components/app.component.ts

106 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-06-15 23:43:23 +02:00
import * as semverGT from 'semver/functions/gt'
2021-06-14 23:00:05 +02:00
import { Component, ElementRef, ViewChild } from '@angular/core'
import { HttpClient } from '@angular/common/http'
import { AppConnectorService } from '../services/appConnector.service'
2021-06-15 23:43:23 +02:00
import { faCog, faUser, faCopy, faTrash, faPlus } from '@fortawesome/free-solid-svg-icons'
2021-06-14 23:00:05 +02:00
@Component({
selector: 'app',
templateUrl: './app.component.pug',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
_logo = require('../assets/logo.svg')
2021-06-15 23:43:23 +02:00
_cogIcon = faCog
_userIcon = faUser
_copyIcon = faCopy
_addIcon = faPlus
_deleteIcon = faTrash
2021-06-14 23:00:05 +02:00
configs: any[] = []
2021-06-15 23:43:23 +02:00
versions: any[] = []
activeVersion?: any
2021-06-14 23:00:05 +02:00
@ViewChild('iframe') iframe: ElementRef
constructor (
private appConnector: AppConnectorService,
private http: HttpClient,
) {
window.addEventListener('message', event => {
if (event.data === 'request-connector') {
this.iframe.nativeElement.contentWindow['__connector__'] = this.appConnector
this.iframe.nativeElement.contentWindow.postMessage('connector-ready', '*')
}
})
}
async ngAfterViewInit () {
this.configs = await this.http.get('/api/1/configs').toPromise()
2021-06-15 23:43:23 +02:00
this.versions = await this.http.get('/api/1/versions').toPromise()
this.versions.sort((a, b) => semverGT(a, b))
if (!this.configs.length) {
await this.createNewConfig()
}
2021-06-14 23:00:05 +02:00
this.selectConfig(this.configs[0])
}
2021-06-15 23:43:23 +02:00
async createNewConfig () {
this.configs.push(await this.http.post('/api/1/configs', {
content: '{}',
last_used_with_version: this.versions[0].version,
}).toPromise())
}
async duplicateConfig () {
const copy = {...this.appConnector.config, pk: undefined}
this.configs.push(await this.http.post('/api/1/configs', copy).toPromise())
}
2021-06-14 23:00:05 +02:00
unloadApp () {
2021-06-15 23:43:23 +02:00
delete this.activeVersion
2021-06-14 23:00:05 +02:00
this.iframe.nativeElement.src = 'about:blank'
}
2021-06-15 23:43:23 +02:00
loadApp (version) {
this.iframe.nativeElement.src = `/terminal?${version.version}`
this.activeVersion = version
2021-06-14 23:00:05 +02:00
}
2021-06-15 23:43:23 +02:00
getActiveConfig () {
return this.appConnector.config
}
selectVersion (version: any) {
// TODO check config incompatibility
2021-06-14 23:00:05 +02:00
this.unloadApp()
setTimeout(() => {
2021-06-15 23:43:23 +02:00
this.loadApp(version)
2021-06-14 23:00:05 +02:00
})
}
2021-06-15 23:43:23 +02:00
async selectConfig (config: any) {
let matchingVersion = this.versions.find(x => x.version === config.last_used_with_version)
if (!matchingVersion) {
// TODO ask to upgrade
matchingVersion = this.versions[0]
}
this.appConnector.config = config
const result = await this.http.patch(`/api/1/configs/${config.id}`, {
last_used_with_version: matchingVersion.version,
}).toPromise()
Object.assign(config, result)
this.selectVersion(matchingVersion)
}
async logout () {
await this.http.post('/api/1/auth/logout', null).toPromise()
}
2021-06-14 23:00:05 +02:00
}