Add ability to hide plugin settings
This commit is contained in:
parent
c713017f3c
commit
3c47fa3bc0
|
@ -19,6 +19,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
|
||||||
pluginTypeLabel: string
|
pluginTypeLabel: string
|
||||||
|
|
||||||
private sub: Subscription
|
private sub: Subscription
|
||||||
|
private npmName: string
|
||||||
|
|
||||||
constructor (
|
constructor (
|
||||||
protected formValidatorService: FormValidatorService,
|
protected formValidatorService: FormValidatorService,
|
||||||
|
@ -33,9 +34,9 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
|
||||||
ngOnInit () {
|
ngOnInit () {
|
||||||
this.sub = this.route.params.subscribe(
|
this.sub = this.route.params.subscribe(
|
||||||
routeParams => {
|
routeParams => {
|
||||||
const npmName = routeParams['npmName']
|
this.npmName = routeParams['npmName']
|
||||||
|
|
||||||
this.loadPlugin(npmName)
|
this.loadPlugin(this.npmName)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -62,7 +63,11 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
|
||||||
}
|
}
|
||||||
|
|
||||||
isSettingHidden (setting: RegisterServerSettingOptions) {
|
isSettingHidden (setting: RegisterServerSettingOptions) {
|
||||||
return false
|
const script = this.pluginService.getRegisteredSettingsScript(this.npmName)
|
||||||
|
|
||||||
|
if (!script?.isSettingHidden) return false
|
||||||
|
|
||||||
|
return script.isSettingHidden({ setting, formValues: this.form.value })
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadPlugin (npmName: string) {
|
private loadPlugin (npmName: string) {
|
||||||
|
@ -74,6 +79,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
|
||||||
.subscribe(
|
.subscribe(
|
||||||
async ({ plugin, registeredSettings }) => {
|
async ({ plugin, registeredSettings }) => {
|
||||||
this.plugin = plugin
|
this.plugin = plugin
|
||||||
|
|
||||||
this.registeredSettings = await this.translateSettings(registeredSettings)
|
this.registeredSettings = await this.translateSettings(registeredSettings)
|
||||||
|
|
||||||
this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type)
|
this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type)
|
||||||
|
@ -110,11 +116,9 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
|
||||||
}
|
}
|
||||||
|
|
||||||
private async translateSettings (settings: RegisterServerSettingOptions[]) {
|
private async translateSettings (settings: RegisterServerSettingOptions[]) {
|
||||||
const npmName = this.pluginService.nameToNpmName(this.plugin.name, this.plugin.type)
|
|
||||||
|
|
||||||
for (const setting of settings) {
|
for (const setting of settings) {
|
||||||
for (const key of [ 'label', 'html', 'descriptionHTML' ]) {
|
for (const key of [ 'label', 'html', 'descriptionHTML' ]) {
|
||||||
if (setting[key]) setting[key] = await this.pluginService.translateBy(npmName, setting[key])
|
if (setting[key]) setting[key] = await this.pluginService.translateBy(this.npmName, setting[key])
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(setting.options)) {
|
if (Array.isArray(setting.options)) {
|
||||||
|
@ -123,7 +127,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
|
||||||
for (const o of setting.options) {
|
for (const o of setting.options) {
|
||||||
newOptions.push({
|
newOptions.push({
|
||||||
value: o.value,
|
value: o.value,
|
||||||
label: await this.pluginService.translateBy(npmName, o.label)
|
label: await this.pluginService.translateBy(this.npmName, o.label)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ import {
|
||||||
PluginTranslation,
|
PluginTranslation,
|
||||||
PluginType,
|
PluginType,
|
||||||
PublicServerSetting,
|
PublicServerSetting,
|
||||||
|
RegisterClientSettingsScript,
|
||||||
ServerConfigPlugin
|
ServerConfigPlugin
|
||||||
} from '@shared/models'
|
} from '@shared/models'
|
||||||
import { environment } from '../../../environments/environment'
|
import { environment } from '../../../environments/environment'
|
||||||
|
@ -58,6 +59,7 @@ export class PluginService implements ClientHook {
|
||||||
private formFields: FormFields = {
|
private formFields: FormFields = {
|
||||||
video: []
|
video: []
|
||||||
}
|
}
|
||||||
|
private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScript } = {}
|
||||||
|
|
||||||
constructor (
|
constructor (
|
||||||
private authService: AuthService,
|
private authService: AuthService,
|
||||||
|
@ -200,6 +202,10 @@ export class PluginService implements ClientHook {
|
||||||
return this.formFields.video.filter(f => f.videoFormOptions.type === type)
|
return this.formFields.video.filter(f => f.videoFormOptions.type === type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getRegisteredSettingsScript (npmName: string) {
|
||||||
|
return this.settingsScripts[npmName]
|
||||||
|
}
|
||||||
|
|
||||||
translateBy (npmName: string, toTranslate: string) {
|
translateBy (npmName: string, toTranslate: string) {
|
||||||
const helpers = this.helpers[npmName]
|
const helpers = this.helpers[npmName]
|
||||||
if (!helpers) {
|
if (!helpers) {
|
||||||
|
@ -220,6 +226,7 @@ export class PluginService implements ClientHook {
|
||||||
return loadPlugin({
|
return loadPlugin({
|
||||||
hooks: this.hooks,
|
hooks: this.hooks,
|
||||||
formFields: this.formFields,
|
formFields: this.formFields,
|
||||||
|
onSettingsScripts: options => this.settingsScripts[npmName] = options,
|
||||||
pluginInfo,
|
pluginInfo,
|
||||||
peertubeHelpersFactory: () => helpers
|
peertubeHelpersFactory: () => helpers
|
||||||
})
|
})
|
||||||
|
|
|
@ -7,7 +7,8 @@ import {
|
||||||
ClientScript,
|
ClientScript,
|
||||||
PluginType,
|
PluginType,
|
||||||
RegisterClientHookOptions,
|
RegisterClientHookOptions,
|
||||||
ServerConfigPlugin
|
ServerConfigPlugin,
|
||||||
|
RegisterClientSettingsScript
|
||||||
} from '../../../shared/models'
|
} from '../../../shared/models'
|
||||||
import { ClientScript as ClientScriptModule } from '../types/client-script.model'
|
import { ClientScript as ClientScriptModule } from '../types/client-script.model'
|
||||||
import { importModule } from './utils'
|
import { importModule } from './utils'
|
||||||
|
@ -54,8 +55,9 @@ function loadPlugin (options: {
|
||||||
pluginInfo: PluginInfo
|
pluginInfo: PluginInfo
|
||||||
peertubeHelpersFactory: (pluginInfo: PluginInfo) => RegisterClientHelpers
|
peertubeHelpersFactory: (pluginInfo: PluginInfo) => RegisterClientHelpers
|
||||||
formFields?: FormFields
|
formFields?: FormFields
|
||||||
|
onSettingsScripts?: (options: RegisterClientSettingsScript) => void
|
||||||
}) {
|
}) {
|
||||||
const { hooks, pluginInfo, peertubeHelpersFactory, formFields } = options
|
const { hooks, pluginInfo, peertubeHelpersFactory, formFields, onSettingsScripts } = options
|
||||||
const { plugin, clientScript } = pluginInfo
|
const { plugin, clientScript } = pluginInfo
|
||||||
|
|
||||||
const registerHook = (options: RegisterClientHookOptions) => {
|
const registerHook = (options: RegisterClientHookOptions) => {
|
||||||
|
@ -86,12 +88,20 @@ function loadPlugin (options: {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const registerSettingsScript = (options: RegisterClientSettingsScript) => {
|
||||||
|
if (!onSettingsScripts) {
|
||||||
|
throw new Error('Registering settings script is not supported')
|
||||||
|
}
|
||||||
|
|
||||||
|
return onSettingsScripts(options)
|
||||||
|
}
|
||||||
|
|
||||||
const peertubeHelpers = peertubeHelpersFactory(pluginInfo)
|
const peertubeHelpers = peertubeHelpersFactory(pluginInfo)
|
||||||
|
|
||||||
console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
|
console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
|
||||||
|
|
||||||
return importModule(clientScript.script)
|
return importModule(clientScript.script)
|
||||||
.then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, peertubeHelpers }))
|
.then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, registerSettingsScript, peertubeHelpers }))
|
||||||
.then(() => sortHooksByPriority(hooks))
|
.then(() => sortHooksByPriority(hooks))
|
||||||
.catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
|
.catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
|
||||||
}
|
}
|
||||||
|
|
|
@ -759,6 +759,7 @@ export class PeerTubeEmbed {
|
||||||
await loadPlugin({
|
await loadPlugin({
|
||||||
hooks: this.peertubeHooks,
|
hooks: this.peertubeHooks,
|
||||||
pluginInfo,
|
pluginInfo,
|
||||||
|
onSettingsScripts: () => undefined,
|
||||||
peertubeHelpersFactory: _ => this.buildPeerTubeHelpers(translations)
|
peertubeHelpersFactory: _ => this.buildPeerTubeHelpers(translations)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,18 @@
|
||||||
import { RegisterClientFormFieldOptions, RegisterClientVideoFieldOptions } from '@shared/models/plugins/register-client-form-field.model'
|
import {
|
||||||
import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model'
|
RegisterClientFormFieldOptions,
|
||||||
import { ServerConfig } from '@shared/models/server'
|
RegisterClientHookOptions,
|
||||||
|
RegisterClientSettingsScript,
|
||||||
|
RegisterClientVideoFieldOptions,
|
||||||
|
ServerConfig
|
||||||
|
} from '@shared/models'
|
||||||
|
|
||||||
export type RegisterClientOptions = {
|
export type RegisterClientOptions = {
|
||||||
registerHook: (options: RegisterClientHookOptions) => void
|
registerHook: (options: RegisterClientHookOptions) => void
|
||||||
|
|
||||||
registerVideoField: (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => void
|
registerVideoField: (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => void
|
||||||
|
|
||||||
|
registerSettingsScript: (options: RegisterClientSettingsScript) => void
|
||||||
|
|
||||||
peertubeHelpers: RegisterClientHelpers
|
peertubeHelpers: RegisterClientHelpers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ export * from './plugin-video-privacy-manager.model'
|
||||||
export * from './plugin.type'
|
export * from './plugin.type'
|
||||||
export * from './public-server.setting'
|
export * from './public-server.setting'
|
||||||
export * from './register-client-hook.model'
|
export * from './register-client-hook.model'
|
||||||
|
export * from './register-client-settings-script.model'
|
||||||
export * from './register-client-form-field.model'
|
export * from './register-client-form-field.model'
|
||||||
export * from './register-server-hook.model'
|
export * from './register-server-hook.model'
|
||||||
export * from './register-server-setting.model'
|
export * from './register-server-setting.model'
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { RegisterServerSettingOptions } from "./register-server-setting.model"
|
||||||
|
|
||||||
|
export interface RegisterClientSettingsScript {
|
||||||
|
isSettingHidden (options: {
|
||||||
|
setting: RegisterServerSettingOptions
|
||||||
|
formValues: { [name: string]: any }
|
||||||
|
}): boolean
|
||||||
|
}
|
Loading…
Reference in New Issue