]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts
Translate plugin options
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-show-installed / plugin-show-installed.component.ts
1 import { Subscription } from 'rxjs'
2 import { map, switchMap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute } from '@angular/router'
5 import { Notifier, PluginService } from '@app/core'
6 import { BuildFormArgument } from '@app/shared/form-validators'
7 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
8 import { PeerTubePlugin, RegisterServerSettingOptions } from '@shared/models'
9 import { PluginApiService } from '../shared/plugin-api.service'
10
11 @Component({
12 selector: 'my-plugin-show-installed',
13 templateUrl: './plugin-show-installed.component.html',
14 styleUrls: [ './plugin-show-installed.component.scss' ]
15 })
16 export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy {
17 plugin: PeerTubePlugin
18 registeredSettings: RegisterServerSettingOptions[] = []
19 pluginTypeLabel: string
20
21 private sub: Subscription
22
23 constructor (
24 protected formValidatorService: FormValidatorService,
25 private pluginService: PluginService,
26 private pluginAPIService: PluginApiService,
27 private notifier: Notifier,
28 private route: ActivatedRoute
29 ) {
30 super()
31 }
32
33 ngOnInit () {
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.pluginAPIService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
51 .subscribe(
52 () => {
53 this.notifier.success($localize`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 isSettingHidden (setting: RegisterServerSettingOptions) {
65 return false
66 }
67
68 private loadPlugin (npmName: string) {
69 this.pluginAPIService.getPlugin(npmName)
70 .pipe(switchMap(plugin => {
71 return this.pluginAPIService.getPluginRegisteredSettings(plugin.name, plugin.type)
72 .pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings })))
73 }))
74 .subscribe(
75 async ({ plugin, registeredSettings }) => {
76 this.plugin = plugin
77 this.registeredSettings = await this.translateSettings(registeredSettings)
78
79 this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type)
80
81 this.buildSettingsForm()
82 },
83
84 err => this.notifier.error(err.message)
85 )
86 }
87
88 private buildSettingsForm () {
89 const buildOptions: BuildFormArgument = {}
90 const settingsValues: any = {}
91
92 for (const setting of this.registeredSettings) {
93 buildOptions[ setting.name ] = null
94 settingsValues[ setting.name ] = this.getSetting(setting.name)
95 }
96
97 this.buildForm(buildOptions)
98
99 this.form.patchValue(settingsValues)
100 }
101
102 private getSetting (name: string) {
103 const settings = this.plugin.settings
104
105 if (settings && settings[name] !== undefined) return settings[name]
106
107 const registered = this.registeredSettings.find(r => r.name === name)
108
109 return registered.default
110 }
111
112 private async translateSettings (settings: RegisterServerSettingOptions[]) {
113 const npmName = this.pluginService.nameToNpmName(this.plugin.name, this.plugin.type)
114
115 for (const setting of settings) {
116 for (const key of [ 'label', 'html', 'descriptionHTML' ]) {
117 if (setting[key]) setting[key] = await this.pluginService.translateBy(npmName, setting[key])
118 }
119
120 if (Array.isArray(setting.options)) {
121 const newOptions = []
122
123 for (const o of setting.options) {
124 newOptions.push({
125 value: o.value,
126 label: await this.pluginService.translateBy(npmName, o.label)
127 })
128 }
129
130 setting.options = newOptions
131 }
132 }
133
134 return settings
135 }
136
137 }