profiles.service.ts 2.0 KB
Newer Older
E
Eugene Pankov 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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
import { Injectable, Inject } from '@angular/core'
import { NewTabParameters } from './tabs.service'
import { BaseTabComponent } from '../components/baseTab.component'
import { Profile, ProfileProvider } from '../api/profileProvider'
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
            if (profile.disableDynamicTitle) {
                tab['enableDynamicTitle'] = false
                tab.setTitle(profile.name)
            }
            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
    }
}