]>
Commit | Line | Data |
---|---|---|
dba85a1e C |
1 | import { Subscription } from 'rxjs' |
2 | import { map, switchMap } from 'rxjs/operators' | |
67ed6552 C |
3 | import { Component, OnDestroy, OnInit } from '@angular/core' |
4 | import { ActivatedRoute } from '@angular/router' | |
5 | import { Notifier } from '@app/core' | |
6 | import { BuildFormArgument, FormReactive, FormValidatorService } from '@app/shared/shared-forms' | |
7 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
8 | import { PeerTubePlugin, RegisterServerSettingOptions } from '@shared/models' | |
9 | import { PluginApiService } from '../shared/plugin-api.service' | |
d00dc28d C |
10 | |
11 | @Component({ | |
12 | selector: 'my-plugin-show-installed', | |
13 | templateUrl: './plugin-show-installed.component.html', | |
14 | styleUrls: [ './plugin-show-installed.component.scss' ] | |
15 | }) | |
109d893f | 16 | export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy { |
dba85a1e | 17 | plugin: PeerTubePlugin |
9ae88819 | 18 | registeredSettings: RegisterServerSettingOptions[] = [] |
dba85a1e C |
19 | pluginTypeLabel: string |
20 | ||
21 | private sub: Subscription | |
22 | ||
23 | constructor ( | |
24 | protected formValidatorService: FormValidatorService, | |
25 | private i18n: I18n, | |
26 | private pluginService: PluginApiService, | |
27 | private notifier: Notifier, | |
28 | private route: ActivatedRoute | |
29 | ) { | |
30 | super() | |
31 | } | |
d00dc28d C |
32 | |
33 | ngOnInit () { | |
dba85a1e C |
34 | this.sub = this.route.params.subscribe( |
35 | routeParams => { | |
36 | const npmName = routeParams['npmName'] | |
37 | ||
38 | this.loadPlugin(npmName) | |
39 | } | |
40 | ) | |
41 | } | |
42 | ||
43 | ngOnDestroy () { | |
44 | if (this.sub) this.sub.unsubscribe() | |
45 | } | |
46 | ||
47 | formValidated () { | |
48 | const settings = this.form.value | |
49 | ||
50 | this.pluginService.updatePluginSettings(this.plugin.name, this.plugin.type, settings) | |
51 | .subscribe( | |
52 | () => { | |
53 | this.notifier.success(this.i18n('Settings updated.')) | |
54 | }, | |
55 | ||
56 | err => this.notifier.error(err.message) | |
57 | ) | |
58 | } | |
59 | ||
60 | hasRegisteredSettings () { | |
61 | return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0 | |
62 | } | |
63 | ||
64 | private loadPlugin (npmName: string) { | |
65 | this.pluginService.getPlugin(npmName) | |
66 | .pipe(switchMap(plugin => { | |
67 | return this.pluginService.getPluginRegisteredSettings(plugin.name, plugin.type) | |
ba211e73 | 68 | .pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings }))) |
dba85a1e C |
69 | })) |
70 | .subscribe( | |
71 | ({ plugin, registeredSettings }) => { | |
72 | this.plugin = plugin | |
73 | this.registeredSettings = registeredSettings | |
74 | ||
75 | this.pluginTypeLabel = this.pluginService.getPluginTypeLabel(this.plugin.type) | |
76 | ||
77 | this.buildSettingsForm() | |
78 | }, | |
79 | ||
80 | err => this.notifier.error(err.message) | |
81 | ) | |
82 | } | |
83 | ||
84 | private buildSettingsForm () { | |
dba85a1e C |
85 | const buildOptions: BuildFormArgument = {} |
86 | const settingsValues: any = {} | |
87 | ||
88 | for (const setting of this.registeredSettings) { | |
89 | buildOptions[ setting.name ] = null | |
90 | settingsValues[ setting.name ] = this.getSetting(setting.name) | |
91 | } | |
92 | ||
93 | this.buildForm(buildOptions) | |
94 | ||
95 | this.form.patchValue(settingsValues) | |
96 | } | |
97 | ||
98 | private getSetting (name: string) { | |
99 | const settings = this.plugin.settings | |
100 | ||
101 | if (settings && settings[name]) return settings[name] | |
102 | ||
103 | const registered = this.registeredSettings.find(r => r.name === name) | |
d00dc28d | 104 | |
dba85a1e | 105 | return registered.default |
d00dc28d C |
106 | } |
107 | ||
108 | } |