profiles.service.ts 2.4 KB
Newer Older
E
Eugene Pankov 已提交
1 2 3 4
import { Injectable, Inject } from '@angular/core'
import { NewTabParameters } from './tabs.service'
import { BaseTabComponent } from '../components/baseTab.component'
import { Profile, ProfileProvider } from '../api/profileProvider'
E
Eugene Pankov 已提交
5
import { SelectorOption } from '../api/selector'
E
Eugene Pankov 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import { AppService } from './app.service'
import { ConfigService } from './config.service'

@Injectable({ providedIn: 'root' })
export class ProfilesService {
    constructor (
        private app: AppService,
        private config: ConfigService,
        @Inject(ProfileProvider) private profileProviders: ProfileProvider[],
    ) { }

    async openNewTabForProfile (profile: Profile): Promise<BaseTabComponent|null> {
        const params = await this.newTabParametersForProfile(profile)
        if (params) {
            const tab = this.app.openNewTab(params)
            ;(this.app.getParentTab(tab) ?? tab).color = profile.color ?? null
22
            tab.setTitle(profile.name)
E
Eugene Pankov 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
            if (profile.disableDynamicTitle) {
                tab['enableDynamicTitle'] = false
            }
            return tab
        }
        return null
    }

    async newTabParametersForProfile (profile: Profile): Promise<NewTabParameters<BaseTabComponent>|null> {
        return this.providerForProfile(profile)?.getNewTabParameters(profile) ?? null
    }

    getProviders (): ProfileProvider[] {
        return [...this.profileProviders]
    }

    async getProfiles (): Promise<Profile[]> {
        const lists = await Promise.all(this.config.enabledServices(this.profileProviders).map(x => x.getBuiltinProfiles()))
        let list = lists.reduce((a, b) => a.concat(b), [])
        list = [
            ...this.config.store.profiles ?? [],
            ...list,
        ]
        list.sort((a, b) => a.group?.localeCompare(b.group ?? '') ?? -1)
        list.sort((a, b) => a.name.localeCompare(b.name))
        list.sort((a, b) => (a.isBuiltin ? 1 : 0) - (b.isBuiltin ? 1 : 0))
        return list
    }

    providerForProfile (profile: Profile): ProfileProvider|null {
        return this.profileProviders.find(x => x.id === profile.type) ?? null
    }
E
Eugene Pankov 已提交
55 56 57 58 59 60 61 62

    selectorOptionForProfile <T> (profile: Profile): SelectorOption<T> {
        return {
            icon: profile.icon,
            name: profile.group ? `${profile.group} / ${profile.name}` : profile.name,
            description: this.providerForProfile(profile)?.getDescription(profile),
        }
    }
E
Eugene Pankov 已提交
63
}