plugin-show-installed.component.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { Component, OnDestroy, OnInit } from '@angular/core'
  2. import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
  3. import { I18n } from '@ngx-translate/i18n-polyfill'
  4. import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
  5. import { Notifier } from '@app/core'
  6. import { ActivatedRoute } from '@angular/router'
  7. import { Subscription } from 'rxjs'
  8. import { map, switchMap } from 'rxjs/operators'
  9. import { RegisterSettingOptions } from '@shared/models/plugins/register-setting.model'
  10. import { BuildFormArgument, FormReactive, FormValidatorService } from '@app/shared'
  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: RegisterSettingOptions[] = []
  19. pluginTypeLabel: string
  20. private sub: Subscription
  21. constructor (
  22. protected formValidatorService: FormValidatorService,
  23. private i18n: I18n,
  24. private pluginService: PluginApiService,
  25. private notifier: Notifier,
  26. private route: ActivatedRoute
  27. ) {
  28. super()
  29. }
  30. ngOnInit () {
  31. this.sub = this.route.params.subscribe(
  32. routeParams => {
  33. const npmName = routeParams['npmName']
  34. this.loadPlugin(npmName)
  35. }
  36. )
  37. }
  38. ngOnDestroy () {
  39. if (this.sub) this.sub.unsubscribe()
  40. }
  41. formValidated () {
  42. const settings = this.form.value
  43. this.pluginService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
  44. .subscribe(
  45. () => {
  46. this.notifier.success(this.i18n('Settings updated.'))
  47. },
  48. err => this.notifier.error(err.message)
  49. )
  50. }
  51. hasRegisteredSettings () {
  52. return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
  53. }
  54. private loadPlugin (npmName: string) {
  55. this.pluginService.getPlugin(npmName)
  56. .pipe(switchMap(plugin => {
  57. return this.pluginService.getPluginRegisteredSettings(plugin.name, plugin.type)
  58. .pipe(map(data => ({ plugin, registeredSettings: data.settings })))
  59. }))
  60. .subscribe(
  61. ({ plugin, registeredSettings }) => {
  62. this.plugin = plugin
  63. this.registeredSettings = registeredSettings
  64. this.pluginTypeLabel = this.pluginService.getPluginTypeLabel(this.plugin.type)
  65. this.buildSettingsForm()
  66. },
  67. err => this.notifier.error(err.message)
  68. )
  69. }
  70. private buildSettingsForm () {
  71. const buildOptions: BuildFormArgument = {}
  72. const settingsValues: any = {}
  73. for (const setting of this.registeredSettings) {
  74. buildOptions[ setting.name ] = null
  75. settingsValues[ setting.name ] = this.getSetting(setting.name)
  76. }
  77. this.buildForm(buildOptions)
  78. this.form.patchValue(settingsValues)
  79. }
  80. private getSetting (name: string) {
  81. const settings = this.plugin.settings
  82. if (settings && settings[name]) return settings[name]
  83. const registered = this.registeredSettings.find(r => r.name === name)
  84. return registered.default
  85. }
  86. }