-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
editProfileModal.component.ts
98 lines (86 loc) · 3.51 KB
/
editProfileModal.component.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { Observable, OperatorFunction, debounceTime, map, distinctUntilChanged } from 'rxjs'
import { Component, Input, ViewChild, ViewContainerRef, ComponentFactoryResolver, Injector } from '@angular/core'
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
import { ConfigProxy, ConfigService, Profile, ProfileProvider, ProfileSettingsComponent, ProfilesService, TAB_COLORS } from 'tabby-core'
const iconsData = require('../../../tabby-core/src/icons.json')
const iconsClassList = Object.keys(iconsData).map(
icon => iconsData[icon].map(
style => `fa${style[0]} fa-${icon}`,
),
).flat()
/** @hidden */
@Component({
templateUrl:'./editProfileModal.component.pug',
})
export class EditProfileModalComponent<P extends Profile> {
@Input() profile: P & ConfigProxy
@Input() profileProvider: ProfileProvider<P>
@Input() settingsComponent: new () => ProfileSettingsComponent<P>
@Input() defaultsMode = false
groupNames: string[]
@ViewChild('placeholder', { read: ViewContainerRef }) placeholder: ViewContainerRef
private _profile: Profile
private settingsComponentInstance?: ProfileSettingsComponent<P>
constructor (
private injector: Injector,
private componentFactoryResolver: ComponentFactoryResolver,
private profilesService: ProfilesService,
config: ConfigService,
private modalInstance: NgbActiveModal,
) {
this.groupNames = [...new Set(
(config.store.profiles as Profile[])
.map(x => x.group)
.filter(x => !!x),
)].sort() as string[]
}
colorsAutocomplete = text$ => text$.pipe(
debounceTime(200),
distinctUntilChanged(),
map((q: string) =>
TAB_COLORS
.filter(x => !q || x.name.toLowerCase().startsWith(q.toLowerCase()))
.map(x => x.value),
),
)
colorsFormatter = value => {
return TAB_COLORS.find(x => x.value === value)?.name ?? value
}
ngOnInit () {
this._profile = this.profile
this.profile = this.profilesService.getConfigProxyForProfile(this.profile, this.defaultsMode)
}
ngAfterViewInit () {
const componentType = this.profileProvider.settingsComponent
if (componentType) {
setTimeout(() => {
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentType)
const componentRef = componentFactory.create(this.injector)
this.settingsComponentInstance = componentRef.instance
this.settingsComponentInstance.profile = this.profile
this.placeholder.insert(componentRef.hostView)
})
}
}
groupTypeahead = (text$: Observable<string>) =>
text$.pipe(
debounceTime(200),
distinctUntilChanged(),
map(q => this.groupNames.filter(x => !q || x.toLowerCase().includes(q.toLowerCase()))),
)
iconSearch: OperatorFunction<string, string[]> = (text$: Observable<string>) =>
text$.pipe(
debounceTime(200),
map(term => iconsClassList.filter(v => v.toLowerCase().includes(term.toLowerCase())).slice(0, 10)),
)
save () {
this.profile.group ||= undefined
this.settingsComponentInstance?.save?.()
this.profile.__cleanup()
this.modalInstance.close(this._profile)
}
cancel () {
this.modalInstance.dismiss()
}
}